rsanchez
2014-10-23 c4d513ca26fe80946a5d90264de5d8e13e4ea974
securis/src/main/java/net/curisit/securis/services/PackResource.java
....@@ -30,9 +30,12 @@
3030 import net.curisit.securis.db.Organization;
3131 import net.curisit.securis.db.Pack;
3232 import net.curisit.securis.db.PackMetadata;
33
+import net.curisit.securis.db.PackStatus;
3334 import net.curisit.securis.db.User;
3435 import net.curisit.securis.security.BasicSecurityContext;
3536 import net.curisit.securis.security.Securable;
37
+import net.curisit.securis.services.exception.SeCurisServiceException;
38
+import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
3639 import net.curisit.securis.utils.TokenHelper;
3740
3841 import org.apache.logging.log4j.LogManager;
....@@ -154,11 +157,12 @@
154157
155158 User user = em.find(User.class, bsc.getUserPrincipal().getName());
156159
160
+ pack.setStatus(PackStatus.CREATED);
157161 pack.setCreatedBy(user);
158162 pack.setCreationTimestamp(new Date());
159163 em.persist(pack);
160
- Set<PackMetadata> newMD = pack.getMetadata();
161
-
164
+ Set<PackMetadata> newMD = pack.getMetadata();
165
+
162166 if (newMD != null) {
163167 for (PackMetadata md : newMD) {
164168 md.setPack(pack);
....@@ -215,12 +219,13 @@
215219
216220 em.persist(currentPack);
217221
218
- Set<PackMetadata> newMD = pack.getMetadata();
222
+ Set<PackMetadata> newMD = pack.getMetadata();
219223 for (PackMetadata currentMd : currentPack.getMetadata()) {
220
- if (newMD == null || !newMD.contains(currentMd));
221
- em.remove(currentMd);
224
+ if (newMD == null || !newMD.contains(currentMd))
225
+ ;
226
+ em.remove(currentMd);
222227 }
223
-
228
+
224229 if (newMD != null) {
225230 for (PackMetadata md : newMD) {
226231 md.setPack(currentPack);
....@@ -231,6 +236,84 @@
231236 return Response.ok(pack).build();
232237 }
233238
239
+ @POST
240
+ @Path("/{packId}/activate")
241
+ @Transactional
242
+ @Securable
243
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
244
+ @Consumes(MediaType.APPLICATION_JSON)
245
+ @Produces({
246
+ MediaType.APPLICATION_JSON
247
+ })
248
+ public Response activate(@PathParam("packId") Integer packId) throws SeCurisServiceException {
249
+ LOG.info("Activating pack with id: {}", packId);
250
+ EntityManager em = emProvider.get();
251
+
252
+ Pack currentPack = em.find(Pack.class, packId);
253
+
254
+ if (!Pack.Status.isActionValid(Pack.Action.ACTIVATION, currentPack.getStatus())) {
255
+ LOG.error("Pack with id {} cannot be activaed from status {}", packId, currentPack.getStatus().name());
256
+ throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be activated in status: " + currentPack.getStatus().name());
257
+ }
258
+
259
+ currentPack.setStatus(PackStatus.ACTIVE);
260
+ em.persist(currentPack);
261
+
262
+ return Response.ok(currentPack).build();
263
+ }
264
+
265
+ @POST
266
+ @Path("/{packId}/putonhold")
267
+ @Transactional
268
+ @Securable
269
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
270
+ @Consumes(MediaType.APPLICATION_JSON)
271
+ @Produces({
272
+ MediaType.APPLICATION_JSON
273
+ })
274
+ public Response onhold(@PathParam("packId") Integer packId) throws SeCurisServiceException {
275
+ LOG.info("Putting On hold pack with id: {}", packId);
276
+ EntityManager em = emProvider.get();
277
+
278
+ Pack currentPack = em.find(Pack.class, packId);
279
+
280
+ if (!Pack.Status.isActionValid(Pack.Action.PUT_ONHOLD, currentPack.getStatus())) {
281
+ LOG.error("Pack with id {} cannot be put on hold from status {}", packId, currentPack.getStatus().name());
282
+ throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be put on hold in status: " + currentPack.getStatus().name());
283
+ }
284
+
285
+ currentPack.setStatus(PackStatus.ON_HOLD);
286
+ em.persist(currentPack);
287
+
288
+ return Response.ok(currentPack).build();
289
+ }
290
+
291
+ @POST
292
+ @Path("/{packId}/cancel")
293
+ @Transactional
294
+ @Securable
295
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
296
+ @Consumes(MediaType.APPLICATION_JSON)
297
+ @Produces({
298
+ MediaType.APPLICATION_JSON
299
+ })
300
+ public Response cancel(@PathParam("packId") Integer packId) throws SeCurisServiceException {
301
+ LOG.info("Putting On hold pack with id: {}", packId);
302
+ EntityManager em = emProvider.get();
303
+
304
+ Pack currentPack = em.find(Pack.class, packId);
305
+
306
+ if (!Pack.Status.isActionValid(Pack.Action.CANCEL, currentPack.getStatus())) {
307
+ LOG.error("Pack with id {} cannot cancelled from status {}", packId, currentPack.getStatus().name());
308
+ throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be cancelled in status: " + currentPack.getStatus().name());
309
+ }
310
+
311
+ currentPack.setStatus(PackStatus.CANCELLED);
312
+ em.persist(currentPack);
313
+
314
+ return Response.ok(currentPack).build();
315
+ }
316
+
234317 private void setPackOrganization(Pack currentPack, Integer orgId, EntityManager em) throws SeCurisException {
235318 Organization org = null;
236319 if (orgId != null) {