1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
72
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
88
89
90
91
92
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
113
114
115
116
117
118
119
120 public DuplicateRule get( final String ruleCode ) throws ResourceNotFoundException
121 {
122 return _cache.get( ruleCode );
123 }
124
125
126
127
128
129
130
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
146
147
148
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
159
160
161
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
172
173
174
175 public void delete( final Integer id )
176 {
177 DuplicateRuleHome.delete( id );
178 _cache.remove( id );
179 }
180
181 }