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.service;
35  
36  import com.fasterxml.jackson.core.JsonProcessingException;
37  import com.fasterxml.jackson.core.type.TypeReference;
38  import com.fasterxml.jackson.databind.ObjectMapper;
39  import fr.paris.lutece.plugins.appointment.modules.rest.business.providers.IAppointmentDataProvider;
40  import fr.paris.lutece.plugins.appointment.modules.rest.business.providers.SolrProvider;
41  import fr.paris.lutece.plugins.appointment.modules.rest.pojo.AppointmentSlotsSearchPOJO;
42  import fr.paris.lutece.plugins.appointment.modules.rest.pojo.InfoSlot;
43  import fr.paris.lutece.plugins.appointment.modules.rest.pojo.SolrAppointmentSlotPOJO;
44  import fr.paris.lutece.plugins.appointment.modules.rest.util.contsants.AppointmentRestConstants;
45  import fr.paris.lutece.portal.service.util.AppLogService;
46  import fr.paris.lutece.portal.service.util.AppPropertiesService;
47  import fr.paris.lutece.util.httpaccess.HttpAccessException;
48  import fr.paris.lutece.util.url.UrlItem;
49  import org.apache.hc.core5.http.NameValuePair;
50  import org.apache.hc.core5.net.URLEncodedUtils;
51  import fr.paris.lutece.portal.service.util.AppException;
52  
53  import java.net.URI;
54  import java.net.URISyntaxException;
55  import java.nio.charset.StandardCharsets;
56  import java.time.LocalDateTime;
57  import java.util.List;
58  import java.util.Map;
59  import java.util.stream.Collectors;
60  
61  public class AppointmentSlotsService
62  {
63  
64      public static final String LUTECE_BASE_URL = "appointment-rest.lutece.base.url";
65  
66      private static IAppointmentDataProvider _dataProvider;
67      private static AppointmentSlotsService _instance;
68      private static String _baseUrl;
69  
70      private AppointmentSlotsService( )
71      {
72      }
73  
74      public static synchronized AppointmentSlotsService getInstance( )
75      {
76          if ( _instance == null )
77          {
78              _instance = new AppointmentSlotsService( );
79              _instance.init( );
80          }
81          _baseUrl = AppPropertiesService.getProperty( LUTECE_BASE_URL );
82  
83          return _instance;
84      }
85  
86      private synchronized void init( )
87      {
88          if ( _dataProvider == null )
89          {
90              _dataProvider = SolrProvider.getInstance( );
91              AppLogService.info( "DatatProvider loaded : " + _dataProvider.getName( ) );
92          }
93      }
94  
95      public Map<String, List<InfoSlot>> getAvailableTimeSlotsAsList( AppointmentSlotsSearchPOJO search )
96      {
97          String response = null;
98          try
99          {
100             response = _dataProvider.getAvailableTimeSlot( search.getAppointmentIds( ), search.getStartDate( ), search.getEndDate( ),
101                     search.getDocumentNumber( ) );
102 
103             ObjectMapper mapper = new ObjectMapper( );
104             TypeReference<List<SolrAppointmentSlotPOJO>> typeReference = new TypeReference<List<SolrAppointmentSlotPOJO>>( )
105             {
106             };
107             List<SolrAppointmentSlotPOJO> solrResponse = mapper.readValue( response, typeReference );
108 
109             return solrResponse.stream( ).collect( Collectors.groupingBy( SolrAppointmentSlotPOJO::getUidFormString, Collectors
110                     .mapping( a -> new InfoSlot( buildDate( a.getUrl( ) ), buildUrl( a.getUrl( ), search.getDocumentNumber( ) ) ), Collectors.toList( ) ) ) );
111         }
112         catch (HttpAccessException | JsonProcessingException e)
113         {
114             AppLogService.error( e.getMessage( ), e );
115             throw new AppException( e.getMessage( ), e );
116         }
117     }
118 
119     private static LocalDateTime buildDate( String strUrl )
120     {
121         return LocalDateTime.parse( getParamFromUrl( strUrl ).get( AppointmentRestConstants.PARAMETER_STARTING_DATE ),
122                 AppointmentRestConstants.SOLR_RESPONSE_DATE_FORMATTER );
123     }
124 
125     public String buildUrl( String strUrl, Integer nbPlaceToTake )
126     {
127         Map<String, String> mapParamaters = getParamFromUrl( strUrl );
128 
129         UrlItem url = new UrlItem( _baseUrl /* + AppointmentRestConstants.XPAGE_APPOINTMENT_ANTS */ );
130         url.addParameter( AppointmentRestConstants.PARAMETER_VIEW, AppointmentRestConstants.VIEW_APPOINTMENT_ANTS );
131         url.addParameter( AppointmentRestConstants.PARAMETER_ID_FORM, mapParamaters.get( AppointmentRestConstants.PARAMETER_ID_FORM ) );
132         url.addParameter( AppointmentRestConstants.PARAMETER_STARTING_DATE, mapParamaters.get( AppointmentRestConstants.PARAMETER_STARTING_DATE ) );
133         url.addParameter( AppointmentRestConstants.PARAMETER_NB_PLACES_TO_TAKE, nbPlaceToTake );
134         return url.getUrl( );
135     }
136 
137     private static Map<String, String> getParamFromUrl( String strUrl )
138     {
139         List<NameValuePair> params;
140         try
141         {
142 
143             params = URLEncodedUtils.parse( new URI( strUrl ), StandardCharsets.UTF_8 );
144         }
145         catch( URISyntaxException e )
146         {
147             AppLogService.error( e.getMessage( ), e );
148             throw new AppException( e.getMessage( ), e );
149         }
150         return params.stream( ).collect( Collectors.toMap( NameValuePair::getName, NameValuePair::getValue ) );
151     }
152 
153 }