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.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
66
67
68 @Path( RestConstants.BASE_PATH + GrustoragedbConstants.PATH_STATUS_REST )
69 public class StatusRest
70 {
71
72
73
74
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
95
96
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
128
129
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
168
169
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
208
209
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
239
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
261
262
263
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 }