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.web;
35  
36  import java.time.LocalTime;
37  import java.time.temporal.ChronoUnit;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import fr.paris.lutece.api.user.User;
45  import fr.paris.lutece.plugins.appointment.business.calendar.CalendarTemplateHome;
46  import fr.paris.lutece.plugins.appointment.service.AppointmentResourceIdService;
47  import fr.paris.lutece.plugins.appointment.service.AppointmentUtilities;
48  import fr.paris.lutece.plugins.appointment.service.CategoryService;
49  import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFormDTO;
50  import fr.paris.lutece.plugins.appointment.web.dto.ReservationRuleDTO;
51  import fr.paris.lutece.portal.business.role.RoleHome;
52  import fr.paris.lutece.portal.business.user.AdminUser;
53  import fr.paris.lutece.portal.service.captcha.CaptchaSecurityService;
54  import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
55  import fr.paris.lutece.portal.service.plugin.Plugin;
56  import fr.paris.lutece.portal.service.plugin.PluginService;
57  import fr.paris.lutece.portal.service.rbac.RBACService;
58  import fr.paris.lutece.portal.service.util.AppPropertiesService;
59  import fr.paris.lutece.portal.service.workflow.WorkflowService;
60  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
61  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
62  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
63  import fr.paris.lutece.util.ReferenceList;
64  import fr.paris.lutece.util.html.AbstractPaginator;
65  import fr.paris.lutece.util.url.UrlItem;
66  
67  public abstract class AbstractAppointmentFormAndSlotJspBean extends MVCAdminJspBean
68  {
69  
70      /**
71       * 
72       */
73      private static final long serialVersionUID = 7709182167218092169L;
74      protected static final String PARAMETER_ERROR_MODIFICATION = "error_modification";
75      protected static final String ERROR_MESSAGE_TIME_START_AFTER_TIME_END = "appointment.message.error.timeStartAfterTimeEnd";
76      protected static final String ERROR_MESSAGE_TIME_START_AFTER_DATE_END = "appointment.message.error.dateStartAfterTimeEnd";
77      protected static final String ERROR_MESSAGE_NO_WORKING_DAY_CHECKED = "appointment.message.error.noWorkingDayChecked";
78      protected static final String ERROR_MESSAGE_APPOINTMENT_SUPERIOR_MIDDLE = "appointment.message.error.formatDaysBeforeAppointmentMiddleSuperior";
79      protected static final String ERROR_MESSAGE_WEEK_IS_OPEN_FO = "appointment.modifyCalendarSlots.errorWeekIsOpenFo";
80      protected static final String MESSAGE_ERROR_DAY_DURATION_APPOINTMENT_NOT_MULTIPLE_FORM = "appointment.message.error.durationAppointmentDayNotMultipleForm";
81      private static final String MESSAGE_ERROR_NUMBER_OF_SEATS_BOOKED = "appointment.message.error.numberOfSeatsBookedAndConcurrentAppointments";
82      private static final String MESSAGE_MULTI_SLOT_ERROR_NUMBER_OF_SEATS_BOOKED = "appointment.message.error.multiSlot.numberOfSeatsBookedAndConcurrentAppointments";
83      private static final String MESSAGE_ERROR_NUMBER_DAY_BETWEEN_TWO_APPOINTMENTS = "appointment.message.error.nbDaysBetweenTwoAppointments";
84      private static final String MESSAGE_ERROR_MAX_APPOINTMENTS_PER_USER = "appointment.message.error.nbMaxAppointmentsPerUser";
85  
86      // Constantes
87      protected static final String VAR_CAP = "var_cap";
88      protected static final String NEW_CAP = "new_cap";
89  
90      // Properties
91      private static final String PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE = "appointment.listItems.itemsPerPage";
92      protected static final String PARAMETER_CAPACITY_MOD = "capacity";
93      private static final String PROPERTY_MODULE_APPOINTMENT_RESOURCE_NAME = "appointment.moduleAppointmentResource.name";
94      private static final String PROPERTY_MODULE_APPOINTMENT_DESK_NAME = "appointment.moduleAppointmentDesk.name";
95      // Parameters
96      private static final String PARAMETER_PAGE_INDEX = "page_index";
97  
98      // Markers
99      private static final String MARK_PAGINATOR = "paginator";
100     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
101     private static final String MARK_USER_WORKGROUP_REF_LIST = "user_workgroup_list";
102     private static final String MARK_APPOINTMENT_RESOURCE_ENABLED = "isResourceInstalled";
103     private static final String MARK_APPOINTMENT_DESK_ENABLED = "isDeskInstalled";
104     private static final String MARK_MAILING_LIST = "mailing_list";
105     private static final String MARK_APPOINTMENT_FORM = "appointmentform";
106     private static final String MARK_LIST_WORKFLOWS = "listWorkflows";
107     private static final String MARK_IS_CAPTCHA_ENABLED = "isCaptchaEnabled";
108     private static final String MARK_REF_LIST_CALENDAR_TEMPLATES = "refListCalendarTemplates";
109     private static final String MARK_REF_LIST_ROLES = "refListRoles";
110     private static final String MARK_LIST_CATEGORIES = "listCategories";
111     protected static final String MARK_LOCALE = "language";
112     // Variables
113     private static final CaptchaSecurityService _captchaSecurityService = new CaptchaSecurityService( );
114     private String _strCurrentPageIndex;
115     private int _nItemsPerPage;
116 
117     /**
118      * Check Constraints
119      * 
120      * @param appointmentForm
121      * @return the boolean
122      */
123     protected boolean checkConstraints( AppointmentFormDTO appointmentForm )
124     {
125         return checkStartingAndEndingTime( appointmentForm ) && checkStartingAndEndingValidityDate( appointmentForm )
126                 && checkSlotCapacityAndPeoplePerAppointment( appointmentForm ) && checkAtLeastOneWorkingDayOpen( appointmentForm )
127                 && checkMultiSlotFormTypeBookablePlaces( appointmentForm ) && checkControlMaxAppointmentsPerUser( appointmentForm );
128     }
129 
130     /**
131      * Return a model that contains the list and paginator infos
132      * 
133      * @param request
134      *            The HTTP request
135      * @param strBookmark
136      *            The bookmark
137      * @param list
138      *            The list of item
139      * @param strManageJsp
140      *            The JSP
141      * @return The model
142      */
143     protected <T> Map<String, Object> getPaginatedListModel( HttpServletRequest request, String strBookmark, List<T> list, String strManageJsp )
144     {
145         int nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE, 50 );
146         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
147         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, nDefaultItemsPerPage );
148 
149         UrlItem url = new UrlItem( strManageJsp );
150         String strUrl = url.getUrl( );
151 
152         // PAGINATOR
153         LocalizedPaginator<T> paginator = new LocalizedPaginator<>( list, _nItemsPerPage, strUrl, PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
154 
155         Map<String, Object> model = getModel( );
156 
157         model.put( MARK_NB_ITEMS_PER_PAGE, String.valueOf( _nItemsPerPage ) );
158         model.put( MARK_PAGINATOR, paginator );
159         model.put( strBookmark, paginator.getPageItems( ) );
160 
161         return model;
162     }
163 
164     /**
165      * Check that the user has checked as least one working day on its form
166      * 
167      * @param appointmentForm
168      *            the appointForm DTO
169      * @return true if at least one working day is checked, false otherwise
170      */
171     private boolean checkAtLeastOneWorkingDayOpen( AppointmentFormDTO appointmentForm )
172     {
173         boolean bReturn = true;
174         if ( !( appointmentForm.getIsOpenMonday( ) || appointmentForm.getIsOpenTuesday( ) || appointmentForm.getIsOpenWednesday( )
175                 || appointmentForm.getIsOpenThursday( ) || appointmentForm.getIsOpenFriday( ) || appointmentForm.getIsOpenSaturday( )
176                 || appointmentForm.getIsOpenSunday( ) ) )
177         {
178             bReturn = false;
179             addError( ERROR_MESSAGE_NO_WORKING_DAY_CHECKED, getLocale( ) );
180         }
181         return bReturn;
182     }
183 
184     /**
185      * Check the starting time and the ending time of the appointmentFormDTO
186      * 
187      * @param appointmentForm
188      *            the appointmentForm DTO
189      * @return false if there is an error
190      */
191     private boolean checkStartingAndEndingTime( AppointmentFormDTO appointmentForm )
192     {
193         boolean bReturn = true;
194         LocalTime startingTime = LocalTime.parse( appointmentForm.getTimeStart( ) );
195         LocalTime endingTime = LocalTime.parse( appointmentForm.getTimeEnd( ) );
196         if ( startingTime.isAfter( endingTime ) )
197         {
198             bReturn = false;
199             addError( ERROR_MESSAGE_TIME_START_AFTER_TIME_END, getLocale( ) );
200         }
201         long lMinutes = startingTime.until( endingTime, ChronoUnit.MINUTES );
202         if ( appointmentForm.getDurationAppointments( ) > lMinutes )
203         {
204             bReturn = false;
205             addError( ERROR_MESSAGE_APPOINTMENT_SUPERIOR_MIDDLE, getLocale( ) );
206         }
207         if ( ( lMinutes % appointmentForm.getDurationAppointments( ) ) != 0 )
208         {
209             bReturn = false;
210             addError( MESSAGE_ERROR_DAY_DURATION_APPOINTMENT_NOT_MULTIPLE_FORM, getLocale( ) );
211         }
212         return bReturn;
213     }
214 
215     /**
216      * Check the starting and the ending validity date of the appointmentForm DTO
217      * 
218      * @param appointmentForm
219      *            the appointmentForm DTO
220      * @return false if there is an error
221      */
222     protected boolean checkStartingAndEndingValidityDate( AppointmentFormDTO appointmentForm )
223     {
224         boolean bReturn = true;
225         if ( appointmentForm.getDateStartValidity( ) != null && appointmentForm.getDateEndValidity( ) != null
226                 && appointmentForm.getDateStartValidity( ).toLocalDate( ).isAfter( appointmentForm.getDateEndValidity( ).toLocalDate( ) ) )
227         {
228             bReturn = false;
229             addError( ERROR_MESSAGE_TIME_START_AFTER_DATE_END, getLocale( ) );
230         }
231         return bReturn;
232     }
233 
234     /**
235      * Check the slot capacity and the max people per appointment of the appointmentForm DTO
236      * 
237      * @param appointmentForm
238      *            the appointmentForm DTO
239      * @return false if the maximum number of people per appointment is bigger than the maximum capacity of the slot
240      */
241     protected boolean checkSlotCapacityAndPeoplePerAppointment( AppointmentFormDTO appointmentForm )
242     {
243         boolean bReturn = true;
244         if ( appointmentForm.getMaxPeoplePerAppointment( ) > appointmentForm.getMaxCapacityPerSlot( ) && !appointmentForm.getBoOverbooking( ) )
245         {
246             bReturn = false;
247             addError( MESSAGE_ERROR_NUMBER_OF_SEATS_BOOKED, getLocale( ) );
248         }
249         return bReturn;
250     }
251 
252     /**
253      * check the number of bookable places will be set to 1 and cann't be modified, when creating a "multi-slot form"
254      * 
255      * @param appointmentForm
256      *            the appointmentForm DTO
257      * @return false if the form type is "multi-slot" and Max people Per Slot is not set to 1
258      */
259     protected boolean checkMultiSlotFormTypeBookablePlaces( AppointmentFormDTO appointmentForm )
260     {
261         boolean bReturn = true;
262         if ( appointmentForm.getIsMultislotAppointment( ) && appointmentForm.getMaxPeoplePerAppointment( ) != 1 )
263         {
264             bReturn = false;
265             addError( MESSAGE_MULTI_SLOT_ERROR_NUMBER_OF_SEATS_BOOKED, getLocale( ) );
266         }
267         return bReturn;
268     }
269 
270     /**
271      * Check that the email is required if the value of max appointments on a defined period or delay between two appointments for the same use is not equal to
272      * 0
273      * 
274      * @param appointmentForm
275      *            the appointmentForm DTO
276      * @return false if email is not required and the value of max appointments on a defined period or delay between two appointments for the same use is not
277      *         equal to 0
278      */
279     protected boolean checkControlMaxAppointmentsPerUser( AppointmentFormDTO appointmentForm )
280     {
281         boolean bReturn = true;
282         if ( appointmentForm.getNbDaysBeforeNewAppointment( ) != 0 && !appointmentForm.getEnableMandatoryEmail( ) )
283         {
284             bReturn = false;
285             addError( MESSAGE_ERROR_NUMBER_DAY_BETWEEN_TWO_APPOINTMENTS, getLocale( ) );
286         }
287         if ( appointmentForm.getNbMaxAppointmentsPerUser( ) != 0 && !appointmentForm.getEnableMandatoryEmail( ) )
288         {
289             bReturn = false;
290             addError( MESSAGE_ERROR_MAX_APPOINTMENTS_PER_USER, getLocale( ) );
291         }
292         return bReturn;
293     }
294 
295     /**
296      * Valdate rule bean
297      * 
298      * @param request
299      * @param strPrefix
300      * @return true if validated otherwise false
301      */
302     protected boolean validateReservationRuleBean( HttpServletRequest request, String strPrefix )
303     {
304 
305         ReservationRuleDTOent/web/dto/ReservationRuleDTO.html#ReservationRuleDTO">ReservationRuleDTO rule = new ReservationRuleDTO( );
306         populate( rule, request );
307         return validateBean( rule, strPrefix );
308 
309     }
310 
311     /**
312      * Valdate rule bean
313      * 
314      * @param appointmentForm
315      * @param strPrefix
316      * @return true if validated otherwise false
317      */
318     protected boolean validateReservationRuleBean( AppointmentFormDTO appointmentForm, String strPrefix )
319     {
320 
321         ReservationRuleDTOent/web/dto/ReservationRuleDTO.html#ReservationRuleDTO">ReservationRuleDTO rule = new ReservationRuleDTO( );
322         AppointmentUtilities.fillInReservationRuleAdvancedParam( rule, appointmentForm );
323 
324         return validateBean( rule, strPrefix );
325 
326     }
327 
328     /**
329      * Add elements to the model to display the left column to modify an appointment form
330      * 
331      * @param request
332      *            The request to store the appointment form in session
333      * @param appointmentForm
334      *            The appointment form
335      * @param user
336      *            The user
337      * @param locale
338      *            The locale
339      * @param model
340      *            the model to add elements in
341      */
342     public static void addElementsToModel( AppointmentFormDTO appointmentForm, AdminUser user, Locale locale, Map<String, Object> model )
343     {
344         Plugin pluginAppointmentResource = PluginService.getPlugin( AppPropertiesService.getProperty( PROPERTY_MODULE_APPOINTMENT_RESOURCE_NAME ) );
345         Plugin moduleAppointmentDesk = PluginService.getPlugin( AppPropertiesService.getProperty( PROPERTY_MODULE_APPOINTMENT_DESK_NAME ) );
346         ReferenceList listRoles = RoleHome.getRolesList( user );
347         model.put( MARK_APPOINTMENT_FORM, appointmentForm );
348         model.put( MARK_LOCALE, locale );
349         model.put( MARK_LIST_WORKFLOWS, WorkflowService.getInstance( ).getWorkflowsEnabled( (User) user, locale ) );
350         model.put( MARK_IS_CAPTCHA_ENABLED, _captchaSecurityService.isAvailable( ) );
351         model.put( MARK_REF_LIST_CALENDAR_TEMPLATES, CalendarTemplateHome.findAllInReferenceList( ) );
352         model.put( MARK_LIST_CATEGORIES, CategoryService.findAllInReferenceList( ) );
353         model.put( MARK_USER_WORKGROUP_REF_LIST, AdminWorkgroupService.getUserWorkgroups( user, locale ) );
354         model.put( MARK_APPOINTMENT_RESOURCE_ENABLED, ( pluginAppointmentResource != null ) && pluginAppointmentResource.isInstalled( ) );
355         model.put( MARK_APPOINTMENT_DESK_ENABLED, ( moduleAppointmentDesk != null ) && moduleAppointmentDesk.isInstalled( ) );
356         model.put( MARK_REF_LIST_ROLES, listRoles );
357         model.put( MARK_MAILING_LIST, AdminMailingListService.getMailingLists( user ) );
358         model.put( AppointmentUtilities.MARK_PERMISSION_ADD_COMMENT, String.valueOf( RBACService.isAuthorized( AppointmentFormDTO.RESOURCE_TYPE,
359                 String.valueOf( appointmentForm.getIdForm( ) ), AppointmentResourceIdService.PERMISSION_ADD_COMMENT_FORM, (User) user ) ) );
360         model.put( AppointmentUtilities.MARK_PERMISSION_MODERATE_COMMENT, String.valueOf( RBACService.isAuthorized( AppointmentFormDTO.RESOURCE_TYPE,
361                 String.valueOf( appointmentForm.getIdForm( ) ), AppointmentResourceIdService.PERMISSION_MODERATE_COMMENT_FORM, (User) user ) ) );
362         model.put( AppointmentUtilities.MARK_PERMISSION_ACCESS_CODE, user.getAccessCode( ) );
363     }
364 }