View Javadoc
1   /*
2    * Copyright (c) 2002-2024, Mairie de 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.grustoragedb.rs.service;
35  
36  import java.util.Optional;
37  
38  import javax.ws.rs.Consumes;
39  import javax.ws.rs.DELETE;
40  import javax.ws.rs.GET;
41  import javax.ws.rs.HeaderParam;
42  import javax.ws.rs.POST;
43  import javax.ws.rs.PUT;
44  import javax.ws.rs.Path;
45  import javax.ws.rs.PathParam;
46  import javax.ws.rs.Produces;
47  import javax.ws.rs.core.MediaType;
48  import javax.ws.rs.core.Response;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  import com.fasterxml.jackson.core.JsonProcessingException;
53  
54  import fr.paris.lutece.plugins.grustoragedb.business.Status;
55  import fr.paris.lutece.plugins.grustoragedb.business.StatusHome;
56  import fr.paris.lutece.plugins.grustoragedb.utils.GrustoragedbConstants;
57  import fr.paris.lutece.plugins.grustoragedb.utils.GrustoragedbUtils;
58  import fr.paris.lutece.plugins.rest.service.RestConstants;
59  import fr.paris.lutece.util.json.ErrorJsonResponse;
60  import fr.paris.lutece.util.json.JsonResponse;
61  import fr.paris.lutece.util.json.JsonUtil;
62  
63  /**
64   * 
65   * StatusRest
66   *
67   */
68  @Path( RestConstants.BASE_PATH + GrustoragedbConstants.PATH_STATUS_REST )
69  public class StatusRest
70  {
71      
72      /**
73       * Gets list status
74       * @return list status
75       */
76      @GET( )
77      @Consumes( MediaType.APPLICATION_JSON )
78      @Produces( MediaType.APPLICATION_JSON )
79      public Response getListStatus( @HeaderParam( GrustoragedbConstants.CONSTANT_X_API_KEY ) String strApiKey )
80      {
81          if( GrustoragedbConstants.PROPERTY_STATUS_REST_API_KEY.equals( strApiKey ) )
82          {
83              return Response.status( Response.Status.OK )
84                      .entity( JsonUtil.buildJsonResponse( new JsonResponse( StatusHome.getStatusList( ) ) ) )
85                      .build( );
86          }
87          
88          return Response.status( Response.Status.UNAUTHORIZED )
89                  .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( GrustoragedbConstants.API_KEY_ERROR ) ) )
90                  .build( );
91      }
92  
93      /**
94       * Get status by id
95       * @param nId
96       * @return status by id
97       */
98      @GET
99      @Path( GrustoragedbConstants.PATH_ID )
100     @Consumes( MediaType.APPLICATION_JSON )
101     @Produces( MediaType.APPLICATION_JSON )
102     public Response getStatus( @HeaderParam( GrustoragedbConstants.CONSTANT_X_API_KEY ) String strApiKey,
103             @PathParam( GrustoragedbConstants.PATH_PARAM_ID ) int nId )
104     {
105         if( GrustoragedbConstants.PROPERTY_STATUS_REST_API_KEY.equals( strApiKey ) )
106         {
107             Optional<Status> status = StatusHome.findByPrimaryKey( nId );
108     
109             if ( status.isPresent( ) )
110             {      
111                 return Response.status( Response.Status.OK )
112                         .entity( JsonUtil.buildJsonResponse( new JsonResponse( status.get( ) ) ) )
113                         .build( );
114             }
115     
116             return Response.status( Response.Status.NOT_FOUND )
117                     .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.getReasonPhrase( ) ) ) )
118                     .build( );
119         }
120         
121         return Response.status( Response.Status.UNAUTHORIZED )
122                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( GrustoragedbConstants.API_KEY_ERROR ) ) )
123                 .build( );
124     }
125 
126     /**
127      * Create status
128      * @param strStatus
129      * @return created status
130      */
131     @POST
132     @Consumes( MediaType.APPLICATION_JSON )
133     @Produces( MediaType.APPLICATION_JSON )
134     public Response doCreateStatus( @HeaderParam( GrustoragedbConstants.CONSTANT_X_API_KEY ) String strApiKey, String strStatus )
135     {
136         if( GrustoragedbConstants.PROPERTY_STATUS_REST_API_KEY.equals( strApiKey ) )
137         {
138             try
139             {
140                 Status status = GrustoragedbUtils.initMapper( ).readValue( strStatus, Status.class );
141     
142                 if ( isStatusCompleted( status ) )
143                 {
144                     StatusHome.create( status );
145                     return Response.status( Response.Status.CREATED )
146                             .entity( JsonUtil.buildJsonResponse( new JsonResponse( status ) ) )
147                             .build( );
148                 }
149                 return Response.status( Response.Status.NOT_ACCEPTABLE )
150                         .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_ACCEPTABLE.name( ), GrustoragedbConstants.ALL_REQUIRED ) ) )
151                         .build( );
152     
153             } catch ( JsonProcessingException e )
154             {
155                 return Response.status( Response.Status.BAD_REQUEST )
156                         .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.BAD_REQUEST.getReasonPhrase( ) ) ) )
157                         .build( );
158             }
159         }
160         
161         return Response.status( Response.Status.UNAUTHORIZED )
162                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( GrustoragedbConstants.API_KEY_ERROR ) ) )
163                 .build( );
164     }
165 
166     /**
167      * Modify status
168      * @param strStatus
169      * @return modified status
170      */
171     @PUT
172     @Consumes( MediaType.APPLICATION_JSON )
173     @Produces( MediaType.APPLICATION_JSON )
174     public Response doModifyStatus( @HeaderParam( GrustoragedbConstants.CONSTANT_X_API_KEY ) String strApiKey, String strStatus )
175     {
176         if( GrustoragedbConstants.PROPERTY_STATUS_REST_API_KEY.equals( strApiKey ) )
177         {
178             try
179             {
180                 Status status = GrustoragedbUtils.initMapper( ).readValue( strStatus, Status.class );
181     
182                 if ( isStatusCompleted( status ) && StatusHome.findByPrimaryKey( status.getId( ) ).isPresent( ) )
183                 {
184                     StatusHome.update( status );
185                     return Response.status( Response.Status.OK )
186                             .entity( JsonUtil.buildJsonResponse( new JsonResponse( status ) ) )
187                             .build( );
188                 }
189                 return Response.status( Response.Status.NOT_MODIFIED )
190                         .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_MODIFIED.name( ), GrustoragedbConstants.ALL_REQUIRED ) ) )
191                         .build( );
192     
193             } catch ( JsonProcessingException e )
194             {
195                 return Response.status( Response.Status.BAD_REQUEST )
196                         .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.BAD_REQUEST.getReasonPhrase( ) ) ) )
197                         .build( );
198             }
199         }
200         
201         return Response.status( Response.Status.UNAUTHORIZED )
202                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( GrustoragedbConstants.API_KEY_ERROR ) ) )
203                 .build( );
204     }
205 
206     /**
207      * Delete status
208      * @param nId
209      * @return ok if is deleted
210      */
211     @DELETE
212     @Path( GrustoragedbConstants.PATH_ID )
213     @Consumes( MediaType.APPLICATION_JSON )
214     @Produces( MediaType.APPLICATION_JSON )
215     public Response doDeleteStatus( @HeaderParam( GrustoragedbConstants.CONSTANT_X_API_KEY ) String strApiKey,
216             @PathParam( GrustoragedbConstants.PATH_PARAM_ID ) int nId )
217     {
218         if( GrustoragedbConstants.PROPERTY_STATUS_REST_API_KEY.equals( strApiKey ) )
219         {
220             if ( StatusHome.findByPrimaryKey( nId ).isPresent( ) )
221             {
222                 StatusHome.remove( nId );
223                 return Response.status( Response.Status.OK )
224                         .entity( JsonUtil.buildJsonResponse( new JsonResponse( Response.Status.OK ) ) )
225                         .build( );
226             }
227             return Response.status( Response.Status.NOT_FOUND )
228                     .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.getReasonPhrase( ) ) ) )
229                     .build( );
230         }
231         
232         return Response.status( Response.Status.UNAUTHORIZED )
233                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( GrustoragedbConstants.API_KEY_ERROR ) ) )
234                 .build( );      
235     }
236     
237     /**
238      * Gets list status
239      * @return list status
240      */
241     @GET( )
242     @Path( GrustoragedbConstants.PATH_GENERIC )
243     @Consumes( MediaType.APPLICATION_JSON )
244     @Produces( MediaType.APPLICATION_JSON )
245     public Response getListGenericStatus( @HeaderParam( GrustoragedbConstants.CONSTANT_X_API_KEY ) String strApiKey )
246     {
247         if( GrustoragedbConstants.PROPERTY_STATUS_REST_API_KEY.equals( strApiKey ) )
248         {
249             return Response.status( Response.Status.OK )
250                     .entity( JsonUtil.buildJsonResponse( new JsonResponse( GrustoragedbUtils.getEnumGenericStatusRefList( ) ) ) )
251                     .build( );
252         }
253         
254         return Response.status( Response.Status.UNAUTHORIZED )
255                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( GrustoragedbConstants.API_KEY_ERROR ) ) )
256                 .build( );       
257     }
258 
259     /**
260      * True if status is completed
261      * 
262      * @param status
263      * @return true if status is completed
264      */
265     private boolean isStatusCompleted( Status status )
266     {
267         return status != null && StringUtils.isNotEmpty( status.getStatus( ) ) && StringUtils.isNotEmpty( status.getCodeStatus( ) ) 
268                 && StringUtils.isNotEmpty( status.getLabelColorCode( ) ) && StringUtils.isNotEmpty( status.getBannerColorCode( ) );
269     }
270 
271 }