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.customerprovisioning.modules.identitystore.services;
35
36 import fr.paris.lutece.plugins.customerprovisioning.services.ICustomerInfoService;
37 import fr.paris.lutece.plugins.grubusiness.business.customer.Customer;
38 import fr.paris.lutece.plugins.identitystore.web.exception.IdentityNotFoundException;
39 import fr.paris.lutece.plugins.identitystore.web.rs.dto.AttributeDto;
40 import fr.paris.lutece.plugins.identitystore.web.rs.dto.AuthorDto;
41 import fr.paris.lutece.plugins.identitystore.web.rs.dto.IdentityChangeDto;
42 import fr.paris.lutece.plugins.identitystore.web.rs.dto.IdentityDto;
43 import fr.paris.lutece.plugins.identitystore.web.service.IdentityService;
44 import fr.paris.lutece.portal.service.util.AppPropertiesService;
45
46 import org.apache.commons.lang.StringUtils;
47
48 import java.util.HashMap;
49 import java.util.Map;
50
51 import javax.inject.Inject;
52 import javax.inject.Named;
53
54
55
56
57
58
59 public class IdentityStoreCustomerInfoService implements ICustomerInfoService
60 {
61 private static final String ATTRIBUTE_USER_NAME_GIVEN = "customerprovisioning.identity.attribute.user.name.given";
62 private static final String ATTRIBUTE_USER_NAME_FAMILLY = "customerprovisioning.identity.attribute.user.name.family";
63 private static final String ATTRIBUTE_USER_HOMEINFO_ONLINE_EMAIL = "customerprovisioning.identity.attribute.user.home-info.online.email";
64 private static final String ATTRIBUTE_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER = "customerprovisioning.identity.attribute.user.home-info.telecom.telephone.number";
65 private static final String ATTRIBUTE_USER_HOMEINFO_TELECOM_MOBILE_NUMBER = "customerprovisioning.identity.attribute.user.home-info.telecom.mobile.number";
66 private static final String ATTRIBUTE_IDENTITY_NAME_GIVEN = AppPropertiesService.getProperty( ATTRIBUTE_USER_NAME_GIVEN );
67 private static final String ATTRIBUTE_IDENTITY_NAME_FAMILLY = AppPropertiesService.getProperty( ATTRIBUTE_USER_NAME_FAMILLY );
68 private static final String ATTRIBUTE_IDENTITY_HOMEINFO_ONLINE_EMAIL = AppPropertiesService.getProperty( ATTRIBUTE_USER_HOMEINFO_ONLINE_EMAIL );
69 private static final String ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_TELEPHONE_NUMBER = AppPropertiesService.getProperty( ATTRIBUTE_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER );
70 private static final String ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_MOBILE_NUMBER = AppPropertiesService.getProperty( ATTRIBUTE_USER_HOMEINFO_TELECOM_MOBILE_NUMBER );
71
72
73 private static final String APPLICATION_CODE = "CustomerProvisioning";
74
75
76 private static final String BEAN_IDENTITYSTORE_SERVICE = "customerprovisioning.identitystore.service";
77 @Inject
78 @Named( BEAN_IDENTITYSTORE_SERVICE )
79 private IdentityService _identityService;
80
81
82
83
84 public IdentityStoreCustomerInfoService( )
85 {
86 super( );
87 }
88
89 @Override
90 public Customer getCustomerByGuid( String strGuid )
91 {
92 Customer customer = null;
93
94 try
95 {
96 IdentityDto identityDto = _identityService.getIdentityByConnectionId( strGuid, APPLICATION_CODE );
97 customer = identityDtoToCustomer( identityDto );
98 }
99 catch ( IdentityNotFoundException e )
100 {
101
102 }
103
104 return customer;
105 }
106
107 @Override
108 public Customer getCustomerByCid( String strCustomerId )
109 {
110 Customer customer = null;
111
112 try
113 {
114
115 IdentityDto identityDto = _identityService.getIdentityByCustomerId( strCustomerId, APPLICATION_CODE );
116 customer = identityDtoToCustomer( identityDto );
117 }
118 catch ( NumberFormatException e )
119 {
120 throw new IllegalArgumentException( e );
121 }
122 catch ( IdentityNotFoundException e )
123 {
124
125 }
126
127 return customer;
128 }
129
130 @Override
131 public Customer createCustomer( Customer customer )
132 {
133 IdentityChangeDto identityChangeDto = new IdentityChangeDto( );
134 IdentityDto identityDto = customerToIdentityDto( customer );
135
136 identityChangeDto.setIdentity( identityDto );
137
138 AuthorDto authorDto = new AuthorDto( );
139 authorDto.setApplicationCode( APPLICATION_CODE );
140
141 identityChangeDto.setAuthor( authorDto );
142
143 identityDto = _identityService.createIdentity( identityChangeDto );
144
145 customer.setId( identityDto.getCustomerId( ) );
146
147 return customer;
148 }
149
150
151
152
153
154
155 private IdentityDto customerToIdentityDto( Customer customer )
156 {
157 IdentityDto identityDto = new IdentityDto( );
158 Map<String, AttributeDto> mapAttributes = new HashMap<String, AttributeDto>( );
159
160 identityDto.setConnectionId( customer.getConnectionId( ) );
161 identityDto.setCustomerId( customer.getId( ) );
162 identityDto.setAttributes( mapAttributes );
163
164 setAttribute( identityDto, ATTRIBUTE_IDENTITY_NAME_GIVEN, customer.getFirstname( ) );
165 setAttribute( identityDto, ATTRIBUTE_IDENTITY_NAME_FAMILLY, customer.getLastname( ) );
166 setAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_ONLINE_EMAIL, customer.getEmail( ) );
167 setAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_TELEPHONE_NUMBER,
168 customer.getFixedPhoneNumber( ) );
169 setAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_MOBILE_NUMBER, customer.getMobilePhone( ) );
170
171 return identityDto;
172 }
173
174
175
176
177
178
179 private Customer identityDtoToCustomer( IdentityDto identityDto )
180 {
181 Customer customer = new Customer( );
182
183 customer.setConnectionId( identityDto.getConnectionId( ) );
184 customer.setId( identityDto.getCustomerId( ) );
185 customer.setFirstname( getAttribute( identityDto, ATTRIBUTE_IDENTITY_NAME_GIVEN ) );
186 customer.setLastname( getAttribute( identityDto, ATTRIBUTE_IDENTITY_NAME_FAMILLY ) );
187 customer.setEmail( getAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_ONLINE_EMAIL ) );
188 customer.setFixedPhoneNumber( getAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_TELEPHONE_NUMBER ) );
189 customer.setMobilePhone( getAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_MOBILE_NUMBER ) );
190
191 return customer;
192 }
193
194
195
196
197
198
199
200 private void setAttribute( IdentityDto identityDto, String strCode, String strValue )
201 {
202 AttributeDto attributeDto = new AttributeDto( );
203 attributeDto.setKey( strCode );
204 attributeDto.setValue( strValue );
205
206 identityDto.getAttributes( ).put( attributeDto.getKey( ), attributeDto );
207 }
208
209
210
211
212
213
214
215 private String getAttribute( IdentityDto identityDto, String strCode )
216 {
217 AttributeDto attribute = identityDto.getAttributes( ).get( strCode );
218
219 return ( attribute == null ) ? null : attribute.getValue( );
220 }
221 }