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.identitystore.modules.indexer.service;
35
36 import org.apache.commons.lang.StringUtils;
37
38 import fr.paris.lutece.plugins.grubusiness.business.customer.Customer;
39 import fr.paris.lutece.plugins.grubusiness.business.indexing.IIndexingService;
40 import fr.paris.lutece.plugins.grubusiness.business.indexing.IndexingException;
41 import fr.paris.lutece.plugins.identitystore.business.Identity;
42 import fr.paris.lutece.plugins.identitystore.business.IdentityAttribute;
43 import fr.paris.lutece.plugins.identitystore.business.IdentityConstants;
44 import fr.paris.lutece.plugins.identitystore.service.IdentityChange;
45 import fr.paris.lutece.plugins.identitystore.service.IdentityChangeType;
46 import fr.paris.lutece.portal.service.util.AppPropertiesService;
47 import java.util.ArrayList;
48 import java.util.List;
49
50
51
52
53 public class IdentityIndexerService implements IIdentityIndexerService
54 {
55 private static final String ATTRIBUTE_IDENTITY_USER_GENDER = AppPropertiesService.getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_GENDER );
56 private static final String ATTRIBUTE_IDENTITY_USER_NAME_GIVEN = AppPropertiesService.getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_NAME_GIVEN );
57 private static final String ATTRIBUTE_IDENTITY_USER_NAME_PREFERRED_NAME = AppPropertiesService
58 .getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_PREFERRED_NAME );
59 private static final String ATTRIBUTE_IDENTITY_USER_HOMEINFO_ONLINE_EMAIL = AppPropertiesService
60 .getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_HOMEINFO_ONLINE_EMAIL );
61 private static final String ATTRIBUTE_IDENTITY_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER = AppPropertiesService
62 .getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER );
63 private static final String ATTRIBUTE_IDENTITY_USER_HOMEINFO_TELECOM_MOBILE_NUMBER = AppPropertiesService
64 .getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_HOMEINFO_TELECOM_MOBILE_NUMBER );
65 private static final String ATTRIBUTE_IDENTITY_USER_BDATE = AppPropertiesService.getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_BDATE );
66 private static final String ATTRIBUTE_IDENTITY_USER_NAME_FAMILY_NAME = AppPropertiesService
67 .getProperty( IdentityConstants.PROPERTY_ATTRIBUTE_USER_FAMILY_NAME );
68
69 private IIndexingService<Customer> _customerIndexService;
70
71
72
73
74
75
76
77 public void setCustomerIndexService( IIndexingService<Customer> customerIndexService )
78 {
79 _customerIndexService = customerIndexService;
80 }
81
82
83
84
85 @Override
86 public void index( IdentityChange identityChange ) throws IndexingException
87 {
88 if ( ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.CREATE.getValue( ) )
89 || ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.UPDATE.getValue( ) ) )
90 {
91 index( identityChange.getIdentity( ) );
92 }
93 else
94 if ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.DELETE.getValue( ) )
95 {
96 deleteIndex( identityChange.getIdentity( ) );
97 }
98 }
99
100
101
102
103 @Override
104 public void index( List<IdentityChange> listIdentityChange ) throws IndexingException
105 {
106 List<Identity> listIdentity = new ArrayList<Identity>( );
107 for ( IdentityChange identityChange : listIdentityChange )
108 {
109 if ( ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.CREATE.getValue( ) )
110 || ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.UPDATE.getValue( ) ) )
111 {
112 listIdentity.add( identityChange.getIdentity( ) );
113 }
114 else
115 if ( identityChange.getChangeType( ).getValue( ) == IdentityChangeType.DELETE.getValue( ) )
116 {
117 deleteIndex( identityChange.getIdentity( ) );
118 }
119 }
120
121 indexIdentities( listIdentity );
122 }
123
124
125
126
127
128
129
130
131
132 private void index( Identity identity ) throws IndexingException
133 {
134 Customer customer = buildCustomer( identity );
135
136 _customerIndexService.index( customer );
137 }
138
139
140
141
142
143
144
145
146
147 private void indexIdentities( List<Identity> listIdentity ) throws IndexingException
148 {
149 List<Customer> listCustomer = new ArrayList<Customer>( );
150 for ( Identity identity : listIdentity )
151 {
152 Customer customer = buildCustomer( identity );
153 listCustomer.add( customer );
154 }
155 _customerIndexService.indexList( listCustomer );
156 }
157
158
159
160
161
162
163
164
165
166
167 private void deleteIndex( Identity identity ) throws IndexingException
168 {
169 Customer customer = buildCustomer( identity );
170
171 _customerIndexService.deleteIndex( customer );
172 }
173
174
175
176
177
178
179
180
181 private Customer buildCustomer( Identity identity )
182 {
183 Customer customer = new Customer( );
184
185 customer.setId( identity.getCustomerId( ) );
186 customer.setConnectionId( identity.getConnectionId( ) );
187
188 if ( identity.getAttributes( ) != null )
189 {
190
191 for ( IdentityAttribute attribute : identity.getAttributes( ).values( ) )
192 {
193 String strKeyName = attribute.getAttributeKey( ).getKeyName( );
194
195 if ( ATTRIBUTE_IDENTITY_USER_GENDER.equals( strKeyName ) )
196 {
197 String strGender = getAttributeValue( attribute );
198 customer.setIdTitle( StringUtils.isBlank( strGender ) || !StringUtils.isNumeric( strGender ) ? 0 : Integer.parseInt( strGender ) );
199 continue;
200 }
201
202 if ( ATTRIBUTE_IDENTITY_USER_NAME_GIVEN.equals( strKeyName ) )
203 {
204 customer.setFirstname( getAttributeValue( attribute ) );
205 continue;
206 }
207
208 if ( ATTRIBUTE_IDENTITY_USER_NAME_PREFERRED_NAME.equals( strKeyName ) )
209 {
210 customer.setLastname( getAttributeValue( attribute ) );
211 continue;
212 }
213
214 if ( ATTRIBUTE_IDENTITY_USER_NAME_FAMILY_NAME.equals( strKeyName ) )
215 {
216 customer.setFamilyname( getAttributeValue( attribute ) );
217 continue;
218 }
219
220 if ( ATTRIBUTE_IDENTITY_USER_HOMEINFO_ONLINE_EMAIL.equals( strKeyName ) )
221 {
222 customer.setEmail( getAttributeValue( attribute ) );
223 continue;
224 }
225
226 if ( ATTRIBUTE_IDENTITY_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER.equals( strKeyName ) )
227 {
228 customer.setFixedPhoneNumber( getAttributeValue( attribute ) );
229 continue;
230 }
231
232 if ( ATTRIBUTE_IDENTITY_USER_HOMEINFO_TELECOM_MOBILE_NUMBER.equals( strKeyName ) )
233 {
234 customer.setMobilePhone( getAttributeValue( attribute ) );
235 continue;
236 }
237
238 if ( ATTRIBUTE_IDENTITY_USER_BDATE.equals( strKeyName ) )
239 {
240 customer.setBirthDate( getAttributeValue( attribute ) );
241 continue;
242 }
243
244 customer.addAttributes( strKeyName, getAttributeValue( attribute ) );
245 }
246
247 }
248
249 return customer;
250 }
251
252
253
254
255
256
257
258
259 private String getAttributeValue( IdentityAttribute identityAttribute )
260 {
261 return ( identityAttribute.getValue( ) == null ) ? StringUtils.EMPTY : identityAttribute.getValue( );
262 }
263
264
265
266
267 @Override
268 public void deleteAllIndexes( ) throws IndexingException
269 {
270 _customerIndexService.deleteAllIndexes( );
271 }
272 }