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