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