View Javadoc
1   /*
2    * Copyright (c) 2002-2024, City of 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.identitystore.v3.web.service;
35  
36  import fr.paris.lutece.plugins.identitystore.v3.business.IExternalAttributeSource;
37  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.AttributeDto;
38  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.IdentityDto;
39  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.RequestAuthor;
40  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.crud.IdentityChangeRequest;
41  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.crud.IdentityChangeResponse;
42  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.exporting.IdentityExportRequest;
43  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.exporting.IdentityExportResponse;
44  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityHistoryGetResponse;
45  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityHistorySearchRequest;
46  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityHistorySearchResponse;
47  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.IdentitySearchRequest;
48  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.IdentitySearchResponse;
49  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.UpdatedIdentitySearchRequest;
50  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.UpdatedIdentitySearchResponse;
51  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.task.IdentityTaskCreateRequest;
52  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.task.IdentityTaskCreateResponse;
53  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.task.IdentityTaskGetResponse;
54  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.task.IdentityTaskGetStatusResponse;
55  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.task.IdentityTaskListGetResponse;
56  import fr.paris.lutece.plugins.identitystore.web.exception.IdentityStoreException;
57  import fr.paris.lutece.portal.service.util.AppException;
58  
59  import java.util.List;
60  
61  /**
62   * IdentityService
63   */
64  public class IdentityService
65  {
66      /** transport provider */
67      protected IIdentityTransportProvider _transportProvider;
68      protected List<IExternalAttributeSource> _listExternalAttributesSource;
69  
70      /**
71       * Simple Constructor
72       */
73      public IdentityService( )
74      {
75          super( );
76      }
77  
78      /**
79       * Constructor with IIdentityTransportProvider in parameters
80       * 
81       * @param transportProvider
82       *            IIdentityTransportProvider
83       */
84      public IdentityService( IIdentityTransportProvider transportProvider )
85      {
86          super( );
87          this._transportProvider = transportProvider;
88      }
89  
90      /**
91       * setter of transportProvider parameter
92       * 
93       * @param transportProvider
94       *            IIdentityTransportProvider
95       */
96      public void setTransportProvider( IIdentityTransportProvider transportProvider )
97      {
98          this._transportProvider = transportProvider;
99      }
100 
101     /**
102      * setter of additional attribute source parameter
103      * 
104      * @param listExternalAttributesSource
105      */
106     public void setExternalAttributesSourceList( List<IExternalAttributeSource> listExternalAttributesSource )
107     {
108         this._listExternalAttributesSource = listExternalAttributesSource;
109     }
110 
111     /**
112      * get identity matching connectionId for provided application code
113      *
114      * @param strConnectionId
115      *            connection Id
116      * @param strClientCode
117      *            client code of calling application
118      * @param author
119      *            the author of the request
120      * @return identity if found
121      * @throws AppException
122      *             if inconsitent parmeters provided, or errors occurs...
123      * @throws IdentityStoreException
124      */
125     public IdentitySearchResponse getIdentityByConnectionId( final String strConnectionId, final String strClientCode, final RequestAuthor author )
126             throws AppException, IdentityStoreException
127     {
128         final IdentitySearchRequest request = new IdentitySearchRequest( );
129         request.setConnectionId( strConnectionId );
130         return searchIdentities( request, strClientCode, author );
131     }
132 
133     /**
134      * get identity matching customerId for provided application code
135      *
136      * @param strCustomerId
137      *            customer Id
138      * @param strClientCode
139      *            client code of calling application
140      * @param author
141      *            the author of the request
142      * @return identity if found
143      * @throws AppException
144      *             if inconsitent parmeters provided, or errors occurs...
145      * @throws IdentityStoreException
146      */
147     public IdentitySearchResponse getIdentityByCustomerId( final String strCustomerId, final String strClientCode, final RequestAuthor author )
148             throws AppException, IdentityStoreException
149     {
150         return getIdentity( strCustomerId, strClientCode, author );
151     }
152 
153     /**
154      * get identity matching connectionId and customerId for provided application code
155      *
156      * @param strCustomerId
157      *            customer Id (can be null if strConnectionId is provided)
158      * @param strClientCode
159      *            client code of calling application
160      * @param author
161      *            the author of the request
162      * @return identity if found
163      * @throws AppException
164      *             if inconsistent parameters provided, or errors occurs...
165      * @throws IdentityStoreException
166      */
167     public IdentitySearchResponse getIdentity( final String strCustomerId, final String strClientCode, final RequestAuthor author )
168             throws AppException, IdentityStoreException
169     {
170         IdentitySearchResponse identitySearchResponse = _transportProvider.getIdentity( strCustomerId, strClientCode, author );
171 
172         return identitySearchResponseWithAdditionnalData( identitySearchResponse );
173     }
174 
175     /**
176      * apply changes to an identity
177      *
178      * @param customerId
179      *            the id of the customer
180      * @param identityChange
181      *            change to apply to identity
182      * @param strClientCode
183      *            client code of calling application
184      * @param author
185      *            the author of the request
186      * @return the updated identity
187      * @throws AppException
188      *             if error occured while updating identity
189      * @throws IdentityStoreException
190      */
191     public IdentityChangeResponse updateIdentity( final String customerId, final IdentityChangeRequest identityChange, final String strClientCode,
192             final RequestAuthor author ) throws AppException, IdentityStoreException
193     {
194         return _transportProvider.updateIdentity( customerId, identityChange, strClientCode, author );
195     }
196 
197     /**
198      * Creates an identity only if the identity does not already exist. The identity is created from the provided attributes.
199      * <p>
200      * The order to test if the identity exists: - by using the provided customer id if present - by using the provided connection id if present
201      *
202      * @param identityChange
203      *            change to apply to identity
204      * @param strClientCode
205      *            client code of calling application
206      * @param author
207      *            the author of the request
208      * @return the created identity
209      * @throws AppException
210      *             if error occured while updating identity
211      * @throws IdentityStoreException
212      */
213     public IdentityChangeResponse createIdentity( final IdentityChangeRequest identityChange, final String strClientCode, final RequestAuthor author )
214             throws AppException, IdentityStoreException
215     {
216         return _transportProvider.createIdentity( identityChange, strClientCode, author );
217     }
218 
219     /**
220      * Deletes an identity from the specified customer_Id
221      * 
222      * @param strCustomerId
223      *            the customer id
224      * @param strClientCode
225      *            client code of calling application
226      * @param author
227      *            the author of the request
228      * @throws IdentityStoreException
229      */
230     public IdentityChangeResponse deleteIdentity( final String strCustomerId, final String strClientCode, final RequestAuthor author )
231             throws IdentityStoreException
232     {
233         return _transportProvider.deleteIdentity( strCustomerId, strClientCode, author );
234     }
235 
236     /**
237      * returns a list of identity from combination of attributes
238      *
239      * @param identitySearchRequest
240      *            search request tpo perform
241      * @param strClientCode
242      *            client code of calling application
243      * @param author
244      *            the author of the request
245      * @return identity filled according to application rights for user identified by connection id
246      * @throws IdentityStoreException
247      */
248     public IdentitySearchResponse searchIdentities( final IdentitySearchRequest identitySearchRequest, final String strClientCode, final RequestAuthor author )
249             throws IdentityStoreException
250     {
251         return _transportProvider.searchIdentities( identitySearchRequest, strClientCode, author );
252     }
253 
254     /**
255      * Gives the identity history (identity+attributes) from a customerID
256      *
257      * @param strCustomerId
258      *            customerID
259      * @param strClientCode
260      *            client code of calling application
261      * @param author
262      *            the author of the request
263      * @return the history
264      */
265     public IdentityHistoryGetResponse getIdentityHistory( final String strCustomerId, final String strClientCode, final RequestAuthor author )
266             throws IdentityStoreException
267     {
268         return _transportProvider.getIdentityHistory( strCustomerId, strClientCode, author );
269     }
270 
271     /**
272      * Search for identities history according to given request
273      *
274      * @param request
275      *            request
276      * @param strClientCode
277      *            client code of calling application
278      * @param author
279      *            the author of the request
280      * @return the history
281      */
282     public IdentityHistorySearchResponse searchIdentityHistory( final IdentityHistorySearchRequest request, final String strClientCode,
283             final RequestAuthor author ) throws IdentityStoreException
284     {
285         return _transportProvider.searchIdentityHistory( request, strClientCode, author );
286     }
287 
288     /**
289      * get identities that have been updated, according to given request.
290      * 
291      * @param request
292      *            the request
293      * @param strClientCode
294      *            client code of calling application
295      * @param author
296      *            the author of the request
297      * @return the list of identities
298      */
299     public UpdatedIdentitySearchResponse getUpdatedIdentities( final UpdatedIdentitySearchRequest request, final String strClientCode,
300             final RequestAuthor author ) throws IdentityStoreException
301     {
302         return _transportProvider.getUpdatedIdentities( request, strClientCode, author );
303     }
304 
305     /**
306      * Exports a list of identities according to the provided request and client code.
307      *
308      * @param request
309      *            the export request
310      * @param strClientCode
311      *            the client code
312      * @return IdentityExportResponse
313      */
314     public IdentityExportResponse exportIdentities( final IdentityExportRequest request, final String strClientCode, final RequestAuthor author )
315             throws IdentityStoreException
316     {
317         return _transportProvider.exportIdentities( request, strClientCode, author );
318     }
319 
320     /**
321      * Create a task related to an {@link fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.IdentityDto} by its customer ID.
322      * @param request the task creation request
323      * @param strClientCode the client code calling the request
324      * @param author the author of the request
325      * @return the code of the created task used to identify that task for further usage
326      * @throws IdentityStoreException in case of error
327      */
328     public IdentityTaskCreateResponse createIdentityTask(final IdentityTaskCreateRequest request, final String strClientCode, final RequestAuthor author ) throws IdentityStoreException
329     {
330         return _transportProvider.createIdentityTask( request, strClientCode, author );
331     }
332 
333     /**
334      * Get the {@link fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.task.IdentityTaskStatusType} of the task.
335      * @param taskCode the code of the task returned at creation time
336      * @param strClientCode the client code calling the request
337      * @param author the author of the request
338      * @return an {@link IdentityTaskGetStatusResponse}
339      * @throws IdentityStoreException in case of error
340      */
341     public IdentityTaskGetStatusResponse getIdentityTaskStatus( final String taskCode, final String strClientCode, final RequestAuthor author ) throws IdentityStoreException
342     {
343         return _transportProvider.getIdentityTaskStatus( taskCode, strClientCode, author );
344     }
345 
346     /**
347      * Get the tasks related to a given resource.
348      * @param resourceId the ID of the resource
349      * @param resourceType the type of the resource
350      * @param strClientCode the client code calling the request
351      * @param author the author of the request
352      * @return an {@link IdentityTaskGetResponse}
353      * @throws IdentityStoreException in case of error
354      */
355     public IdentityTaskListGetResponse getIdentityTaskList(final String resourceId, final String resourceType, final String strClientCode, final RequestAuthor author ) throws IdentityStoreException
356     {
357         return _transportProvider.getIdentityTaskList( resourceId, resourceType, strClientCode, author );
358     }
359 
360 
361     /**
362      * Complete attribute list with external sources
363      * 
364      * @param identitySearchResponse
365      *            the search response to be completed
366      * @return the IdentitySearchResponse
367      */
368     private IdentitySearchResponse identitySearchResponseWithAdditionnalData( final IdentitySearchResponse identitySearchResponse )
369             throws IdentityStoreException
370     {
371         // none
372         if ( _listExternalAttributesSource == null )
373         {
374             return identitySearchResponse;
375         }
376 
377         try
378         {
379             // Check all external sources
380             for ( IExternalAttributeSource source : _listExternalAttributesSource )
381             {
382                 // fill each identity
383                 for ( final IdentityDto identity : identitySearchResponse.getIdentities( ) )
384                 {
385                     List<AttributeDto> listAdditionnalAttributes = source.getAdditionnalAttributes( identity.getCustomerId( ) );
386 
387                     identity.getAttributes( ).addAll( listAdditionnalAttributes );
388                 }
389             }
390         }
391         catch( Exception e )
392         {
393             throw new IdentityStoreException( e.getMessage( ) );
394         }
395 
396         return identitySearchResponse;
397     }
398 
399 }