View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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                  // Need to get all the informations to create the slot
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                 // Need to get all the informations to create the slot
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 }