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.service.user.attribute;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Map;
40  import java.util.Map.Entry;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.collections.CollectionUtils;
45  import org.apache.commons.fileupload.FileItem;
46  import org.apache.commons.lang3.StringUtils;
47  
48  import fr.paris.lutece.portal.business.user.AdminUser;
49  import fr.paris.lutece.portal.business.user.attribute.AdminUserField;
50  import fr.paris.lutece.portal.business.user.attribute.AdminUserFieldFilter;
51  import fr.paris.lutece.portal.business.user.attribute.AdminUserFieldHome;
52  import fr.paris.lutece.portal.business.user.attribute.IAttribute;
53  import fr.paris.lutece.portal.service.message.AdminMessage;
54  import fr.paris.lutece.portal.service.message.AdminMessageService;
55  import fr.paris.lutece.portal.service.spring.SpringContextService;
56  import fr.paris.lutece.portal.web.constants.Messages;
57  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
58  
59  /**
60   *
61   * AdminUserFieldService
62   *
63   */
64  public final class AdminUserFieldService
65  {
66      // CONSTANTS
67      private static final String CONSTANT_EMPTY_STRING = "";
68      private static final String CONSTANT_UNDERSCORE = "_";
69  
70      // PARAMETERS
71      private static final String PARAMETER_ATTRIBUTE = "attribute";
72      private static final String PARAMETER_UPDATE_ATTRIBUTE = "update_attribute";
73      private static final AttributeService _attributeService = AttributeService.getInstance( );
74  
75      /**
76       * Instantiates a new admin user field service.
77       */
78      private AdminUserFieldService( )
79      {
80      }
81  
82      /**
83       * Check if the user fields are correctly filled
84       * 
85       * @param request
86       *            HttpServletRequest
87       * @param locale
88       *            locale
89       * @return null if there are no problem
90       */
91      public static String checkUserFields( HttpServletRequest request, Locale locale )
92      {
93          // Specific attributes
94          List<IAttribute> listAttributes = _attributeService.getAllAttributesWithoutFields( locale );
95  
96          for ( IAttribute attribute : listAttributes )
97          {
98              if ( attribute.isAttributeImage( ) )
99              {
100                 MultipartHttpServletRequestutece/portal/web/upload/MultipartHttpServletRequest.html#MultipartHttpServletRequest">MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
101                 FileItem fileItem = multipartRequest.getFile( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + attribute.getIdAttribute( ) );
102                 String strUpdateAttribute = request.getParameter( PARAMETER_UPDATE_ATTRIBUTE + CONSTANT_UNDERSCORE + attribute.getIdAttribute( ) );
103 
104                 if ( attribute.isMandatory( ) && ( strUpdateAttribute != null ) && ( ( fileItem == null ) || ( fileItem.getSize( ) == 0 ) ) )
105                 {
106                     return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
107                 }
108             }
109             else
110             {
111                 String value = request.getParameter( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + attribute.getIdAttribute( ) );
112 
113                 if ( attribute.isMandatory( ) && ( ( value == null ) || value.equals( CONSTANT_EMPTY_STRING ) ) )
114                 {
115                     return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
116                 }
117             }
118         }
119 
120         return null;
121     }
122 
123     /**
124      * Create the user fields
125      * 
126      * @param user
127      *            Adminuser
128      * @param request
129      *            HttpServletRequest
130      * @param locale
131      *            locale
132      */
133     public static void doCreateUserFields( AdminUser user, HttpServletRequest request, Locale locale )
134     {
135         // Attributes created in the Back-Office
136         List<IAttribute> listAttributes = _attributeService.getCoreAttributesWithoutFields( locale );
137 
138         for ( IAttribute attribute : listAttributes )
139         {
140             List<AdminUserField> listUserFields = attribute.getUserFieldsData( request, user );
141 
142             for ( AdminUserField userField : listUserFields )
143             {
144                 if ( userField != null )
145                 {
146                     AdminUserFieldHome.create( userField );
147                 }
148             }
149         }
150 
151         // Attributes associated to the plugins
152         for ( AdminUserFieldListenerService adminUserFieldListenerService : SpringContextService.getBeansOfType( AdminUserFieldListenerService.class ) )
153         {
154             adminUserFieldListenerService.doCreateUserFields( user, request, locale );
155         }
156     }
157 
158     /**
159      * Modify the user fields
160      * 
161      * @param user
162      *            AdminUser
163      * @param request
164      *            HttpServletRequest
165      * @param locale
166      *            locale
167      * @param currentUser
168      *            current user
169      */
170     public static void doModifyUserFields( AdminUserl/business/user/AdminUser.html#AdminUser">AdminUser user, HttpServletRequest request, Locale locale, AdminUser currentUser )
171     {
172         // Attributes created in the Back-Office
173         List<IAttribute> listAttributes = _attributeService.getCoreAttributesWithoutFields( locale );
174         Map<Integer, List<AdminUserField>> map = new HashMap<>( );
175 
176         for ( IAttribute attribute : listAttributes )
177         {
178             List<AdminUserField> listUserFields = attribute.getUserFieldsData( request, user );
179 
180             map.put( attribute.getIdAttribute( ), listUserFields );
181         }
182 
183         // Remove all user fields
184         AdminUserFieldFilterribute/AdminUserFieldFilter.html#AdminUserFieldFilter">AdminUserFieldFilter auFieldFilter = new AdminUserFieldFilter( );
185         auFieldFilter.setIdUser( user.getUserId( ) );
186         AdminUserFieldHome.removeByFilter( auFieldFilter );
187 
188         for ( Entry<Integer, List<AdminUserField>> entry : map.entrySet( ) )
189         {
190             for ( AdminUserField userField : entry.getValue( ) )
191             {
192                 if ( userField != null )
193                 {
194                     AdminUserFieldHome.create( userField );
195                 }
196             }
197         }
198 
199         // Attributes associated to the plugins
200         for ( AdminUserFieldListenerService adminUserFieldListenerService : SpringContextService.getBeansOfType( AdminUserFieldListenerService.class ) )
201         {
202             adminUserFieldListenerService.doModifyUserFields( user, request, locale, currentUser );
203         }
204     }
205 
206     /**
207      * Remove the user fields
208      * 
209      * @param user
210      *            Adminuser
211      * @param request
212      *            HttpServletRequest
213      * @param locale
214      *            locale
215      */
216     public static void doRemoveUserFields( AdminUser user, HttpServletRequest request, Locale locale )
217     {
218         AdminUserFieldFilterribute/AdminUserFieldFilter.html#AdminUserFieldFilter">AdminUserFieldFilter auFieldFilter = new AdminUserFieldFilter( );
219         auFieldFilter.setIdUser( user.getUserId( ) );
220         AdminUserFieldHome.removeByFilter( auFieldFilter );
221 
222         // Attributes associated to the plugins
223         for ( AdminUserFieldListenerService adminUserFieldListenerService : SpringContextService.getBeansOfType( AdminUserFieldListenerService.class ) )
224         {
225             adminUserFieldListenerService.doRemoveUserFields( user, request, locale );
226         }
227     }
228 
229     /**
230      * Remove the user fields from a given ID attribute
231      * 
232      * @param nIdAttribute
233      *            the ID attribute
234      */
235     public static void doRemoveUserFieldsByIdAttribute( int nIdAttribute )
236     {
237         AdminUserFieldFilterribute/AdminUserFieldFilter.html#AdminUserFieldFilter">AdminUserFieldFilter auFieldFilter = new AdminUserFieldFilter( );
238         auFieldFilter.setIdAttribute( nIdAttribute );
239         AdminUserFieldHome.removeByFilter( auFieldFilter );
240     }
241 
242     /**
243      * Remove the user fields from a given ID attribute field
244      * 
245      * @param nIdAttributeField
246      *            the attribute field ID
247      */
248     public static void doRemoveUserFieldsByIdField( int nIdAttributeField )
249     {
250         AdminUserFieldHome.removeUserFieldsFromIdField( nIdAttributeField );
251     }
252 
253     /**
254      * Get the user attribute fields
255      * 
256      * @param nUserId
257      *            the user ID
258      * @param locale
259      *            the {@link Locale}
260      * @return a Map of (ID Attribute, Object). The object could be either a File or a list of {@link AdminUserField}
261      */
262     public static Map<String, Object> getAdminUserFields( int nUserId, Locale locale )
263     {
264         List<IAttribute> listAttributes = _attributeService.getAllAttributesWithFields( locale );
265 
266         return getAdminUserFields( listAttributes, nUserId, locale );
267     }
268 
269     /**
270      * Get the user attribute fields
271      * 
272      * @param listAttributes
273      *            the list of attributes
274      * @param nUserId
275      *            the user ID
276      * @param locale
277      *            the {@link Locale}
278      * @return a Map of (ID Attribute, Object). The object could be either a File or a list of {@link AdminUserField}
279      */
280     public static Map<String, Object> getAdminUserFields( List<IAttribute> listAttributes, int nUserId, Locale locale )
281     {
282         Map<String, Object> map = new HashMap<>( );
283 
284         for ( IAttribute attribute : listAttributes )
285         {
286             List<AdminUserField> listUserFields = AdminUserFieldHome.selectUserFieldsByIdUserIdAttribute( nUserId, attribute.getIdAttribute( ) );
287 
288             if ( attribute.isAttributeImage( ) )
289             {
290                 if ( CollectionUtils.isNotEmpty( listUserFields ) )
291                 {
292                     AdminUserField userField = listUserFields.get( 0 );
293 
294                     if ( userField.getFile( ) != null )
295                     {
296                         map.put( String.valueOf( attribute.getIdAttribute( ) ), userField.getFile( ) );
297                     }
298                 }
299             }
300             else
301             {
302                 if ( CollectionUtils.isEmpty( listUserFields ) )
303                 {
304                     AdminUserFields/user/attribute/AdminUserField.html#AdminUserField">AdminUserField userField = new AdminUserField( );
305                     userField.setValue( StringUtils.EMPTY );
306                     listUserFields.add( userField );
307                 }
308 
309                 map.put( String.valueOf( attribute.getIdAttribute( ) ), listUserFields );
310             }
311         }
312 
313         return map;
314     }
315 
316     /**
317      * Get the admin user field from a given attribute and a given user ID
318      * 
319      * @param attribute
320      *            a {@link IAttribute}
321      * @param nUserId
322      *            the user ID
323      * @param locale
324      *            the {@link Locale}
325      * @return either a File (if the attribute is a type img) or a list of {@link AdminUserField}
326      */
327     public Object getAdminUserField( IAttribute attribute, int nUserId, Locale locale )
328     {
329         List<AdminUserField> listUserFields = AdminUserFieldHome.selectUserFieldsByIdUserIdAttribute( nUserId, attribute.getIdAttribute( ) );
330 
331         if ( attribute.isAttributeImage( ) )
332         {
333             if ( CollectionUtils.isNotEmpty( listUserFields ) )
334             {
335                 AdminUserField userField = listUserFields.get( 0 );
336 
337                 if ( userField.getFile( ) != null )
338                 {
339                     return userField.getFile( );
340                 }
341             }
342         }
343         else
344         {
345             if ( CollectionUtils.isEmpty( listUserFields ) )
346             {
347                 AdminUserFields/user/attribute/AdminUserField.html#AdminUserField">AdminUserField userField = new AdminUserField( );
348                 userField.setValue( StringUtils.EMPTY );
349                 listUserFields.add( userField );
350             }
351 
352             return listUserFields;
353         }
354 
355         return null;
356     }
357 }