View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.appointment.service;
35  
36  import java.time.DayOfWeek;
37  import java.time.LocalDate;
38  import java.time.LocalTime;
39  import java.util.ArrayList;
40  import java.util.List;
41  import java.util.stream.Collectors;
42  
43  import org.apache.commons.collections.CollectionUtils;
44  
45  import fr.paris.lutece.plugins.appointment.business.appointment.Appointment;
46  import fr.paris.lutece.plugins.appointment.business.comment.CommentHome;
47  import fr.paris.lutece.plugins.appointment.business.display.Display;
48  import fr.paris.lutece.plugins.appointment.business.display.DisplayHome;
49  import fr.paris.lutece.plugins.appointment.business.form.Form;
50  import fr.paris.lutece.plugins.appointment.business.form.FormHome;
51  import fr.paris.lutece.plugins.appointment.business.localization.Localization;
52  import fr.paris.lutece.plugins.appointment.business.localization.LocalizationHome;
53  import fr.paris.lutece.plugins.appointment.business.message.FormMessage;
54  import fr.paris.lutece.plugins.appointment.business.message.FormMessageHome;
55  import fr.paris.lutece.plugins.appointment.business.planning.ClosingDay;
56  import fr.paris.lutece.plugins.appointment.business.planning.ClosingDayHome;
57  import fr.paris.lutece.plugins.appointment.business.planning.TimeSlot;
58  import fr.paris.lutece.plugins.appointment.business.planning.TimeSlotHome;
59  import fr.paris.lutece.plugins.appointment.business.planning.WeekDefinition;
60  import fr.paris.lutece.plugins.appointment.business.planning.WeekDefinitionHome;
61  import fr.paris.lutece.plugins.appointment.business.planning.WorkingDay;
62  import fr.paris.lutece.plugins.appointment.business.planning.WorkingDayHome;
63  import fr.paris.lutece.plugins.appointment.business.rule.FormRule;
64  import fr.paris.lutece.plugins.appointment.business.rule.FormRuleHome;
65  import fr.paris.lutece.plugins.appointment.business.rule.ReservationRule;
66  import fr.paris.lutece.plugins.appointment.business.rule.ReservationRuleHome;
67  import fr.paris.lutece.plugins.appointment.business.slot.Slot;
68  import fr.paris.lutece.plugins.appointment.business.slot.SlotHome;
69  import fr.paris.lutece.plugins.appointment.service.listeners.AppointmentListenerManager;
70  import fr.paris.lutece.plugins.appointment.service.listeners.FormListenerManager;
71  import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFormDTO;
72  import fr.paris.lutece.plugins.genericattributes.business.Entry;
73  import fr.paris.lutece.plugins.genericattributes.business.EntryFilter;
74  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
75  import fr.paris.lutece.portal.service.util.AppException;
76  import fr.paris.lutece.portal.service.util.AppLogService;
77  import fr.paris.lutece.util.ReferenceList;
78  import fr.paris.lutece.util.sql.TransactionManager;
79  
80  /**
81   * Service class for a form
82   * 
83   * @author Laurent Payen
84   *
85   */
86  public final class FormService
87  {
88  
89      /**
90       * Private constructor - this class does not need to be instantiated
91       */
92      private FormService( )
93      {
94      }
95  
96      /**
97       * Make a copy of form, with all its values
98       * 
99       * @param nIdForm
100      *            the Form Id to copy
101      * @param newNameForCopy
102      *            the new Name of the copy
103      * @return the id of the form created
104      */
105     public static int copyForm( int nIdForm, String newNameForCopy )
106     {
107         // Build the simple form to copy with the values of the original form
108         AppointmentFormDTO appointmentForm = buildAppointmentForm( nIdForm, 0 );
109         appointmentForm.setTitle( newNameForCopy );
110         appointmentForm.setIsActive( Boolean.FALSE );
111         appointmentForm.setDateStartValidity( null );
112         appointmentForm.setDateEndValidity( null );
113         // Save it
114         Form form = FormService.createForm( appointmentForm );
115         int nIdNewForm = form.getIdForm( );
116         // Add the display
117         DisplayService.createDisplay( appointmentForm, nIdNewForm );
118         // Add the localization
119         LocalizationService.createLocalization( appointmentForm, nIdNewForm );
120         // Add the form rule
121         FormRuleService.createFormRule( appointmentForm, nIdNewForm );
122         // Get all the weekDefinitions, WorkingDays and TimeSlots of the
123         // original form and set the new id
124         // of the copy of the form and save them
125         WeekDefinition copyWeekDefinition;
126         int idCopyReservationRule;
127         WorkingDay copyWorkingDay;
128         int idCopyWorkingDay;
129         TimeSlot copyTimeSlot;
130         List<WeekDefinition> listWeekDefinitions = WeekDefinitionService.findListWeekDefinition( nIdForm );
131         List<WorkingDay> listWorkingDays;
132         List<TimeSlot> listTimeSlots;
133 
134         // Get all the reservation rules of the original form and set the new id
135         // of the copy of the form and save them
136         ReservationRule copyReservationRule;
137         List<ReservationRule> listReservationRules = ReservationRuleService.findListReservationRule( nIdForm );
138         for ( ReservationRule reservationRule : listReservationRules )
139         {
140             int nOldReservationRule = reservationRule.getIdReservationRule( );
141             copyReservationRule = reservationRule;
142             copyReservationRule.setIdReservationRule( 0 );
143             copyReservationRule.setIdForm( nIdNewForm );
144             ReservationRuleService.saveReservationRule( copyReservationRule );
145             listWorkingDays = copyReservationRule.getListWorkingDay( );
146             idCopyReservationRule = copyReservationRule.getIdReservationRule( );
147 
148             for ( WorkingDay workingDay : listWorkingDays )
149             {
150                 copyWorkingDay = workingDay;
151                 copyWorkingDay.setIdWorkingDay( 0 );
152                 copyWorkingDay.setIdReservationRule( idCopyReservationRule );
153                 copyWorkingDay = WorkingDayService.saveWorkingDay( copyWorkingDay );
154                 idCopyWorkingDay = copyWorkingDay.getIdWorkingDay( );
155                 listTimeSlots = workingDay.getListTimeSlot( );
156                 for ( TimeSlot timeSlot : listTimeSlots )
157                 {
158                     copyTimeSlot = timeSlot;
159                     copyTimeSlot.setIdTimeSlot( 0 );
160                     copyTimeSlot.setIdWorkingDay( idCopyWorkingDay );
161                     TimeSlotService.saveTimeSlot( copyTimeSlot );
162                 }
163             }
164             List<WeekDefinition> listWeekDef = listWeekDefinitions.stream( ).filter( week -> week.getIdReservationRule( ) == nOldReservationRule )
165                     .collect( Collectors.toList( ) );
166             if ( CollectionUtils.isNotEmpty( listWeekDef ) )
167             {
168 
169                 for ( WeekDefinition weekDefinition : listWeekDef )
170                 {
171                     copyWeekDefinition = weekDefinition;
172                     copyWeekDefinition.setIdWeekDefinition( 0 );
173                     copyWeekDefinition.setIdReservationRule( idCopyReservationRule );
174                     WeekDefinitionHome.create( copyWeekDefinition );
175                 }
176             }
177         }
178 
179         // Copy the messages of the original form and add them to the copy
180         FormMessage formMessage = FormMessageService.findFormMessageByIdForm( nIdForm );
181         FormMessage copyFormMessage = formMessage;
182         copyFormMessage.setIdFormMessage( 0 );
183         copyFormMessage.setIdForm( nIdNewForm );
184         FormMessageService.saveFormMessage( copyFormMessage );
185         // Get all the closing days of the original form and add them to the
186         // copy
187         List<ClosingDay> listClosingDays = ClosingDayService.findListClosingDay( nIdForm );
188         ClosingDay copyClosingDay;
189         for ( ClosingDay closingDay : listClosingDays )
190         {
191             copyClosingDay = closingDay;
192             copyClosingDay.setIdClosingDay( 0 );
193             copyClosingDay.setIdForm( nIdNewForm );
194             ClosingDayService.saveClosingDay( copyClosingDay );
195         }
196         // Get all the specific slots of the original form and copy them for the
197         // new form
198         List<Slot> listSpecificSlots = SlotService.findSpecificSlotsByIdForm( nIdForm );
199         Slot copySpecificSlot;
200         for ( Slot specificSlot : listSpecificSlots )
201         {
202             copySpecificSlot = specificSlot;
203 
204             copySpecificSlot.setIdSlot( 0 );
205             copySpecificSlot.setIdForm( nIdNewForm );
206             copySpecificSlot.setNbPotentialRemainingPlaces( specificSlot.getMaxCapacity( ) );
207             copySpecificSlot.setNbRemainingPlaces( specificSlot.getMaxCapacity( ) );
208             copySpecificSlot.setNbPlacestaken( 0 );
209 
210             SlotHome.create( copySpecificSlot );
211         }
212         // Copy the entries of the original form
213         EntryFilter entryFilter = new EntryFilter( );
214         entryFilter.setIdResource( nIdForm );
215         entryFilter.setResourceType( AppointmentFormDTO.RESOURCE_TYPE );
216         entryFilter.setEntryParentNull( EntryFilter.FILTER_TRUE );
217         entryFilter.setFieldDependNull( EntryFilter.FILTER_TRUE );
218         List<Entry> listEntries = EntryHome.getEntryList( entryFilter );
219         for ( Entry entry : listEntries )
220         {
221             entry.setIdResource( nIdNewForm );
222             EntryHome.copy( entry );
223         }
224         FormListenerManager.notifyListenersFormCreation( nIdForm );
225         return nIdNewForm;
226     }
227 
228     /**
229      * Save a form in database
230      * 
231      * @param form
232      *            the form to save
233      * @return the form saved (with its id)
234      */
235     public static Form="../../../../../../fr/paris/lutece/plugins/appointment/business/form/Form.html#Form">Form saveForm( Form form )
236     {
237         return FormHome.create( form );
238     }
239 
240     /**
241      * Create a form from an appointmentForm DTO
242      * 
243      * @param appointmentForm
244      *            the appointmentForm DTO
245      * @return the id of the form created
246      */
247     public static int createAppointmentForm( AppointmentFormDTO appointmentForm )
248     {
249         Form form = FormService.createForm( appointmentForm );
250         int nIdForm = form.getIdForm( );
251         FormMessageService.createFormMessageWithDefaultValues( nIdForm );
252         LocalDate dateNow = LocalDate.now( );
253         DisplayService.createDisplay( appointmentForm, nIdForm );
254         LocalizationService.createLocalization( appointmentForm, nIdForm );
255         FormRuleService.createFormRule( appointmentForm, nIdForm );
256         ReservationRule reservationRule = ReservationRuleService.createReservationRule( appointmentForm, nIdForm );
257         int nMaxCapacity = reservationRule.getMaxCapacityPerSlot( );
258         LocalDate startWeek = ( appointmentForm.getDateStartValidity( ) != null ) ? appointmentForm.getDateStartValidity( ).toLocalDate( ) : dateNow;
259         LocalDate endWeek = ( appointmentForm.getDateEndValidity( ) != null ) ? appointmentForm.getDateEndValidity( ).toLocalDate( ) : startWeek.plusYears( 1 );
260         WeekDefinitionService.createWeekDefinition( reservationRule.getIdReservationRule( ), startWeek, endWeek );
261         LocalTime startingTime = LocalTime.parse( appointmentForm.getTimeStart( ) );
262         LocalTime endingTime = LocalTime.parse( appointmentForm.getTimeEnd( ) );
263         int nDuration = appointmentForm.getDurationAppointments( );
264         for ( DayOfWeek dayOfWeek : WorkingDayService.getOpenDays( appointmentForm ) )
265         {
266             WorkingDayService.generateWorkingDayAndListTimeSlot( reservationRule.getIdReservationRule( ), dayOfWeek, startingTime, endingTime, nDuration,
267                     nMaxCapacity );
268         }
269         FormListenerManager.notifyListenersFormCreation( nIdForm );
270         return nIdForm;
271     }
272 
273     /**
274      * Update a form with the new values of the Global Parameters of an appointmentForm DTO
275      * 
276      * @param appointmentForm
277      *            the appointmentForm DTO
278      * 
279      */
280     public static void updateGlobalParameters( AppointmentFormDTO appointmentForm )
281     {
282         int nIdForm = appointmentForm.getIdForm( );
283         DisplayService.updateDisplay( appointmentForm, nIdForm );
284         LocalizationService.updateLocalization( appointmentForm, nIdForm );
285         FormRuleService.updateFormRule( appointmentForm, nIdForm );
286         FormService.updateForm( appointmentForm );
287     }
288 
289     /**
290      * Build all the active forms
291      * 
292      * @return the list of appointment form dto that are active
293      */
294     public static List<AppointmentFormDTO> buildAllActiveAppointmentForm( )
295     {
296         List<AppointmentFormDTO> listActiveAppointmentForm = new ArrayList<>( );
297         for ( Form form : FormHome.findActiveForms( ) )
298         {
299             listActiveAppointmentForm.add( buildAppointmentForm( form.getIdForm( ), 0 ) );
300         }
301         return listActiveAppointmentForm;
302     }
303 
304     /**
305      * Build all the appointForm DTO of the database light because the appointFormDTO is only fill in with the form id, the title and if the form is active or
306      * not
307      * 
308      * @return the list of all the appointmentForm DTO
309      */
310     public static List<AppointmentFormDTO> buildAllAppointmentFormLight( )
311     {
312         List<AppointmentFormDTO> listAppointmentFormLight = new ArrayList<>( );
313         for ( Form form : FormService.findAllForms( ) )
314         {
315             listAppointmentFormLight.add( buildAppointmentFormLight( form ) );
316         }
317         return listAppointmentFormLight;
318     }
319 
320     /**
321      * Build a list of all the forms (id, title)
322      * 
323      * @return the reference list
324      */
325     public static ReferenceList findAllInReferenceList( )
326     {
327         List<Form> listForm = findAllForms( );
328         ReferenceList refListForms = new ReferenceList( listForm.size( ) );
329         for ( Form form : listForm )
330         {
331             refListForms.addItem( form.getIdForm( ), form.getTitle( ) );
332         }
333         return refListForms;
334     }
335 
336     /**
337      * Build all the active forms of the database
338      * 
339      * @return a list of appointmentForm DTO
340      */
341     public static List<AppointmentFormDTO> buildAllActiveAndDisplayedOnPortletAppointmentForm( )
342     {
343         List<AppointmentFormDTO> listAppointmentForm = new ArrayList<>( );
344         for ( Form form : FormService.findAllActiveAndDisplayedOnPortletForms( ) )
345         {
346             listAppointmentForm.add( buildAppointmentForm( form.getIdForm( ), 0 ) );
347         }
348         return listAppointmentForm;
349     }
350 
351     /**
352      * Build an appointmentFormDTO light
353      * 
354      * @param form
355      *            the form object
356      * @return the appointmentForm DTO
357      */
358     public static AppointmentFormDTO buildAppointmentFormLight( Form form )
359     {
360         AppointmentFormDTO/AppointmentFormDTO.html#AppointmentFormDTO">AppointmentFormDTO appointmentForm = new AppointmentFormDTO( );
361         if ( form != null )
362         {
363             fillAppointmentFormWithFormPart( appointmentForm, form );
364         }
365         return appointmentForm;
366     }
367 
368     /**
369      * Build an appointmentForm light
370      * 
371      * @param nIdForm
372      *            the form Id
373      * @return the appointmentForm DTO
374      */
375     public static AppointmentFormDTO buildAppointmentFormLight( int nIdForm )
376     {
377         Form form = FormService.findFormLightByPrimaryKey( nIdForm );
378         return buildAppointmentFormLight( form );
379     }
380 
381     /**
382      * Build an appointmentForm DTO
383      * 
384      * @param nIdForm
385      *            the Form Id
386      * @param nIdReservationRule
387      *            the Reservation Rule Id
388      * 
389      * @return the apointmentForm DTO built
390      */
391     public static AppointmentFormDTO buildAppointmentForm( int nIdForm, int nIdReservationRule )
392     {
393         ReservationRule reservationRule = null;
394         if ( nIdReservationRule > 0 )
395         {
396             reservationRule = ReservationRuleService.findReservationRuleById( nIdReservationRule );
397         }
398         return buildAppointmentForm( nIdForm, reservationRule );
399     }
400 
401     /**
402      * Build an appointmentForm DTO
403      * 
404      * @param form
405      *            the Form object
406      * @param ReservationRule
407      *            the Reservation Rule object
408      * 
409      * @return the apointmentForm DTO built
410      */
411     public static AppointmentFormDTO buildAppointmentForm( int nIdForm, ReservationRule reservationRule )
412     {
413         AppointmentFormDTO appointmentForm = buildAppointmentFormWithoutReservationRule( nIdForm );
414         LocalDate dateOfApply = LocalDate.now( );
415         if ( reservationRule == null )
416         {
417             reservationRule = ReservationRuleService.findReservationRuleByIdFormAndClosestToDateOfApply( nIdForm, dateOfApply );
418         }
419 
420         if ( reservationRule != null )
421         {
422             fillAppointmentFormWithReservationRulePart( appointmentForm, reservationRule );
423         }
424 
425         return appointmentForm;
426     }
427 
428     /**
429      * Build an appointmentForm DTO
430      * 
431      * @param form
432      *            the Form object
433      * 
434      * @return the apointmentForm DTO built
435      */
436     public static AppointmentFormDTO buildAppointmentFormWithoutReservationRule( int nIdForm )
437     {
438         AppointmentFormDTO/AppointmentFormDTO.html#AppointmentFormDTO">AppointmentFormDTO appointmentForm = new AppointmentFormDTO( );
439         Form form = FormService.findFormLightByPrimaryKey( nIdForm );
440         fillAppointmentFormWithFormPart( appointmentForm, form );
441         Display display = DisplayService.findDisplayWithFormId( form.getIdForm( ) );
442         if ( display != null )
443         {
444             fillAppointmentFormWithDisplayPart( appointmentForm, display );
445         }
446         Localization localization = LocalizationService.findLocalizationWithFormId( form.getIdForm( ) );
447         if ( localization != null )
448         {
449             fillAppointmentFormWithLocalizationPart( appointmentForm, localization );
450         }
451         FormRule formRule = FormRuleService.findFormRuleWithFormId( form.getIdForm( ) );
452         if ( formRule != null )
453         {
454             fillAppointmentFormWithFormRulePart( appointmentForm, formRule );
455         }
456 
457         return appointmentForm;
458     }
459 
460     /**
461      * Fill the appointmentForm DTO with the Reservation Rule
462      * 
463      * @param appointmentForm
464      *            the appointmentForm DTO
465      * @param reservationRule
466      *            the reservation rule
467      */
468     public static void fillAppointmentFormWithReservationRulePart( AppointmentFormDTO appointmentForm, ReservationRule reservationRule )
469     {
470         List<WorkingDay> listWorkingDay = reservationRule.getListWorkingDay( );
471         if ( CollectionUtils.isNotEmpty( listWorkingDay ) )
472         {
473             for ( WorkingDay workingDay : listWorkingDay )
474             {
475                 DayOfWeek dayOfWeek = DayOfWeek.of( workingDay.getDayOfWeek( ) );
476                 switch( dayOfWeek )
477                 {
478                     case MONDAY:
479                         appointmentForm.setIsOpenMonday( Boolean.TRUE );
480                         break;
481                     case TUESDAY:
482                         appointmentForm.setIsOpenTuesday( Boolean.TRUE );
483                         break;
484                     case WEDNESDAY:
485                         appointmentForm.setIsOpenWednesday( Boolean.TRUE );
486                         break;
487                     case THURSDAY:
488                         appointmentForm.setIsOpenThursday( Boolean.TRUE );
489                         break;
490                     case FRIDAY:
491                         appointmentForm.setIsOpenFriday( Boolean.TRUE );
492                         break;
493                     case SATURDAY:
494                         appointmentForm.setIsOpenSaturday( Boolean.TRUE );
495                         break;
496                     case SUNDAY:
497                         appointmentForm.setIsOpenSunday( Boolean.TRUE );
498                         break;
499                 }
500             }
501             // We suppose that all the days have the same opening and closing
502             // hours (it can be modified after)
503             LocalTime minStartingTime = WorkingDayService.getMinStartingTimeOfAListOfWorkingDay( listWorkingDay );
504             LocalTime maxEndingTime = WorkingDayService.getMaxEndingTimeOfAListOfWorkingDay( listWorkingDay );
505             appointmentForm.setTimeStart( minStartingTime.toString( ) );
506             appointmentForm.setTimeEnd( maxEndingTime.toString( ) );
507         }
508         appointmentForm.setDurationAppointments( reservationRule.getDurationAppointments( ) );
509         appointmentForm.setIdReservationRule( reservationRule.getIdReservationRule( ) );
510         appointmentForm.setMaxCapacityPerSlot( reservationRule.getMaxCapacityPerSlot( ) );
511         appointmentForm.setMaxPeoplePerAppointment( reservationRule.getMaxPeoplePerAppointment( ) );
512         appointmentForm.setName( reservationRule.getName( ) );
513         appointmentForm.setDescriptionRule( reservationRule.getDescriptionRule( ) );
514         appointmentForm.setColor( reservationRule.getColor( ) );
515     }
516 
517     /**
518      * Fill the appointmentForm DTO with the form rule
519      * 
520      * @param appointmentForm
521      *            the appointmentForm DTO
522      * @param formRule
523      *            the form rule
524      */
525     private static void fillAppointmentFormWithFormRulePart( AppointmentFormDTO appointmentForm, FormRule formRule )
526     {
527         appointmentForm.setEnableCaptcha( formRule.getIsCaptchaEnabled( ) );
528         appointmentForm.setEnableMandatoryEmail( formRule.getIsMandatoryEmailEnabled( ) );
529         appointmentForm.setActiveAuthentication( formRule.getIsActiveAuthentication( ) );
530         appointmentForm.setNbDaysBeforeNewAppointment( formRule.getNbDaysBeforeNewAppointment( ) );
531         appointmentForm.setMinTimeBeforeAppointment( formRule.getMinTimeBeforeAppointment( ) );
532         appointmentForm.setNbMaxAppointmentsPerUser( formRule.getNbMaxAppointmentsPerUser( ) );
533         appointmentForm.setNbDaysForMaxAppointmentsPerUser( formRule.getNbDaysForMaxAppointmentsPerUser( ) );
534         appointmentForm.setBoOverbooking( formRule.getBoOverbooking( ) );
535     }
536 
537     /**
538      * Fill the appointmentForm DTO with the form
539      * 
540      * @param appointmentForm
541      *            the appointmentForm DTO
542      * @param form
543      *            the Form
544      */
545     private static void fillAppointmentFormWithFormPart( AppointmentFormDTO appointmentForm, Form form )
546     {
547         appointmentForm.setIdForm( form.getIdForm( ) );
548         appointmentForm.setTitle( form.getTitle( ) );
549         appointmentForm.setDescription( form.getDescription( ) );
550         appointmentForm.setReference( form.getReference( ) );
551         if ( form.getIdCategory( ) == null || form.getIdCategory( ) == 0 )
552         {
553             appointmentForm.setIdCategory( -1 );
554         }
555         else
556         {
557             appointmentForm.setIdCategory( form.getIdCategory( ) );
558         }
559         appointmentForm.setDateStartValidity( form.getStartingValiditySqlDate( ) );
560         appointmentForm.setDateEndValidity( form.getEndingValiditySqlDate( ) );
561         appointmentForm.setIdWorkflow( form.getIdWorkflow( ) );
562         appointmentForm.setWorkgroup( form.getWorkgroup( ) );
563         appointmentForm.setIsActive( form.getIsActive( ) );
564         appointmentForm.setIsMultislotAppointment( form.getIsMultislotAppointment( ) );
565         appointmentForm.setNbConsecutiveSlots( form.getNbConsecutiveSlots( ) );
566         appointmentForm.setRole( form.getRole( ) );
567         appointmentForm.setCapacityPerSlot( form.getCapacityPerSlot( ) );
568         appointmentForm.setAnonymizable(form.isAnonymizable());
569         appointmentForm.setAnonymizationPattern(form.getAnonymizationPattern());
570     }
571 
572     /**
573      * Fill the appointmentForm DTO with the display
574      * 
575      * @param appointmentForm
576      *            the appointmentForm DTO
577      * @param display
578      *            the display
579      */
580     private static void fillAppointmentFormWithDisplayPart( AppointmentFormDTO appointmentForm, Display display )
581     {
582         appointmentForm.setDisplayTitleFo( display.isDisplayTitleFo( ) );
583         appointmentForm.setIcon( display.getIcon( ) );
584         appointmentForm.setNbWeeksToDisplay( display.getNbWeeksToDisplay( ) );
585         appointmentForm.setIsDisplayedOnPortlet( display.isDisplayedOnPortlet( ) );
586         appointmentForm.setCalendarTemplateId( display.getIdCalendarTemplate( ) );
587     }
588 
589     /**
590      * Fill the appointmentForm DTO with the localization
591      * 
592      * @param appointmentForm
593      *            the appointmentForm DTO
594      * @param localization
595      *            the localization
596      */
597     private static void fillAppointmentFormWithLocalizationPart( AppointmentFormDTO appointmentForm, Localization localization )
598     {
599         if ( localization != null )
600         {
601             if ( localization.getLongitude( ) != null )
602             {
603                 appointmentForm.setLongitude( localization.getLongitude( ) );
604             }
605             if ( localization.getLatitude( ) != null )
606             {
607                 appointmentForm.setLatitude( localization.getLatitude( ) );
608             }
609             if ( localization.getAddress( ) != null )
610             {
611                 appointmentForm.setAddress( localization.getAddress( ) );
612             }
613         }
614 
615     }
616 
617     /**
618      * Create a form from an appointmentForm DTO
619      * 
620      * @param appointmentForm
621      *            the appointmentForm DTO
622      * @return the Form created
623      */
624     private static Form createForm( AppointmentFormDTO appointmentForm )
625     {
626         Formugins/appointment/business/form/Form.html#Form">Form form = new Form( );
627         fillInFormWithAppointmentForm( form, appointmentForm );
628         FormHome.create( form );
629         return form;
630     }
631 
632     /**
633      * Update a form object with the values of the appointmentForm DTO
634      * 
635      * @param appointmentForm
636      *            the appointmentForm DTO
637      * @return the Form object updated
638      */
639     public static Form updateForm( AppointmentFormDTO appointmentForm )
640     {
641         Form form = FormService.findFormLightByPrimaryKey( appointmentForm.getIdForm( ) );
642         fillInFormWithAppointmentForm( form, appointmentForm );
643         FormService.updateForm( form );
644         return form;
645     }
646 
647     /**
648      * Update a form
649      * 
650      * @param form
651      *            the form
652      * @return the form updated
653      */
654     public static Form../../../../../../fr/paris/lutece/plugins/appointment/business/form/Form.html#Form">Form updateForm( Form form )
655     {
656         Form formUpdated = FormHome.update( form );
657         FormListenerManager.notifyListenersFormChange( formUpdated.getIdForm( ) );
658         return formUpdated;
659     }
660 
661     /**
662      * Fill the form object with the values of the appointmentForm DTO
663      * 
664      * @param form
665      *            the form object
666      * @param appointmentForm
667      *            the appointmentForm DTO
668      * @return the form completed
669      */
670     public static Formr/paris/lutece/plugins/appointment/business/form/Form.html#Form">Form fillInFormWithAppointmentForm( Form form, AppointmentFormDTO appointmentForm )
671     {
672         form.setTitle( appointmentForm.getTitle( ) );
673         form.setDescription( appointmentForm.getDescription( ) );
674         form.setReference( appointmentForm.getReference( ) );
675         if ( appointmentForm.getIdCategory( ) == -1 )
676         {
677             form.setIdCategory( null );
678         }
679         else
680         {
681             form.setIdCategory( appointmentForm.getIdCategory( ) );
682         }
683         form.setStartingValiditySqlDate( appointmentForm.getDateStartValidity( ) );
684         form.setEndingValiditySqlDate( appointmentForm.getDateEndValidity( ) );
685         form.setIsActive( appointmentForm.getIsActive( ) );
686         form.setIdWorkflow( appointmentForm.getIdWorkflow( ) );
687         form.setWorkgroup( appointmentForm.getWorkgroup( ) );
688         form.setIsMultislotAppointment( appointmentForm.getIsMultislotAppointment( ) );
689         form.setNbConsecutiveSlots( appointmentForm.getNbConsecutiveSlots( ) );
690         form.setRole( appointmentForm.getRole( ) );
691         form.setCapacityPerSlot( appointmentForm.getCapacityPerSlot( ) );
692         form.setAnonymizable(appointmentForm.isAnonymizable());
693         form.setAnonymizationPattern( appointmentForm.getAnonymizationPattern( ) );
694         return form;
695     }
696 
697     /**
698      * Remove a Form from the database
699      * 
700      * @param nIdForm
701      *            the form id to remove
702      */
703     public static void removeForm( int nIdForm )
704     {
705         TransactionManager.beginTransaction( AppointmentPlugin.getPlugin( ) );
706         try
707         {
708             // Delete all the responses linked to all the appointments of the form
709             for ( Appointment appointment : AppointmentService.findListAppointmentByIdForm( nIdForm ) )
710             {
711                 AppointmentResponseService.removeResponsesByIdAppointment( appointment.getIdAppointment( ) );
712             }
713 
714             SlotHome.deleteByIdForm( nIdForm );
715 
716             for ( ReservationRule rule : ReservationRuleHome.findByIdForm( nIdForm ) )
717             {
718 
719                 List<WorkingDay> listWorkingDay = WorkingDayService.findListWorkingDayByWeekDefinitionRule( rule.getIdReservationRule( ) );
720                 for ( WorkingDay workingDay : listWorkingDay )
721                 {
722 
723                     TimeSlotHome.deleteByIdWorkingDay( workingDay.getIdWorkingDay( ) );
724                     WorkingDayHome.delete( workingDay.getIdWorkingDay( ) );
725 
726                 }
727                 WeekDefinitionHome.deleteByIdReservationRule( rule.getIdReservationRule( ) );
728                 ReservationRuleHome.delete( rule.getIdReservationRule( ) );
729             }
730 
731             ClosingDayHome.deleteByIdForm( nIdForm );
732             FormRuleHome.deleteByIdFom( nIdForm );
733             DisplayHome.deleteByIdForm( nIdForm );
734             LocalizationHome.deleteByIdForm( nIdForm );
735             FormMessageHome.deleteByIdForm( nIdForm );
736             CommentHome.removeByIdFom( nIdForm );
737             FormHome.delete( nIdForm );
738             EntryService.getService( ).removeEntriesByIdAppointmentForm( nIdForm );
739 
740             TransactionManager.commitTransaction( AppointmentPlugin.getPlugin( ) );
741 
742             FormListenerManager.notifyListenersFormRemoval( nIdForm );
743             AppointmentListenerManager.notifyListenersAppointmentFormRemoval( nIdForm );
744 
745         }
746         catch( Exception e )
747         {
748             TransactionManager.rollBack( AppointmentPlugin.getPlugin( ) );
749             AppLogService.error( "Error delete form: " + nIdForm + e.getMessage( ), e );
750             throw new AppException( e.getMessage( ), e );
751 
752         }
753     }
754 
755     /**
756      * Find all the forms in the database
757      * 
758      * @return a list of all the forms
759      */
760     public static List<Form> findAllForms( )
761     {
762         return FormHome.findAllForms( );
763     }
764 
765     /**
766      * Find all the active forms in database
767      * 
768      * @return a list of all the active forms
769      */
770     public static List<Form> findAllActiveForms( )
771     {
772         return FormHome.findActiveForms( );
773     }
774 
775     /**
776      * Find all the active forms that have to be displayed on portlet in database
777      * 
778      * @return a list of all the found forms
779      */
780     public static List<Form> findAllActiveAndDisplayedOnPortletForms( )
781     {
782         return FormHome.findActiveAndDisplayedOnPortletForms( );
783     }
784 
785     /**
786      * find a form by its primary key
787      * 
788      * @param nIdForm
789      *            the form Id
790      * @return the Form
791      */
792     public static Form findFormLightByPrimaryKey( int nIdForm )
793     {
794         return FormHome.findByPrimaryKey( nIdForm );
795     }
796 
797     /**
798      * Find forms by the title
799      * 
800      * @param strTitle
801      *            the form title
802      * @return the Forms with this title
803      */
804     public static List<Form> findFormsByTitle( String strTitle )
805     {
806         return FormHome.findByTitle( strTitle );
807     }
808 
809 }