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