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