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.rules.duplicate;
35  
36  import fr.paris.lutece.plugins.identitystore.business.attribute.AttributeKey;
37  import fr.paris.lutece.plugins.identitystore.web.exception.IdentityStoreException;
38  import fr.paris.lutece.portal.service.i18n.I18nService;
39  
40  import java.io.Serializable;
41  import java.sql.Timestamp;
42  import java.util.ArrayList;
43  import java.util.List;
44  import java.util.Locale;
45  
46  /**
47   * The aim of this class is to define a rule to be applied when performing duplicate identities search.<br>
48   * It must be applied as follows. <br>
49   * <br>
50   * Eligible identities for the search:
51   * <ul>
52   * <li><strong>_listCheckedAttributes</strong> is the list of {@link AttributeKey} that are checked by the rule</li>
53   * <li><strong>_nNbFilledAttributes</strong> is the minimum number of checked attributes that must be present and valued in the Identities that are selected to
54   * execute the rule</li>
55   * </ul>
56   * The search must return the identities that match:
57   * <ul>
58   * <li><strong>_nNbEqualAttributes</strong> is the exact number of checked attributes that must be strictly equal in the result</li>
59   * <li><strong>_nNbMissingAttributes</strong> is the maximum number of checked attributes that can be absent in the result</li>
60   * <li><strong>_listAttributeTreatments</strong> defines a list of conditions (APPROXIMATED or DIFFERENT) on the checked attributes that are not strictly
61   * equal.</li>
62   * </ul>
63   * The logical rules are: <br>
64   * <ul>
65   * <li>Identities selected for the search:</li>
66   *
67   * <pre>
68   * _listCheckedAttributes && _nNbFilledAttribute
69   * </pre>
70   *
71   * <li>Result of the search:</li>
72   *
73   * <pre>
74   *      _listCheckedAttributes && _nNbEqualAttributes && _nNbMissingAttributes && (_listAttributeTreatments[0] || _listAttributeTreatments[1] || ... || _listAttributeTreatments[n])
75   * </pre>
76   * </ul>
77   * <p>
78   * E.g:
79   *
80   * <pre>
81   * | checked | filled | equal | missing | treatments                                                  | result                                                                                                                                                                  |
82   * |---------|--------|-------|---------|-------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
83   * | 7       | 7      | 7     | 0       | empty                                                       | Every Identity that matches exactly the given attributes                                                                                                                |
84   * | 7       | 7      | 6     | 0       | empty                                                       | Every Identity that have 6 matching attributes over the 7 required, the 7th must exist but its value is discarded                                                       |
85   * | 7       | 7      | 6     | 1       | empty                                                       | Every Identity that have 6 matching attributes over the 7 required, the 7th can be missing                                                                              |
86   * | 7       | 7      | 6     | 0       | attribute_key_a : DIFFERENT, attribute_key_b : APPROXIMATED | Every Identity that have 6 matching attributes over the 7 required, the 7th can be attribute_key_a with a different value OR attribute_key_b with an approximated value |
87   *
88   * </pre>
89   */
90  public class DuplicateRule implements Serializable
91  {
92      private int _nId;
93      private String _strName;
94      private String _strCode;
95      private String _strDescription;
96      private List<AttributeKey> _listCheckedAttributes = new ArrayList<>( );
97      private int _nNbFilledAttributes;
98      private int _nNbEqualAttributes;
99      private int _nNbMissingAttributes;
100     private List<DuplicateRuleAttributeTreatment> _listAttributeTreatments = new ArrayList<>( );
101     private int _nPriority;
102     private boolean _bActive;
103     private boolean _bDaemon;
104     private Timestamp _dateDaemonLastExecDate;
105     private int _nDetectionLimit = -1;
106 
107     public void validate( ) throws IdentityStoreException
108     {
109         int i = this.getNbEqualAttributes( ) + this.getNbMissingAttributes( );
110         final int nbCheckedAttributes = this.getCheckedAttributes( ).size( );
111         if ( !_listAttributeTreatments.isEmpty( ) )
112         {
113             for ( final DuplicateRuleAttributeTreatment treatment : _listAttributeTreatments )
114             {
115                 if ( nbCheckedAttributes != i + treatment.getAttributes( ).size( ) )
116                 {
117                     throw new IdentityStoreException( this.getValidationErrorMessage( ) );
118                 }
119             }
120         }
121         if ( nbCheckedAttributes == i )
122         {
123             throw new IdentityStoreException( this.getValidationErrorMessage( ) );
124         }
125     }
126 
127     private String getValidationErrorMessage( )
128     {
129         return I18nService.getLocalizedString( "identitystore.message.error.duplicaterule.validation", Locale.getDefault( ) );
130     }
131 
132     public int getId( )
133     {
134         return _nId;
135     }
136 
137     public void setId( int _nId )
138     {
139         this._nId = _nId;
140     }
141 
142     public String getName( )
143     {
144         return _strName;
145     }
146 
147     public void setName( String _strName )
148     {
149         this._strName = _strName;
150     }
151 
152     public String getCode( )
153     {
154         return _strCode;
155     }
156 
157     public void setCode( String _strCode )
158     {
159         this._strCode = _strCode;
160     }
161 
162     public String getDescription( )
163     {
164         return _strDescription;
165     }
166 
167     public void setDescription( String _strDescription )
168     {
169         this._strDescription = _strDescription;
170     }
171 
172     public List<AttributeKey> getCheckedAttributes( )
173     {
174         return _listCheckedAttributes;
175     }
176 
177     public void setCheckedAttributes( List<AttributeKey> _listCheckedAttributes )
178     {
179         this._listCheckedAttributes = _listCheckedAttributes;
180     }
181 
182     public int getNbFilledAttributes( )
183     {
184         return _nNbFilledAttributes;
185     }
186 
187     public void setNbFilledAttributes( int _nNbFilledAttributes )
188     {
189         this._nNbFilledAttributes = _nNbFilledAttributes;
190     }
191 
192     public int getNbEqualAttributes( )
193     {
194         return _nNbEqualAttributes;
195     }
196 
197     public void setNbEqualAttributes( int _nNbEqualAttributes )
198     {
199         this._nNbEqualAttributes = _nNbEqualAttributes;
200     }
201 
202     public int getNbMissingAttributes( )
203     {
204         return _nNbMissingAttributes;
205     }
206 
207     public void setNbMissingAttributes( int _nNbMissingAttributes )
208     {
209         this._nNbMissingAttributes = _nNbMissingAttributes;
210     }
211 
212     public List<DuplicateRuleAttributeTreatment> getAttributeTreatments( )
213     {
214         return _listAttributeTreatments;
215     }
216 
217     public void setAttributeTreatments( List<DuplicateRuleAttributeTreatment> _listAttributeTreatments )
218     {
219         this._listAttributeTreatments = _listAttributeTreatments;
220     }
221 
222     public int getPriority( )
223     {
224         return _nPriority;
225     }
226 
227     public void setPriority( int _priority )
228     {
229         this._nPriority = _priority;
230     }
231 
232     public boolean isActive( )
233     {
234         return _bActive;
235     }
236 
237     public void setActive( boolean _bActive )
238     {
239         this._bActive = _bActive;
240     }
241 
242     public boolean isDaemon( )
243     {
244         return _bDaemon;
245     }
246 
247     public void setDaemon( boolean _bDaemon )
248     {
249         this._bDaemon = _bDaemon;
250     }
251 
252     public Timestamp getDaemonLastExecDate( )
253     {
254         return _dateDaemonLastExecDate;
255     }
256 
257     public void setDaemonLastExecDate( Timestamp _dateDaemonLastExecDate )
258     {
259         this._dateDaemonLastExecDate = _dateDaemonLastExecDate;
260     }
261 
262     /**
263      * Default value -1 means unlimited detection.
264      */
265     public int getDetectionLimit( )
266     {
267         return _nDetectionLimit;
268     }
269 
270     public void setDetectionLimit( int _nDetectionLimit )
271     {
272         this._nDetectionLimit = _nDetectionLimit;
273     }
274 }