View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.appointment.web;
35  
36  import java.text.ParseException;
37  import java.time.LocalTime;
38  import java.time.temporal.ChronoUnit;
39  
40  import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFormDTO;
41  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
42  
43  public abstract class AbstractAppointmentFormAndSlotJspBean extends MVCAdminJspBean
44  {
45  
46      /**
47  	 * 
48  	 */
49      private static final long serialVersionUID = 7709182167218092169L;
50      static final String ERROR_MESSAGE_TIME_START_AFTER_TIME_END = "appointment.message.error.timeStartAfterTimeEnd";
51      static final String ERROR_MESSAGE_TIME_START_AFTER_DATE_END = "appointment.message.error.dateStartAfterTimeEnd";
52      static final String ERROR_MESSAGE_NO_WORKING_DAY_CHECKED = "appointment.message.error.noWorkingDayChecked";
53      static final String ERROR_MESSAGE_APPOINTMENT_SUPERIOR_MIDDLE = "appointment.message.error.formatDaysBeforeAppointmentMiddleSuperior";
54      static final String MESSAGE_ERROR_DAY_DURATION_APPOINTMENT_NOT_MULTIPLE_FORM = "appointment.message.error.durationAppointmentDayNotMultipleForm";
55      private static final String MESSAGE_ERROR_NUMBER_OF_SEATS_BOOKED = "appointment.message.error.numberOfSeatsBookedAndConcurrentAppointments";
56  
57      /**
58       * Check Constraints
59       * 
60       * @param appointmentForm
61       * @return
62       * @throws ParseException
63       */
64      protected boolean checkConstraints( AppointmentFormDTO appointmentForm )
65      {
66          return checkStartingAndEndingTime( appointmentForm ) && checkStartingAndEndingValidityDate( appointmentForm )
67                  && checkSlotCapacityAndPeoplePerAppointment( appointmentForm ) && checkAtLeastOneWorkingDayOpen( appointmentForm );
68      }
69  
70      /**
71       * Check that the user has checked as least one working day on its form
72       * 
73       * @param appointmentForm
74       *            the appointForm DTO
75       * @return true if at least one working day is checked, false otherwise
76       */
77      private boolean checkAtLeastOneWorkingDayOpen( AppointmentFormDTO appointmentForm )
78      {
79          boolean bReturn = true;
80          if ( !( appointmentForm.getIsOpenMonday( ) || appointmentForm.getIsOpenTuesday( ) || appointmentForm.getIsOpenWednesday( )
81                  || appointmentForm.getIsOpenThursday( ) || appointmentForm.getIsOpenFriday( ) || appointmentForm.getIsOpenSaturday( ) || appointmentForm
82                      .getIsOpenSunday( ) ) )
83          {
84              bReturn = false;
85              addError( ERROR_MESSAGE_NO_WORKING_DAY_CHECKED, getLocale( ) );
86          }
87          return bReturn;
88      }
89  
90      /**
91       * Check the starting time and the ending time of the appointmentFormDTO
92       * 
93       * @param appointmentForm
94       *            the appointmentForm DTO
95       * @return false if there is an error
96       */
97      private boolean checkStartingAndEndingTime( AppointmentFormDTO appointmentForm )
98      {
99          boolean bReturn = true;
100         LocalTime startingTime = LocalTime.parse( appointmentForm.getTimeStart( ) );
101         LocalTime endingTime = LocalTime.parse( appointmentForm.getTimeEnd( ) );
102         if ( startingTime.isAfter( endingTime ) )
103         {
104             bReturn = false;
105             addError( ERROR_MESSAGE_TIME_START_AFTER_TIME_END, getLocale( ) );
106         }
107         long lMinutes = startingTime.until( endingTime, ChronoUnit.MINUTES );
108         if ( appointmentForm.getDurationAppointments( ) > lMinutes )
109         {
110             bReturn = false;
111             addError( ERROR_MESSAGE_APPOINTMENT_SUPERIOR_MIDDLE, getLocale( ) );
112         }
113         if ( ( lMinutes % appointmentForm.getDurationAppointments( ) ) != 0 )
114         {
115             bReturn = false;
116             addError( MESSAGE_ERROR_DAY_DURATION_APPOINTMENT_NOT_MULTIPLE_FORM, getLocale( ) );
117         }
118         return bReturn;
119     }
120 
121     /**
122      * Check the starting and the ending validity date of the appointmentForm DTO
123      * 
124      * @param appointmentForm
125      *            the appointmentForm DTO
126      * @return false if there is an error
127      */
128     private boolean checkStartingAndEndingValidityDate( AppointmentFormDTO appointmentForm )
129     {
130         boolean bReturn = true;
131         if ( ( appointmentForm.getDateStartValidity( ) != null ) && ( appointmentForm.getDateEndValidity( ) != null ) )
132         {
133             if ( appointmentForm.getDateStartValidity( ).toLocalDate( ).isAfter( appointmentForm.getDateEndValidity( ).toLocalDate( ) ) )
134             {
135                 bReturn = false;
136                 addError( ERROR_MESSAGE_TIME_START_AFTER_DATE_END, getLocale( ) );
137             }
138         }
139         return bReturn;
140     }
141 
142     /**
143      * Check the slot capacity and the max people per appointment of the appointmentForm DTO
144      * 
145      * @param appointmentForm
146      *            athe appointmentForm DTO
147      * @return false if the maximum number of people per appointment is bigger than the maximum capacity of the slot
148      */
149     private boolean checkSlotCapacityAndPeoplePerAppointment( AppointmentFormDTO appointmentForm )
150     {
151         boolean bReturn = true;
152         if ( appointmentForm.getMaxPeoplePerAppointment( ) > appointmentForm.getMaxCapacityPerSlot( ) )
153         {
154             bReturn = false;
155             addError( MESSAGE_ERROR_NUMBER_OF_SEATS_BOOKED, getLocale( ) );
156         }
157         return bReturn;
158     }
159 }