View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de 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.service;
35  
36  import java.time.LocalDate;
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.List;
40  
41  import org.apache.commons.collections.CollectionUtils;
42  
43  import fr.paris.lutece.plugins.appointment.business.rule.ReservationRule;
44  import fr.paris.lutece.plugins.appointment.business.rule.ReservationRuleHome;
45  import fr.paris.lutece.plugins.appointment.service.listeners.FormListenerManager;
46  import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFormDTO;
47  import fr.paris.lutece.util.ReferenceList;
48  
49  /**
50   * Service class for the reservation rule
51   * 
52   * @author Laurent Payen
53   *
54   */
55  public final class ReservationRuleService
56  {
57  
58      /**
59       * Private constructor - this class does not need to be instantiated
60       */
61      private ReservationRuleService( )
62      {
63      }
64  
65      /**
66       * Create in database a reservation rule object from an appointmentForm DTO
67       * 
68       * @param appointmentForm
69       *            the appointmentForm DTO
70       * @param nIdForm
71       *            the form Id
72       * @param dateOfApply
73       *            the date of the reservation rule
74       * @return the Reservation Rule object created
75       */
76      public static ReservationRule createReservationRule( AppointmentFormDTO appointmentForm, int nIdForm, LocalDate dateOfApply )
77      {
78          ReservationRule reservationRule = new ReservationRule( );
79          fillInReservationRule( reservationRule, appointmentForm, nIdForm, dateOfApply );
80          ReservationRuleHome.create( reservationRule );
81          return reservationRule;
82      }
83  
84      /**
85       * Delete a reservation rule by its id
86       * 
87       * @param reservationRule
88       *            the reservation rule to delete
89       */
90      public static void removeReservationRule( ReservationRule reservationRule )
91      {
92          FormListenerManager.notifyListenersFormChange( reservationRule.getIdForm( ) );
93          ReservationRuleHome.delete( reservationRule.getIdReservationRule( ) );
94      }
95  
96      /**
97       * save a reservation rule
98       * 
99       * @param reservationRule
100      *            the reservation rule to save
101      */
102     public static void saveReservationRule( ReservationRule reservationRule )
103     {
104         ReservationRuleHome.create( reservationRule );
105     }
106 
107     /**
108      * Update in database a reservation rule with the values of an appointmentForm DTO
109      * 
110      * @param appointmentForm
111      *            the appointmentForm DTO
112      * @param nIdForm
113      *            the form Id
114      * @param dateOfApply
115      *            the date of the update
116      * @return the reservation rule object updated
117      */
118     public static ReservationRule updateReservationRule( AppointmentFormDTO appointmentForm, int nIdForm, LocalDate dateOfApply )
119     {
120         ReservationRule reservationRule = ReservationRuleService.findReservationRuleByIdFormAndDateOfApply( nIdForm, dateOfApply );
121         if ( reservationRule == null )
122         {
123             reservationRule = createReservationRule( appointmentForm, nIdForm, dateOfApply );
124         }
125         else
126         {
127             fillInReservationRule( reservationRule, appointmentForm, nIdForm, dateOfApply );
128             ReservationRuleHome.update( reservationRule );
129         }
130         return reservationRule;
131     }
132 
133     /**
134      * Fill the reservation rule object with the corresponding values of an appointmentForm DTO
135      * 
136      * @param reservationRule
137      *            the reservation rule object to fill in
138      * @param appointmentForm
139      *            the appointmentForm DTO
140      * @param nIdForm
141      *            the form Id
142      * @param dateOfApply
143      *            the date of the reservation rule
144      */
145     public static void fillInReservationRule( ReservationRule reservationRule, AppointmentFormDTO appointmentForm, int nIdForm, LocalDate dateOfApply )
146     {
147         reservationRule.setDateOfApply( dateOfApply );
148         reservationRule.setMaxCapacityPerSlot( appointmentForm.getMaxCapacityPerSlot( ) );
149         reservationRule.setMaxPeoplePerAppointment( appointmentForm.getMaxPeoplePerAppointment( ) );
150         reservationRule.setIdForm( nIdForm );
151     }
152 
153     /**
154      * Find in database a reservation rule of a form closest to a date
155      * 
156      * @param nIdForm
157      *            the form Id
158      * @param dateOfApply
159      *            the date
160      * @return the reservation rule to apply at this date
161      */
162     public static ReservationRule findReservationRuleByIdFormAndClosestToDateOfApply( int nIdForm, LocalDate dateOfApply )
163     {
164         // Get all the reservation rules
165         List<ReservationRule> listReservationRule = ReservationRuleHome.findByIdForm( nIdForm );
166         List<LocalDate> listDate = new ArrayList<>( );
167         for ( ReservationRule reservationRule : listReservationRule )
168         {
169             listDate.add( reservationRule.getDateOfApply( ) );
170         }
171         // Try to get the closest date in past of the dateOfApply
172         LocalDate closestDate = Utilities.getClosestDateInPast( listDate, dateOfApply );
173         ReservationRule reservationRule = null;
174         // If there is no closest date in past
175         if ( closestDate == null )
176         {
177             // if the list of reservation rules is not empty
178             if ( CollectionUtils.isNotEmpty( listReservationRule ) )
179             {
180                 // Get the next week definition in future
181                 reservationRule = listReservationRule.stream( ).min( ( w1, w2 ) -> w1.getDateOfApply( ).compareTo( w2.getDateOfApply( ) ) ).get( );
182             }
183         }
184         else
185         {
186             // The closest date in past is not null
187             if ( CollectionUtils.isNotEmpty( listReservationRule ) )
188             {
189                 // Get the corresponding reservation rule
190                 reservationRule = listReservationRule.stream( ).filter( x -> closestDate.isEqual( x.getDateOfApply( ) ) ).findAny( ).orElse( null );
191             }
192         }
193         return reservationRule;
194     }
195 
196     /**
197      * Find the reservation rule of a form on a specific date
198      * 
199      * @param nIdForm
200      *            the form Id
201      * @param dateOfApply
202      *            the date of the reservation rule
203      * @return the reservation rule object
204      */
205     public static ReservationRule findReservationRuleByIdFormAndDateOfApply( int nIdForm, LocalDate dateOfApply )
206     {
207         ReservationRule reservationRule = ReservationRuleHome.findByIdFormAndDateOfApply( nIdForm, dateOfApply );
208         return reservationRule;
209     }
210 
211     /**
212      * Find a reservation rule with its primary key
213      * 
214      * @param nIdReservationRule
215      *            the reservation rule Id
216      * @return the Reservation Rule Object
217      */
218     public static ReservationRule findReservationRuleById( int nIdReservationRule )
219     {
220         ReservationRule reservationRule = ReservationRuleHome.findByPrimaryKey( nIdReservationRule );
221         return reservationRule;
222     }
223 
224     /**
225      * Build a reference list of all the dates of all the reservation rules of a form
226      * 
227      * @param nIdForm
228      *            the form Id
229      * @return the reference list (id reservation rule / date of apply of the reservation rule)
230      */
231     public static ReferenceList findAllDateOfReservationRule( int nIdForm )
232     {
233         ReferenceList listDate = new ReferenceList( );
234         List<ReservationRule> listReservationRule = ReservationRuleHome.findByIdForm( nIdForm );
235         for ( ReservationRule reservationRule : listReservationRule )
236         {
237             listDate.addItem( reservationRule.getIdReservationRule( ), reservationRule.getDateOfApply( ).format( Utilities.getFormatter( ) ) );
238         }
239         return listDate;
240     }
241 
242     /**
243      * Find all the reservation rule of a form
244      * 
245      * @param nIdForm
246      *            the form Id
247      * @return an HashMap with the date of apply in key and the reservation rule in value
248      */
249     public static HashMap<LocalDate, ReservationRule> findAllReservationRule( int nIdForm )
250     {
251         HashMap<LocalDate, ReservationRule> mapReservationRule = new HashMap<>( );
252         List<ReservationRule> listReservationRule = ReservationRuleHome.findByIdForm( nIdForm );
253         for ( ReservationRule reservationRule : listReservationRule )
254         {
255             mapReservationRule.put( reservationRule.getDateOfApply( ), reservationRule );
256         }
257         return mapReservationRule;
258     }
259 
260     /**
261      * Returns a list of the reservation rules of a form
262      * 
263      * @param nIdForm
264      *            the form id
265      * @return a list of reservation rules of the form
266      */
267     public static List<ReservationRule> findListReservationRule( int nIdForm )
268     {
269         return ReservationRuleHome.findByIdForm( nIdForm );
270     }
271 
272 }