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.management.business.search;
35  
36  import java.time.Instant;
37  import java.time.LocalDateTime;
38  import java.time.LocalTime;
39  import java.util.TimeZone;
40  
41  import org.apache.commons.lang3.StringUtils;
42  import org.apache.lucene.document.Document;
43  
44  import fr.paris.lutece.plugins.appointment.service.Utilities;
45  import fr.paris.lutece.portal.service.search.SearchItem;
46  import fr.paris.lutece.portal.service.util.AppLogService;
47  
48  /**
49   * This class is use for processing searches in Appointment
50   */
51  public class AppointmentSearchItem extends SearchItem
52  {
53  
54      public static final String FIELD_DATE_SUFFIX = "_date";
55      public static final String FIELD_INT_SUFFIX = "_int";
56  
57      public static final String FIELD_ID_APPOINTMENT = "id_appointment";
58      public static final String FIELD_ID_FORM = "id_form";
59      public static final String FIELD_FIRST_NAME = "first_name";
60      public static final String FIELD_FIRST_NAME_SEARCH = "first_name_search";
61      public static final String FIELD_LAST_NAME = "last_name";
62      public static final String FIELD_LAST_NAME_SEARCH = "last_name_search";
63      public static final String FIELD_MAIL = "mail";
64      public static final String FIELD_MAIL_SEARCH = "mail_search";
65      public static final String FIELD_PHONE_NUMBER = "phone_number";
66      public static final String FIELD_START_DATE = "start_date";
67      public static final String FIELD_END_DATE = "end_date";
68      public static final String FIELD_ADMIN = "admin";
69      public static final String FIELD_CANCELLED = "cancelled";
70      public static final String FIELD_ID_WORKFLOW_STATE = "id_workflow_state";
71      public static final String FIELD_NB_SEATS = "nb_seats_int";
72      public static final String FIELD_DATE_APPOINTMENT_TAKEN = "appointment_taken_date";
73      public static final String FIELD_ID_CATEGORY = "id_category";
74  
75      private static final int INTEGER_MINUS_ONE = -1;
76  
77      private int _idAppointment;
78      private int _idForm;
79      private String _firstName;
80      private String _lastName;
81      private String _mail;
82      private String _phoneNumber;
83      private LocalDateTime _startDate;
84      private LocalDateTime _endDate;
85      private String _admin;
86      private boolean _cancelled;
87      private int _idState;
88      private int _nbSeats;
89      private LocalDateTime _dateAppointmentTaken;
90  
91      private String _dateOfTheAppointment;
92      private LocalTime _startingTime;
93      private LocalTime _endingTime;
94  
95      private int _idCategory;
96  
97      private String _stateTitle = "";
98      private String _formTitle = "";
99      private String _categoryTitle = "";
100 
101     public AppointmentSearchItem( Document document )
102     {
103         super( document );
104 
105         _idAppointment = manageIntegerNullValue( document.get( AppointmentSearchItem.FIELD_ID_APPOINTMENT ) );
106         _idForm = manageIntegerNullValue( document.get( AppointmentSearchItem.FIELD_ID_FORM ) );
107         _firstName = document.get( AppointmentSearchItem.FIELD_FIRST_NAME );
108         _lastName = document.get( AppointmentSearchItem.FIELD_LAST_NAME );
109         _mail = document.get( AppointmentSearchItem.FIELD_MAIL );
110         _startDate = parseDate( document.get( FIELD_START_DATE ) );
111         _phoneNumber = document.get( AppointmentSearchItem.FIELD_PHONE_NUMBER ) != null ? document.get( AppointmentSearchItem.FIELD_PHONE_NUMBER ) : "";
112         _endDate = parseDate( document.get( FIELD_END_DATE ) );
113         _admin = document.get( AppointmentSearchItem.FIELD_ADMIN );
114         _cancelled = Boolean.valueOf( document.get( AppointmentSearchItem.FIELD_CANCELLED ) );
115         _idState = manageIntegerNullValue( document.get( AppointmentSearchItem.FIELD_ID_WORKFLOW_STATE ) );
116         _nbSeats = manageIntegerNullValue( document.get( AppointmentSearchItem.FIELD_NB_SEATS ) );
117         _dateAppointmentTaken = parseDate( document.get( FIELD_DATE_APPOINTMENT_TAKEN ) );
118 
119         _dateOfTheAppointment = _startDate.toLocalDate( ).format( Utilities.getFormatter( ) );
120         _startingTime = _startDate.toLocalTime( );
121         _endingTime = _endDate.toLocalTime( );
122         _idCategory = manageIntegerNullValue( document.get( AppointmentSearchItem.FIELD_ID_CATEGORY ) );
123     }
124 
125     /**
126      * @return the idAppointment
127      */
128     public int getIdAppointment( )
129     {
130         return _idAppointment;
131     }
132 
133     /**
134      * @return the idForm
135      */
136     public int getIdForm( )
137     {
138         return _idForm;
139     }
140 
141     /**
142      * @return the firstName
143      */
144     public String getFirstName( )
145     {
146         return _firstName;
147     }
148 
149     /**
150      * @return the lastName
151      */
152     public String getLastName( )
153     {
154         return _lastName;
155     }
156 
157     /**
158      * @return the mail
159      */
160     public String getMail( )
161     {
162         return _mail;
163     }
164 
165     /**
166      * @return the phone number
167      */
168     public String getPhoneNumber( )
169     {
170         return _phoneNumber;
171     }
172 
173     /**
174      * @return the startDate
175      */
176     public LocalDateTime getStartDate( )
177     {
178         return _startDate;
179     }
180 
181     /**
182      * @return the endDate
183      */
184     public LocalDateTime getEndDate( )
185     {
186         return _endDate;
187     }
188 
189     /**
190      * @return the admin
191      */
192     public String getAdmin( )
193     {
194         return _admin;
195     }
196 
197     /**
198      * @return the idState
199      */
200     public int getIdState( )
201     {
202         return _idState;
203     }
204 
205     /**
206      * @return the nbSeats
207      */
208     public int getNbSeats( )
209     {
210         return _nbSeats;
211     }
212 
213     /**
214      * @return the dateAppointmentTaken
215      */
216     public LocalDateTime getDateAppointmentTaken( )
217     {
218         return _dateAppointmentTaken;
219     }
220 
221     private LocalDateTime parseDate( String strDocumentValue )
222     {
223         LocalDateTime date = null;
224 
225         if ( StringUtils.isNotEmpty( strDocumentValue ) )
226         {
227             date = LocalDateTime.ofInstant( Instant.ofEpochMilli( Long.valueOf( strDocumentValue ) ), TimeZone.getDefault( ).toZoneId( ) );
228         }
229         return date;
230     }
231 
232     private Integer manageIntegerNullValue( String strDocumentValue )
233     {
234         Integer nReturn = INTEGER_MINUS_ONE;
235         if ( strDocumentValue != null )
236         {
237             try
238             {
239                 nReturn = Integer.parseInt( strDocumentValue );
240             }
241             catch( NumberFormatException e )
242             {
243                 AppLogService.error( "Unable to convert " + strDocumentValue + " to integer." );
244             }
245         }
246         return nReturn;
247     }
248 
249     /**
250      * @return the stateTitle
251      */
252     public String getStateTitle( )
253     {
254         return _stateTitle;
255     }
256 
257     /**
258      * @param stateTitle
259      *            the stateTitle to set
260      */
261     public void setStateTitle( String stateTitle )
262     {
263         _stateTitle = stateTitle;
264     }
265 
266     /**
267      * @return the formTitle
268      */
269     public String getFormTitle( )
270     {
271         return _formTitle;
272     }
273 
274     /**
275      * @param formTitle
276      *            the formTitle to set
277      */
278     public void setFormTitle( String formTitle )
279     {
280         _formTitle = formTitle;
281     }
282 
283     /**
284      * @return the dateOfTheAppointment
285      */
286     public String getDateOfTheAppointment( )
287     {
288         return _dateOfTheAppointment;
289     }
290 
291     /**
292      * @return the startingTime
293      */
294     public LocalTime getStartingTime( )
295     {
296         return _startingTime;
297     }
298 
299     /**
300      * @return the endingTime
301      */
302     public LocalTime getEndingTime( )
303     {
304         return _endingTime;
305     }
306 
307     /**
308      * 
309      * @return _cancelled
310      */
311     public boolean isCancelled( )
312     {
313         return _cancelled;
314     }
315 
316     /**
317      * @return the categoryTitle
318      */
319     public String getCategoryTitle( )
320     {
321         return _categoryTitle;
322     }
323 
324     /**
325      * @param categoryTitle
326      *            the categoryTitle to set
327      */
328     public void setCategoryTitle( String categoryTitle )
329     {
330         _categoryTitle = categoryTitle;
331     }
332 
333     /**
334      * @return the idCategory
335      */
336     public int getIdCategory( )
337     {
338         return _idCategory;
339     }
340 }