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