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