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.rs;
35
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import fr.paris.lutece.plugins.appointment.modules.rest.pojo.AppointmentSlotsSearchPOJO;
38 import fr.paris.lutece.plugins.appointment.modules.rest.pojo.InfoSlot;
39 import fr.paris.lutece.plugins.appointment.modules.rest.pojo.MeetingPointPOJO;
40 import fr.paris.lutece.plugins.appointment.modules.rest.rs.filter.QueryParamValidator;
41 import fr.paris.lutece.plugins.appointment.modules.rest.rs.filter.ValidationErrorResponse;
42 import fr.paris.lutece.plugins.appointment.modules.rest.service.IAppointmentRestService;
43 import fr.paris.lutece.plugins.appointment.modules.rest.util.contsants.AppointmentRestConstants;
44 import fr.paris.lutece.plugins.appointment.service.AppointmentPlugin;
45 import fr.paris.lutece.plugins.rest.service.RestConstants;
46 import fr.paris.lutece.portal.service.util.AppPropertiesService;
47 import fr.paris.lutece.util.httpaccess.HttpAccessException;
48 import org.apache.commons.collections.CollectionUtils;
49 import org.springframework.stereotype.Component;
50
51 import javax.inject.Inject;
52 import javax.ws.rs.*;
53 import javax.ws.rs.container.ContainerRequestContext;
54 import javax.ws.rs.core.Context;
55 import javax.ws.rs.core.MediaType;
56 import javax.ws.rs.core.Response;
57 import java.time.LocalDate;
58 import java.util.List;
59 import java.util.Map;
60 import java.util.Optional;
61
62 @Component
63 @Path( RestConstants.BASE_PATH + AppointmentPlugin.PLUGIN_NAME )
64 public class AppointmentRest
65 {
66
67 public static final String SOLR_EXCEPTION = "appointment-rest.solr.exception.message";
68 public static final String JSON_EXCEPTION = "appointment-rest.json.exception.message";
69
70 @Inject
71 IAppointmentRestService _appointmentRestService;
72
73 @GET
74 @Path( AppointmentRestConstants.SLASH + AppointmentRestConstants.PATH_API + AppointmentRestConstants.SLASH + AppointmentRestConstants.PATH_AVAILABLE_SLOTS )
75 @Produces( MediaType.APPLICATION_JSON )
76 public Response getAvailableTimeSlots( @Context ContainerRequestContext request,
77 @QueryParam( value = AppointmentRestConstants.JSON_TAG_MEETING_POINT_IDS ) List<String> appointementIds,
78 @QueryParam( value = AppointmentRestConstants.JSON_TAG_START_DATE ) String startDate,
79 @QueryParam( value = AppointmentRestConstants.JSON_TAG_END_DATE ) String endDate,
80 @QueryParam( value = AppointmentRestConstants.JSON_TAG_REASON ) String reason,
81 @QueryParam( value = AppointmentRestConstants.JSON_TAG_DOCUMENTS_NUMBER ) String documentsNumber )
82 {
83 ValidationErrorResponse errors = QueryParamValidator.validate( request );
84 if ( CollectionUtils.isNotEmpty( errors.getDetail( ) ) )
85 {
86 return Response.status( 422 ).entity( errors ).build( );
87 }
88 AppointmentSlotsSearchPOJOmodules/rest/pojo/AppointmentSlotsSearchPOJO.html#AppointmentSlotsSearchPOJO">AppointmentSlotsSearchPOJO search = new AppointmentSlotsSearchPOJO( appointementIds,
89 LocalDate.parse( startDate, AppointmentRestConstants.SEARCH_DATE_FORMATTER ),
90 LocalDate.parse( endDate, AppointmentRestConstants.SEARCH_DATE_FORMATTER ), reason,
91 Optional.ofNullable( documentsNumber ).map( Integer::valueOf ).orElse( null ) );
92 Map<String, List<InfoSlot>> availableTimeSlots;
93 try
94 {
95 availableTimeSlots = _appointmentRestService.getAvailableTimeSlots( search );
96 }
97 catch( HttpAccessException e )
98 {
99 throw new WebApplicationException(
100 Response.status( Response.Status.SERVICE_UNAVAILABLE ).entity( AppPropertiesService.getProperty( SOLR_EXCEPTION ) ).build( ) );
101 }
102 catch( JsonProcessingException e )
103 {
104 throw new WebApplicationException(
105 Response.status( Response.Status.BAD_REQUEST ).entity( AppPropertiesService.getProperty( JSON_EXCEPTION ) ).build( ) );
106 }
107 return Response.ok( availableTimeSlots ).build( );
108 }
109
110 @GET
111 @Path( AppointmentRestConstants.SLASH + AppointmentRestConstants.PATH_API + AppointmentRestConstants.SLASH
112 + AppointmentRestConstants.PATH_MANAGED_MEETING_POINTS )
113 @Produces( MediaType.APPLICATION_JSON )
114 public List<MeetingPointPOJO> getManagedMeetingPoints( )
115 {
116 List<MeetingPointPOJO> managedMeetingPoints;
117 try
118 {
119 managedMeetingPoints = _appointmentRestService.getManagedMeetingPoints( );
120 }
121 catch( HttpAccessException e )
122 {
123 throw new WebApplicationException(
124 Response.status( Response.Status.SERVICE_UNAVAILABLE ).entity( AppPropertiesService.getProperty( SOLR_EXCEPTION ) ).build( ) );
125 }
126 catch( JsonProcessingException e )
127 {
128 throw new WebApplicationException(
129 Response.status( Response.Status.BAD_REQUEST ).entity( AppPropertiesService.getProperty( JSON_EXCEPTION ) ).build( ) );
130 }
131 return managedMeetingPoints;
132 }
133 }