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.user;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  import fr.paris.lutece.portal.service.util.AppPropertiesService;
38  import fr.paris.lutece.util.url.UrlItem;
39  
40  import org.apache.commons.lang3.StringUtils;
41  
42  import java.io.Serializable;
43  import java.io.UnsupportedEncodingException;
44  
45  import java.net.URLEncoder;
46  
47  import javax.servlet.http.HttpServletRequest;
48  
49  /**
50   * This class provides a filter for users search function
51   */
52  public class AdminUserFilter implements Serializable
53  {
54      private static final long serialVersionUID = 4791880812924591795L;
55  
56      // Constants
57      private static final String CONSTANT_LEAST_ONE = "-1";
58      private static final String CONSTANT_DEFAULT_LEVEL = "noValue";
59      private static final String CONSTANT_EQUAL = "=";
60      private static final String CONSTANT_AMPERSAND = "&";
61  
62      // Parameteres
63      private static final String PARAMETER_SEARCH_ACCESS_CODE = "search_access_code";
64      private static final String PARAMETER_SEARCH_FIRST_NAME = "search_first_name";
65      private static final String PARAMETER_SEARCH_LAST_NAME = "search_last_name";
66      private static final String PARAMETER_SEARCH_EMAIL = "search_email";
67      private static final String PARAMETER_SEARCH_STATUS = "search_status";
68      private static final String PARAMETER_SEARCH_USER_LEVEL = "search_user_level";
69      private static final String PARAMETER_SEARCH_IS_SEARCH = "search_is_search";
70  
71      // Properties
72      private static final String PROPERTY_ENCODING_URL = "lutece.encoding.url";
73      private String _strAccessCode;
74      private String _strFirstName;
75      private String _strLastName;
76      private String _strEmail;
77      private int _nStatus;
78      private int _nUserLevel;
79  
80      /**
81       * Constructor
82       */
83      public AdminUserFilter( )
84      {
85          // Ctor
86      }
87  
88      /**
89       * Initialize each component of the object
90       */
91      public void init( )
92      {
93          _strAccessCode = StringUtils.EMPTY;
94          _strFirstName = StringUtils.EMPTY;
95          _strLastName = StringUtils.EMPTY;
96          _strEmail = StringUtils.EMPTY;
97          _nStatus = -1;
98          _nUserLevel = -1;
99      }
100 
101     /**
102      * Get the access code
103      * 
104      * @return The access code
105      */
106     public String getAccessCode( )
107     {
108         return _strAccessCode;
109     }
110 
111     /**
112      * Set the access code
113      * 
114      * @param strAccessCode
115      *            The Access Code
116      */
117     public void setAccessCode( String strAccessCode )
118     {
119         _strAccessCode = strAccessCode;
120     }
121 
122     /**
123      * @return the strFirstName
124      */
125     public String getFirstName( )
126     {
127         return _strFirstName;
128     }
129 
130     /**
131      * @param strFirstName
132      *            the strFirstName to set
133      */
134     public void setFirstName( String strFirstName )
135     {
136         _strFirstName = strFirstName;
137     }
138 
139     /**
140      * Get the last name
141      * 
142      * @return The last name
143      */
144     public String getLastName( )
145     {
146         return _strLastName;
147     }
148 
149     /**
150      * Set the last name
151      * 
152      * @param strLastName
153      *            The Last Name
154      */
155     public void setLastName( String strLastName )
156     {
157         _strLastName = strLastName;
158     }
159 
160     /**
161      * Get the email
162      * 
163      * @return The email
164      */
165     public String getEmail( )
166     {
167         return _strEmail;
168     }
169 
170     /**
171      * Set the email
172      * 
173      * @param strEmail
174      *            The email
175      */
176     public void setEmail( String strEmail )
177     {
178         _strEmail = strEmail;
179     }
180 
181     /**
182      * Get the status
183      * 
184      * @return The status level
185      */
186     public int getStatus( )
187     {
188         return _nStatus;
189     }
190 
191     /**
192      * Set the status
193      * 
194      * @param nStatus
195      *            The Status
196      */
197     public void setStatus( int nStatus )
198     {
199         _nStatus = nStatus;
200     }
201 
202     /**
203      * Get the user level
204      * 
205      * @return The user level
206      */
207     public int getUserLevel( )
208     {
209         return _nUserLevel;
210     }
211 
212     /**
213      * Set the user level
214      * 
215      * @param nUserLevel
216      *            The User Level
217      */
218     public void setUserLevel( int nUserLevel )
219     {
220         _nUserLevel = nUserLevel;
221     }
222 
223     /**
224      * Set the value of the AdminUserFilter
225      * 
226      * @param request
227      *            HttpServletRequest
228      * @return true if there is a search
229      */
230     public boolean setAdminUserFilter( HttpServletRequest request )
231     {
232         boolean bIsSearch = false;
233         String strIsSearch = request.getParameter( PARAMETER_SEARCH_IS_SEARCH );
234 
235         if ( strIsSearch != null )
236         {
237             bIsSearch = true;
238             _strAccessCode = request.getParameter( PARAMETER_SEARCH_ACCESS_CODE );
239             _strFirstName = request.getParameter( PARAMETER_SEARCH_FIRST_NAME );
240             _strLastName = request.getParameter( PARAMETER_SEARCH_LAST_NAME );
241             _strEmail = request.getParameter( PARAMETER_SEARCH_EMAIL );
242 
243             String strStatus = request.getParameter( PARAMETER_SEARCH_STATUS );
244             String strUserLevel = request.getParameter( PARAMETER_SEARCH_USER_LEVEL );
245 
246             if ( CONSTANT_DEFAULT_LEVEL.equals( strStatus ) || CONSTANT_LEAST_ONE.equals( strStatus ) )
247             {
248                 setStatus( -1 );
249             }
250             else
251             {
252                 setStatus( Integer.valueOf( strStatus ) );
253             }
254 
255             if ( ( strUserLevel == null ) || CONSTANT_DEFAULT_LEVEL.equals( strUserLevel ) || CONSTANT_LEAST_ONE.equals( strUserLevel ) )
256             {
257                 setUserLevel( -1 );
258             }
259             else
260             {
261                 setUserLevel( Integer.valueOf( strUserLevel ) );
262             }
263         }
264         else
265         {
266             init( );
267         }
268 
269         return bIsSearch;
270     }
271 
272     /**
273      * Build url attributes
274      * 
275      * @param url
276      *            The url item
277      */
278     public void setUrlAttributes( UrlItem url )
279     {
280         url.addParameter( PARAMETER_SEARCH_IS_SEARCH, Boolean.TRUE.toString( ) );
281         url.addParameter( PARAMETER_SEARCH_USER_LEVEL, _nUserLevel );
282         url.addParameter( PARAMETER_SEARCH_STATUS, _nStatus );
283 
284         try
285         {
286             url.addParameter( PARAMETER_SEARCH_ACCESS_CODE, URLEncoder.encode( _strAccessCode, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
287             url.addParameter( PARAMETER_SEARCH_FIRST_NAME, URLEncoder.encode( _strFirstName, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
288             url.addParameter( PARAMETER_SEARCH_LAST_NAME, URLEncoder.encode( _strLastName, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
289             url.addParameter( PARAMETER_SEARCH_EMAIL, URLEncoder.encode( _strEmail, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
290         }
291         catch( UnsupportedEncodingException e )
292         {
293             AppLogService.error( e.getMessage( ), e );
294         }
295     }
296 
297     /**
298      * Build url attributes
299      * 
300      * @return the url attributes
301      */
302     public String getUrlAttributes( )
303     {
304         StringBuilder sbUrlAttributes = new StringBuilder( );
305         sbUrlAttributes.append( PARAMETER_SEARCH_IS_SEARCH + CONSTANT_EQUAL ).append( Boolean.TRUE )
306                 .append( CONSTANT_AMPERSAND + PARAMETER_SEARCH_USER_LEVEL + CONSTANT_EQUAL ).append( _nUserLevel )
307                 .append( CONSTANT_AMPERSAND + PARAMETER_SEARCH_STATUS + CONSTANT_EQUAL ).append( _nStatus );
308 
309         try
310         {
311             sbUrlAttributes.append( CONSTANT_AMPERSAND + PARAMETER_SEARCH_ACCESS_CODE + CONSTANT_EQUAL )
312                     .append( URLEncoder.encode( _strAccessCode, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) )
313                     .append( CONSTANT_AMPERSAND + PARAMETER_SEARCH_LAST_NAME + CONSTANT_EQUAL )
314                     .append( URLEncoder.encode( _strLastName, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) )
315                     .append( CONSTANT_AMPERSAND + PARAMETER_SEARCH_EMAIL + CONSTANT_EQUAL )
316                     .append( URLEncoder.encode( _strEmail, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) )
317                     .append( CONSTANT_AMPERSAND + PARAMETER_SEARCH_FIRST_NAME + CONSTANT_EQUAL )
318                     .append( URLEncoder.encode( _strFirstName, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
319         }
320         catch( UnsupportedEncodingException e )
321         {
322             AppLogService.error( e.getMessage( ), e );
323         }
324 
325         return sbUrlAttributes.toString( );
326     }
327 }