View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.plugins.extend.modules.follow.business;
35  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  
39  import java.io.Serializable;
40  
41  import org.apache.commons.lang3.StringUtils;
42  
43  /**
44   *
45   * Follower filter
46   *
47   */
48  public class FollowFilter implements Serializable
49  {
50      /** The Constant ALL. */
51      public static final int ALL = -1;
52      private static final long serialVersionUID = -3449479672496815070L;
53  
54      // SQL
55      private static final String SQL_WHERE = " WHERE ";
56      private static final String SQL_AND = " AND ";
57      private static final String SQL_OR = " OR ";
58  
59      // FILTERS
60      private static final String SQL_FILTER_ID_FOLLOW = " id_follow = ? ";
61      private static final String SQL_FILTER_ID_RESOURCE = " id_resource = ? ";
62      private static final String SQL_FILTER_RESOURCE_TYPE = " resource_type = ? ";
63      private int _nIdFollow;
64      private String _strIdExtendableResource;
65      private String _strExtendableResourceType;
66      private boolean _bIsWideSearch;
67  
68      /**
69       * CONSTRUCTOR INIT
70       */
71      public FollowFilter( )
72      {
73          _nIdFollow = ALL;
74          _strIdExtendableResource = StringUtils.EMPTY;
75          _strExtendableResourceType = StringUtils.EMPTY;
76          _bIsWideSearch = false;
77      }
78  
79      /**
80       * Get Id ExtendableResource
81       * 
82       * @return Id ExtendableResource
83       */
84      public String getIdExtendableResource( )
85      {
86          return _strIdExtendableResource;
87      }
88  
89      /**
90       * Set Id ExtendableResource
91       * 
92       * @param strIdExtendableResource
93       *            the Id ExtendableResource
94       */
95      public void setIdExtendableResource( String strIdExtendableResource )
96      {
97          this._strIdExtendableResource = strIdExtendableResource;
98      }
99  
100     /**
101      * GET ExtendableResourceType
102      * 
103      * @return Extendable Resource Type
104      */
105     public String getExtendableResourceType( )
106     {
107         return _strExtendableResourceType;
108     }
109 
110     /**
111      * SET the extendable resource type
112      * 
113      * @param strExtendableResourceType
114      *            the resource type
115      */
116     public void setExtendableResourceType( String strExtendableResourceType )
117     {
118         this._strExtendableResourceType = strExtendableResourceType;
119     }
120 
121     /**
122      * Get Id follow
123      * 
124      * @return Id follow
125      */
126     public int getIdFollow( )
127     {
128         return _nIdFollow;
129     }
130 
131     /**
132      * Set the Id follow
133      * 
134      * @param nIdFollow
135      *            Id follow
136      */
137     public void setIdFollow( int nIdFollow )
138     {
139         this._nIdFollow = nIdFollow;
140     }
141 
142     /**
143      * Check if the filter is applied to a wide search or not. <br/>
144      * In other words, the SQL query will use
145      * <ul>
146      * <li>SQL <b>OR</b> if it is applied to a wide search</li>
147      * <li>SQL <b>AND</b> if it is not applied to a wide search</li>
148      * </ul>
149      * 
150      * @return true if it is applied to a wide search
151      */
152     public boolean isWideSearch( )
153     {
154         return _bIsWideSearch;
155     }
156 
157     /**
158      * Set true if the filter is applied to a wide search. <br/>
159      * In other words, the SQL query will use
160      * <ul>
161      * <li>SQL <b>OR</b> if it is applied to a wide search</li>
162      * <li>SQL <b>AND</b> if it is not applied to a wide search</li>
163      * </ul>
164      * 
165      * @param bIsWideSearch
166      *            true if it a wide search, false otherwise
167      */
168     public void setIsWideSearch( boolean bIsWideSearch )
169     {
170         this._bIsWideSearch = bIsWideSearch;
171     }
172 
173     /**
174      * Contains IdFollow.
175      *
176      * @return true, if successful
177      */
178     public boolean containsIdFollow( )
179     {
180         return _nIdFollow != ALL;
181     }
182 
183     /**
184      * Contains Id ExtendableResource.
185      *
186      * @return true, if successful
187      */
188     public boolean containsIdExtendableResource( )
189     {
190         return StringUtils.isNotBlank( _strIdExtendableResource );
191     }
192 
193     /**
194      * Contains Id ExtendableResource.
195      *
196      * @return true, if successful
197      */
198     public boolean containsExtendableResourceType( )
199     {
200         return StringUtils.isNotBlank( _strExtendableResourceType );
201     }
202 
203     /**
204      * Builds the filter.
205      *
206      * @param sbSQL
207      *            the sb sql
208      * @param bAddFilter
209      *            the b add filter
210      * @param strSQL
211      *            the str sql
212      * @param nIndex
213      *            the n index
214      * @return the int
215      */
216     private int buildFilter( StringBuilder sbSQL, boolean bAddFilter, String strSQL, int nIndex )
217     {
218         int nIndexTmp = nIndex;
219 
220         if ( bAddFilter )
221         {
222             nIndexTmp = addSQLWhereOr( isWideSearch( ), sbSQL, nIndex );
223             sbSQL.append( strSQL );
224         }
225 
226         return nIndexTmp;
227     }
228 
229     /**
230      * Add a <b>WHERE</b> or a <b>OR</b> depending of the index. <br/>
231      * <ul>
232      * <li>if <code>nIndex</code> == 1, then we add a <b>WHERE</b></li>
233      * <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>
234      * </ul>
235      * 
236      * @param bIsWideSearch
237      *            true if it is a wide search, false otherwise
238      * @param sbSQL
239      *            the SQL query
240      * @param nIndex
241      *            the index
242      * @return the new index
243      */
244     private int addSQLWhereOr( boolean bIsWideSearch, StringBuilder sbSQL, int nIndex )
245     {
246         if ( nIndex == 1 )
247         {
248             sbSQL.append( SQL_WHERE );
249         }
250         else
251         {
252             sbSQL.append( bIsWideSearch ? SQL_OR : SQL_AND );
253         }
254 
255         return nIndex + 1;
256     }
257 
258     /**
259      * Builds the sql query.
260      *
261      * @param strSQL
262      *            the str sql
263      * @return the string
264      */
265     public String buildSQLQuery( String strSQL )
266     {
267         StringBuilder sbSQL = new StringBuilder( strSQL );
268         int nIndex = 1;
269 
270         nIndex = buildFilter( sbSQL, containsIdFollow( ), SQL_FILTER_ID_FOLLOW, nIndex );
271         nIndex = buildFilter( sbSQL, containsIdExtendableResource( ), SQL_FILTER_ID_RESOURCE, nIndex );
272         buildFilter( sbSQL, containsExtendableResourceType( ), SQL_FILTER_RESOURCE_TYPE, nIndex );
273 
274         return sbSQL.toString( );
275     }
276 
277     /**
278      * Sets the filter values.
279      *
280      * @param daoUtil
281      *            the new filter values
282      */
283     public void setFilterValues( DAOUtil daoUtil )
284     {
285         int nIndex = 1;
286 
287         if ( containsIdFollow( ) )
288         {
289             daoUtil.setInt( nIndex++, getIdFollow( ) );
290         }
291 
292         if ( containsIdExtendableResource( ) )
293         {
294             daoUtil.setString( nIndex++, getIdExtendableResource( ) );
295         }
296 
297         if ( containsExtendableResourceType( ) )
298         {
299             daoUtil.setString( nIndex++, getExtendableResourceType( ) );
300         }
301     }
302 }