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 static java.lang.Math.toIntExact;
37
38 import java.time.DayOfWeek;
39 import java.time.LocalTime;
40 import java.time.temporal.ChronoUnit;
41 import java.util.ArrayList;
42 import java.util.HashSet;
43 import java.util.List;
44
45 import fr.paris.lutece.plugins.appointment.business.planning.TimeSlot;
46 import fr.paris.lutece.plugins.appointment.business.planning.TimeSlotHome;
47 import fr.paris.lutece.plugins.appointment.business.planning.WorkingDay;
48 import fr.paris.lutece.plugins.appointment.business.planning.WorkingDayHome;
49 import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFormDTO;
50
51
52
53
54
55
56
57 public final class WorkingDayService
58 {
59
60
61
62
63 private WorkingDayService( )
64 {
65 }
66
67
68
69
70
71
72
73
74
75
76 public static WorkingDay generateWorkingDay( int nIdReservationRule, DayOfWeek dayOfWeek )
77 {
78 WorkingDaytment/business/planning/WorkingDay.html#WorkingDay">WorkingDay workingDay = new WorkingDay( );
79 workingDay.setIdReservationRule( nIdReservationRule );
80 workingDay.setDayOfWeek( dayOfWeek.getValue( ) );
81 WorkingDayHome.create( workingDay );
82 return workingDay;
83 }
84
85
86
87
88
89
90
91
92 public static WorkingDay./../../fr/paris/lutece/plugins/appointment/business/planning/WorkingDay.html#WorkingDay">WorkingDay saveWorkingDay( WorkingDay workingDay )
93 {
94 return WorkingDayHome.create( workingDay );
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 public static void generateWorkingDayAndListTimeSlot( int nIdReservationRule, DayOfWeek dayOfWeek, LocalTime startingTime, LocalTime endingTime,
114 int nDuration, int nMaxCapacity )
115 {
116 WorkingDay workingDay = generateWorkingDay( nIdReservationRule, dayOfWeek );
117 TimeSlotService.createListTimeSlot(
118 TimeSlotService.generateListTimeSlot( workingDay.getIdWorkingDay( ), startingTime, endingTime, nDuration, nMaxCapacity, Boolean.FALSE ) );
119 }
120
121
122
123
124
125
126
127
128 public static List<DayOfWeek> getOpenDays( AppointmentFormDTO appointmentForm )
129 {
130 List<DayOfWeek> openDays = new ArrayList<>( );
131 if ( appointmentForm.getIsOpenMonday( ) )
132 {
133 openDays.add( DayOfWeek.MONDAY );
134 }
135 if ( appointmentForm.getIsOpenTuesday( ) )
136 {
137 openDays.add( DayOfWeek.TUESDAY );
138 }
139 if ( appointmentForm.getIsOpenWednesday( ) )
140 {
141 openDays.add( DayOfWeek.WEDNESDAY );
142 }
143 if ( appointmentForm.getIsOpenThursday( ) )
144 {
145 openDays.add( DayOfWeek.THURSDAY );
146 }
147 if ( appointmentForm.getIsOpenFriday( ) )
148 {
149 openDays.add( DayOfWeek.FRIDAY );
150 }
151 if ( appointmentForm.getIsOpenSaturday( ) )
152 {
153 openDays.add( DayOfWeek.SATURDAY );
154 }
155 if ( appointmentForm.getIsOpenSunday( ) )
156 {
157 openDays.add( DayOfWeek.SUNDAY );
158 }
159 return openDays;
160 }
161
162
163
164
165
166
167
168
169 public static List<WorkingDay> findListWorkingDayByWeekDefinitionRule( int nIdWeekDefinitionRule )
170 {
171 List<WorkingDay> listWorkingDay = WorkingDayHome.findByIdReservationRule( nIdWeekDefinitionRule );
172 for ( WorkingDay workingDay : listWorkingDay )
173 {
174 workingDay.setListTimeSlot( TimeSlotService.findListTimeSlotByWorkingDay( workingDay.getIdWorkingDay( ) ) );
175 }
176 return listWorkingDay;
177 }
178
179
180
181
182
183
184
185 public static void deleteListWorkingDay( List<WorkingDay> listWorkingDay )
186 {
187 for ( WorkingDay workingDay : listWorkingDay )
188 {
189 TimeSlotHome.deleteByIdWorkingDay( workingDay.getIdWorkingDay( ) );
190 WorkingDayHome.delete( workingDay.getIdWorkingDay( ) );
191 }
192 }
193
194
195
196
197
198
199
200
201 public static WorkingDay findWorkingDayLightById( int nIdWorkingDay )
202 {
203 return WorkingDayHome.findByPrimaryKey( nIdWorkingDay );
204 }
205
206
207
208
209
210
211
212
213 public static WorkingDay findWorkingDayById( int nIdWorkingDay )
214 {
215 WorkingDay workingDay = WorkingDayHome.findByPrimaryKey( nIdWorkingDay );
216 workingDay.setListTimeSlot( TimeSlotService.findListTimeSlotByWorkingDay( nIdWorkingDay ) );
217 return workingDay;
218 }
219
220
221
222
223
224
225
226
227 public static LocalTime getMaxEndingTimeOfAWorkingDay( WorkingDay workingDay )
228 {
229 return workingDay.getListTimeSlot( ).stream( ).map( TimeSlot::getEndingTime ).max( LocalTime::compareTo ).orElse( null );
230 }
231
232
233
234
235
236
237
238
239 public static LocalTime getMaxEndingTimeOfAListOfWorkingDay( List<WorkingDay> listWorkingDay )
240 {
241 LocalTime maxEndingTime = null;
242 for ( WorkingDay workingDay : listWorkingDay )
243 {
244 LocalTime endingTimeTemp = getMaxEndingTimeOfAWorkingDay( workingDay );
245
246 maxEndingTime = endingTimeTemp;
247
248 }
249 return maxEndingTime;
250 }
251
252
253
254
255
256
257
258
259 public static LocalTime getMinStartingTimeOfAWorkingDay( WorkingDay workingDay )
260 {
261 return workingDay.getListTimeSlot( ).stream( ).map( TimeSlot::getStartingTime ).min( LocalTime::compareTo ).orElse( null );
262 }
263
264
265
266
267
268
269
270
271 public static LocalTime getMinStartingTimeOfAListOfWorkingDay( List<WorkingDay> listWorkingDay )
272 {
273 LocalTime minStartingTime = null;
274 LocalTime startingTimeTemp;
275 for ( WorkingDay workingDay : listWorkingDay )
276 {
277 startingTimeTemp = getMinStartingTimeOfAWorkingDay( workingDay );
278 if ( minStartingTime == null || ( startingTimeTemp != null && startingTimeTemp.isBefore( minStartingTime ) ) )
279 {
280 minStartingTime = startingTimeTemp;
281 }
282 }
283 return minStartingTime;
284 }
285
286
287
288
289
290
291
292
293 public static int getMinDurationTimeSlotOfAWorkingDay( WorkingDay workingDay )
294 {
295 long lMinDuration = 0;
296 LocalTime startingTimeTemp;
297 LocalTime endingTimeTemp;
298 long lDurationTemp;
299 for ( TimeSlot timeSlot : workingDay.getListTimeSlot( ) )
300 {
301 startingTimeTemp = timeSlot.getStartingTime( );
302 endingTimeTemp = timeSlot.getEndingTime( );
303 lDurationTemp = startingTimeTemp.until( endingTimeTemp, ChronoUnit.MINUTES );
304 if ( lMinDuration == 0 || lMinDuration > lDurationTemp )
305 {
306 lMinDuration = lDurationTemp;
307 }
308 }
309 return toIntExact( lMinDuration );
310 }
311
312
313
314
315
316
317
318
319 public static int getMinDurationTimeSlotOfAListOfWorkingDay( List<WorkingDay> listWorkingDay )
320 {
321 long lMinDuration = 0;
322 long lDurationTemp;
323 for ( WorkingDay workingDay : listWorkingDay )
324 {
325 lDurationTemp = getMinDurationTimeSlotOfAWorkingDay( workingDay );
326 if ( lMinDuration == 0 || lMinDuration > lDurationTemp )
327 {
328 lMinDuration = lDurationTemp;
329 }
330 }
331 return toIntExact( lMinDuration );
332 }
333
334
335
336
337
338
339
340
341 public static HashSet<String> getSetDaysOfWeekOfAListOfWorkingDayForFullCalendar( List<WorkingDay> listWorkingDay )
342 {
343 HashSet<String> setDayOfWeek = new HashSet<>( );
344 for ( WorkingDay workingDay : listWorkingDay )
345 {
346 int dow = workingDay.getDayOfWeek( );
347 if ( dow == DayOfWeek.SUNDAY.getValue( ) )
348 {
349
350 dow = 0;
351 }
352 setDayOfWeek.add( Integer.toString( dow ) );
353 }
354 return setDayOfWeek;
355 }
356
357
358
359
360
361
362
363
364
365
366 public static WorkingDay getWorkingDayOfDayOfWeek( List<WorkingDay> listWorkingDay, DayOfWeek dayOfWeek )
367 {
368 return listWorkingDay.stream( ).filter( x -> x.getDayOfWeek( ) == dayOfWeek.getValue( ) ).findFirst( ).orElse( null );
369 }
370
371 }