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.business.user.AdminUser;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.portal.service.user.attribute.AttributeService;
39  
40  import org.apache.commons.lang3.StringUtils;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  import java.util.Locale;
45  
46  import javax.servlet.http.HttpServletRequest;
47  
48  /**
49   * Implementation of AdminUserFieldListener that allow attributes to be exported.
50   */
51  public abstract class SimpleAdminUserFieldListener implements AdminUserFieldListener
52  {
53      /**
54       * Get the plugin
55       * 
56       * @return The plugin
57       */
58      public abstract Plugin getPlugin( );
59  
60      /**
61       * Create user fields
62       * 
63       * @param user
64       *            Adminuser
65       * @param listUserFields
66       *            The list of user fields to create
67       * @param locale
68       *            locale
69       */
70      public abstract void doCreateUserFields( AdminUser user, List<AdminUserField> listUserFields, Locale locale );
71  
72      /**
73       * Modify user fields
74       * 
75       * @param user
76       *            Adminuser
77       * @param listUserFields
78       *            The list of user fields to modify
79       * @param locale
80       *            locale
81       * @param currentUser
82       *            current user
83       */
84      public abstract void doModifyUserFields( AdminUser/AdminUser.html#AdminUser">AdminUser user, List<AdminUserField> listUserFields, Locale locale, AdminUser currentUser );
85  
86      /**
87       * Remove user fields
88       * 
89       * @param user
90       *            Adminuser
91       * @param locale
92       *            locale
93       */
94      public abstract void doRemoveUserFields( AdminUser user, Locale locale );
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public void doCreateUserFields( AdminUser user, HttpServletRequest request, Locale locale )
101     {
102         List<IAttribute> listAttributes = AttributeService.getInstance( ).getPluginAttributesWithoutFields( getPlugin( ).getName( ), locale );
103         List<AdminUserField> listUserFields = new ArrayList<>( );
104 
105         for ( IAttribute attribute : listAttributes )
106         {
107             List<AdminUserField> userFields = attribute.getUserFieldsData( request, user );
108 
109             for ( AdminUserField userField : userFields )
110             {
111                 if ( ( userField != null ) && StringUtils.isNotBlank( userField.getValue( ) ) )
112                 {
113                     // Change the value of the user field
114                     // Instead of having the ID of the attribute field, we put the attribute field title
115                     // which represents the profile's ID
116                     userField.setValue( userField.getAttributeField( ).getTitle( ) );
117                     AdminUserFieldHome.create( userField );
118                     listUserFields.add( userField );
119                 }
120             }
121         }
122 
123         doCreateUserFields( user, listUserFields, locale );
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public void doModifyUserFields( AdminUserl/business/user/AdminUser.html#AdminUser">AdminUser user, HttpServletRequest request, Locale locale, AdminUser currentUser )
131     {
132         List<IAttribute> listAttributes = AttributeService.getInstance( ).getPluginAttributesWithoutFields( getPlugin( ).getName( ), locale );
133         List<AdminUserField> listUserFields = new ArrayList<>( );
134 
135         for ( IAttribute attribute : listAttributes )
136         {
137             List<AdminUserField> userFields = attribute.getUserFieldsData( request, user );
138 
139             for ( AdminUserField userField : userFields )
140             {
141                 if ( ( userField != null ) && StringUtils.isNotBlank( userField.getValue( ) ) )
142                 {
143                     // Change the value of the user field
144                     // Instead of having the ID of the attribute field, we put the attribute field title
145                     // which represents the profile's ID
146                     userField.setValue( userField.getAttributeField( ).getTitle( ) );
147                     AdminUserFieldHome.create( userField );
148                     listUserFields.add( userField );
149                 }
150             }
151         }
152 
153         doModifyUserFields( user, listUserFields, locale, currentUser );
154     }
155 
156     /**
157      * Remove user fields
158      * 
159      * @param user
160      *            Adminuser
161      * @param request
162      *            HttpServletRequest
163      * @param locale
164      *            locale
165      */
166     public void doRemoveUserFields( AdminUser user, HttpServletRequest request, Locale locale )
167     {
168         doRemoveUserFields( user, locale );
169     }
170 }