View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.rest.business.providers;
35  
36  import com.fasterxml.jackson.core.JsonProcessingException;
37  import com.fasterxml.jackson.databind.JsonNode;
38  import com.fasterxml.jackson.databind.ObjectMapper;
39  import fr.paris.lutece.plugins.appointment.modules.rest.pojo.SolrAppointmentSlotPOJO;
40  import fr.paris.lutece.plugins.appointment.modules.rest.pojo.SolrMeetingPointPOJO;
41  import fr.paris.lutece.plugins.appointment.modules.rest.util.contsants.AppointmentRestConstants;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  import fr.paris.lutece.util.httpaccess.HttpAccess;
45  import fr.paris.lutece.util.httpaccess.HttpAccessException;
46  import fr.paris.lutece.util.signrequest.BasicAuthorizationAuthenticator;
47  import org.apache.commons.lang3.StringUtils;
48  
49  import java.io.UnsupportedEncodingException;
50  import java.net.URLEncoder;
51  import java.time.LocalDate;
52  import java.time.LocalTime;
53  import java.util.List;
54  import java.util.stream.Collectors;
55  
56  public class SolrProvider implements IAppointmentDataProvider
57  {
58  
59      // Constants
60      private static final String PROVIDER_NAME = "solr.provider";
61      private static final String PROPERTY_ENCODE_URI_ENCODING = "search.encode.uri.encoding";
62      private static final String DEFAULT_URI_ENCODING = "ISO-8859-1";
63      private static final String PROPERTY_SOLR_BASE_URL = "appointment-rest.solr.base_url";
64      private static final String PROPERTY_SOLR_ROWS = "appointment-rest.solr.rows";
65      private static final String PROPERTY_SOLR_USERNAME = "appointment-rest.solr.username";
66      private static final String PROPERTY_SOLR_PASSWORD = "appointment-rest.solr.password";
67  
68      private static SolrProvider _instance;
69      private static String _strBaseUrl;
70      private static String _strRows;
71      private static BasicAuthorizationAuthenticator _authenticator;
72  
73      @Override
74      public String getName( )
75      {
76          return PROVIDER_NAME;
77      }
78  
79      public static synchronized SolrProvider getInstance( )
80      {
81          if ( _instance == null )
82          {
83              _instance = new SolrProvider( );
84              _instance.init( );
85          }
86  
87          return _instance;
88      }
89  
90      private synchronized void init( )
91      {
92          if ( _strBaseUrl == null )
93          {
94              _strBaseUrl = AppPropertiesService.getProperty( PROPERTY_SOLR_BASE_URL );
95          }
96          _strRows = AppPropertiesService.getProperty( PROPERTY_SOLR_ROWS, "10000" );
97          _authenticator = new BasicAuthorizationAuthenticator( AppPropertiesService.getProperty( PROPERTY_SOLR_USERNAME ),
98                  AppPropertiesService.getProperty( PROPERTY_SOLR_PASSWORD ) );
99      }
100 
101     @Override
102     public String getAvailableTimeSlot( List<String> appointmentIds, LocalDate startDate, LocalDate endDate, Integer documentNumber )
103             throws HttpAccessException, JsonProcessingException
104     {
105         HttpAccess httpAccess = new HttpAccess( );
106 
107         StringBuilder query = generateAvailableTimeSlotSolrQuery( appointmentIds, startDate, endDate, documentNumber );
108 
109         AppLogService.debug( "Solr query getAvailableTimeSlot : {}", query );
110 
111         String strUrl = _strBaseUrl + query;
112 
113         String response = httpAccess.doGet( strUrl, _authenticator, null );
114         ObjectMapper mapper = new ObjectMapper( );
115         JsonNode jsonNode = mapper.readTree( response );
116         return jsonNode.get( "response" ).get( "docs" ).toString( );
117     }
118 
119     private static StringBuilder generateAvailableTimeSlotSolrQuery( List<String> appointmentIds, LocalDate startDate, LocalDate endDate,
120             Integer documentNumber )
121     {
122         String strStartDate = startDate.atStartOfDay( ).format( AppointmentRestConstants.SOLR_DATE_FORMATTER );
123 
124         if ( LocalDate.now( ).isEqual( startDate ) )
125         {
126             strStartDate = AppointmentRestConstants.SOLR_QUERY_NOW;
127         }
128 
129         StringBuilder query = new StringBuilder( );
130         query.append( AppointmentRestConstants.SOLR_QUERY_SELECT + AppointmentRestConstants.SOLR_QUERY_Q )
131                 .append( encoder( AppointmentRestConstants.SOLR_QUERY_Q_VALUE ) );
132         query.append( AppointmentRestConstants.SOLR_QUERY_FIELD );
133         query.append( encoder(
134                 SolrAppointmentSlotPOJO.SOLR_FIELD_UID + AppointmentRestConstants.SOLR_QUERY_COMMA + SolrAppointmentSlotPOJO.SOLR_FIELD_DATE + AppointmentRestConstants.SOLR_QUERY_COMMA + SolrAppointmentSlotPOJO.SOLR_FIELD_URL ) );
135         query.append( AppointmentRestConstants.SOLR_QUERY_FILTER_QUERY );
136         query.append( SolrAppointmentSlotPOJO.SOLR_FIELD_DATE ).append( encoder( AppointmentRestConstants.SOLR_QUERY_COLON ) );
137         query.append( encoder( AppointmentRestConstants.SOLR_QUERY_LB ) ).append( strStartDate ).append( encoder( AppointmentRestConstants.SOLR_QUERY_TO ) )
138                 .append( LocalTime.MAX.atDate( endDate ).format( AppointmentRestConstants.SOLR_DATE_FORMATTER ) )
139                 .append( encoder( AppointmentRestConstants.SOLR_QUERY_RB ) );
140         query.append( AppointmentRestConstants.SOLR_QUERY_FILTER_QUERY );
141         query.append( SolrAppointmentSlotPOJO.SOLR_FIELD_UID ).append( encoder( AppointmentRestConstants.SOLR_QUERY_COLON ) )
142                 .append( AppointmentRestConstants.SOLR_QUERY_LP );
143         query.append( encoder( appointmentIds.stream( ).collect( Collectors.joining( StringUtils.SPACE ) ) ) );
144         query.append( AppointmentRestConstants.SOLR_QUERY_RP );
145         query.append( AppointmentRestConstants.SOLR_QUERY_FILTER_QUERY );
146         query.append(
147                 encoder( SolrAppointmentSlotPOJO.SOLR_FIELD_DAY_OPEN + AppointmentRestConstants.SOLR_QUERY_COLON + AppointmentRestConstants.SOLR_QUERY_TRUE ) );
148         query.append( AppointmentRestConstants.SOLR_QUERY_FILTER_QUERY );
149         query.append(
150                 encoder( SolrAppointmentSlotPOJO.SOLR_FIELD_ENABLED + AppointmentRestConstants.SOLR_QUERY_COLON + AppointmentRestConstants.SOLR_QUERY_TRUE ) );
151         query.append( AppointmentRestConstants.SOLR_QUERY_FILTER_QUERY );
152         query.append( encoder(
153                 SolrAppointmentSlotPOJO.SOLR_FIELD_APPOINTMENT_ACTIVE + AppointmentRestConstants.SOLR_QUERY_COLON + AppointmentRestConstants.SOLR_QUERY_TRUE ) );
154         query.append( AppointmentRestConstants.SOLR_QUERY_FILTER_QUERY );
155         query.append( SolrAppointmentSlotPOJO.SOLR_FIELD_NB_CONSECUTIVES_SLOTS ).append( encoder( AppointmentRestConstants.SOLR_QUERY_COLON ) );
156         query.append( encoder( AppointmentRestConstants.SOLR_QUERY_LB ) ).append( documentNumber ).append( encoder( AppointmentRestConstants.SOLR_QUERY_TO ) )
157                 .append( AppointmentRestConstants.SOLR_QUERY_STAR ).append( encoder( AppointmentRestConstants.SOLR_QUERY_RB ) );
158         query.append( AppointmentRestConstants.SOLR_QUERY_FILTER_QUERY );
159         query.append( SolrAppointmentSlotPOJO.SOLR_FIELD_MAX_CONSECUTIVES_SLOTS ).append( encoder( AppointmentRestConstants.SOLR_QUERY_COLON ) );
160         query.append( encoder( AppointmentRestConstants.SOLR_QUERY_LB ) ).append( documentNumber ).append( encoder( AppointmentRestConstants.SOLR_QUERY_TO ) )
161                 .append( AppointmentRestConstants.SOLR_QUERY_STAR ).append( encoder( AppointmentRestConstants.SOLR_QUERY_RB ) );
162         query.append( AppointmentRestConstants.SOLR_QUERY_FILTER_ROWS + _strRows );
163         return query;
164     }
165 
166     @Override
167     public String getManagedMeetingPoints( ) throws HttpAccessException
168     {
169         HttpAccess httpAccess = new HttpAccess( );
170 
171         StringBuilder query = generateManagedMeetingPoints( );
172 
173         AppLogService.debug( "Solr query getManagedMeetingPoints : {}", query );
174 
175         String strUrl = _strBaseUrl + query;
176 
177         return httpAccess.doGet( strUrl, _authenticator, null );
178     }
179 
180     private static StringBuilder generateManagedMeetingPoints( )
181     {
182         StringBuilder query = new StringBuilder( );
183         query.append( AppointmentRestConstants.SOLR_QUERY_SELECT + AppointmentRestConstants.SOLR_QUERY_Q )
184                 .append( encoder( AppointmentRestConstants.SOLR_QUERY_Q_VALUE ) );
185         query.append( AppointmentRestConstants.SOLR_QUERY_FIELD );
186         query.append( encoder(
187                 SolrMeetingPointPOJO.SOLR_FIELD_UID + AppointmentRestConstants.SOLR_QUERY_COMMA + SolrMeetingPointPOJO.SOLR_FIELD_TITLE + AppointmentRestConstants.SOLR_QUERY_COMMA + SolrMeetingPointPOJO.SOLR_FIELD_ADDRESS + AppointmentRestConstants.SOLR_QUERY_COMMA + SolrMeetingPointPOJO.SOLR_FIELD_GEOLOC + AppointmentRestConstants.SOLR_QUERY_COMMA + SolrMeetingPointPOJO.SOLR_FIELD_ICON_URL ) );
188         query.append( AppointmentRestConstants.SOLR_QUERY_FILTER_QUERY );
189         query.append( SolrMeetingPointPOJO.SOLR_FIELD_TYPE ).append( encoder( AppointmentRestConstants.SOLR_QUERY_COLON ) )
190                 .append( SolrMeetingPointPOJO.SOLR_FIELD_TYPE_APPOINTMENT );
191         query.append( AppointmentRestConstants.SOLR_QUERY_GROUP );
192         query.append( AppointmentRestConstants.SOLR_QUERY_GROUP_FIELD + SolrMeetingPointPOJO.SOLR_FIELD_UID );
193         return query;
194     }
195 
196     public static String encoder( String strSource )
197     {
198         String strEncoded = StringUtils.EMPTY;
199 
200         try
201         {
202             strEncoded = URLEncoder.encode( strSource, getEncoding( ) );
203         }
204         catch( UnsupportedEncodingException e )
205         {
206             AppLogService.error( e.getMessage( ), e );
207         }
208 
209         return strEncoded;
210     }
211 
212     public static String getEncoding( )
213     {
214         return AppPropertiesService.getProperty( PROPERTY_ENCODE_URI_ENCODING, DEFAULT_URI_ENCODING );
215     }
216 }