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