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.identitypicker.service;
35
36 import fr.paris.lutece.api.user.User;
37 import fr.paris.lutece.plugins.identitypicker.business.IdentitySearchCriteria;
38 import fr.paris.lutece.plugins.identitypicker.business.Referential;
39 import fr.paris.lutece.plugins.identitypicker.business.Rules;
40 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.AttributeTreatmentType;
41 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.AuthorType;
42 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.IdentityDto;
43 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.RequestAuthor;
44 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.ResponseDto;
45 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.ResponseStatusType;
46 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.contract.ServiceContractDto;
47 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.referentiel.AttributeSearchResponse;
48 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.referentiel.LevelSearchResponse;
49 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.referentiel.ProcessusSearchResponse;
50 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.IdentitySearchRequest;
51 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.IdentitySearchResponse;
52 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.SearchAttribute;
53 import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.SearchDto;
54 import fr.paris.lutece.plugins.identitystore.v3.web.rs.util.Constants;
55 import fr.paris.lutece.plugins.identitystore.v3.web.service.IdentityService;
56 import fr.paris.lutece.plugins.identitystore.v3.web.service.ReferentialService;
57 import fr.paris.lutece.plugins.identitystore.v3.web.service.ServiceContractServiceExtended;
58 import fr.paris.lutece.plugins.identitystore.web.exception.IdentityStoreException;
59 import fr.paris.lutece.portal.service.spring.SpringContextService;
60 import fr.paris.lutece.portal.service.util.AppLogService;
61 import fr.paris.lutece.portal.service.util.AppPropertiesService;
62
63 import java.util.ArrayList;
64 import java.util.List;
65
66 public class IdentityPickerService
67 {
68
69 private static final String PROPERTY_DEFAULT_CLIENT_CODE = "identitypicker.default.client.code";
70 private static final String BEAN_IDENTITY_SERVICE = "identityService.rest.httpAccess";
71 private static final String BEAN_REFERENTIAL_SERVICE = "identity.ReferentialService";
72 private static final String BEAN_CONTRACT_SERVICE = "identity.serviceContractService";
73 private static final String ERROR_SEARCHING_IDENTITIES = "Error while searching identities: ";
74
75 private static IdentityPickerService instance;
76 private final IdentityService identityService = SpringContextService.getBean( BEAN_IDENTITY_SERVICE );
77 private final String clientCode = AppPropertiesService.getProperty( PROPERTY_DEFAULT_CLIENT_CODE );
78
79 private ReferentialService _referentialService;
80 private ServiceContractServiceExtended _serviceContract;
81
82
83
84
85 private IdentityPickerService( )
86 {
87 _referentialService = SpringContextService.getBean( BEAN_REFERENTIAL_SERVICE );
88 _serviceContract = SpringContextService.getBean( BEAN_CONTRACT_SERVICE );
89 }
90
91
92
93
94
95
96 public static IdentityPickerService getInstance( )
97 {
98 if ( instance == null )
99 {
100 instance = new IdentityPickerService( );
101
102 }
103 return instance;
104 }
105
106
107
108
109
110
111
112
113
114
115 public List<IdentityDto> searchIdentities( IdentitySearchCriteria criteria, User luteceUser ) throws IdentityStoreException
116 {
117 IdentitySearchRequest searchRequest = createSearchRequest( criteria );
118 return doSearch( searchRequest, createRequestAuthor( luteceUser ) );
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132 public IdentityDto getIdentity( String customerId, User luteceUser ) throws IdentityStoreException
133 {
134 try
135 {
136 IdentitySearchResponse response = identityService.getIdentity( customerId, clientCode, createRequestAuthor( luteceUser ) );
137 if ( isSuccess( response ) )
138 {
139 return (IdentityDto) response.getIdentities( ).get( 0 );
140 }
141 else
142 {
143 AppLogService.error( "Error while fetching identity: " + response.getStatus( ).getMessage( ) );
144 return null;
145 }
146 }
147 catch( IdentityStoreException e )
148 {
149 AppLogService.error( "Error while fetching identity", e );
150 throw e;
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163 public Rules getRules( User luteceUser ) throws IdentityStoreException
164 {
165 try
166 {
167 RequestAuthor author = createRequestAuthor( luteceUser );
168 ProcessusSearchResponse processList = _referentialService.getProcessList( clientCode, author );
169 LevelSearchResponse levelList = _referentialService.getLevelList( clientCode, author );
170 AttributeSearchResponse attributeKeyList = _referentialService.getAttributeKeyList( clientCode, author );
171 Referentialpicker/business/Referential.html#Referential">Referential referential = new Referential( processList, levelList, attributeKeyList );
172 ServiceContractDto contract = _serviceContract.getActiveServiceContract( clientCode, clientCode, createRequestAuthor( luteceUser ) )
173 .getServiceContract( );
174
175 return new Rules( referential, contract );
176 }
177 catch( IdentityStoreException e )
178 {
179 AppLogService.error( "Error while fetching referential data", e );
180 throw e;
181 }
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195 private IdentitySearchRequest createSearchRequest( IdentitySearchCriteria criteria )
196 {
197 IdentitySearchRequest searchRequest = new IdentitySearchRequest( );
198 SearchDto search = new SearchDto( );
199 List<SearchAttribute> attributes = new ArrayList<>( );
200
201 if ( criteria.hasCommonEmail( ) )
202 {
203 attributes.add( new SearchAttribute( Constants.PARAM_COMMON_EMAIL, criteria.getCommonEmail( ), AttributeTreatmentType.STRICT ) );
204 }
205 else
206 {
207 attributes.add( new SearchAttribute( Constants.PARAM_FIRST_NAME, criteria.getFirstName( ), AttributeTreatmentType.APPROXIMATED ) );
208 attributes.add( new SearchAttribute( Constants.PARAM_COMMON_LASTNAME, criteria.getCommonLastName( ), AttributeTreatmentType.APPROXIMATED ) );
209 attributes.add( new SearchAttribute( Constants.PARAM_BIRTH_DATE, criteria.getBirthDate( ), AttributeTreatmentType.STRICT ) );
210 }
211
212 search.setAttributes( attributes );
213 searchRequest.setSearch( search );
214 return searchRequest;
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228 private List<IdentityDto> doSearch( IdentitySearchRequest searchRequest, RequestAuthor author ) throws IdentityStoreException
229 {
230 try
231 {
232 IdentitySearchResponse searchResponse = identityService.searchIdentities( searchRequest, clientCode, author );
233 if ( isSuccess( searchResponse ) )
234 {
235 return searchResponse.getIdentities( );
236 }
237 else
238 {
239 AppLogService.error( ERROR_SEARCHING_IDENTITIES + searchResponse.getStatus( ).getMessage( ) );
240 return new ArrayList<>( );
241 }
242 }
243 catch( IdentityStoreException e )
244 {
245 AppLogService.error( ERROR_SEARCHING_IDENTITIES, e );
246 throw e;
247 }
248 }
249
250
251
252
253
254
255 private RequestAuthor createRequestAuthor( User luteceUser )
256 {
257 RequestAuthor author = new RequestAuthor( );
258 author.setName( luteceUser.getEmail( ) );
259 author.setType( AuthorType.application );
260 return author;
261 }
262
263
264
265
266
267
268
269
270 private boolean isSuccess( final ResponseDto apiResponse )
271 {
272 return apiResponse != null && ( apiResponse.getStatus( ).getType( ) == ResponseStatusType.SUCCESS
273 || apiResponse.getStatus( ).getType( ) == ResponseStatusType.INCOMPLETE_SUCCESS
274 || apiResponse.getStatus( ).getType( ) == ResponseStatusType.OK );
275 }
276 }