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.attribute;
35  
36  import fr.paris.lutece.portal.service.user.attribute.AttributeService;
37  import fr.paris.lutece.portal.service.util.AppLogService;
38  import fr.paris.lutece.portal.service.util.AppPropertiesService;
39  import fr.paris.lutece.util.url.UrlItem;
40  
41  import org.apache.commons.lang.StringUtils;
42  
43  import java.io.UnsupportedEncodingException;
44  
45  import java.net.URLEncoder;
46  
47  import java.util.ArrayList;
48  import java.util.List;
49  import java.util.Locale;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  
54  /**
55   *
56   * AdminUserFieldFilter
57   *
58   */
59  public class AdminUserFieldFilter
60  {
61      // CONSTANTS
62      private static final String CONSTANT_ESPERLUETTE = "&";
63      private static final String CONSTANT_EQUAL = "=";
64      private static final String CONSTANT_UNDERSCORE = "_";
65      private static final int ALL_INT = -1;
66  
67      // PARAMETERS
68      private static final String PARAMETER_SEARCH_IS_SEARCH = "search_is_search";
69      private static final String PARAMETER_ATTRIBUTE = "attribute";
70  
71      // PROPERTIES
72      private static final String PROPERTY_ENCODING_URL = "lutece.encoding.url";
73      private List<AdminUserField> _listUserFields;
74      private int _nIdUser = ALL_INT;
75      private int _nIdAttribute = ALL_INT;
76      private int _nIdField = ALL_INT;
77  
78      /**
79       * Get list user fields
80       * @return list user fields
81       */
82      public List<AdminUserField> getListUserFields(  )
83      {
84          return _listUserFields;
85      }
86  
87      /**
88       * Set list user fields
89       * @param listUserFields list user fields
90       */
91      public void setListUserFields( List<AdminUserField> listUserFields )
92      {
93          _listUserFields = listUserFields;
94      }
95  
96      /**
97       * Get id user
98       * @return id user
99       */
100     public int getIdUser(  )
101     {
102         return _nIdUser;
103     }
104 
105     /**
106      * Set id user
107      * @param nIdUser id User
108      */
109     public void setIdUser( int nIdUser )
110     {
111         _nIdUser = nIdUser;
112     }
113 
114     /**
115      * Get id attribute
116      * @return id attribute
117      */
118     public int getIdAttribute(  )
119     {
120         return _nIdAttribute;
121     }
122 
123     /**
124      * Set id attirbute
125      * @param nIdAttribute id attribute
126      */
127     public void setIdAttribute( int nIdAttribute )
128     {
129         _nIdAttribute = nIdAttribute;
130     }
131 
132     /**
133      * Get id field
134      * @return id field
135      */
136     public int getIdField(  )
137     {
138         return _nIdField;
139     }
140 
141     /**
142      * Set id field
143      * @param nIdField id field
144      */
145     public void setIdField( int nIdField )
146     {
147         _nIdField = nIdField;
148     }
149 
150     /**
151      * Check if the filter contains an id attribute
152      * @return true if it contains, false otherwise
153      */
154     public boolean containsIdAttribute(  )
155     {
156         return ( _nIdAttribute != ALL_INT );
157     }
158 
159     /**
160      * Check if the filter contains an id user
161      * @return true if it contains, false otherwise
162      */
163     public boolean containsIdUser(  )
164     {
165         return ( _nIdUser != ALL_INT );
166     }
167 
168     /**
169      * Check if the filter contains an id field
170      * @return true if it contains, false otherwise
171      */
172     public boolean containsIdField(  )
173     {
174         return ( _nIdField != ALL_INT );
175     }
176 
177     /**
178      * Set admin user field filter
179      * @param request HttpServletRequest
180      * @param locale locale
181      */
182     public void setAdminUserFieldFilter( HttpServletRequest request, Locale locale )
183     {
184         _listUserFields = new ArrayList<AdminUserField>(  );
185 
186         String strIsSearch = request.getParameter( PARAMETER_SEARCH_IS_SEARCH );
187 
188         if ( strIsSearch != null )
189         {
190             List<IAttribute> listAttributes = AttributeService.getInstance(  ).getAllAttributesWithoutFields( locale );
191 
192             for ( IAttribute attribute : listAttributes )
193             {
194                 for ( AdminUserField userField : attribute.getUserFieldsData( request, null ) )
195                 {
196                     if ( ( userField != null ) && StringUtils.isNotBlank( userField.getValue(  ) ) )
197                     {
198                         _listUserFields.add( userField );
199                     }
200                 }
201             }
202         }
203     }
204 
205     /**
206     * Build url attributes
207     * @param url The url item
208     */
209     public void setUrlAttributes( UrlItem url )
210     {
211         for ( AdminUserField userField : _listUserFields )
212         {
213             try
214             {
215                 url.addParameter( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE +
216                     userField.getAttribute(  ).getIdAttribute(  ),
217                     URLEncoder.encode( userField.getValue(  ), AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
218             }
219             catch ( UnsupportedEncodingException e )
220             {
221                 AppLogService.error( e.getMessage(  ), e );
222             }
223         }
224     }
225 
226     /**
227      * Build url attributes
228      * @return the url attributes
229      */
230     public String getUrlAttributes(  )
231     {
232         StringBuilder sbUrlAttributes = new StringBuilder(  );
233 
234         for ( AdminUserField userField : _listUserFields )
235         {
236             try
237             {
238                 sbUrlAttributes.append( CONSTANT_ESPERLUETTE + PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE +
239                     userField.getAttribute(  ).getIdAttribute(  ) + CONSTANT_EQUAL +
240                     URLEncoder.encode( userField.getValue(  ), AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
241             }
242             catch ( UnsupportedEncodingException e )
243             {
244                 AppLogService.error( e.getMessage(  ), e );
245             }
246         }
247 
248         return sbUrlAttributes.toString(  );
249     }
250 }