1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
87 protected static final String VAR_CAP = "var_cap";
88 protected static final String NEW_CAP = "new_cap";
89
90
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
96 private static final String PARAMETER_PAGE_INDEX = "page_index";
97
98
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
113 private static final CaptchaSecurityService _captchaSecurityService = new CaptchaSecurityService( );
114 private String _strCurrentPageIndex;
115 private int _nItemsPerPage;
116
117
118
119
120
121
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
132
133
134
135
136
137
138
139
140
141
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
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
166
167
168
169
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
186
187
188
189
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
217
218
219
220
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
236
237
238
239
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
254
255
256
257
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
272
273
274
275
276
277
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
297
298
299
300
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
313
314
315
316
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
330
331
332
333
334
335
336
337
338
339
340
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 }