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.duplicates.suspicions;
35  
36  import fr.paris.lutece.plugins.identitystore.business.identity.Identity;
37  import fr.paris.lutece.plugins.identitystore.business.identity.IdentityHome;
38  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.search.SearchAttribute;
39  import fr.paris.lutece.plugins.identitystore.web.exception.IdentityNotFoundException;
40  import fr.paris.lutece.plugins.identitystore.web.exception.IdentityStoreException;
41  import fr.paris.lutece.portal.service.plugin.Plugin;
42  import fr.paris.lutece.portal.service.plugin.PluginService;
43  import fr.paris.lutece.portal.service.spring.SpringContextService;
44  import fr.paris.lutece.util.ReferenceList;
45  import org.apache.commons.collections.CollectionUtils;
46  
47  import java.util.List;
48  import java.util.Objects;
49  import java.util.Optional;
50  
51  /**
52   * This class provides instances management methods (create, find, ...) for SuspiciousIdentity objects
53   */
54  public final class SuspiciousIdentityHome
55  {
56      // Static variable pointed at the DAO instance
57      private static final ISuspiciousIdentityDAO _dao = SpringContextService.getBean( "identitystore-quality.suspiciousIdentityDAO" );
58      private static final Plugin _plugin = PluginService.getPlugin( "identitystore-quality" );
59  
60      /**
61       * Private constructor - this class need not be instantiated
62       */
63      private SuspiciousIdentityHome( )
64      {
65      }
66  
67      /**
68       * Create an instance of the suspiciousIdentity class
69       * 
70       * @param suspiciousIdentity
71       *            The instance of the SuspiciousIdentity which contains the informations to store
72       * @return The instance of suspiciousIdentity which has been created with its primary key.
73       */
74      public static SuspiciousIdentity./../../../../fr/paris/lutece/plugins/identitystore/business/duplicates/suspicions/SuspiciousIdentity.html#SuspiciousIdentity">SuspiciousIdentity create( SuspiciousIdentity suspiciousIdentity )
75      {
76          _dao.insert( suspiciousIdentity, _plugin );
77  
78          return suspiciousIdentity;
79      }
80  
81      /**
82       * Update of the suspiciousIdentity which is specified in parameter
83       * 
84       * @param suspiciousIdentity
85       *            The instance of the SuspiciousIdentity which contains the data to store
86       * @return The instance of the suspiciousIdentity which has been updated
87       */
88      public static SuspiciousIdentity./../../../../fr/paris/lutece/plugins/identitystore/business/duplicates/suspicions/SuspiciousIdentity.html#SuspiciousIdentity">SuspiciousIdentity update( SuspiciousIdentity suspiciousIdentity )
89      {
90          _dao.store( suspiciousIdentity, _plugin );
91  
92          return suspiciousIdentity;
93      }
94  
95      /**
96       * Update of the suspiciousIdentity which is specified in parameter
97       *
98       * @param firstCuid
99       *            The CUID of the SuspiciousIdentity which contains the data to store
100      * @param secondCuid
101      *            The CUID of the SuspiciousIdentity which contains the data to store
102      * @return The instance of the suspiciousIdentity which has been updated
103      */
104     public static void exclude( String firstCuid, String secondCuid, String authorType, String authorName )
105     {
106         if ( !excluded( firstCuid, secondCuid ) ) // TODO handle response with author if already excluded ?
107         {
108             _dao.insertExcluded( firstCuid, secondCuid, authorType, authorName, _plugin );
109         }
110     }
111 
112     /**
113      * Check if a couple of suspicious identities are marked as excluded
114      *
115      * @param firstCuid
116      *            The CUID of the SuspiciousIdentity which contains the data to store
117      * @param secondCuid
118      *            The CUID of the SuspiciousIdentity which contains the data to store
119      * @return true if excluded
120      */
121     public static boolean excluded( String firstCuid, String secondCuid )
122     {
123         return _dao.checkIfExcluded( firstCuid, secondCuid, _plugin );
124     }
125 
126     /**
127      * Check if a couple of suspicious identities are marked as excluded
128      *
129      * @param firstCuid
130      *            The CUID of the SuspiciousIdentity which contains the data to store
131      * @param cuids
132      *            The CUIDs of the SuspiciousIdentities which contains the data to store
133      * @return true if excluded
134      */
135     public static boolean excluded( String firstCuid, List<String> cuids )
136     {
137         return _dao.checkIfExcluded( firstCuid, cuids, _plugin );
138     }
139 
140     /**
141      * Verify if at least one customer ID within a list is identified as suspicious
142      *
143      * @param customerIds
144      *            The list of CUID
145      * @return true if excluded
146      */
147     public static boolean hasSuspicious( final List<String> customerIds )
148     {
149         return CollectionUtils.isNotEmpty( customerIds ) && _dao.checkIfContainsSuspicious( customerIds, _plugin );
150     }
151 
152     /**
153      * Remove the suspiciousIdentity whose identifier is specified in parameter
154      * 
155      * @param nId
156      *            The suspiciousIdentity customer Id
157      */
158     public static void remove( int nId )
159     {
160         _dao.delete( nId, _plugin );
161     }
162 
163     /**
164      * Remove the suspiciousIdentity whose identifier is specified in parameter
165      *
166      * @param customerId
167      *            The suspiciousIdentity customer Id
168      */
169     public static void remove( String customerId )
170     {
171         _dao.delete( customerId, _plugin );
172     }
173 
174     /**
175      * Returns an instance of a suspiciousIdentity whose identifier is specified in parameter
176      * 
177      * @param nKey
178      *            The suspiciousIdentity primary key
179      * @return an instance of SuspiciousIdentity
180      */
181     public static Optional<SuspiciousIdentity> findByPrimaryKey( int nKey )
182     {
183         return _dao.load( nKey, _plugin );
184     }
185 
186     /**
187      * Load the data of all the suspiciousIdentity objects and returns them as a list
188      *
189      * @param max
190      *            max number of suspicious identities to return
191      * @return the list which contains the data of all the suspiciousIdentity objects
192      */
193     public static List<SuspiciousIdentity> getSuspiciousIdentitysList( final String ruleCode, final int max, final Integer priority )
194             throws IdentityStoreException
195     {
196         return _dao.selectSuspiciousIdentitysList( ruleCode, max, priority, _plugin );
197     }
198 
199     /**
200      * Load the data of all the suspiciousIdentity objects and returns them as a list
201      *
202      * @param attributes
203      *            attributes to filter the results on
204      * @param max
205      *            max number of suspicious identities to return
206      * @return the list which contains the data of all the suspiciousIdentity objects
207      */
208     public static List<SuspiciousIdentity> getSuspiciousIdentitysList( final String ruleCode, final List<SearchAttribute> attributes, final Integer max,
209             final Integer priority ) throws IdentityStoreException
210     {
211         return _dao.selectSuspiciousIdentitysList( ruleCode, attributes, max, priority, _plugin );
212     }
213 
214     /**
215      * Load the data of all the excluded identities objects and returns them as a list
216      *
217      * @return the list which contains the data of all the suspiciousIdentity objects
218      */
219     public static List<ExcludedIdentities> getExcludedIdentitiesList( )
220     {
221         return _dao.selectExcludedIdentitiesList( _plugin );
222     }
223 
224     /**
225      * Load the data of all the excluded identities objects and returns them as a list
226      *
227      * @return the list which contains the data of all the suspiciousIdentity objects
228      */
229     public static List<ExcludedIdentities> getExcludedIdentitiesList( final String customerId )
230     {
231         return _dao.selectExcludedIdentitiesList( customerId, _plugin );
232     }
233 
234     public static void removeExcludedIdentities( final String firstCuid, final String secondCuid )
235     {
236         _dao.removeExcludedIdentities( firstCuid, secondCuid, _plugin );
237     }
238 
239     public static void removeExcludedIdentities( final String cuid )
240     {
241         _dao.removeExcludedIdentities( cuid, _plugin );
242     }
243 
244     /**
245      * Load the data of all the suspiciousIdentity objects and returns them as a list
246      *
247      * @param ruleCode
248      *            code of the duplicate rule
249      * @return the list which contains the data of all the suspiciousIdentity objects
250      */
251     public static List<String> getSuspiciousIdentityCuidsList( final String ruleCode )
252     {
253         return _dao.selectSuspiciousIdentityCuidsList( ruleCode, _plugin );
254     }
255 
256     /**
257      * Load the id of all the suspiciousIdentity objects and returns them as a list
258      * 
259      * @return the list which contains the id of all the suspiciousIdentity objects
260      */
261     public static List<Integer> getIdSuspiciousIdentitysList( )
262     {
263         return _dao.selectIdSuspiciousIdentitysList( _plugin );
264     }
265 
266     /**
267      * Load the data of all the suspiciousIdentity objects and returns them as a referenceList
268      * 
269      * @return the referenceList which contains the data of all the suspiciousIdentity objects
270      */
271     public static ReferenceList getSuspiciousIdentitysReferenceList( )
272     {
273         return _dao.selectSuspiciousIdentitysReferenceList( _plugin );
274     }
275 
276     /**
277      * Load the data of all the avant objects and returns them as a list
278      * 
279      * @param listIds
280      *            liste of ids
281      * @return the list which contains the data of all the avant objects
282      */
283     public static List<SuspiciousIdentity> getSuspiciousIdentitysListByIds( List<Integer> listIds )
284     {
285         return _dao.selectSuspiciousIdentitysListByIds( _plugin, listIds );
286     }
287 
288     public static SuspiciousIdentity selectByCustomerID( String customerId )
289     {
290         return _dao.selectByCustomerID( customerId, _plugin );
291     }
292 
293     public static List<SuspiciousIdentity> selectByCustomerIDs( List<String> customerIds )
294     {
295         return _dao.selectByCustomerIDs( customerIds, _plugin );
296     }
297 
298     public static void purge( )
299     {
300         _dao.purge( _plugin );
301     }
302 
303     public static int countSuspiciousIdentity( final int ruleId )
304     {
305         return _dao.countSuspiciousIdentities( ruleId, _plugin );
306     }
307 
308     public static int countSuspiciousIdentity( )
309     {
310         return _dao.countSuspiciousIdentities( _plugin );
311     }
312 
313     public static boolean manageLock( String customerId, String authorName, String authorType, boolean lock ) throws IdentityStoreException
314     {
315         final Identity identity = IdentityHome.findByCustomerId( customerId );
316         if ( identity == null )
317         {
318             throw new IdentityNotFoundException( "Could not find identity with customerId " + customerId );
319         }
320 
321         final SuspiciousIdentity suspiciousIdentity = SuspiciousIdentityHome.selectByCustomerID( customerId );
322         if ( suspiciousIdentity == null )
323         {
324             throw new IdentityNotFoundException( "Could not find suspicious identity with customerId " + customerId );
325         }
326 
327         final boolean isAlreadyLocked = suspiciousIdentity.getLock( ).isLocked( );
328         final boolean sameAuthorName = Objects.equals( suspiciousIdentity.getLock( ).getAuthorName( ), authorName );
329         final boolean sameAuthorType = Objects.equals( suspiciousIdentity.getLock( ).getAuthorType( ), authorType );
330         final boolean sameAuthor = sameAuthorName && sameAuthorType;
331         if ( lock && isAlreadyLocked && !sameAuthor )
332         {
333             throw new SuspiciousIdentityLockedException(
334                     "Suspicious identity with customerId " + customerId + " is locked by " + suspiciousIdentity.getLock( ).getAuthorName( ) + "." );
335         }
336 
337         if ( !lock && !isAlreadyLocked )
338         {
339             throw new SuspiciousIdentityLockedException( "Suspicious identity with customerId " + customerId + " is already unlocked." );
340         }
341 
342         if ( !lock && isAlreadyLocked && !sameAuthor )
343         {
344             throw new SuspiciousIdentityLockedException( "Suspicious identity with customerId " + customerId + " is locked by "
345                     + suspiciousIdentity.getLock( ).getAuthorName( ) + ". User" + authorName + " is not allowed to unlock." );
346         }
347 
348         if ( lock && isAlreadyLocked && sameAuthor )
349         {
350             // the request user has already locked the resource, do nothing.
351             return true;
352         }
353 
354         return _dao.manageLock( customerId, lock, authorType, authorName, _plugin );
355     }
356 
357     public static void purgeLocks( )
358     {
359         _dao.purgeLocks( _plugin );
360     }
361 }