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.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 );
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 }