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.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
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
69
70
71
72
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
90
91
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
106
107
108
109
110
111
112
113
114
115
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
135
136
137
138
139
140
141
142
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
166
167
168
169
170
171
172
173
174
175
176
177
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
198
199
200
201
202
203
204
205
206
207
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
239
240
241
242
243
244
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
262
263
264
265
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
285
286
287
288
289
290
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
308
309
310
311
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 }