View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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  
35  package fr.paris.lutece.plugins.grustoragedb.modules.broadcast.rs;
36  
37  import fr.paris.lutece.plugins.grustoragedb.modules.broadcast.business.Subscription;
38  import fr.paris.lutece.plugins.grustoragedb.modules.broadcast.business.SubscriptionHome;
39  import fr.paris.lutece.plugins.rest.service.RestConstants;
40  import fr.paris.lutece.util.json.ErrorJsonResponse;
41  import fr.paris.lutece.util.json.JsonResponse;
42  import fr.paris.lutece.util.json.JsonUtil;
43  
44  import org.apache.commons.lang3.StringUtils;
45  import org.apache.log4j.Logger;
46  import java.util.List;
47  import javax.ws.rs.FormParam;
48  import javax.ws.rs.GET;
49  import javax.ws.rs.POST;
50  import javax.ws.rs.PUT;
51  import javax.ws.rs.DELETE;
52  import javax.ws.rs.Path;
53  import javax.ws.rs.PathParam;
54  import javax.ws.rs.Produces;
55  import javax.ws.rs.core.MediaType;
56  import javax.ws.rs.core.Response;
57  
58  /**
59   * SubscriptionRest
60   */
61  @Path( RestConstants.BASE_PATH + Constants.API_PATH + Constants.VERSION_PATH + Constants.SUBSCRIPTION_PATH )
62  public class SubscriptionRest
63  {
64      private static final int VERSION_1 = 1;
65      private final Logger _logger = Logger.getLogger( RestConstants.REST_LOGGER );
66  
67      /**
68       * Get Subscription List
69       * 
70       * @param nVersion
71       *            the API version
72       * @return the Subscription List
73       */
74      @GET
75      @Path( StringUtils.EMPTY )
76      @Produces( MediaType.APPLICATION_JSON )
77      public Response getSubscriptionList( @PathParam( Constants.VERSION ) Integer nVersion )
78      {
79          if ( nVersion == VERSION_1 )
80          {
81              return getSubscriptionListV1( );
82          }
83          _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
84          return Response.status( Response.Status.NOT_FOUND )
85                  .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) ).build( );
86      }
87  
88      /**
89       * Get Subscription List V1
90       * 
91       * @return the Subscription List for the version 1
92       */
93      private Response getSubscriptionListV1( )
94      {
95          List<Subscription> listSubscriptions = SubscriptionHome.getSubscriptionsList( );
96  
97          if ( listSubscriptions.isEmpty( ) )
98          {
99              return Response.status( Response.Status.NO_CONTENT ).entity( JsonUtil.buildJsonResponse( new JsonResponse( Constants.EMPTY_OBJECT ) ) ).build( );
100         }
101         return Response.status( Response.Status.OK ).entity( JsonUtil.buildJsonResponse( new JsonResponse( listSubscriptions ) ) ).build( );
102     }
103 
104     /**
105      * Create Subscription
106      * 
107      * @param nVersion
108      *            the API version
109      * @param demand_type_id
110      *            the demand_type_id
111      * @param mail
112      *            the mail
113      * @param frequency
114      *            the frequency
115      * @return the Subscription if created
116      */
117     @POST
118     @Path( StringUtils.EMPTY )
119     @Produces( MediaType.APPLICATION_JSON )
120     public Response createSubscription( @FormParam( Constants.SUBSCRIPTION_ATTRIBUTE_DEMAND_TYPE_ID ) String demand_type_id,
121             @FormParam( Constants.SUBSCRIPTION_ATTRIBUTE_MAIL ) String mail, @FormParam( Constants.SUBSCRIPTION_ATTRIBUTE_FREQUENCY ) String frequency,
122             @PathParam( Constants.VERSION ) Integer nVersion )
123     {
124         if ( nVersion == VERSION_1 )
125         {
126             return createSubscriptionV1( demand_type_id, mail, frequency );
127         }
128         _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
129         return Response.status( Response.Status.NOT_FOUND )
130                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) ).build( );
131     }
132 
133     /**
134      * Create Subscription V1
135      * 
136      * @param demand_type_id
137      *            the demand_type_id
138      * @param mail
139      *            the mail
140      * @param frequency
141      *            the frequency
142      * @return the Subscription if created for the version 1
143      */
144     private Response createSubscriptionV1( String demand_type_id, String mail, String frequency )
145     {
146         if ( StringUtils.isEmpty( demand_type_id ) || StringUtils.isEmpty( mail ) || StringUtils.isEmpty( frequency ) )
147         {
148             _logger.error( Constants.ERROR_BAD_REQUEST_EMPTY_PARAMETER );
149             return Response.status( Response.Status.BAD_REQUEST )
150                     .entity( JsonUtil
151                             .buildJsonResponse( new ErrorJsonResponse( Response.Status.BAD_REQUEST.name( ), Constants.ERROR_BAD_REQUEST_EMPTY_PARAMETER ) ) )
152                     .build( );
153         }
154 
155         Subscriptiontoragedb/modules/broadcast/business/Subscription.html#Subscription">Subscription subscription = new Subscription( );
156         subscription.setDemandTypeId( Integer.parseInt( demand_type_id ) );
157         subscription.setMail( mail );
158         subscription.setFrequency( Integer.parseInt( frequency ) );
159         SubscriptionHome.create( subscription );
160 
161         return Response.status( Response.Status.OK ).entity( JsonUtil.buildJsonResponse( new JsonResponse( subscription ) ) ).build( );
162     }
163 
164     /**
165      * Modify Subscription
166      * 
167      * @param nVersion
168      *            the API version
169      * @param id
170      *            the id
171      * @param demand_type_id
172      *            the demand_type_id
173      * @param mail
174      *            the mail
175      * @param frequency
176      *            the frequency
177      * @return the Subscription if modified
178      */
179     @PUT
180     @Path( Constants.ID_PATH )
181     @Produces( MediaType.APPLICATION_JSON )
182     public Response modifySubscription( @PathParam( Constants.ID ) Integer id,
183             @FormParam( Constants.SUBSCRIPTION_ATTRIBUTE_DEMAND_TYPE_ID ) String demand_type_id,
184             @FormParam( Constants.SUBSCRIPTION_ATTRIBUTE_MAIL ) String mail, @FormParam( Constants.SUBSCRIPTION_ATTRIBUTE_FREQUENCY ) String frequency,
185             @PathParam( Constants.VERSION ) Integer nVersion )
186     {
187         if ( nVersion == VERSION_1 )
188         {
189             return modifySubscriptionV1( id, demand_type_id, mail, frequency );
190         }
191         _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
192         return Response.status( Response.Status.NOT_FOUND )
193                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) ).build( );
194     }
195 
196     /**
197      * Modify Subscription V1
198      * 
199      * @param id
200      *            the id
201      * @param demand_type_id
202      *            the demand_type_id
203      * @param mail
204      *            the mail
205      * @param frequency
206      *            the frequency
207      * @return the Subscription if modified for the version 1
208      */
209     private Response modifySubscriptionV1( Integer id, String demand_type_id, String mail, String frequency )
210     {
211         if ( StringUtils.isEmpty( demand_type_id ) || StringUtils.isEmpty( mail ) || StringUtils.isEmpty( frequency ) )
212         {
213             _logger.error( Constants.ERROR_BAD_REQUEST_EMPTY_PARAMETER );
214             return Response.status( Response.Status.BAD_REQUEST )
215                     .entity( JsonUtil
216                             .buildJsonResponse( new ErrorJsonResponse( Response.Status.BAD_REQUEST.name( ), Constants.ERROR_BAD_REQUEST_EMPTY_PARAMETER ) ) )
217                     .build( );
218         }
219 
220         Subscription subscription = SubscriptionHome.findByPrimaryKey( id );
221         if ( subscription == null )
222         {
223             _logger.error( Constants.ERROR_NOT_FOUND_RESOURCE );
224             return Response.status( Response.Status.NOT_FOUND )
225                     .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_RESOURCE ) ) )
226                     .build( );
227         }
228 
229         subscription.setDemandTypeId( Integer.parseInt( demand_type_id ) );
230         subscription.setMail( mail );
231         subscription.setFrequency( Integer.parseInt( frequency ) );
232         SubscriptionHome.update( subscription );
233 
234         return Response.status( Response.Status.OK ).entity( JsonUtil.buildJsonResponse( new JsonResponse( subscription ) ) ).build( );
235     }
236 
237     /**
238      * Delete Subscription
239      * 
240      * @param nVersion
241      *            the API version
242      * @param id
243      *            the id
244      * @return the Subscription List if deleted
245      */
246     @DELETE
247     @Path( Constants.ID_PATH )
248     @Produces( MediaType.APPLICATION_JSON )
249     public Response deleteSubscription( @PathParam( Constants.VERSION ) Integer nVersion, @PathParam( Constants.ID ) Integer id )
250     {
251         if ( nVersion == VERSION_1 )
252         {
253             return deleteSubscriptionV1( id );
254         }
255         _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
256         return Response.status( Response.Status.NOT_FOUND )
257                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) ).build( );
258     }
259 
260     /**
261      * Delete Subscription V1
262      * 
263      * @param id
264      *            the id
265      * @return the Subscription List if deleted for the version 1
266      */
267     private Response deleteSubscriptionV1( Integer id )
268     {
269         Subscription subscription = SubscriptionHome.findByPrimaryKey( id );
270         if ( subscription == null )
271         {
272             _logger.error( Constants.ERROR_NOT_FOUND_RESOURCE );
273             return Response.status( Response.Status.NOT_FOUND )
274                     .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_RESOURCE ) ) )
275                     .build( );
276         }
277 
278         SubscriptionHome.remove( id );
279 
280         return Response.status( Response.Status.OK ).entity( JsonUtil.buildJsonResponse( new JsonResponse( Constants.EMPTY_OBJECT ) ) ).build( );
281     }
282 
283     /**
284      * Get Subscription
285      * 
286      * @param nVersion
287      *            the API version
288      * @param id
289      *            the id
290      * @return the Subscription
291      */
292     @GET
293     @Path( Constants.ID_PATH )
294     @Produces( MediaType.APPLICATION_JSON )
295     public Response getSubscription( @PathParam( Constants.VERSION ) Integer nVersion, @PathParam( Constants.ID ) Integer id )
296     {
297         if ( nVersion == VERSION_1 )
298         {
299             return getSubscriptionV1( id );
300         }
301         _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
302         return Response.status( Response.Status.NOT_FOUND )
303                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) ).build( );
304     }
305 
306     /**
307      * Get Subscription V1
308      * 
309      * @param id
310      *            the id
311      * @return the Subscription for the version 1
312      */
313     private Response getSubscriptionV1( Integer id )
314     {
315         Subscription subscription = SubscriptionHome.findByPrimaryKey( id );
316         if ( subscription == null )
317         {
318             _logger.error( Constants.ERROR_NOT_FOUND_RESOURCE );
319             return Response.status( Response.Status.NOT_FOUND )
320                     .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_RESOURCE ) ) )
321                     .build( );
322         }
323 
324         return Response.status( Response.Status.OK ).entity( JsonUtil.buildJsonResponse( new JsonResponse( subscription ) ) ).build( );
325     }
326 }