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