1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
64
65 private CRMUserAttributesService( )
66 {
67 }
68
69
70
71
72
73
74 public static CRMUserAttributesService getService( )
75 {
76 return SpringContextService.getBean( BEAN_CRM_CRMUSERATTRIBUTESSERVICE );
77 }
78
79
80
81
82
83
84
85 public void setCRMUserService( CRMUserService crmUserService )
86 {
87 _crmUserService = crmUserService;
88 }
89
90
91
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
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
131
132
133
134
135
136 public Map<String, String> getAttributes( int nIdCRMUser )
137 {
138 return CRMUserAttributeHome.findByPrimaryKey( nIdCRMUser );
139 }
140
141
142
143
144
145
146
147 public void remove( int nIdCRMUser )
148 {
149 CRMUserAttributeHome.remove( nIdCRMUser );
150 }
151
152
153
154
155
156
157
158
159
160
161
162 public void create( int nIdCRMUser, String strUserAttributeKey, String strUserAttributeValue )
163 {
164 CRMUserAttributeHome.create( nIdCRMUser, strUserAttributeKey, strUserAttributeValue );
165 }
166
167
168
169
170
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
195
196
197
198
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
214
215
216
217
218
219
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 }