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
35 package fr.paris.lutece.plugins.appointment.modules.ants.service;
36
37 import java.io.IOException;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42
43 import com.fasterxml.jackson.core.type.TypeReference;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45
46 import fr.paris.lutece.plugins.appointment.modules.ants.utils.HttpCallsUtils;
47 import fr.paris.lutece.plugins.appointment.modules.ants.web.PreDemandeStatusEnum;
48 import fr.paris.lutece.plugins.appointment.modules.ants.web.PredemandeResponse;
49 import fr.paris.lutece.portal.service.util.AppLogService;
50 import fr.paris.lutece.portal.service.util.AppPropertiesService;
51 import fr.paris.lutece.util.httpaccess.HttpAccess;
52 import fr.paris.lutece.util.httpaccess.HttpAccessException;
53 import fr.paris.lutece.util.url.UrlItem;
54 import fr.paris.lutece.portal.service.datastore.DatastoreService;
55 import fr.paris.lutece.portal.service.html.EncodingService;
56
57
58
59
60 public class PreDemandeValidationService
61 {
62
63 private static final String PROPERTY_ENDPOINT_STATUS = AppPropertiesService.getProperty( "ants.api.opt.get.status" );
64 private static final String PROPERTY_API_OPT_AUTH_TOKEN_KEY = AppPropertiesService.getProperty( "ants.auth.token" );
65 private static final String PROPERTY_API_OPT_AUTH_TOKEN_VALUE = String.valueOf( DatastoreService.getDataValue( "module.appointment.ants.site_property.token" ,"") );
66 private static final String PROPERTY_ID_APPLICATION_PARAMETER = AppPropertiesService.getProperty( "ants.ids_application.parameters" );
67 private static final String PROPERTY_MEETING_POINT_ID_PARAMETER = AppPropertiesService.getProperty( "ants.meeting_point_id.parameters" );
68 private static final String PROPERTY_MEETING_POINT_ID_DEFAULT_VALUE = AppPropertiesService.getProperty( "ants.meeting_point_id.defaultValue" );
69
70
71 private static final String PROPERTY_SOCKET_TIMEOUT = "ants.api.socketTimeout";
72 private static final String PROPERTY_CONNECTION_TIMEOUT = "ants.api.connectionTimeout";
73
74 private PreDemandeValidationService( )
75 {
76 }
77
78
79
80
81
82
83
84
85
86
87 public static boolean checkPredemandeCodesValidationAndAppointments( List<String> codes ) throws IOException
88 {
89 Map<String, PredemandeResponse> responseMap = getPreDemandeStatusAndAppointments( codes );
90
91 if ( responseMap.isEmpty( ) )
92 {
93 return false;
94 }
95
96 for ( PredemandeResponse predemande : responseMap.values( ) )
97 {
98 List<PredemandeResponse.Appointment> appointments = predemande.getAppointments( );
99
100 if ( !appointments.isEmpty( ) || !PreDemandeStatusEnum.VALIDATED.name( ).equalsIgnoreCase( predemande.getStatus( ) ) )
101 {
102 return false;
103 }
104 }
105
106 return true;
107 }
108
109
110
111
112
113
114
115
116
117
118 private static Map<String, PredemandeResponse> getPreDemandeStatusAndAppointments( List<String> codes ) throws IOException
119 {
120 UrlItem urlItem;
121 String apiUrl = null;
122
123 if ( !codes.isEmpty( ) )
124 {
125 urlItem = new UrlItem( PROPERTY_ENDPOINT_STATUS + PROPERTY_ID_APPLICATION_PARAMETER + "=" + codes.get( 0 ) );
126 for ( int i = 1; i < codes.size( ); i++ )
127 {
128 urlItem.addParameter( PROPERTY_ID_APPLICATION_PARAMETER, codes.get( i ) );
129 }
130
131 String strMeetingPointId = EncodingService.encodeUrl( PROPERTY_MEETING_POINT_ID_DEFAULT_VALUE ).replace( "+", "%20" );
132 urlItem.addParameter( PROPERTY_MEETING_POINT_ID_PARAMETER, strMeetingPointId );
133
134 apiUrl = urlItem.toString( );
135 }
136
137
138
139 HttpAccess httpAccess = HttpCallsUtils.getHttpAccessTimeoutFromProperties( PROPERTY_SOCKET_TIMEOUT, PROPERTY_CONNECTION_TIMEOUT );
140
141 Map<String, String> headers = new HashMap<>( );
142 headers.put( PROPERTY_API_OPT_AUTH_TOKEN_KEY, PROPERTY_API_OPT_AUTH_TOKEN_VALUE );
143
144 try
145 {
146 String jsonResponse = httpAccess.doGet( apiUrl, null, null, headers );
147 ObjectMapper mapper = new ObjectMapper( );
148 TypeReference<HashMap<String, PredemandeResponse>> typeRef = new TypeReference<HashMap<String, PredemandeResponse>>( )
149 {
150 };
151 return mapper.readValue( jsonResponse, typeRef );
152 }
153 catch( HttpAccessException | IOException ex )
154 {
155 AppLogService.error( "Error calling API {}", apiUrl, ex );
156 return Collections.emptyMap( );
157 }
158 }
159 }