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.service;
35
36 import java.time.LocalDateTime;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.StringJoiner;
40 import java.util.stream.Collectors;
41
42 import javax.servlet.http.HttpServletRequest;
43
44 import org.apache.commons.collections.CollectionUtils;
45 import org.apache.commons.lang3.StringUtils;
46
47 import fr.paris.lutece.plugins.appointment.business.appointment.Appointment;
48 import fr.paris.lutece.plugins.appointment.business.appointment.AppointmentHome;
49 import fr.paris.lutece.plugins.appointment.business.appointment.AppointmentSlot;
50 import fr.paris.lutece.plugins.appointment.business.form.Form;
51 import fr.paris.lutece.plugins.appointment.business.slot.Slot;
52 import fr.paris.lutece.plugins.appointment.business.user.User;
53 import fr.paris.lutece.plugins.appointment.business.user.UserHome;
54 import fr.paris.lutece.plugins.appointment.service.listeners.AppointmentListenerManager;
55 import fr.paris.lutece.plugins.appointment.service.listeners.SlotListenerManager;
56 import fr.paris.lutece.plugins.appointment.web.dto.AppointmentDTO;
57 import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFilterDTO;
58 import fr.paris.lutece.plugins.genericattributes.business.Response;
59 import fr.paris.lutece.plugins.genericattributes.business.ResponseHome;
60 import fr.paris.lutece.portal.business.user.AdminUser;
61 import fr.paris.lutece.portal.business.user.AdminUserHome;
62 import fr.paris.lutece.portal.service.util.AppException;
63 import fr.paris.lutece.portal.service.util.AppLogService;
64 import fr.paris.lutece.portal.service.util.AppPropertiesService;
65 import fr.paris.lutece.portal.service.util.CryptoService;
66 import fr.paris.lutece.portal.service.workflow.WorkflowService;
67 import fr.paris.lutece.util.sql.TransactionManager;
68
69
70
71
72
73
74
75 public final class AppointmentService
76 {
77
78 private static final String PROPERTY_REF_ENCRYPTION_ALGORITHM = "appointment.refEncryptionAlgorithm";
79 private static final String CONSTANT_SHA256 = "SHA-256";
80 private static final String PROPERTY_REF_SIZE_RANDOM_PART = "appointment.refSizeRandomPart";
81 private static final String CONSTANT_SEPARATOR = "$";
82
83
84
85 private static final int CONSTANT_REF_SIZE_RANDOM_PART = 5;
86
87
88
89
90 private AppointmentService( )
91 {
92 }
93
94
95
96
97
98
99
100
101 public static List<Appointment> findListAppointmentByListSlot( List<Slot> listSlot )
102 {
103 List<Integer> listIdSlot = listSlot.stream( ).map( Slot::getIdSlot ).collect( Collectors.toList( ) );
104
105 return AppointmentHome.findByListIdSlot( listIdSlot );
106 }
107
108
109
110
111
112
113
114
115 public static List<Appointment> findListAppointmentBySlot( int nIdSlot )
116 {
117 return AppointmentHome.findByIdSlot( nIdSlot );
118 }
119
120
121
122
123
124
125
126
127 public static List<Appointment> findListAppointmentByUserId( int nIdUser )
128 {
129 return AppointmentHome.findByIdUser( nIdUser );
130
131 }
132
133
134
135
136
137
138
139
140 public static List<Appointment> findListAppointmentByUserGuid( String strGuidUser )
141 {
142 List<Appointment> listAppointment = AppointmentHome.findByGuidUser( strGuidUser );
143 for ( Appointment appointment : listAppointment )
144 {
145 List<Slot> listSlot = SlotService.findListSlotByIdAppointment( appointment.getIdAppointment( ) );
146 appointment.setSlot( listSlot );
147
148 }
149 return listAppointment;
150
151 }
152
153
154
155
156
157
158
159
160
161 public static List<Appointment> findListAppointmentByIdForm( int nIdForm )
162 {
163 return AppointmentHome.findByIdForm( nIdForm );
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177 static Appointment buildAndCreateAppointment( AppointmentDTO appointmentDTO, User user )
178 {
179 Appointmentent/business/appointment/Appointment.html#Appointment">Appointment appointment = new Appointment( );
180 if ( appointmentDTO.getIdAppointment( ) != 0 )
181 {
182 appointment = AppointmentService.findAppointmentById( appointmentDTO.getIdAppointment( ) );
183 }
184 if ( appointmentDTO.getIdAdminUser( ) != 0 )
185 {
186 appointment.setIdAdminUser( appointmentDTO.getIdAdminUser( ) );
187 }
188 if ( appointmentDTO.getAdminUserCreate( ) != null )
189 {
190 appointment.setAdminUserCreate( appointmentDTO.getAdminUserCreate( ) );
191 }
192 appointment.setListAppointmentSlot( appointmentDTO.getListAppointmentSlot( ) );
193 appointment.setNbPlaces( appointmentDTO.getNbBookedSeats( ) );
194 appointment.setIdUser( user.getIdUser( ) );
195 appointment.setIsSurbooked( appointmentDTO.getIsSurbooked( ) );
196
197 if ( appointment.getIdAppointment( ) == 0 )
198 {
199 appointment = AppointmentHome.create( appointment );
200 String strEmailLastNameFirstName = new StringJoiner( StringUtils.SPACE ).add( user.getEmail( ) ).add( CONSTANT_SEPARATOR )
201 .add( user.getLastName( ) ).add( CONSTANT_SEPARATOR ).add( user.getFirstName( ) ).toString( );
202 String strReference = appointment.getIdAppointment( ) + CryptoService
203 .encrypt( appointment.getIdAppointment( ) + strEmailLastNameFirstName,
204 AppPropertiesService.getProperty( PROPERTY_REF_ENCRYPTION_ALGORITHM, CONSTANT_SHA256 ) )
205 .substring( 0, AppPropertiesService.getPropertyInt( PROPERTY_REF_SIZE_RANDOM_PART, CONSTANT_REF_SIZE_RANDOM_PART ) );
206
207 Form form = FormService.findFormLightByPrimaryKey( appointmentDTO.getIdForm( ) );
208 if ( StringUtils.isNotEmpty( form.getReference( ) ) )
209 {
210 strReference = form.getReference( ) + strReference;
211 }
212 appointment.setReference( strReference );
213 appointment = AppointmentHome.update( appointment );
214 }
215 else
216 {
217 AppointmentHome.updateAppointmentDate( appointment );
218
219 }
220 return appointment;
221 }
222
223
224
225
226
227
228
229
230 public static Appointment findAppointmentById( int nIdAppointment )
231 {
232 return AppointmentHome.findByPrimaryKey( nIdAppointment );
233 }
234
235
236
237
238
239
240
241
242 public static Appointment findAppointmentByReference( String strReference )
243 {
244 return AppointmentHome.findByReference( strReference );
245 }
246
247
248
249
250
251
252
253
254 public static List<AppointmentDTO> findListAppointmentsDTOByFilter( AppointmentFilterDTO appointmentFilter )
255 {
256 List<AppointmentDTO> listAppointmentsDTO = new ArrayList<>( );
257 for ( Appointment appointment : AppointmentHome.findByFilter( appointmentFilter ) )
258 {
259 listAppointmentsDTO.add( buildAppointmentDTO( appointment ) );
260 }
261 return listAppointmentsDTO;
262 }
263
264
265
266
267
268
269
270
271
272
273 public static List<AppointmentDTO> findAppointmentByMailAndCategory( int nIdCategory, String mail )
274 {
275 List<AppointmentDTO> listAppointmentsDTO = new ArrayList<>( );
276 for ( Appointment appointment : AppointmentHome.findByMailAndCategory( nIdCategory, mail ) )
277 {
278 listAppointmentsDTO.add( buildAppointmentDTO( appointment ) );
279 }
280 return listAppointmentsDTO;
281 }
282
283
284
285
286
287
288
289 public static List<Appointment> findListAppointmentsByFilter( AppointmentFilterDTO appointmentFilter )
290 {
291 return AppointmentHome.findByFilter( appointmentFilter );
292 }
293
294
295
296
297
298
299
300
301 public static List<Integer> findListAppointmentsIdsByFilter( AppointmentFilterDTO appointmentFilter )
302 {
303 return AppointmentHome.findIdsByFilter( appointmentFilter );
304 }
305
306
307
308
309
310
311
312
313 private static AppointmentDTO buildAppointmentDTO( Appointment appointment )
314 {
315 AppointmentDTOb/dto/AppointmentDTO.html#AppointmentDTO">AppointmentDTO appointmentDTO = new AppointmentDTO( );
316 appointmentDTO.setIdForm( appointment.getSlot( ).get( 0 ).getIdForm( ) );
317 appointmentDTO.setIdUser( appointment.getIdUser( ) );
318 appointmentDTO.setListAppointmentSlot( appointment.getListAppointmentSlot( ) );
319 appointmentDTO.setIdAppointment( appointment.getIdAppointment( ) );
320 appointmentDTO.setFirstName( appointment.getUser( ).getFirstName( ) );
321 appointmentDTO.setLastName( appointment.getUser( ).getLastName( ) );
322 appointmentDTO.setEmail( appointment.getUser( ).getEmail( ) );
323 appointmentDTO.setPhoneNumber( appointment.getUser( ).getPhoneNumber( ) );
324 appointmentDTO.setGuid( appointment.getUser( ).getGuid( ) );
325 appointmentDTO.setReference( appointment.getReference( ) );
326 LocalDateTime startingDateTime = AppointmentUtilities.getStartingDateTime( appointment );
327 LocalDateTime endingDateTime = AppointmentUtilities.getEndingDateTime( appointment );
328 appointmentDTO.setStartingDateTime( startingDateTime );
329 appointmentDTO.setEndingDateTime( endingDateTime );
330 appointmentDTO.setDateOfTheAppointment( startingDateTime.toLocalDate( ).format( Utilities.getFormatter( ) ) );
331 appointmentDTO.setStartingTime( startingDateTime.toLocalTime( ) );
332 appointmentDTO.setEndingTime( endingDateTime.toLocalTime( ) );
333 appointmentDTO.setIsCancelled( appointment.getIsCancelled( ) );
334 appointmentDTO.setNbBookedSeats( appointment.getNbPlaces( ) );
335 for ( Slot slt : appointment.getSlot( ) )
336 {
337
338 SlotService.addDateAndTimeToSlot( slt );
339 }
340 appointmentDTO.setSlot( appointment.getSlot( ) );
341 appointmentDTO.setUser( appointment.getUser( ) );
342 if ( appointment.getIdAdminUser( ) != 0 )
343 {
344 AdminUser adminUser = AdminUserHome.findByPrimaryKey( appointment.getIdAdminUser( ) );
345 if ( adminUser != null )
346 {
347 appointmentDTO.setIdAdminUser( adminUser.getUserId());
348 appointmentDTO.setAdminUser(
349 new StringBuilder( adminUser.getFirstName( ) + org.apache.commons.lang3.StringUtils.SPACE + adminUser.getLastName( ) ).toString( ) );
350 }
351 }
352 else
353 {
354 appointmentDTO.setAdminUser( StringUtils.EMPTY );
355 }
356 appointmentDTO.setAdminUserCreate( appointment.getAdminUserCreate( ) );
357 appointmentDTO.setDateAppointmentTaken( appointment.getDateAppointmentTaken( ) );
358 return appointmentDTO;
359 }
360
361
362
363
364
365
366
367 public static void addAppointmentResponses( AppointmentDTO appointmentDto )
368 {
369
370 appointmentDto.setListResponse( AppointmentResponseService.findListResponse( appointmentDto.getIdAppointment( ) ) );
371 }
372
373
374
375
376
377
378
379
380
381 public static void deleteAppointment( int nIdAppointment )
382 {
383 TransactionManager.beginTransaction( AppointmentPlugin.getPlugin( ) );
384 try
385 {
386 Appointment appointmentToDelete = AppointmentHome.findByPrimaryKey( nIdAppointment );
387 deleteWorkflowResource( nIdAppointment );
388 if ( !appointmentToDelete.getIsCancelled( ) )
389 {
390 for ( AppointmentSlot appSlot : appointmentToDelete.getListAppointmentSlot( ) )
391 {
392
393 SlotSafeService.updateRemaningPlacesWithAppointmentMovedDeletedOrCanceled( appSlot.getNbPlaces( ), appSlot.getIdSlot( ) );
394 }
395
396 }
397
398 AppointmentResponseService.removeResponsesByIdAppointment( nIdAppointment );
399 AppointmentService.deleteAppointment( appointmentToDelete );
400 UserHome.delete( appointmentToDelete.getIdUser( ) );
401 TransactionManager.commitTransaction( AppointmentPlugin.getPlugin( ) );
402 AppointmentListenerManager.notifyListenersAppointmentRemoval( nIdAppointment );
403 for ( AppointmentSlot appSlot : appointmentToDelete.getListAppointmentSlot( ) )
404 {
405 SlotListenerManager.notifyListenersSlotChange( appSlot.getIdSlot( ) );
406 }
407 }
408 catch( Exception e )
409 {
410 TransactionManager.rollBack( AppointmentPlugin.getPlugin( ) );
411 AppLogService.error( "Error delete appointment " + e.getMessage( ), e );
412 throw new AppException( e.getMessage( ), e );
413 }
414 }
415
416 private static void deleteWorkflowResource( int nIdAppointment )
417 {
418 if ( WorkflowService.getInstance( ).isAvailable( ) )
419 {
420 try
421 {
422 WorkflowService.getInstance( ).doRemoveWorkFlowResource( nIdAppointment, Appointment.APPOINTMENT_RESOURCE_TYPE );
423 }
424 catch( Exception e )
425 {
426 AppLogService.error( "Error Workflow", e );
427 }
428 }
429 }
430
431
432
433
434
435
436
437 private static void deleteAppointment( Appointment appointment )
438 {
439 AppointmentHome.delete( appointment.getIdAppointment( ) );
440 }
441
442
443
444
445
446
447
448
449 public static AppointmentDTO buildAppointmentDTOFromIdAppointment( int nIdAppointment )
450 {
451 Appointment appointment = AppointmentService.findAppointmentById( nIdAppointment );
452 User user = UserService.findUserById( appointment.getIdUser( ) );
453 List<Slot> listSlot = SlotService.findListSlotByIdAppointment( appointment.getIdAppointment( ) );
454 appointment.setSlot( listSlot );
455 appointment.setUser( user );
456 return buildAppointmentDTO( appointment );
457 }
458
459
460
461
462
463
464
465
466 public static AppointmentDTO buildAppointmentDTOFromRefAppointment( String refAppointment )
467 {
468 Appointment appointment = AppointmentService.findAppointmentByReference( refAppointment );
469
470 if ( appointment == null )
471 {
472 return null;
473 }
474 User user = UserService.findUserById( appointment.getIdUser( ) );
475 List<Slot> listSlot = SlotService.findListSlotByIdAppointment( appointment.getIdAppointment( ) );
476 appointment.setSlot( listSlot );
477 appointment.setUser( user );
478
479 return buildAppointmentDTO( appointment );
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493
494 public static void updateAppointmentDTO( int nIdappointment, User user, List<Response> listResponse, boolean deleteBoOnly )
495 {
496 UserHome.update( user );
497 AppointmentResponseService.removeResponsesByIdAppointmentAndBoOnly( nIdappointment, deleteBoOnly );
498 if ( CollectionUtils.isNotEmpty( listResponse ) )
499 {
500 for ( Response response : listResponse )
501 {
502 ResponseHome.create( response );
503 AppointmentResponseService.insertAppointmentResponse( nIdappointment, response.getIdResponse( ) );
504 }
505 }
506 AppointmentListenerManager.notifyListenersAppointmentUpdated( nIdappointment );
507 }
508
509
510
511
512
513
514
515 public static void updateAppointment( Appointment appointment )
516 {
517 boolean statusUpdated = false;
518
519 Appointment oldAppointment = AppointmentService.findAppointmentById( appointment.getIdAppointment( ) );
520
521 TransactionManager.beginTransaction( AppointmentPlugin.getPlugin( ) );
522 try
523 {
524 if ( !oldAppointment.getIsCancelled( ) && appointment.getIsCancelled( ) )
525 {
526 for ( AppointmentSlot appSlot : oldAppointment.getListAppointmentSlot( ) )
527 {
528
529 SlotSafeService.updateRemaningPlacesWithAppointmentMovedDeletedOrCanceled( appSlot.getNbPlaces( ), appSlot.getIdSlot( ) );
530 }
531 statusUpdated = true;
532 }
533 else
534 if ( oldAppointment.getIsCancelled( ) && !appointment.getIsCancelled( ) )
535 {
536 for ( AppointmentSlot appSlot : oldAppointment.getListAppointmentSlot( ) )
537 {
538
539 SlotSafeService.updateRemaningPlacesWithAppointmentReactivated( appSlot.getNbPlaces( ), appSlot.getIdSlot( ) );
540 }
541 statusUpdated = true;
542 }
543 AppointmentHome.update( appointment );
544 TransactionManager.commitTransaction( AppointmentPlugin.getPlugin( ) );
545 AppointmentListenerManager.notifyListenersAppointmentUpdated( appointment.getIdAppointment( ) );
546 if ( statusUpdated )
547 {
548 for ( AppointmentSlot appSlot : oldAppointment.getListAppointmentSlot( ) )
549 {
550 SlotListenerManager.notifyListenersSlotChange( appSlot.getIdSlot( ) );
551 }
552 }
553 }
554 catch( Exception e )
555 {
556 TransactionManager.rollBack( AppointmentPlugin.getPlugin( ) );
557 AppLogService.error( "Error update appointment " + e.getMessage( ), e );
558 throw new AppException( e.getMessage( ), e );
559 }
560
561 }
562
563
564
565
566
567
568
569
570
571 public static int saveAppointment( AppointmentDTO appointmentDTO )
572 {
573 return SlotSafeService.saveAppointment( appointmentDTO, null );
574
575 }
576
577
578
579
580
581
582
583
584
585 public static int saveAppointment( AppointmentDTO appointmentDTO, HttpServletRequest request )
586 {
587 return SlotSafeService.saveAppointment( appointmentDTO, request );
588
589 }
590
591 public static void buildListAppointmentSlot( AppointmentDTO appointmentDTO )
592 {
593
594 List<AppointmentSlot> listApptSlot = new ArrayList<>( );
595 int nIdAppointment = appointmentDTO.getIdAppointment( );
596 int nNumberPlace = appointmentDTO.getNbBookedSeats( );
597 List<Slot> listSlot = appointmentDTO.getSlot( );
598
599 if ( listSlot.size( ) > 1 )
600 {
601
602 nNumberPlace = 1;
603 listSlot.sort( ( slot1, slot2 ) -> slot1.getStartingDateTime( ).compareTo( slot2.getStartingDateTime( ) ) );
604 }
605 for ( Slot slot : listSlot )
606 {
607
608 AppointmentSlotnt/business/appointment/AppointmentSlot.html#AppointmentSlot">AppointmentSlot apptSlot = new AppointmentSlot( );
609 apptSlot.setIdAppointment( nIdAppointment );
610 apptSlot.setIdSlot( slot.getIdSlot( ) );
611
612 apptSlot.setNbPlaces( nNumberPlace );
613 listApptSlot.add( apptSlot );
614 }
615 appointmentDTO.setListAppointmentSlot( listApptSlot );
616 }
617
618 }