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.solr.service;
35
36 import fr.paris.lutece.plugins.appointment.business.category.Category;
37 import fr.paris.lutece.plugins.appointment.business.category.CategoryHome;
38 import fr.paris.lutece.plugins.appointment.business.slot.Slot;
39 import fr.paris.lutece.plugins.appointment.service.FormService;
40 import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFormDTO;
41 import fr.paris.lutece.plugins.search.solr.indexer.SolrIndexerService;
42 import fr.paris.lutece.plugins.search.solr.indexer.SolrItem;
43 import fr.paris.lutece.portal.service.image.ImageResourceManager;
44 import fr.paris.lutece.portal.web.l10n.LocaleService;
45 import fr.paris.lutece.util.url.UrlItem;
46 import org.apache.commons.lang3.StringUtils;
47
48 import java.time.DayOfWeek;
49 import java.time.LocalDate;
50 import java.time.LocalDateTime;
51 import java.time.temporal.WeekFields;
52 import java.util.Arrays;
53 import java.util.List;
54
55
56
57
58
59
60
61 public final class FormUtil
62 {
63
64 private static final String MIN_HOURS_BEFORE_APPOINTMENT = "min_hours_before_appointment";
65 private static final String FORM_ID_CATEGORY = "form_id_category";
66 private static final String APPOINTMENT_ACTIVE = "appointment_active";
67 private static final String URL_BASE = "url_base";
68 private static final String FORM_ID_TITLE = "form_id_title";
69 private static final String APPOINTMENT_MULTISLOTS = "appointment_multislots";
70 private static final String APPOINTMENT_MAX_CONSECUTIVES_SLOTS = "appointment_max_consecutives_slots";
71 private static final String APPOINTMENT_NB_FREE_PLACES = "appointment_nb_free_places";
72 private static final String APPOINTMENT_NB_PLACES = "appointment_nb_places";
73 private static final String VIEW_APPOINTMENT = "getViewAppointmentCalendar";
74
75 private static final String FORM_ID_TITLE_SEPARATOR = "|";
76 private static final String DASH = "-";
77 private static final String SLASH = "/";
78
79 public static final String PARAMETER_ID_FORM = "id_form";
80 private static final String IMAGE_RESOURCE_TYPE_ID = "appointmentForm_icon";
81 private static final String ICON_URL = "appointment_form_icon_url";
82
83
84
85
86 private FormUtil( )
87 {
88 }
89
90
91
92
93
94
95
96
97 public static String getFormUid( int nIdForm )
98 {
99 return SolrIndexerService.getWebAppName( ) + Utilities.UNDERSCORE
100 + Utilities.buildResourceUid( Integer.toString( nIdForm ), Utilities.RESOURCE_TYPE_APPOINTMENT );
101 }
102
103
104
105
106
107
108
109
110 public static String getFormUrl( int nIdForm )
111 {
112 UrlItem url = new UrlItem( SolrIndexerService.getBaseUrl( ) );
113 url.addParameter( Utilities.PARAMETER_XPAGE, Utilities.XPAGE_APPOINTMENT );
114 url.addParameter( Utilities.PARAMETER_VIEW, VIEW_APPOINTMENT );
115 url.addParameter( PARAMETER_ID_FORM, nIdForm );
116 return url.getUrl( );
117 }
118
119
120
121
122
123
124
125 public static SolrItem getDefaultFormItem( AppointmentFormDTO appointmentForm )
126 {
127 SolrItem item = new SolrItem( );
128 item.setSummary( appointmentForm.getDescription( ) );
129 item.setTitle( appointmentForm.getTitle( ) );
130 item.setSite( SolrIndexerService.getWebAppName( ) );
131 item.setRole( appointmentForm.getRole( ) );
132 item.setXmlContent( StringUtils.EMPTY );
133 Category category = CategoryHome.findByPrimaryKey( appointmentForm.getIdCategory( ) );
134 if ( category != null )
135 {
136 item.setCategorie( Arrays.asList( category.getLabel( ) ) );
137 item.addDynamicField( FORM_ID_CATEGORY, (long) category.getIdCategory( ) );
138 }
139 StringBuilder stringBuilder = new StringBuilder( );
140 item.setContent( stringBuilder.toString( ) );
141 item.addDynamicField( MIN_HOURS_BEFORE_APPOINTMENT, (long) appointmentForm.getMinTimeBeforeAppointment( ) );
142 item.addDynamicFieldNotAnalysed( APPOINTMENT_ACTIVE, Boolean.toString( appointmentForm.getIsActive( ) ) );
143 item.addDynamicFieldNotAnalysed( URL_BASE, SolrIndexerService.getRootUrl( ) );
144 item.addDynamicFieldNotAnalysed( FORM_ID_TITLE, getFormUid( appointmentForm.getIdForm( ) ) + FORM_ID_TITLE_SEPARATOR + appointmentForm.getTitle( ) );
145 item.addDynamicFieldNotAnalysed( ICON_URL, SolrIndexerService.getRootUrl( ) + ImageResourceManager.getImageUrl( IMAGE_RESOURCE_TYPE_ID, appointmentForm.getIdForm() ));
146 return item;
147 }
148
149
150
151
152
153
154
155
156
157
158 public static SolrItem getFormItem( AppointmentFormDTO appointmentForm, List<Slot> listSlots )
159 {
160 SolrItem item = getDefaultFormItem( appointmentForm );
161 item.setUrl( getFormUrl( appointmentForm.getIdForm( ) ) );
162 item.setUid( Utilities.buildResourceUid( Integer.toString( appointmentForm.getIdForm( ) ), Utilities.RESOURCE_TYPE_APPOINTMENT ) );
163 item.setDate( appointmentForm.getDateStartValidity( ) );
164 item.setType( Utilities.SHORT_NAME_APPOINTMENT );
165 int freePlaces = 0;
166 int places = 0;
167 for ( Slot slot : listSlots )
168 {
169 freePlaces += Math.max( 0, slot.getNbPotentialRemainingPlaces( ) );
170 places += slot.getMaxCapacity( );
171 }
172 if ( StringUtils.isNotEmpty( appointmentForm.getAddress( ) ) && appointmentForm.getLongitude( ) != null && appointmentForm.getLatitude( ) != null )
173 {
174 item.addDynamicFieldGeoloc( Utilities.SHORT_NAME_APPOINTMENT, appointmentForm.getAddress( ), appointmentForm.getLongitude( ),
175 appointmentForm.getLatitude( ), Utilities.SHORT_NAME_APPOINTMENT + DASH + freePlaces + SLASH + places );
176 }
177 item.addDynamicField( APPOINTMENT_MULTISLOTS, Boolean.toString( appointmentForm.getIsMultislotAppointment( ) ) );
178 if( appointmentForm.getIsMultislotAppointment( ) ) {
179 item.addDynamicField(APPOINTMENT_MAX_CONSECUTIVES_SLOTS, Long.valueOf(appointmentForm.getNbConsecutiveSlots()));
180 }
181 else {
182 item.addDynamicField(APPOINTMENT_MAX_CONSECUTIVES_SLOTS, 1L);
183 }
184 item.addDynamicField( APPOINTMENT_NB_FREE_PLACES, Long.valueOf( freePlaces ) );
185 item.addDynamicField( APPOINTMENT_NB_PLACES, Long.valueOf( places ) );
186
187 if ( appointmentForm.getDateStartValidity( ) != null )
188 {
189 item.setHieDate( appointmentForm.getDateStartValidity( ).toLocalDate( ).format( Utilities.HIE_DATE_FORMATTER ) );
190 }
191 return item;
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205 public static boolean isPeriodValidToIndex( int nIdForm, LocalDate stratingDate, LocalDate endingDate )
206 {
207
208 AppointmentFormDTO form = FormService.buildAppointmentFormWithoutReservationRule( nIdForm );
209
210 int nNbWeeksToDisplay = form.getNbWeeksToDisplay( );
211 LocalDate startingDateOfDisplay = LocalDateTime.now( ).plusHours( form.getMinTimeBeforeAppointment( ) ).toLocalDate( );
212 if ( form.getDateStartValidity( ) != null && startingDateOfDisplay.isBefore( form.getDateStartValidity( ).toLocalDate( ) ) )
213 {
214 startingDateOfDisplay = form.getDateStartValidity( ).toLocalDate( );
215 }
216
217
218 LocalDate endingDateOfDisplay = startingDateOfDisplay.with( WeekFields.of( LocaleService.getDefault( ) ).dayOfWeek( ), DayOfWeek.SUNDAY.getValue( ) )
219 .plusWeeks( nNbWeeksToDisplay - 1L );
220 if ( form.getDateEndValidity( ) != null && endingDateOfDisplay.isAfter( form.getDateEndValidity( ).toLocalDate( ) ) )
221 {
222 endingDateOfDisplay = form.getDateEndValidity( ).toLocalDate( );
223 }
224
225 return !( stratingDate.isAfter( endingDateOfDisplay ) || endingDate.isBefore( startingDateOfDisplay ) );
226
227 }
228
229 }