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.desk.service;
35
36 import java.time.LocalDateTime;
37 import java.time.LocalTime;
38 import java.util.List;
39 import java.util.concurrent.locks.Lock;
40
41 import org.apache.commons.lang.StringUtils;
42
43 import fr.paris.lutece.plugins.appointment.business.planning.ClosingDay;
44 import fr.paris.lutece.plugins.appointment.business.slot.Slot;
45 import fr.paris.lutece.plugins.appointment.modules.desk.util.AppointmentDeskPlugin;
46 import fr.paris.lutece.plugins.appointment.modules.desk.util.IncrementSlot;
47 import fr.paris.lutece.plugins.appointment.modules.desk.util.IncrementingType;
48 import fr.paris.lutece.plugins.appointment.service.ClosingDayService;
49 import fr.paris.lutece.plugins.appointment.service.SlotSafeService;
50 import fr.paris.lutece.plugins.appointment.service.SlotService;
51 import fr.paris.lutece.portal.service.util.AppLogService;
52 import fr.paris.lutece.util.sql.TransactionManager;
53
54 public class AppointmentDeskService
55 {
56
57 private AppointmentDeskService( )
58 {
59
60 }
61
62 public static void closeAppointmentDesk( List<Slot> listSlot )
63 {
64
65 for ( Slot slot : listSlot )
66 {
67
68 if ( slot.getIdSlot( ) == 0 )
69 {
70
71
72 SlotService.addDateAndTimeToSlot( slot );
73 slot.setNbRemainingPlaces( slot.getMaxCapacity( ) );
74 slot.setNbPotentialRemainingPlaces( slot.getMaxCapacity( ) );
75 slot = SlotSafeService.saveSlot( slot );
76
77 }
78 Lock lock = SlotSafeService.getLockOnSlot( slot.getIdSlot( ) );
79 lock.lock( );
80 try
81 {
82 Slot oldSlot = SlotService.findSlotById( slot.getIdSlot( ) );
83
84 if ( oldSlot.getMaxCapacity( ) > 0 )
85 {
86
87 slot.setMaxCapacity( oldSlot.getMaxCapacity( ) - 1 );
88 slot.setNbPotentialRemainingPlaces( oldSlot.getNbPotentialRemainingPlaces( ) - 1 );
89 slot.setNbRemainingPlaces( oldSlot.getNbRemainingPlaces( ) - 1 );
90 slot.setNbPlacestaken( oldSlot.getNbPlacesTaken( ) );
91 slot.setIsSpecific( SlotService.isSpecificSlot( slot ) );
92
93 TransactionManager.beginTransaction( AppointmentDeskPlugin.getPlugin( ) );
94
95 SlotSafeService.saveSlot( slot );
96 TransactionManager.commitTransaction( AppointmentDeskPlugin.getPlugin( ) );
97 }
98
99 }
100 catch( Exception e )
101 {
102 TransactionManager.rollBack( AppointmentDeskPlugin.getPlugin( ) );
103 AppLogService.error( "Error close appointment desk" + e.getMessage( ), e );
104
105 }
106 finally
107 {
108
109 lock.unlock( );
110 }
111
112 }
113
114 }
115
116 public static void openAppointmentDesk( List<Slot> listSlot, int nMaxCapacity )
117 {
118
119 ClosingDay closingDay = null;
120 if ( !listSlot.isEmpty( ) )
121 {
122
123 closingDay = ClosingDayService.findClosingDayByIdFormAndDateOfClosingDay( listSlot.get( 0 ).getIdForm( ),
124 listSlot.get( 0 ).getStartingDateTime( ).toLocalDate( ) );
125 }
126
127 for ( Slot slot : listSlot )
128 {
129
130 if ( slot.getIdSlot( ) == 0 )
131 {
132
133
134 SlotService.addDateAndTimeToSlot( slot );
135 slot.setNbRemainingPlaces( slot.getMaxCapacity( ) );
136 slot.setNbPotentialRemainingPlaces( slot.getMaxCapacity( ) );
137 slot = SlotSafeService.saveSlot( slot );
138
139 }
140 if ( closingDay == null )
141 {
142
143 Lock lock = SlotSafeService.getLockOnSlot( slot.getIdSlot( ) );
144 lock.lock( );
145 try
146 {
147
148 Slot oldSlot = SlotService.findSlotById( slot.getIdSlot( ) );
149 if ( oldSlot.getMaxCapacity( ) < nMaxCapacity )
150 {
151
152 slot.setMaxCapacity( oldSlot.getMaxCapacity( ) + 1 );
153 slot.setNbPotentialRemainingPlaces( oldSlot.getNbPotentialRemainingPlaces( ) + 1 );
154 slot.setNbRemainingPlaces( oldSlot.getNbRemainingPlaces( ) + 1 );
155 slot.setNbPlacestaken( oldSlot.getNbPlacesTaken( ) );
156 slot.setIsOpen( true );
157 slot.setIsSpecific( SlotService.isSpecificSlot( slot ) );
158
159 TransactionManager.beginTransaction( AppointmentDeskPlugin.getPlugin( ) );
160
161 SlotSafeService.saveSlot( slot );
162 TransactionManager.commitTransaction( AppointmentDeskPlugin.getPlugin( ) );
163 }
164 }
165 catch( Exception e )
166 {
167 TransactionManager.rollBack( AppointmentDeskPlugin.getPlugin( ) );
168 AppLogService.error( "Error open appointment desk" + e.getMessage( ), e );
169
170 }
171 finally
172 {
173
174 lock.unlock( );
175 }
176 }
177 else
178 {
179
180 break;
181 }
182
183 }
184
185 }
186
187 public static void incrementMaxCapacity( IncrementSlot incrementSlot )
188 {
189
190 LocalDateTime startingDateTimes;
191 LocalDateTime endingDateTimes;
192 boolean lace = false;
193
194 if ( StringUtils.isNotEmpty( incrementSlot.getStartingTime( ) )
195 && IncrementingType.HALFTIMEMORNING.getValue( ) != incrementSlot.getType( ).getValue( ) )
196 {
197 startingDateTimes = incrementSlot.getStartingDate( ).atTime( LocalTime.parse( incrementSlot.getStartingTime( ) ) );
198 }
199 else
200 {
201 startingDateTimes = incrementSlot.getStartingDate( ).atStartOfDay( );
202 }
203
204 if ( StringUtils.isNotEmpty( incrementSlot.getEndingTime( ) )
205 && IncrementingType.HALFTIMEAFTERNOON.getValue( ) != incrementSlot.getType( ).getValue( ) )
206 {
207 endingDateTimes = incrementSlot.getEndingDate( ).atTime( LocalTime.parse( incrementSlot.getEndingTime( ) ) );
208 }
209 else
210 {
211 endingDateTimes = incrementSlot.getEndingDate( ).atTime( LocalTime.MAX );
212 }
213
214 if ( incrementSlot.getType( ).getValue( ) == IncrementingType.LACE.getValue( ) )
215 {
216 lace = true;
217 }
218
219 SlotSafeService.incrementMaxCapacity( incrementSlot.getIdForm( ), incrementSlot.getIncrementingValue( ), startingDateTimes, endingDateTimes, lace );
220 }
221
222 }