View Javadoc
1   /*
2    * Copyright (c) 2002-2019, Mairie de 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.atelieraba.business;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.Comparator;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Map;
43  
44  import fr.paris.lutece.portal.service.plugin.Plugin;
45  import fr.paris.lutece.portal.service.plugin.PluginService;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.util.ReferenceList;
49  
50  /**
51   * This class provides instances management methods (create, find, ...) for Inscrit objects
52   */
53  public final class InscritHome
54  {
55      // Static variable pointed at the DAO instance
56  	public static final String[] potential_duplicate_fields = { "Adresse mail", "Téléphone", "Adresse mail et numéro de téléphone" };
57      private static IInscritDAO _dao    = SpringContextService.getBean( "atelieraba.inscritDAO" );
58      private static Plugin      _plugin = PluginService.getPlugin( "atelieraba" );
59      private static final String       MARK_INSCRIT                            = "inscrit";
60  
61      /**
62       * Private constructor - this class need not be instantiated
63       */
64      private InscritHome( )
65      {
66      }
67  
68      /**
69       * Create an instance of the inscrit class
70       *
71       * @param inscrit
72       *            The instance of the Inscrit which contains the informations to store
73       * @return The instance of inscrit which has been created with its primary key.
74       */
75      public static Inscrit create( Inscrit inscrit )
76      {
77          _dao.insert( inscrit, _plugin );
78  
79          return inscrit;
80      }
81  
82      /**
83       * Update of the inscrit which is specified in parameter
84       *
85       * @param inscrit
86       *            The instance of the Inscrit which contains the data to store
87       * @return The instance of the inscrit which has been updated
88       */
89      public static Inscrit update( Inscrit inscrit )
90      {
91          _dao.store( inscrit, _plugin );
92  
93          return inscrit;
94      }
95  
96      /**
97       * Remove the inscrit whose identifier is specified in parameter
98       *
99       * @param nKey
100      *            The inscrit Id
101      */
102     public static void remove( int nKey )
103     {
104         _dao.delete( nKey, _plugin );
105     }
106 
107     /**
108      * Returns an instance of a inscrit whose identifier is specified in parameter
109      *
110      * @param nKey
111      *            The inscrit primary key
112      * @return an instance of Inscrit
113      */
114     public static Inscrit findByPrimaryKey( int nKey )
115     {
116         return _dao.load( nKey, _plugin );
117     }
118 
119     /**
120      * Returns an list of Inscrits whose identifier is specified in parameter
121      *
122      * @param strGuid
123      *            The guid identifier
124      * @return an list of Inscrits
125      */
126     public static List<Inscrit> findByGuid( String strGuid )
127     {
128         return _dao.findByGuid( strGuid, _plugin );
129     }
130 
131     /**
132      * Load the data of all the inscrit objects and returns them as a list
133      *
134      * @return the list which contains the data of all the inscrit objects
135      */
136     public static List<Inscrit> getInscritsList( )
137     {
138         return _dao.selectInscritsList( _plugin );
139     }
140 
141     /**
142      * Load the id of all the inscrit objects and returns them as a list
143      *
144      * @return the list which contains the id of all the inscrit objects
145      */
146     public static List<Integer> getIdInscritsList( )
147     {
148         return _dao.selectIdInscritsList( _plugin );
149     }
150 
151     /**
152      * Load the data of all the inscrit objects and returns them as a referenceList
153      *
154      * @return the referenceList which contains the data of all the inscrit objects
155      */
156     public static ReferenceList getInscritsReferenceList( )
157     {
158         return _dao.selectInscritsReferenceList( _plugin );
159     }
160 
161     /**
162      * Load the data of all the inscrit objects and returns them in form of a collection
163      *
164      * @param validatedFilters
165      *            The validatedFilters
166      * @return the list which contains the data of all the inscrit objects
167      */
168     public static List<Map<String, Object>> getInscritsListByFilter( Map<String[], String> validatedFilters )
169     {
170         return _dao.selectInscritsListByFilter( validatedFilters, _plugin );
171     }
172     
173     /**
174      * Load the data of all the inscrit objects and returns them in form of a collection
175      * with an order to find partial duplicates except for field
176      * @param field the field
177      * @return the collection which contains the data of all the inscrit objects
178      */
179     public static Collection<Map<String, Object>> getInscritsListOrderedPartialDuplicates(List<Map<String, Object>> listCandidatsDetailed,String field )
180     {
181         Collection<Map<String, Object>> export = getInscritsListOrderedPartialDuplicatesExport( listCandidatsDetailed,field );
182         ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>( export.size(  ) );
183 
184         for ( Map<String, Object> candidatDetailed : export )
185         {
186             result.add( candidatDetailed );
187         }
188 
189         return result;
190     }
191     
192     /**
193      * Load the data of all the inscrit objects and returns them in form of a collection
194      * with an order to find partial duplicates except for field, with export info
195      * @param field the field
196      * @return the collection which contains the data of all the export objects
197      */
198     public static Collection<Map<String, Object>> getInscritsListOrderedPartialDuplicatesExport( List<Map<String, Object>> listCandidatsDetailed,String field )
199     {
200     	
201         ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>(  );
202 
203 //        List<Inscrit> exportinscrits =  _dao.selectInscritsList( _plugin );
204         Comparator<Map<String, Object>> dedoublonnageManuelComparator;
205 
206         if ( potential_duplicate_fields[0].equals( field ) )
207         {
208             dedoublonnageManuelComparator = new Comparator<Map<String, Object>>(  )
209                     {
210                         public int compare( Map<String, Object> entry1, Map<String, Object> entry2 )
211                         {
212                             return compareInscritManuelEmail( (Inscrit)entry1.get(MARK_INSCRIT), (Inscrit)entry2.get(MARK_INSCRIT) );
213                         }
214                     };
215         }
216         else if ( potential_duplicate_fields[1].equals( field ) )
217         {
218             dedoublonnageManuelComparator = new Comparator<Map<String, Object>>(  )
219                     {
220                         public int compare( Map<String, Object> entry1, Map<String, Object> entry2  )
221                         {
222                             return compareInscritManuelTelephone( (Inscrit)entry1.get(MARK_INSCRIT), (Inscrit)entry2.get(MARK_INSCRIT) );
223                         }
224                     };
225         }
226         else if ( potential_duplicate_fields[2].equals( field ) )
227         {
228             dedoublonnageManuelComparator = new Comparator<Map<String, Object>>(  )
229                     {
230                         public int compare( Map<String, Object> entry1, Map<String, Object> entry2  )
231                         {
232                             return compareInscritManuelEmailTelephone( (Inscrit)entry1.get(MARK_INSCRIT), (Inscrit)entry2.get(MARK_INSCRIT) );
233                         }
234                     };
235         }
236         else
237         {
238             AppLogService.error( "DACIC: dedoublonnage manuel failed: unexpected field: " + field );
239 
240             return new ArrayList<Map<String, Object>>(  );
241         }
242 
243         Collections.sort( listCandidatsDetailed, dedoublonnageManuelComparator );
244 
245         Map<String, Object> previousCandidatDetailed = null;
246         Inscrit previousInscrit = null;
247         boolean previousWasFirst = false;
248 
249         for ( Map<String, Object> candidatDetailed : listCandidatsDetailed )
250         {
251         	Inscrit inscrit=(Inscrit) candidatDetailed.get(MARK_INSCRIT);
252         	
253             if ( ( previousInscrit == null ) ||
254                     !( ( potential_duplicate_fields[0].equals( field ) &&
255                     previousInscrit.getContactEmail(  ).equals( inscrit.getContactEmail(  ) ) ) ||
256                     ( potential_duplicate_fields[1].equals( field ) &&
257                     previousInscrit.getContactTelephone(  ).equals( inscrit.getContactTelephone(  ) ) ) ||
258                     ( potential_duplicate_fields[2].equals( field ) &&
259                     previousInscrit.getContactEmail(  ).equals( inscrit.getContactEmail(  ) )   && previousInscrit.getContactTelephone(  ).equals( inscrit.getContactTelephone(  )))))
260             {
261                 previousWasFirst = true;
262             }
263             else
264             {
265             	result.add( candidatDetailed );
266                 if ( previousWasFirst )
267                 {
268                     result.add( previousCandidatDetailed );
269                 }
270 
271                 previousWasFirst = false;
272             }
273 
274             previousInscrit = inscrit;
275             previousCandidatDetailed = candidatDetailed;
276         }
277 
278         return result;
279     }
280     
281     /**
282      * Sort the inscrits according to our dedoublonnage manuel
283      * @param inscrit1 Inscrit
284      * @param inscrit2 Inscrit
285      * @return a negative value if smaller, 0 if equal, a positive value if greater
286      */   
287     private static int compareInscritManuelEmailTelephone( Inscrit inscrit1, Inscrit inscrit2 )
288     {
289     	return inscrit1.getContactEmail(  ).compareTo( inscrit2.getContactEmail(  ) )+inscrit1.getContactTelephone().compareTo(inscrit2.getContactTelephone());              
290     }
291     
292     /**
293      * Sort the inscrits according to our dedoublonnage manuel
294      * @param inscrit1 Inscrit
295      * @param inscrit2 Inscrit
296      * @return a negative value if smaller, 0 if equal, a positive value if greater
297      */
298     private static int compareInscritManuelEmail( Inscrit inscrit1, Inscrit inscrit2 )
299     {  
300     	return inscrit1.getContactEmail(  ).compareTo( inscrit2.getContactEmail(  ) );                
301     }
302     
303     /**
304      * Sort the inscrits according to our dedoublonnage manuel
305      * @param inscrit1 Inscrit
306      * @param inscrit2 Inscrit
307      * @return a negative value if smaller, 0 if equal, a positive value if greater
308      */
309     private static int compareInscritManuelTelephone( Inscrit inscrit1, Inscrit inscrit2 )
310     {
311     	return inscrit1.getContactTelephone(  ).compareTo( inscrit2.getContactTelephone(  ) );  
312     }
313 
314     /**
315      * Checks if there already is an inscrit object with the same lastname, firstname and birthdate
316      *
317      * @param inscrit
318      * @return idInscrit
319      */
320     public static int getInscritDoublon( Inscrit inscrit )
321     {
322         return _dao.getInscritDoublon( inscrit, _plugin );
323     }
324 }