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.service.duplicate;
35  
36  import fr.paris.lutece.plugins.identitystore.business.duplicates.suspicions.SuspiciousIdentityHome;
37  import fr.paris.lutece.plugins.identitystore.business.rules.duplicate.DuplicateRule;
38  import fr.paris.lutece.plugins.identitystore.business.rules.duplicate.DuplicateRuleHome;
39  import fr.paris.lutece.plugins.identitystore.cache.DuplicateRulesCache;
40  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.duplicate.DuplicateRuleSummaryDto;
41  import fr.paris.lutece.portal.service.spring.SpringContextService;
42  
43  import java.util.ArrayList;
44  import java.util.Comparator;
45  import java.util.List;
46  import java.util.stream.Collectors;
47  
48  public class DuplicateRuleService
49  {
50  
51      private final DuplicateRulesCache _cache = SpringContextService.getBean( "identitystore.duplicateRulesCache" );
52      private static DuplicateRuleService _instance;
53  
54      public static DuplicateRuleService instance( )
55      {
56          if ( _instance == null )
57          {
58              _instance = new DuplicateRuleService( );
59              _instance._cache.refresh( );
60          }
61          return _instance;
62      }
63  
64      private DuplicateRuleService( )
65      {
66      }
67  
68      /**
69       *
70       * @return
71       * @throws DuplicateRuleNotFoundException
72       */
73      public List<DuplicateRule> findAll( ) throws DuplicateRuleNotFoundException
74      {
75          final List<String> allCodes = DuplicateRuleHome.findAllCodes( );
76          final List<DuplicateRule> list = new ArrayList<>( );
77          for ( final String code : allCodes )
78          {
79              final DuplicateRule duplicateRule = _cache.get( code );
80              list.add( duplicateRule );
81          }
82          return list;
83      }
84  
85      /**
86       * Find all summaries of rules having priority or higher
87       * 
88       * @param priority
89       *            the min priority
90       * @return a list of rule summaries
91       * @throws DuplicateRuleNotFoundException
92       */
93      public List<DuplicateRuleSummaryDto> findSummaries( final Integer priority ) throws DuplicateRuleNotFoundException
94      {
95          return this.findAll( ).stream( ).filter( rule -> priority == null || rule.getPriority( ) <= priority ).map( rule -> {
96              final DuplicateRuleSummaryDto ruleDto = new DuplicateRuleSummaryDto( );
97              ruleDto.setId( rule.getId( ) );
98              ruleDto.setName( rule.getName( ) );
99              ruleDto.setCode( rule.getCode( ) );
100             ruleDto.setDescription( rule.getDescription( ) );
101             ruleDto.setPriority( rule.getPriority( ) );
102             ruleDto.setDaemonLastExecDate( rule.getDaemonLastExecDate( ) );
103             ruleDto.setDuplicateCount( SuspiciousIdentityHome.countSuspiciousIdentity( rule.getId( ) ) );
104             ruleDto.setActive( rule.isActive( ) );
105             ruleDto.setDaemon( rule.isDaemon( ) );
106             return ruleDto;
107         } ).sorted( Comparator.comparing( DuplicateRuleSummaryDto::getPriority ) ).collect( Collectors.toList( ) );
108     }
109 
110     /**
111      * Get {@link DuplicateRule} from cache by its code.
112      *
113      * @param ruleCode
114      *            the rule code
115      * @return DuplicateRule
116      * @throws DuplicateRuleNotFoundException
117      *             if the rule is not found for the provided code
118      */
119     public DuplicateRule get( final String ruleCode ) throws DuplicateRuleNotFoundException
120     {
121         return _cache.get( ruleCode );
122     }
123 
124     /**
125      * Get {@link DuplicateRule} from cache by its code.
126      *
127      * @param ruleCode
128      *            the rule code
129      * @return {@link DuplicateRule}, or null if the rule is not found for the provided code
130      */
131     public DuplicateRule safeGet( final String ruleCode )
132     {
133         try
134         {
135             return _cache.get( ruleCode );
136         }
137         catch( final DuplicateRuleNotFoundException e )
138         {
139             return null;
140         }
141     }
142 
143     /**
144      * Creates a new {@link DuplicateRule} and adds it to cache.
145      * 
146      * @param duplicateRule
147      * @return
148      */
149     public DuplicateRulentitystore/business/rules/duplicate/DuplicateRule.html#DuplicateRule">DuplicateRule create( final DuplicateRule duplicateRule )
150     {
151         DuplicateRuleHome.create( duplicateRule );
152         _cache.put( duplicateRule );
153         return duplicateRule;
154     }
155 
156     /**
157      * Update an existing {@link DuplicateRule} (if possible).
158      * 
159      * @param duplicateRule
160      * @return
161      */
162     public DuplicateRulentitystore/business/rules/duplicate/DuplicateRule.html#DuplicateRule">DuplicateRule update( final DuplicateRule duplicateRule )
163     {
164         DuplicateRuleHome.update( duplicateRule );
165         _cache.put( duplicateRule );
166         return duplicateRule;
167     }
168 
169     /**
170      * Deletes a {@link DuplicateRule} by its id in the database and cache
171      * 
172      * @param id
173      */
174     public void delete( final Integer id )
175     {
176         DuplicateRuleHome.delete( id );
177         _cache.remove( id );
178     }
179 
180 }