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.plugins.crm.service.user;
35  
36  import fr.paris.lutece.plugins.crm.business.user.CRMUser;
37  import fr.paris.lutece.plugins.crm.business.user.CRMUserAttributeHome;
38  import fr.paris.lutece.plugins.crm.util.constants.CRMConstants;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.security.UserAttributesService;
41  import fr.paris.lutece.portal.service.spring.SpringContextService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  
44  import java.util.ArrayList;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Locale;
48  import java.util.Map;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  /**
53   *
54   * CRMUserAttributesService
55   *
56   */
57  public final class CRMUserAttributesService implements UserAttributesService
58  {
59      private static final String BEAN_CRM_CRMUSERATTRIBUTESSERVICE = "crm.crmUserAttributesService";
60      private CRMUserService _crmUserService;
61  
62      /**
63       * Private constructor
64       */
65      private CRMUserAttributesService( )
66      {
67      }
68  
69      /**
70       * Get the instance of CRMUserAttributesService
71       * 
72       * @return the instance of {@link CRMUserAttributesService}
73       */
74      public static CRMUserAttributesService getService( )
75      {
76          return SpringContextService.getBean( BEAN_CRM_CRMUSERATTRIBUTESSERVICE );
77      }
78  
79      /**
80       * Set the CRMUserService
81       * 
82       * @param crmUserService
83       *            the CRMUserService
84       */
85      public void setCRMUserService( CRMUserService crmUserService )
86      {
87          _crmUserService = crmUserService;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public String getAttribute( String strUserId, String strAttribute )
95      {
96          String strAttributeValue = StringUtils.EMPTY;
97          CRMUser user = _crmUserService.findByUserGuid( strUserId );
98  
99          if ( ( user != null ) && ( user.getUserAttributes( ) != null ) )
100         {
101             strAttributeValue = user.getUserAttributes( ).get( strAttribute );
102 
103             if ( StringUtils.isBlank( strAttributeValue ) )
104             {
105                 strAttributeValue = StringUtils.EMPTY;
106             }
107         }
108 
109         return strAttributeValue;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public Map<String, String> getAttributes( String strUserId )
117     {
118         Map<String, String> listAttributes = new HashMap<String, String>( );
119         CRMUser user = _crmUserService.findByUserGuid( strUserId );
120 
121         if ( user != null )
122         {
123             listAttributes = user.getUserAttributes( );
124         }
125 
126         return listAttributes;
127     }
128 
129     /**
130      * Get the user attributes from a given id crm user
131      * 
132      * @param nIdCRMUser
133      *            the id crm user
134      * @return a map of user attribute key, user attribute value
135      */
136     public Map<String, String> getAttributes( int nIdCRMUser )
137     {
138         return CRMUserAttributeHome.findByPrimaryKey( nIdCRMUser );
139     }
140 
141     /**
142      * Remove the user attributes of the crm user
143      * 
144      * @param nIdCRMUser
145      *            the id crm user
146      */
147     public void remove( int nIdCRMUser )
148     {
149         CRMUserAttributeHome.remove( nIdCRMUser );
150     }
151 
152     /**
153      * Create a new user attribute
154      * 
155      * @param nIdCRMUser
156      *            the id crm user
157      * @param strUserAttributeKey
158      *            the user attribute key
159      * @param strUserAttributeValue
160      *            the user attribute value
161      */
162     public void create( int nIdCRMUser, String strUserAttributeKey, String strUserAttributeValue )
163     {
164         CRMUserAttributeHome.create( nIdCRMUser, strUserAttributeKey, strUserAttributeValue );
165     }
166 
167     /**
168      * Get the list of user attribute keys defined in <b>crm.properties</b>
169      * 
170      * @return a list of user attribute keys
171      */
172     public List<String> getUserAttributeKeys( )
173     {
174         List<String> listUserAttributeKeys = new ArrayList<String>( );
175         String strUserAttributeKeys = AppPropertiesService.getProperty( CRMConstants.PROPERTY_CRM_USER_ATTRIBUTE_KEYS );
176 
177         if ( StringUtils.isNotBlank( strUserAttributeKeys ) )
178         {
179             String [ ] userAttributeKeys = strUserAttributeKeys.split( CRMConstants.COMMA );
180 
181             if ( ( userAttributeKeys != null ) && ( userAttributeKeys.length > 0 ) )
182             {
183                 for ( String strUserAttributeKey : userAttributeKeys )
184                 {
185                     listUserAttributeKeys.add( strUserAttributeKey );
186                 }
187             }
188         }
189 
190         return listUserAttributeKeys;
191     }
192 
193     /**
194      * Gets the user attribute key labels.
195      *
196      * @param locale
197      *            the locale
198      * @return the user attribute key labels
199      */
200     public List<String> getUserAttributeKeyLabels( Locale locale )
201     {
202         List<String> listLabels = new ArrayList<String>( );
203 
204         for ( String strAttributeKey : getUserAttributeKeys( ) )
205         {
206             listLabels.add( I18nService.getLocalizedString( strAttributeKey, locale ) );
207         }
208 
209         return listLabels;
210     }
211 
212     /**
213      * Check if a value of an attribute is used by a user for a given attribute key
214      * 
215      * @param strUserAttributeValue
216      *            The value of the attribute
217      * @param strUserAttributeKey
218      *            The key of the attribute
219      * @return True if a user has an attribute matching the given key and value, false otherwise. Also return false if the given attribute key is null or empty.
220      */
221     public boolean isAttributeValueInUse( String strAttributeValue, String strUserAttributeKey )
222     {
223         if ( StringUtils.isEmpty( strUserAttributeKey ) )
224         {
225             return false;
226         }
227         return CRMUserAttributeHome.countAttributeValueInstances( strAttributeValue, strUserAttributeKey ) > 0;
228     }
229 }