View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.portal.business.mailinglist;
35  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  import org.apache.commons.lang.StringUtils;
39  
40  import java.io.Serializable;
41  
42  
43  /**
44   *
45   * MailingListFilter
46   *
47   */
48  public class MailingListFilter implements Serializable
49  {
50      private static final long serialVersionUID = -2176789297248798592L;
51      private static final String SQL_FILTER_NAME = " name LIKE ? ";
52      private static final String SQL_FILTER_DESCRIPTION = " description LIKE ? ";
53      private static final String SQL_FILTER_WORKGROUP = " workgroup = ? ";
54      private static final String SQL_WHERE = " WHERE ";
55      private static final String SQL_AND = " AND ";
56      private static final String SQL_OR = " OR ";
57      private static final String PERCENT = "%";
58      private String _strName;
59      private String _strDescription;
60      private String _strWorkgroup;
61      private boolean _bIsWideSearch;
62  
63      /**
64       * Instantiates a new mailing list filter.
65       */
66      public MailingListFilter(  )
67      {
68      }
69  
70      /**
71       * Instantiates a new mailing list filter.
72       *
73       * @param filter the filter
74       */
75      public MailingListFilter( MailingListFilter filter )
76      {
77          _strName = filter.getName(  );
78          _strDescription = filter.getDescription(  );
79          _strWorkgroup = filter.getWorkgroup(  );
80          _bIsWideSearch = filter.isWideSearch(  );
81      }
82  
83      /**
84       * Returns the Name
85       *
86       * @return The Name
87       */
88      public String getName(  )
89      {
90          return _strName;
91      }
92  
93      /**
94       * Sets the Name
95       *
96       * @param strName The Name
97       */
98      public void setName( String strName )
99      {
100         _strName = strName;
101     }
102 
103     /**
104      * Contains name.
105      *
106      * @return true, if successful
107      */
108     public boolean containsName(  )
109     {
110         return StringUtils.isNotBlank( _strName );
111     }
112 
113     /**
114      * Returns the Description
115      *
116      * @return The Description
117      */
118     public String getDescription(  )
119     {
120         return _strDescription;
121     }
122 
123     /**
124      * Sets the Description
125      *
126      * @param strDescription The Description
127      */
128     public void setDescription( String strDescription )
129     {
130         _strDescription = strDescription;
131     }
132 
133     /**
134      * Contains description.
135      *
136      * @return true, if successful
137      */
138     public boolean containsDescription(  )
139     {
140         return StringUtils.isNotBlank( _strDescription );
141     }
142 
143     /**
144      * Returns the Workgroup
145      *
146      * @return The Workgroup
147      */
148     public String getWorkgroup(  )
149     {
150         return _strWorkgroup;
151     }
152 
153     /**
154      * Sets the Workgroup
155      *
156      * @param strWorkgroup The Workgroup
157      */
158     public void setWorkgroup( String strWorkgroup )
159     {
160         _strWorkgroup = strWorkgroup;
161     }
162 
163     /**
164      * Contains workgroup.
165      *
166      * @return true, if successful
167      */
168     public boolean containsWorkgroup(  )
169     {
170         return StringUtils.isNotBlank( _strWorkgroup );
171     }
172 
173     /**
174      * Set true if the search is wide, false otherwise
175      * @param isWideSearch true if the search is wide, false otherwise
176      */
177     public void setWideSearch( boolean isWideSearch )
178     {
179         _bIsWideSearch = isWideSearch;
180     }
181 
182     /**
183      * Return true if the search is wide, false otherwise
184      * @return true if the search is wide, false otherwise
185      */
186     public boolean isWideSearch(  )
187     {
188         return _bIsWideSearch;
189     }
190 
191     /**
192      * Builds the sql query.
193      *
194      * @param strSQL the str sql
195      * @return the string
196      */
197     public String buildSQLQuery( String strSQL )
198     {
199         StringBuilder sbSQL = new StringBuilder( strSQL );
200         int nIndex = 1;
201 
202         nIndex = buildFilter( sbSQL, containsName(  ), SQL_FILTER_NAME, nIndex );
203         nIndex = buildFilter( sbSQL, containsDescription(  ), SQL_FILTER_DESCRIPTION, nIndex );
204         buildFilter( sbSQL, containsWorkgroup(  ), SQL_FILTER_WORKGROUP, nIndex );
205 
206         return sbSQL.toString(  );
207     }
208 
209     /**
210      * Sets the filter values.
211      *
212      * @param daoUtil the new filter values
213      */
214     public void setFilterValues( DAOUtil daoUtil )
215     {
216         int nIndex = 1;
217 
218         if ( containsName(  ) )
219         {
220             daoUtil.setString( nIndex++, PERCENT + getName(  ) + PERCENT );
221         }
222 
223         if ( containsDescription(  ) )
224         {
225             daoUtil.setString( nIndex++, PERCENT + getDescription(  ) + PERCENT );
226         }
227 
228         if ( containsWorkgroup(  ) )
229         {
230             daoUtil.setString( nIndex, getWorkgroup(  ) );
231         }
232     }
233 
234     /**
235      * Builds the filter.
236      *
237      * @param sbSQL the sb sql
238      * @param bAddFilter the b add filter
239      * @param strSQL the str sql
240      * @param nIndex the n index
241      * @return the int
242      */
243     private int buildFilter( StringBuilder sbSQL, boolean bAddFilter, String strSQL, int nIndex )
244     {
245         int nIndexTmp = nIndex;
246 
247         if ( bAddFilter )
248         {
249             nIndexTmp = addSQLWhereOr( isWideSearch(  ), sbSQL, nIndex );
250             sbSQL.append( strSQL );
251         }
252 
253         return nIndexTmp;
254     }
255 
256     /**
257      * Add a <b>WHERE</b> or a <b>OR</b> depending of the index.
258      * <br/>
259      * <ul>
260      * <li>if <code>nIndex</code> == 1, then we add a <b>WHERE</b></li>
261      * <li>if <code>nIndex</code> != 1, then we add a <b>OR</b> or a <b>AND</b> depending of the wide search characteristic</li>
262      * </ul>
263      * @param bIsWideSearch true if it is a wide search, false otherwise
264      * @param sbSQL the SQL query
265      * @param nIndex the index
266      * @return the new index
267      */
268     private int addSQLWhereOr( boolean bIsWideSearch, StringBuilder sbSQL, int nIndex )
269     {
270         if ( nIndex == 1 )
271         {
272             sbSQL.append( SQL_WHERE );
273         }
274         else
275         {
276             sbSQL.append( bIsWideSearch ? SQL_OR : SQL_AND );
277         }
278 
279         return nIndex + 1;
280     }
281 }