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.business.identity;
35  
36  import fr.paris.lutece.plugins.identitystore.service.IdentityStorePlugin;
37  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.crud.UpdatedIdentityDto;
38  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityChange;
39  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityChangeType;
40  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.SearchAttribute;
41  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.SearchUpdatedAttribute;
42  import fr.paris.lutece.plugins.identitystore.web.exception.IdentityStoreException;
43  import fr.paris.lutece.portal.service.plugin.Plugin;
44  import fr.paris.lutece.portal.service.plugin.PluginService;
45  import fr.paris.lutece.portal.service.spring.SpringContextService;
46  import fr.paris.lutece.portal.service.util.AppPropertiesService;
47  import org.apache.commons.lang3.tuple.Pair;
48  
49  import java.sql.Timestamp;
50  import java.util.Date;
51  import java.util.List;
52  import java.util.Map;
53  import java.util.Objects;
54  
55  /**
56   * This class provides instances management methods (create, find, ...) for Identity objects
57   */
58  public final class IdentityHome
59  {
60      private static final int PROPERTY_MAX_NB_IDENTITY_RETURNED = AppPropertiesService.getPropertyInt("identitystore.search.maxNbIdentityReturned", 0);
61  
62      // Static variable pointed at the DAO instance
63      private static final IIdentityDAO _dao = SpringContextService.getBean( IIdentityDAO.BEAN_NAME );
64      private static final Plugin _plugin = PluginService.getPlugin( IdentityStorePlugin.PLUGIN_NAME );
65  
66      /**
67       * Private constructor - this class need not be instantiated
68       */
69      private IdentityHome( )
70      {
71      }
72  
73      /**
74       * Create an instance of the identity class
75       *
76       * @param identity
77       *            The instance of the Identity which contains the informations to store
78       * @return The instance of identity which has been created with its primary key.
79       */
80      public static Identitys/identitystore/business/identity/Identity.html#Identity">Identity create( final Identity identity, final int dataRetentionPeriodInMonth )
81      {
82          _dao.insert( identity, dataRetentionPeriodInMonth, _plugin );
83  
84          return identity;
85      }
86  
87      /**
88       * Update of the identity which is specified in parameter
89       *
90       * @param identity
91       *            The instance of the Identity which contains the data to store
92       * @return The instance of the identity which has been updated
93       */
94      public static Identity../../../../../../../fr/paris/lutece/plugins/identitystore/business/identity/Identity.html#Identity">Identity update( Identity identity )
95      {
96          _dao.store( identity, _plugin );
97  
98          return identity;
99      }
100 
101     /**
102      * Archive identities which have been merge as secondary
103      *
104      * @param identity
105      * @return
106      */
107     public static Identity"../../../../../../../fr/paris/lutece/plugins/identitystore/business/identity/Identity.html#Identity">Identity merge( Identity identity )
108     {
109         _dao.merge( identity, _plugin );
110         IdentityAttributeHome.removeAllAttributes( identity.getId() );
111 
112         return identity;
113     }
114 
115     public static void cancelMerge( Identity identity )
116     {
117         _dao.cancelMerge( identity, _plugin );
118     }
119 
120     /**
121      * Remove the identity whose identifier is specified in parameter
122      *
123      * @param nIdentityId
124      *            The identity Id
125      */
126     public static void hardRemove( int nIdentityId )
127     {
128         IdentityAttributeHome.removeAllAttributes( nIdentityId );
129         _dao.hardDelete( nIdentityId, _plugin );
130     }
131 
132     public static void softRemove( String strCuid )
133     {
134         _dao.softDelete( strCuid, _plugin );
135     }
136 
137     /**
138      * Find an identity ID from the specified customer ID
139      *
140      * @param strCustomerId
141      *            the customer ID
142      * @return the identity ID
143      */
144     public static int findIdByCustomerId( String strCustomerId )
145     {
146         return _dao.selectIdByCustomerId( strCustomerId, _plugin );
147     }
148 
149     /**
150      * Returns an instance of a identity whose identifier is specified in parameter
151      *
152      * @param nKey
153      *            The identity primary key
154      * @return an instance of Identity
155      */
156     public static Identity findByPrimaryKey( int nKey )
157     {
158         return _dao.load( nKey, _plugin );
159     }
160 
161     /**
162      * Select All {@link Identity}
163      *
164      * @return The Identity
165      */
166     public static List<Identity> findAll( )
167     {
168         final List<Identity> identities = _dao.selectAll( _plugin );
169         identities.forEach( identity -> identity.setAttributes( IdentityAttributeHome.getAttributes( identity.getId( ) ) ) );
170         return identities;
171     }
172 
173     /**
174      * Find by connection ID
175      *
176      * @param strConnectionId
177      *            The connection ID
178      * @return The Identity
179      */
180     public static Identity findByConnectionId( String strConnectionId )
181     {
182         return _dao.selectByConnectionId( strConnectionId, _plugin );
183     }
184 
185     /**
186      * Find history by customer ID
187      *
188      * @param strCustomerId
189      *            The customer ID
190      * @return The Identity
191      */
192     public static List<IdentityChange> findHistoryByCustomerId( String strCustomerId ) throws IdentityStoreException
193     {
194         return _dao.selectIdentityHistoryByCustomerId( strCustomerId, _plugin );
195     }
196 
197     /**
198      * Find history by customer search parameters
199      *
200      * @param strCustomerId
201      *            The customer ID
202      * @return The Identity
203      */
204     public static List<IdentityChange> findHistoryBySearchParameters( final String strCustomerId, final String clientCode, final String authorName,
205             final IdentityChangeType changeType, final String changeStatus, final String authorType, final Date modificationDate, final Map<String, String> metadata, final Integer nbDaysFrom,
206             final Pair<Date, Date> modificationDateInterval, final int max ) throws IdentityStoreException
207     {
208         int nMaxNbIdentityReturned = ( max > 0 ) ? max : PROPERTY_MAX_NB_IDENTITY_RETURNED;
209         return _dao.selectIdentityHistoryBySearchParameters( strCustomerId, clientCode, authorName, changeType, changeStatus, authorType, modificationDate, metadata, nbDaysFrom,
210                 modificationDateInterval, _plugin, nMaxNbIdentityReturned );
211     }
212 
213     /**
214      * Find by customer ID
215      *
216      * @param strCustomerId
217      *            The customer ID
218      * @return The Identity
219      */
220     public static Identity findByCustomerId( String strCustomerId )
221     {
222         Identity identity = _dao.selectByCustomerId( strCustomerId, _plugin );
223 
224         if ( identity != null )
225         {
226             identity.setAttributes( IdentityAttributeHome.getAttributes( identity.getId( ) ) );
227         }
228 
229         return identity;
230     }
231 
232     /**
233      * Find by customer ID. Does not load the attributes.
234      *
235      * @param strCustomerId
236      *            The customer ID
237      * @return The Identity without attributes
238      */
239     public static Identity findByCustomerIdNoAttributes( String strCustomerId )
240     {
241         return _dao.selectByCustomerId( strCustomerId, _plugin );
242     }
243 
244     /**
245      * Find by customer ID
246      *
247      * @param strCustomerId
248      *            The customer ID
249      * @return The Identity
250      */
251     public static Identity findMasterIdentityByCustomerId( String strCustomerId )
252     {
253         Identity identity = _dao.selectNotMergedByCustomerId( strCustomerId, _plugin );
254 
255         if ( identity != null )
256         {
257             identity.setAttributes( IdentityAttributeHome.getAttributes( identity.getId( ) ) );
258         }
259 
260         return identity;
261     }
262 
263     /**
264      * Get the master identity last update date coresponding to the given customer ID
265      * 
266      * @param customerId
267      *            the customer ID
268      * @return the last update date or null if the identity doesn't exist for the provided CUID
269      */
270     public static Timestamp getMasterIdentityLastUpdateDate( final String customerId )
271     {
272         final Identity identity = _dao.selectNotMergedByCustomerId( customerId, _plugin );
273         return identity != null ? identity.getLastUpdateDate( ) : null;
274     }
275 
276     /**
277      * Find by connection ID
278      *
279      * @param strConnectionId
280      *            The customer ID
281      * @return The Identity
282      */
283     public static Identity findMasterIdentityByConnectionId( String strConnectionId )
284     {
285         Identity identity = _dao.selectNotMergedByConnectionId( strConnectionId, _plugin );
286 
287         if ( identity != null )
288         {
289             identity.setAttributes( IdentityAttributeHome.getAttributes( identity.getId( ) ) );
290         }
291 
292         return identity;
293     }
294 
295     /**
296      * Get the master identity without attributes coresponding to the given connection ID
297      * 
298      * @param connectionId
299      *            the connection ID
300      * @return the identity or null if the identity doesn't exist for the provided connection ID
301      */
302     public static Identity getMasterIdentityNoAttributesByConnectionId( final String connectionId )
303     {
304         return _dao.selectNotMergedByConnectionId( connectionId, _plugin );
305     }
306 
307     /**
308      * Find all identities matching one of the values defined on each of the selected Attributes. One value must be found for all selected attributes. Always
309      * performs an exact search.
310      *
311      * @param mapAttributes
312      *            A map that associates the id of each attributes selected with the list of values
313      * @return list of Identity
314      */
315     public static List<Identity> findByAttributesValueForApiSearch(final List<SearchAttribute> searchAttributes, final int max )
316     {
317         int nMaxNbIdentityReturned = ( max > 0 ) ? max : PROPERTY_MAX_NB_IDENTITY_RETURNED;
318         return _dao.selectByAttributesValueForApiSearch( searchAttributes, nMaxNbIdentityReturned, _plugin );
319     }
320 
321     /**
322      * Find all identities that have all attributes specified in the list in parameters.<br/>
323      * Identities <b>MUST</b> have all those attributes in order to be returned.
324      *
325      * @param idAttributeList
326      *            the attributes id
327      * @param notMerged
328      *            if the returned identities have to be not merged
329      * @param notSuspicious
330      *            if the returned identities have to not be suspicious
331      * @param nbFilledAttributes
332      *            minimum number of filled attributes over idAttributeList
333      * @return A list of matching customer IDs
334      */
335     public static List<String> findByAttributeExisting( final List<Integer> idAttributeList, final int nbFilledAttributes, final boolean notMerged,
336             final boolean notSuspicious, final int rulePriority )
337     {
338         return _dao.selectByAttributeExisting( idAttributeList, nbFilledAttributes, notMerged, notSuspicious, rulePriority, _plugin );
339     }
340 
341     /**
342      * add an identity change event in history table
343      *
344      * @param identityChange
345      *            identity change event
346      */
347     public static void addIdentityChangeHistory( IdentityChange identityChange ) throws IdentityStoreException
348     {
349         if ( Objects.equals( identityChange.getChangeType( ), IdentityChangeType.READ ) )
350         {
351             _dao.addOrUpdateChangeHistory( identityChange, _plugin );
352         }
353         else
354         {
355             _dao.addChangeHistory( identityChange, _plugin );
356         }
357     }
358 
359     /**
360      * get identities that have been updated during the previous `days`.
361      *
362      * @param days
363      *            max number of days since the last update
364      * @param identityChangeTypes
365      *            filters on specific change types
366      * @param updatedAttributes
367      *            filters on specific updated attributes
368      * @return the list of identities
369      */
370     public static List<UpdatedIdentityDto> findUpdatedIdentities( final Integer days, final List<IdentityChangeType> identityChangeTypes,
371             final List<SearchUpdatedAttribute> updatedAttributes, final Integer max )
372     {
373         int nMaxNbIdentityReturned = ( max > 0 ) ? max : PROPERTY_MAX_NB_IDENTITY_RETURNED;
374         return _dao.selectUpdated( days, identityChangeTypes, updatedAttributes, nMaxNbIdentityReturned, _plugin );
375     }
376 
377     /**
378      * get identity IDs that have been updated during the previous `days`.
379      *
380      * @param days
381      *            max number of days since the last update
382      * @param identityChangeTypes
383      *            filters on specific change types
384      * @param updatedAttributes
385      *            filters on specific updated attributes
386      * @return the list of identities
387      */
388     public static List<Integer> findUpdatedIdentityIds( final Integer days, final List<IdentityChangeType> identityChangeTypes,
389             final List<SearchUpdatedAttribute> updatedAttributes, final Integer max )
390     {
391         int nMaxNbIdentityReturned = ( max > 0 ) ? max : PROPERTY_MAX_NB_IDENTITY_RETURNED;
392         return _dao.selectUpdatedIds( days, identityChangeTypes, updatedAttributes, nMaxNbIdentityReturned, _plugin );
393     }
394 
395     /**
396      * get updated identities from their IDs.
397      * 
398      * @param identityIds
399      *            list of desired identity IDs
400      * @return the list of identities
401      */
402     public static List<UpdatedIdentityDto> getUpdatedIdentitiesFromIds( final List<Integer> identityIds )
403     {
404         return _dao.selectUpdatedFromIds( identityIds, _plugin );
405     }
406 
407     /**
408      * Search for identities that are not connected, and have a past expirationDate.
409      * 
410      * @param limit
411      *            the max number of returned results
412      * @return a list of expired {@link Identity}
413      */
414     public static List<Identity> findExpiredNotMergedAndNotConnectedIdentities( final int limit )
415     {
416         return _dao.selectExpiredNotMergedAndNotConnectedIdentities( limit, _plugin );
417     }
418 
419     /**
420      * Search for identity customer IDs that are not connected, not merged, and have the specified attribute non-certified.
421      *
422      * @param attributeKey
423      *            the attribute key to be present and non-certified
424      * @param limit
425      *            the max number of returned identities
426      * @return a list of customer IDs
427      */
428     public static List<String> findNotMergedNotConnectedWithNonCertifiedAttributeCustomerIds( final String attributeKey, String certProcess, final int limit )
429     {
430         return _dao.selectNotMergedNotConnectedWithNonCertifiedAttributeCustomerIds( attributeKey, certProcess, limit, _plugin );
431     }
432 
433     /**
434      * Search for identities that are merged to the provided identity ID.
435      * 
436      * @param identityId
437      *            the 'master' identity ID
438      * @return a list of {@link Identity}
439      */
440     public static List<Identity> findMergedIdentities( final int identityId )
441     {
442         return _dao.selectMergedIdentities( identityId, _plugin );
443     }
444 
445     /**
446      * Delete all attribute history of the identity's provided id.
447      * 
448      * @param identityId
449      *            the identity id
450      */
451     public static void deleteAttributeHistory( final int identityId )
452     {
453         _dao.deleteAttributeHistory( identityId, _plugin );
454     }
455 
456     /**
457      * Count All Identities.
458      */
459     public static Integer getCountIdentities( )
460     {
461         return _dao.getCountIdentities( _plugin );
462     }
463 
464     /**
465      * Count All identities that has been deleted or not.
466      *
467      * @param deleted
468      *            define if the dao count deleted or not deleted identities
469      */
470     public static Integer getCountDeletedIdentities( final boolean deleted )
471     {
472         return _dao.getCountDeletedIdentities( deleted, _plugin) ;
473     }
474 
475     /**
476      * Count All identities that has been merged or not.
477      *
478      * @param merged
479      *            define if the dao count merged or not merged identities
480      */
481     public static Integer getCountMergedIdentities( final boolean merged )
482     {
483         return _dao.getCountMergedIdentities( merged, _plugin );
484     }
485 
486     /**
487      * Count All identities that has been connected or not.
488      *
489      * @param monParisActive
490      *            define if the dao count connected or not connected identities
491      */
492     public static Integer getCountActiveMonParisdentities( final boolean monParisActive )
493     {
494         return _dao.getCountActiveMonParisdentities( monParisActive, _plugin );
495     }
496 
497     /**
498      * Count how many attributes each entity has.
499      */
500     public static Map<Integer, Integer> getCountAttributesByIdentities( )
501     {
502         return _dao.getCountAttributesByIdentities( _plugin );
503     }
504 
505     /**
506      * Count how many attributes has no attribute and isn't merged.
507      */
508     public static Integer getCountUnmergedIdentitiesWithoutAttributes( )
509     {
510         return _dao.getCountUnmergedIdentitiesWithoutAttributes( _plugin );
511     }
512 
513     public static Integer getCountIndexEligibleIdentities( )
514     {
515         return _dao.getCountIndexEligibleIdentities( _plugin );
516     }
517 
518     public static Integer getCountIndexNotEligibleIdentities( )
519     {
520         return _dao.getCountIndexNotEligibleIdentities( _plugin );
521     }
522 
523     public static List<IndicatorsActionsType> getActionsTypesDuringInterval(int interval)
524     {
525         return _dao.getActionsTypesDuringInterval(interval, _plugin);
526     }
527 
528     public static List<String> getHistoryStatusList( )
529     {
530         return _dao.getHistoryStatusList( _plugin );
531     }
532 }