View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.plugins.mylutece.service.attribute;
35  
36  import fr.paris.lutece.plugins.mylutece.business.attribute.AttributeHome;
37  import fr.paris.lutece.plugins.mylutece.business.attribute.IAttribute;
38  import fr.paris.lutece.plugins.mylutece.business.attribute.MyLuteceUserField;
39  import fr.paris.lutece.plugins.mylutece.business.attribute.MyLuteceUserFieldHome;
40  import fr.paris.lutece.plugins.mylutece.service.MyLutecePlugin;
41  import fr.paris.lutece.portal.business.user.AdminUser;
42  import fr.paris.lutece.portal.service.message.AdminMessage;
43  import fr.paris.lutece.portal.service.message.AdminMessageService;
44  import fr.paris.lutece.portal.service.plugin.Plugin;
45  import fr.paris.lutece.portal.service.plugin.PluginService;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.portal.web.constants.Messages;
48  
49  import java.util.List;
50  import java.util.Locale;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  /**
55   *
56   * MyLuteceUserFieldService
57   *
58   */
59  public class MyLuteceUserFieldService
60  {
61      // CONSTANTS
62      private static final String CONSTANT_EMPTY_STRING = "";
63      private static final String CONSTANT_UNDERSCORE = "_";
64  
65      // PARAMETERS
66      private static final String PARAMETER_ATTRIBUTE = "attribute";
67  
68      private static Plugin getMyLutecePlugin( )
69      {
70          return PluginService.getPlugin( MyLutecePlugin.PLUGIN_NAME );
71      }
72  
73      /**
74       * Check if the user fields are correctly filled
75       * 
76       * @param request
77       *            HttpServletRequest
78       * @param locale
79       *            locale
80       * @return null if there are no problem
81       */
82      public static String checkUserFields( HttpServletRequest request, Locale locale )
83      {
84          // Specific attributes
85          List<IAttribute> listAttributes = AttributeHome.findAll( locale, getMyLutecePlugin( ) );
86  
87          for ( IAttribute attribute : listAttributes )
88          {
89              String value = request.getParameter( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + attribute.getIdAttribute( ) );
90  
91              if ( attribute.isMandatory( ) && ( ( value == null ) || value.equals( CONSTANT_EMPTY_STRING ) ) )
92              {
93                  return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
94              }
95          }
96  
97          return null;
98      }
99  
100     /**
101      * Create the user fields
102      * 
103      * @param nIdUser
104      *            The id of the user
105      * @param request
106      *            HttpServletRequest
107      * @param locale
108      *            locale
109      */
110     public static void doCreateUserFields( int nIdUser, HttpServletRequest request, Locale locale )
111     {
112         // Attributes created in the Back-Office
113         List<IAttribute> listAttributes = AttributeHome.findMyLuteceAttributes( locale, getMyLutecePlugin( ) );
114 
115         for ( IAttribute attribute : listAttributes )
116         {
117             List<MyLuteceUserField> listUserFields = attribute.getUserFieldsData( request, nIdUser );
118 
119             for ( MyLuteceUserField userField : listUserFields )
120             {
121                 if ( userField != null )
122                 {
123                     MyLuteceUserFieldHome.create( userField, getMyLutecePlugin( ) );
124                 }
125             }
126         }
127 
128         // Attributes associated to the plugins
129         for ( MyLuteceUserFieldListenerService myLuteceUserFieldListenerService : SpringContextService
130                 .getBeansOfType( MyLuteceUserFieldListenerService.class ) )
131         {
132             myLuteceUserFieldListenerService.doCreateUserFields( nIdUser, request, locale );
133         }
134     }
135 
136     /**
137      * Modify the user fields
138      * 
139      * @param nIdUser
140      *            The id of the user
141      * @param request
142      *            HttpServletRequest
143      * @param locale
144      *            locale
145      * @param currentUser
146      *            current user
147      */
148     public static void doModifyUserFields( int nIdUser, HttpServletRequest request, Locale locale, AdminUser currentUser )
149     {
150         // Remove all user fields
151         MyLuteceUserFieldHome.removeUserFieldsFromIdUser( nIdUser, getMyLutecePlugin( ) );
152 
153         // Attributes created in the Back-Office
154         List<IAttribute> listAttributes = AttributeHome.findMyLuteceAttributes( locale, getMyLutecePlugin( ) );
155 
156         for ( IAttribute attribute : listAttributes )
157         {
158             List<MyLuteceUserField> listUserFields = attribute.getUserFieldsData( request, nIdUser );
159 
160             for ( MyLuteceUserField userField : listUserFields )
161             {
162                 if ( userField != null )
163                 {
164                     MyLuteceUserFieldHome.create( userField, getMyLutecePlugin( ) );
165                 }
166             }
167         }
168 
169         // Attributes associated to the plugins
170         for ( MyLuteceUserFieldListenerService myLuteceUserFieldListenerService : SpringContextService
171                 .getBeansOfType( MyLuteceUserFieldListenerService.class ) )
172         {
173             myLuteceUserFieldListenerService.doModifyUserFields( nIdUser, request, locale, currentUser );
174         }
175     }
176 
177     /**
178      * Remove the user fields
179      * 
180      * @param nIdUser
181      *            The id of the user
182      * @param request
183      *            HttpServletRequest
184      * @param locale
185      *            locale
186      */
187     public static void doRemoveUserFields( int nIdUser, HttpServletRequest request, Locale locale )
188     {
189         MyLuteceUserFieldHome.removeUserFieldsFromIdUser( nIdUser, getMyLutecePlugin( ) );
190 
191         // Attributes associated to the plugins
192         for ( MyLuteceUserFieldListenerService myLuteceUserFieldListenerService : SpringContextService
193                 .getBeansOfType( MyLuteceUserFieldListenerService.class ) )
194         {
195             myLuteceUserFieldListenerService.doRemoveUserFields( nIdUser, request, locale );
196         }
197     }
198 
199     /**
200      * Remove the user fields
201      * 
202      * @param nIdUser
203      *            The id of the user
204      * @param locale
205      *            locale
206      */
207     public static void doRemoveUserFields( int nIdUser, Locale locale )
208     {
209         MyLuteceUserFieldHome.removeUserFieldsFromIdUser( nIdUser, getMyLutecePlugin( ) );
210 
211         // Attributes associated to the plugins
212         for ( MyLuteceUserFieldListenerService myLuteceUserFieldListenerService : SpringContextService
213                 .getBeansOfType( MyLuteceUserFieldListenerService.class ) )
214         {
215             myLuteceUserFieldListenerService.doRemoveUserFields( nIdUser, locale );
216         }
217     }
218 }