From c4d513ca26fe80946a5d90264de5d8e13e4ea974 Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Thu, 23 Oct 2014 17:21:24 +0000
Subject: [PATCH] #2021 feature - Added pack actions in server and in frontend

---
 securis/src/main/java/net/curisit/securis/services/PackResource.java |   95 ++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 89 insertions(+), 6 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/services/PackResource.java b/securis/src/main/java/net/curisit/securis/services/PackResource.java
index 9205945..e876cb5 100644
--- a/securis/src/main/java/net/curisit/securis/services/PackResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/PackResource.java
@@ -30,9 +30,12 @@
 import net.curisit.securis.db.Organization;
 import net.curisit.securis.db.Pack;
 import net.curisit.securis.db.PackMetadata;
+import net.curisit.securis.db.PackStatus;
 import net.curisit.securis.db.User;
 import net.curisit.securis.security.BasicSecurityContext;
 import net.curisit.securis.security.Securable;
+import net.curisit.securis.services.exception.SeCurisServiceException;
+import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
 import net.curisit.securis.utils.TokenHelper;
 
 import org.apache.logging.log4j.LogManager;
@@ -154,11 +157,12 @@
 
         User user = em.find(User.class, bsc.getUserPrincipal().getName());
 
+        pack.setStatus(PackStatus.CREATED);
         pack.setCreatedBy(user);
         pack.setCreationTimestamp(new Date());
         em.persist(pack);
-        Set<PackMetadata> newMD = pack.getMetadata(); 
-        
+        Set<PackMetadata> newMD = pack.getMetadata();
+
         if (newMD != null) {
             for (PackMetadata md : newMD) {
                 md.setPack(pack);
@@ -215,12 +219,13 @@
 
         em.persist(currentPack);
 
-        Set<PackMetadata> newMD = pack.getMetadata(); 
+        Set<PackMetadata> newMD = pack.getMetadata();
         for (PackMetadata currentMd : currentPack.getMetadata()) {
-            if (newMD == null || !newMD.contains(currentMd));
-                em.remove(currentMd);
+            if (newMD == null || !newMD.contains(currentMd))
+                ;
+            em.remove(currentMd);
         }
-        
+
         if (newMD != null) {
             for (PackMetadata md : newMD) {
                 md.setPack(currentPack);
@@ -231,6 +236,84 @@
         return Response.ok(pack).build();
     }
 
+    @POST
+    @Path("/{packId}/activate")
+    @Transactional
+    @Securable
+    @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces({
+        MediaType.APPLICATION_JSON
+    })
+    public Response activate(@PathParam("packId") Integer packId) throws SeCurisServiceException {
+        LOG.info("Activating pack with id: {}", packId);
+        EntityManager em = emProvider.get();
+
+        Pack currentPack = em.find(Pack.class, packId);
+
+        if (!Pack.Status.isActionValid(Pack.Action.ACTIVATION, currentPack.getStatus())) {
+            LOG.error("Pack with id {} cannot be activaed from status {}", packId, currentPack.getStatus().name());
+            throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be activated in status: " + currentPack.getStatus().name());
+        }
+
+        currentPack.setStatus(PackStatus.ACTIVE);
+        em.persist(currentPack);
+
+        return Response.ok(currentPack).build();
+    }
+
+    @POST
+    @Path("/{packId}/putonhold")
+    @Transactional
+    @Securable
+    @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces({
+        MediaType.APPLICATION_JSON
+    })
+    public Response onhold(@PathParam("packId") Integer packId) throws SeCurisServiceException {
+        LOG.info("Putting On hold pack with id: {}", packId);
+        EntityManager em = emProvider.get();
+
+        Pack currentPack = em.find(Pack.class, packId);
+
+        if (!Pack.Status.isActionValid(Pack.Action.PUT_ONHOLD, currentPack.getStatus())) {
+            LOG.error("Pack with id {} cannot be put on hold from status {}", packId, currentPack.getStatus().name());
+            throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be put on hold in status: " + currentPack.getStatus().name());
+        }
+
+        currentPack.setStatus(PackStatus.ON_HOLD);
+        em.persist(currentPack);
+
+        return Response.ok(currentPack).build();
+    }
+
+    @POST
+    @Path("/{packId}/cancel")
+    @Transactional
+    @Securable
+    @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces({
+        MediaType.APPLICATION_JSON
+    })
+    public Response cancel(@PathParam("packId") Integer packId) throws SeCurisServiceException {
+        LOG.info("Putting On hold pack with id: {}", packId);
+        EntityManager em = emProvider.get();
+
+        Pack currentPack = em.find(Pack.class, packId);
+
+        if (!Pack.Status.isActionValid(Pack.Action.CANCEL, currentPack.getStatus())) {
+            LOG.error("Pack with id {} cannot cancelled from status {}", packId, currentPack.getStatus().name());
+            throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be cancelled in status: " + currentPack.getStatus().name());
+        }
+
+        currentPack.setStatus(PackStatus.CANCELLED);
+        em.persist(currentPack);
+
+        return Response.ok(currentPack).build();
+    }
+
     private void setPackOrganization(Pack currentPack, Integer orgId, EntityManager em) throws SeCurisException {
         Organization org = null;
         if (orgId != null) {

--
Gitblit v1.3.2