View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.lang3.StringUtils;
39  
40  import java.io.Serializable;
41  
42  /**
43   *
44   * MailingListFilter
45   *
46   */
47  public class MailingListFilter implements Serializable
48  {
49      private static final long serialVersionUID = -2176789297248798592L;
50      private static final String SQL_FILTER_NAME = " name LIKE ? ";
51      private static final String SQL_FILTER_DESCRIPTION = " description LIKE ? ";
52      private static final String SQL_FILTER_WORKGROUP = " workgroup = ? ";
53      private static final String SQL_WHERE = " WHERE ";
54      private static final String SQL_AND = " AND ";
55      private static final String SQL_OR = " OR ";
56      private static final String PERCENT = "%";
57      private String _strName;
58      private String _strDescription;
59      private String _strWorkgroup;
60      private boolean _bIsWideSearch;
61  
62      /**
63       * Instantiates a new mailing list filter.
64       */
65      public MailingListFilter( )
66      {
67      }
68  
69      /**
70       * Instantiates a new mailing list filter.
71       *
72       * @param filter
73       *            the filter
74       */
75      public MailingListFilter/../../../../../fr/paris/lutece/portal/business/mailinglist/MailingListFilter.html#MailingListFilter">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
97       *            The Name
98       */
99      public void setName( String strName )
100     {
101         _strName = strName;
102     }
103 
104     /**
105      * Contains name.
106      *
107      * @return true, if successful
108      */
109     public boolean containsName( )
110     {
111         return StringUtils.isNotBlank( _strName );
112     }
113 
114     /**
115      * Returns the Description
116      *
117      * @return The Description
118      */
119     public String getDescription( )
120     {
121         return _strDescription;
122     }
123 
124     /**
125      * Sets the Description
126      *
127      * @param strDescription
128      *            The Description
129      */
130     public void setDescription( String strDescription )
131     {
132         _strDescription = strDescription;
133     }
134 
135     /**
136      * Contains description.
137      *
138      * @return true, if successful
139      */
140     public boolean containsDescription( )
141     {
142         return StringUtils.isNotBlank( _strDescription );
143     }
144 
145     /**
146      * Returns the Workgroup
147      *
148      * @return The Workgroup
149      */
150     public String getWorkgroup( )
151     {
152         return _strWorkgroup;
153     }
154 
155     /**
156      * Sets the Workgroup
157      *
158      * @param strWorkgroup
159      *            The Workgroup
160      */
161     public void setWorkgroup( String strWorkgroup )
162     {
163         _strWorkgroup = strWorkgroup;
164     }
165 
166     /**
167      * Contains workgroup.
168      *
169      * @return true, if successful
170      */
171     public boolean containsWorkgroup( )
172     {
173         return StringUtils.isNotBlank( _strWorkgroup );
174     }
175 
176     /**
177      * Set true if the search is wide, false otherwise
178      * 
179      * @param isWideSearch
180      *            true if the search is wide, false otherwise
181      */
182     public void setWideSearch( boolean isWideSearch )
183     {
184         _bIsWideSearch = isWideSearch;
185     }
186 
187     /**
188      * Return true if the search is wide, false otherwise
189      * 
190      * @return true if the search is wide, false otherwise
191      */
192     public boolean isWideSearch( )
193     {
194         return _bIsWideSearch;
195     }
196 
197     /**
198      * Builds the sql query.
199      *
200      * @param strSQL
201      *            the str sql
202      * @return the string
203      */
204     public String buildSQLQuery( String strSQL )
205     {
206         StringBuilder sbSQL = new StringBuilder( strSQL );
207         int nIndex = 1;
208 
209         nIndex = buildFilter( sbSQL, containsName( ), SQL_FILTER_NAME, nIndex );
210         nIndex = buildFilter( sbSQL, containsDescription( ), SQL_FILTER_DESCRIPTION, nIndex );
211         buildFilter( sbSQL, containsWorkgroup( ), SQL_FILTER_WORKGROUP, nIndex );
212 
213         return sbSQL.toString( );
214     }
215 
216     /**
217      * Sets the filter values.
218      *
219      * @param daoUtil
220      *            the new filter values
221      */
222     public void setFilterValues( DAOUtil daoUtil )
223     {
224         int nIndex = 1;
225 
226         if ( containsName( ) )
227         {
228             daoUtil.setString( nIndex++, PERCENT + getName( ) + PERCENT );
229         }
230 
231         if ( containsDescription( ) )
232         {
233             daoUtil.setString( nIndex++, PERCENT + getDescription( ) + PERCENT );
234         }
235 
236         if ( containsWorkgroup( ) )
237         {
238             daoUtil.setString( nIndex, getWorkgroup( ) );
239         }
240     }
241 
242     /**
243      * Builds the filter.
244      *
245      * @param sbSQL
246      *            the sb sql
247      * @param bAddFilter
248      *            the b add filter
249      * @param strSQL
250      *            the str sql
251      * @param nIndex
252      *            the n index
253      * @return the int
254      */
255     private int buildFilter( StringBuilder sbSQL, boolean bAddFilter, String strSQL, int nIndex )
256     {
257         int nIndexTmp = nIndex;
258 
259         if ( bAddFilter )
260         {
261             nIndexTmp = addSQLWhereOr( isWideSearch( ), sbSQL, nIndex );
262             sbSQL.append( strSQL );
263         }
264 
265         return nIndexTmp;
266     }
267 
268     /**
269      * Add a <b>WHERE</b> or a <b>OR</b> depending of the index. <br>
270      * <ul>
271      * <li>if <code>nIndex</code> == 1, then we add a <b>WHERE</b></li>
272      * <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>
273      * </ul>
274      * 
275      * @param bIsWideSearch
276      *            true if it is a wide search, false otherwise
277      * @param sbSQL
278      *            the SQL query
279      * @param nIndex
280      *            the index
281      * @return the new index
282      */
283     private int addSQLWhereOr( boolean bIsWideSearch, StringBuilder sbSQL, int nIndex )
284     {
285         if ( nIndex == 1 )
286         {
287             sbSQL.append( SQL_WHERE );
288         }
289         else
290         {
291             sbSQL.append( bIsWideSearch ? SQL_OR : SQL_AND );
292         }
293 
294         return nIndex + 1;
295     }
296 }