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.modules.resource.web;
35
36 import java.sql.Date;
37 import java.time.LocalTime;
38 import java.util.ArrayList;
39 import java.util.Calendar;
40 import java.util.Collections;
41 import java.util.Comparator;
42 import java.util.GregorianCalendar;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Locale;
46 import java.util.Map;
47 import java.util.Map.Entry;
48 import java.util.Optional;
49
50 import javax.servlet.http.HttpServletRequest;
51
52 import org.apache.commons.lang.StringUtils;
53
54 import fr.paris.lutece.plugins.appointment.business.rule.ReservationRule;
55 import fr.paris.lutece.plugins.appointment.modules.resource.business.AppointmentResourceHome;
56 import fr.paris.lutece.plugins.appointment.modules.resource.business.calendar.CalendarAppointmentResourceDTO;
57 import fr.paris.lutece.plugins.appointment.modules.resource.business.calendar.CalendarDayDTO;
58 import fr.paris.lutece.plugins.appointment.modules.resource.business.form.FormResourceCalendar;
59 import fr.paris.lutece.plugins.appointment.service.AppointmentService;
60 import fr.paris.lutece.plugins.appointment.service.FormService;
61 import fr.paris.lutece.plugins.appointment.service.ReservationRuleService;
62 import fr.paris.lutece.plugins.appointment.service.WeekDefinitionService;
63 import fr.paris.lutece.plugins.appointment.web.AppointmentFormJspBean;
64 import fr.paris.lutece.plugins.appointment.web.dto.AppointmentDTO;
65 import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFormDTO;
66 import fr.paris.lutece.plugins.resource.business.IResource;
67 import fr.paris.lutece.plugins.resource.service.ResourceService;
68 import fr.paris.lutece.plugins.resource.service.provider.IResourceProvider;
69 import fr.paris.lutece.portal.business.user.AdminUser;
70 import fr.paris.lutece.portal.service.template.AppTemplateService;
71 import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
72 import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
73 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
74 import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
75 import fr.paris.lutece.util.html.HtmlTemplate;
76 import fr.paris.lutece.util.url.UrlItem;
77
78
79
80
81 @Controller( controllerJsp = AppointmentResourceJspBean.CONTROLLER_JSP, controllerPath = AppointmentResourceJspBean.CONTROLLER_PATH, right = AppointmentFormJspBean.RIGHT_MANAGEAPPOINTMENTFORM )
82 public class AppointmentResourceJspBean extends MVCAdminJspBean
83 {
84
85
86
87 public static final String CONTROLLER_PATH = "jsp/admin/plugins/appointment/modules/resource";
88
89
90
91
92 public static final String CONTROLLER_JSP = "ManageAppointmentResources.jsp";
93 private static final long serialVersionUID = 3684357156472596848L;
94
95
96 private static final String VIEW_USER_CALENDAR = "VIEW_USER_CALENDAR";
97 private static final String VIEW_RESOURCE_CALENDAR = "viewResourceCalendar";
98
99
100 private static final String TEMPLATE_RESOURCE_CALENDAR = "admin/plugins/appointment/modules/resource/resource_calendar.html";
101 private static final String TEMPLATE_VIEW_RESOURCE_CALENDAR = "admin/plugins/appointment/modules/resource/view_resource_calendar.html";
102 private static final String TEMPLATE_VIEW_USER_CALENDAR = "admin/plugins/appointment/modules/resource/view_user_calendar.html";
103 private static final String TEMPLATE_APPOINTMENT_DESCRIPTION = "admin/plugins/appointment/modules/resource/appointment_description.html";
104
105
106 private static final String CONTROLLER_JSP_URL = CONTROLLER_PATH + "/" + CONTROLLER_JSP;
107
108
109 private static final String MARK_RESOURCE = "resource";
110 private static final String MARK_LIST_DAYS = "listDays";
111 private static final String MARK_LIST_DAYS_OF_WEEK = "list_days_of_week";
112 private static final String MARK_STARTING_TIME = "startingTime";
113 private static final String MARK_ENDING_TIME = "endingTime";
114 private static final String MARK_STARTING_HOUR = "startingHour";
115 private static final String MARK_STARTING_MINUTE = "startingMinute";
116 private static final String MARK_ENDING_HOUR = "endingHour";
117 private static final String MARK_ENDING_MINUTE = "endingMinute";
118 private static final String MARK_DURATION = "duration";
119 private static final String MARK_CALENDAR = "calendar";
120 private static final String MARK_APPOINTMENT = "appointment";
121
122
123 private static final String PARAMETER_ID_RESOURCE = "id_resource";
124 private static final String PARAMETER_RESOURCE_TYPE = "resource_type";
125 private static final String PARAMETER_REFERER = "referer";
126 private static final String PARAMETER_FROM_URL = "fromUrl";
127 private static final String PARAMETER_OFFSET_WEEK = "offsetWeek";
128
129
130 private static final String MESSAGE_RESOURCE_CALENDAR_PAGE_TITLE = "module.appointment.resource.resource_calendar.pageTitle";
131 private static final String MESSAGE_USER_CALENDAR_PAGE_TITLE = "module.appointment.resource.user_calendar.pageTitle";
132
133
134 private static final int DEFAULT_APPOINTMENT_DURATION = 30;
135
136
137
138
139 private static final String [ ] MESSAGE_LIST_DAYS_OF_WEEK = {
140 "module.appointment.resource.manageCalendarSlots.labelMonday", "module.appointment.resource.manageCalendarSlots.labelTuesday",
141 "module.appointment.resource.manageCalendarSlots.labelWednesday", "module.appointment.resource.manageCalendarSlots.labelThursday",
142 "module.appointment.resource.manageCalendarSlots.labelFriday", "module.appointment.resource.manageCalendarSlots.labelSaturday",
143 "module.appointment.resource.manageCalendarSlots.labelSunday",
144 };
145
146
147
148
149
150
151
152
153 @View( value = VIEW_USER_CALENDAR, defaultView = true )
154 public String getViewUserCalendar( HttpServletRequest request )
155 {
156 String strFromUrl = request.getParameter( PARAMETER_FROM_URL );
157
158 if ( StringUtils.isEmpty( strFromUrl ) )
159 {
160 strFromUrl = request.getHeader( PARAMETER_REFERER );
161
162 if ( StringUtils.isEmpty( strFromUrl ) )
163 {
164 strFromUrl = AppointmentFormJspBean.getURLManageAppointmentForms( request );
165 }
166 }
167
168 String strOffsetWeek = request.getParameter( PARAMETER_OFFSET_WEEK );
169
170 int nOffsetWeek = 0;
171
172 if ( StringUtils.isNotEmpty( strOffsetWeek ) )
173 {
174 nOffsetWeek = parseInt( strOffsetWeek );
175 }
176
177 AdminUser adminUser = getUser( );
178
179 IResource resource = ResourceService.getInstance( ).getResource( Integer.toString( adminUser.getUserId( ) ), AdminUser.RESOURCE_TYPE );
180
181 Map<String, Object> model = getModel( );
182 model.put( MARK_CALENDAR, getWeekResourceCalendar( resource, nOffsetWeek, getLocale( ) ) );
183 model.put( MARK_RESOURCE, resource );
184 model.put( PARAMETER_OFFSET_WEEK, nOffsetWeek );
185 model.put( PARAMETER_FROM_URL, strFromUrl );
186
187 return getPage( MESSAGE_USER_CALENDAR_PAGE_TITLE, TEMPLATE_VIEW_USER_CALENDAR, model );
188 }
189
190
191
192
193
194
195
196
197 @View( VIEW_RESOURCE_CALENDAR )
198 public String getResourceCalendar( HttpServletRequest request )
199 {
200 String strIdResource = request.getParameter( PARAMETER_ID_RESOURCE );
201 String strResourceType = request.getParameter( PARAMETER_RESOURCE_TYPE );
202
203 if ( StringUtils.isEmpty( strIdResource ) || StringUtils.isEmpty( strResourceType ) )
204 {
205 return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
206 }
207
208 String strFromUrl = request.getParameter( PARAMETER_FROM_URL );
209
210 if ( StringUtils.isEmpty( strFromUrl ) )
211 {
212 strFromUrl = request.getHeader( PARAMETER_REFERER );
213
214 if ( StringUtils.isEmpty( strFromUrl ) )
215 {
216 strFromUrl = AppointmentFormJspBean.getURLManageAppointmentForms( request );
217 }
218 }
219
220 String strOffsetWeek = request.getParameter( PARAMETER_OFFSET_WEEK );
221
222 int nOffsetWeek = 0;
223
224 if ( StringUtils.isNotEmpty( strOffsetWeek ) )
225 {
226 nOffsetWeek = parseInt( strOffsetWeek );
227 }
228
229 IResourceProvider provider = ResourceService.getInstance( ).getResourceProvider( strResourceType );
230
231 if ( provider != null )
232 {
233 IResource resource = provider.getResource( strIdResource, strResourceType );
234
235 if ( resource != null )
236 {
237 Map<String, Object> model = getModel( );
238 model.put( MARK_CALENDAR, getWeekResourceCalendar( resource, nOffsetWeek, getLocale( ) ) );
239 model.put( MARK_RESOURCE, resource );
240 model.put( PARAMETER_OFFSET_WEEK, nOffsetWeek );
241 model.put( PARAMETER_FROM_URL, strFromUrl );
242
243 return getPage( MESSAGE_RESOURCE_CALENDAR_PAGE_TITLE, TEMPLATE_VIEW_RESOURCE_CALENDAR, model );
244 }
245 }
246
247 if ( StringUtils.isNotEmpty( strFromUrl ) )
248 {
249 return redirect( request, strFromUrl );
250 }
251
252 return redirect( request, AppointmentFormJspBean.getURLManageAppointmentForms( request ) );
253 }
254
255
256
257
258
259
260
261
262
263
264
265
266 public static String getWeekResourceCalendar( IResource resource, int nOffsetWeek, Locale locale )
267 {
268
269 Map<String, Object> model = new HashMap<>( );
270
271 model.put( MARK_RESOURCE, resource );
272
273
274 Date dateMonday = getDateMonday( nOffsetWeek );
275 Calendar calendar = new GregorianCalendar( );
276 calendar.setTime( dateMonday );
277
278 calendar.add( Calendar.DAY_OF_WEEK, 7 );
279
280 Date dateMax = new Date( calendar.getTimeInMillis( ) );
281
282
283 List<Integer> listIdAppointments = AppointmentResourceHome.findIdAppointmentsByResourceAndDate( resource.getIdResource( ), resource.getResourceType( ),
284 dateMonday, dateMax );
285
286 List<AppointmentDTO> listAppointment = new ArrayList<>( listIdAppointments.size( ) );
287
288 Map<Integer, List<CalendarAppointmentResourceDTO>> mapCalendarAppointmentResourceByDayOfWeek = new HashMap<>( );
289
290 int nStartingHour = 0;
291 int nStartingMinute = 0;
292 int nEndingHour = 0;
293 int nEndingMinute = 0;
294 int nMinGlobalStartingTime = 9999;
295 int nMaxGlobalEndingTime = 0;
296 int nMinDuration = -1;
297
298 for ( int i = 1; i < 8; i++ )
299 {
300 mapCalendarAppointmentResourceByDayOfWeek.put( i, new ArrayList<>( ) );
301 }
302
303
304
305
306 for ( int nIdAppointment : listIdAppointments )
307 {
308 AppointmentDTO appointment = AppointmentService.buildAppointmentDTOFromIdAppointment( nIdAppointment );
309 listAppointment.add( appointment );
310
311 CalendarAppointmentResourceDTO/calendar/CalendarAppointmentResourceDTO.html#CalendarAppointmentResourceDTO">CalendarAppointmentResourceDTO calendarAppointmentResource = new CalendarAppointmentResourceDTO( appointment.getIdAppointment( ),
312 appointment.getStartingDateTime( ).getHour( ), appointment.getStartingDateTime( ).getMinute( ), appointment.getEndingDateTime( ).getHour( ),
313 appointment.getEndingDateTime( ).getMinute( ), getAppointmentRecap( appointment, locale ), appointment.getIdForm( ) );
314 long startThen = appointment.getStartingDateTime( ).getHour( ) * 60 + appointment.getStartingDateTime( ).getMinute( );
315 long endThen = appointment.getEndingDateTime( ).getHour( ) * 60 + appointment.getEndingDateTime( ).getMinute( );
316
317 int nStartingTimeSlot = (int) startThen;
318
319 if ( nStartingTimeSlot < nMinGlobalStartingTime )
320 {
321 nMinGlobalStartingTime = nStartingTimeSlot;
322 nStartingHour = appointment.getStartingDateTime( ).getHour( );
323 nStartingMinute = appointment.getStartingDateTime( ).getMinute( );
324 }
325
326 int nEndingTimeSlot = (int) endThen;
327
328 if ( nEndingTimeSlot > nMaxGlobalEndingTime )
329 {
330 nMaxGlobalEndingTime = nEndingTimeSlot;
331 nEndingHour = appointment.getEndingDateTime( ).getHour( );
332 nEndingMinute = appointment.getEndingDateTime( ).getMinute( );
333 }
334
335 if ( ( calendarAppointmentResource.getDuration( ) < nMinDuration ) || ( nMinDuration == -1 ) )
336 {
337 nMinDuration = calendarAppointmentResource.getDuration( );
338 }
339
340 int nDayOfWeek = appointment.getStartingDateTime( ).getDayOfWeek( ).getValue( );
341 List<CalendarAppointmentResourceDTO> listCalendar = mapCalendarAppointmentResourceByDayOfWeek.get( nDayOfWeek );
342 listCalendar.add( calendarAppointmentResource );
343 }
344
345 List<CalendarDayDTO> listDays = new ArrayList<>( 7 );
346
347 for ( Entry<Integer, List<CalendarAppointmentResourceDTO>> entry : mapCalendarAppointmentResourceByDayOfWeek.entrySet( ) )
348 {
349 CalendarDayDTOns/appointment/modules/resource/business/calendar/CalendarDayDTO.html#CalendarDayDTO">CalendarDayDTO day = new CalendarDayDTO( );
350 Calendar calendarDay = new GregorianCalendar( );
351 calendarDay.setTime( dateMonday );
352 calendarDay.add( Calendar.DAY_OF_WEEK, entry.getKey( ) - 1 );
353 day.setDate( calendarDay.getTime( ) );
354
355 List<CalendarAppointmentResourceDTO> listCalendarApp = entry.getValue( );
356 Collections.sort( listCalendarApp );
357 day.setListAppointmentResourceDTO( listCalendarApp );
358 listDays.add( day );
359 }
360
361 Collections.sort( listDays );
362
363 List<AppointmentFormDTO> listForm = FormService.buildAllActiveAppointmentForm( );
364
365
366
367
368 for ( AppointmentFormDTO form : listForm )
369 {
370 FormResourceCalendar resourceTimeRange = getResourceCalendarTimeRange( form.getIdForm( ) );
371
372 int nOpeningTime = ( resourceTimeRange.getStartingTime( ).getHour( ) * 60 ) + resourceTimeRange.getStartingTime( ).getMinute( );
373
374 if ( nOpeningTime < nMinGlobalStartingTime )
375 {
376 nMinGlobalStartingTime = nOpeningTime;
377 nStartingHour = resourceTimeRange.getStartingTime( ).getHour( );
378 nStartingMinute = resourceTimeRange.getStartingTime( ).getMinute( );
379 }
380
381 int nClosingTime = ( resourceTimeRange.getEndingTime( ).getHour( ) * 60 ) + resourceTimeRange.getEndingTime( ).getMinute( );
382
383 if ( nClosingTime > nMaxGlobalEndingTime )
384 {
385 nMaxGlobalEndingTime = nClosingTime;
386 nEndingHour = resourceTimeRange.getEndingTime( ).getHour( );
387 nEndingMinute = resourceTimeRange.getEndingTime( ).getMinute( );
388 }
389
390
391 if ( ( resourceTimeRange.getDuration( ) < nMinDuration ) || ( nMinDuration < 0 ) )
392 {
393 nMinDuration = resourceTimeRange.getDuration( );
394 }
395 }
396
397 model.put( MARK_LIST_DAYS, listDays );
398 model.put( PARAMETER_OFFSET_WEEK, nOffsetWeek );
399 model.put( MARK_LIST_DAYS_OF_WEEK, MESSAGE_LIST_DAYS_OF_WEEK );
400 model.put( MARK_STARTING_TIME, nMinGlobalStartingTime );
401 model.put( MARK_ENDING_TIME, nMaxGlobalEndingTime );
402 model.put( MARK_DURATION, nMinDuration );
403 model.put( MARK_STARTING_HOUR, nStartingHour );
404 model.put( MARK_STARTING_MINUTE, nStartingMinute );
405 model.put( MARK_ENDING_HOUR, nEndingHour );
406 model.put( MARK_ENDING_MINUTE, nEndingMinute );
407
408 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_RESOURCE_CALENDAR, locale, model );
409
410 return template.getHtml( );
411 }
412
413
414
415
416
417
418
419
420 private static FormResourceCalendar getResourceCalendarTimeRange( int idForm )
421 {
422 List<ReservationRule> listReservationRules = ReservationRuleService.findListReservationRule( idForm );
423
424 LocalTime maxEndingTime = WeekDefinitionService.getMaxEndingTimeOfAListOfWeekDefinition( listReservationRules );
425 LocalTime minStartingTime = WeekDefinitionService.getMinStartingTimeOfAListOfWeekDefinition( listReservationRules );
426
427 int maxAppointmentDuration = DEFAULT_APPOINTMENT_DURATION;
428
429 Optional<ReservationRule> reservationRuleWithMaxDuration = listReservationRules.stream( )
430 .max( Comparator.comparingInt( ReservationRule::getDurationAppointments ) );
431
432 if ( reservationRuleWithMaxDuration.isPresent( ) )
433 {
434 maxAppointmentDuration = reservationRuleWithMaxDuration.get( ).getDurationAppointments( );
435 }
436 return new FormResourceCalendar( idForm, minStartingTime, maxEndingTime, maxAppointmentDuration );
437 }
438
439
440
441
442
443
444
445
446
447
448 private static String getAppointmentRecap( AppointmentDTO appointment, Locale locale )
449 {
450 Map<String, Object> model = new HashMap<>( );
451 model.put( MARK_APPOINTMENT, appointment );
452
453 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_APPOINTMENT_DESCRIPTION, locale, model );
454
455 return template.getHtml( );
456 }
457
458
459
460
461
462
463
464
465
466
467 public static String getUrlResourceCalendar( String strIdResource, String strResourceType )
468 {
469 UrlItem urlItem = new UrlItem( CONTROLLER_JSP_URL );
470 urlItem.addParameter( MVCUtils.PARAMETER_VIEW, VIEW_RESOURCE_CALENDAR );
471 urlItem.addParameter( PARAMETER_ID_RESOURCE, strIdResource );
472 urlItem.addParameter( PARAMETER_RESOURCE_TYPE, strResourceType );
473
474 return urlItem.getUrl( );
475 }
476
477
478
479
480
481
482
483
484 private static Date getDateMonday( int nOffsetWeek )
485 {
486 Date date = new Date( System.currentTimeMillis( ) );
487 Calendar calendar = Calendar.getInstance( Locale.FRANCE );
488 calendar.setTime( date );
489
490 calendar.add( Calendar.DAY_OF_MONTH, 7 * nOffsetWeek );
491
492
493 int nCurrentDayOfWeek = calendar.get( Calendar.DAY_OF_WEEK );
494
495 calendar.add( Calendar.DAY_OF_WEEK, Calendar.MONDAY - nCurrentDayOfWeek );
496
497 return new Date( calendar.getTimeInMillis( ) );
498 }
499
500
501
502
503
504
505
506
507 private int parseInt( String strNumber )
508 {
509 int nNumber = 0;
510
511 if ( StringUtils.isEmpty( strNumber ) )
512 {
513 return nNumber;
514 }
515
516 if ( strNumber.startsWith( "-" ) )
517 {
518 String strParseableNumber = strNumber.substring( 1 );
519
520 if ( StringUtils.isNumeric( strParseableNumber ) )
521 {
522 nNumber = Integer.parseInt( strParseableNumber ) * -1;
523 }
524 }
525 else
526 if ( StringUtils.isNumeric( strNumber ) )
527 {
528 nNumber = Integer.parseInt( strNumber );
529 }
530
531 return nNumber;
532 }
533 }