From 1a0d1f15efa2b4cbdc6dd30b5a85b111d0599b63 Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Wed, 22 Jan 2014 20:59:13 +0000
Subject: [PATCH] #396 feature - Added session timeout and responsive layour untill 1600px

---
 securis/src/main/java/net/curisit/securis/services/PackResource.java |   59 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 52 insertions(+), 7 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 c6927a2..76adfa1 100644
--- a/securis/src/main/java/net/curisit/securis/services/PackResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/PackResource.java
@@ -9,11 +9,9 @@
 import javax.inject.Provider;
 import javax.persistence.EntityManager;
 import javax.persistence.TypedQuery;
-import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
-import javax.ws.rs.HeaderParam;
 import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
@@ -26,7 +24,10 @@
 
 import net.curisit.integrity.commons.Utils;
 import net.curisit.securis.DefaultExceptionHandler;
+import net.curisit.securis.db.LicenseType;
+import net.curisit.securis.db.Organization;
 import net.curisit.securis.db.Pack;
+import net.curisit.securis.db.User;
 import net.curisit.securis.security.BasicSecurityContext;
 import net.curisit.securis.security.Securable;
 import net.curisit.securis.utils.TokenHelper;
@@ -78,7 +79,6 @@
 			q = em.createNamedQuery("list-packs-by-orgs", Pack.class);
 			if (bsc.getOrganizationsIds() == null)
 				Response.ok().build();
-			// log.info("Getting only {} orgs for user: {}", securityContext.getOrganizationsIds(), securityContext.getUserPrincipal());
 			q.setParameter("list_ids", bsc.getOrganizationsIds());
 		}
 
@@ -130,10 +130,32 @@
 	@Produces(
 		{ MediaType.APPLICATION_JSON })
 	@Transactional
-	public Response create(Pack pack, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
+	public Response create(Pack pack, @Context BasicSecurityContext bsc) {
 		log.info("Creating new pack");
 		EntityManager em = emProvider.get();
 
+		Organization org = null;
+		if (pack.getOrgId() != null) {
+			org = em.find(Organization.class, pack.getOrgId());
+			if (org == null) {
+				log.error("Organization pack with id {} not found in DB", pack.getOrgId());
+				return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build();
+			}
+		}
+		LicenseType lt = null;
+		if (pack.getLicTypeId() != null) {
+			lt = em.find(LicenseType.class, pack.getLicTypeId());
+			if (lt == null) {
+				log.error("Pack license type with id {} not found in DB", pack.getLicTypeId());
+				return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build();
+			}
+		}
+
+		User user = em.find(User.class, bsc.getUserPrincipal().getName());
+
+		pack.setCreatedBy(user);
+		pack.setLicenseType(lt);
+		pack.setOrganization(org);
 		pack.setCreationTimestamp(new Date());
 		em.persist(pack);
 
@@ -149,11 +171,34 @@
 	@Consumes(MediaType.APPLICATION_JSON)
 	@Produces(
 		{ MediaType.APPLICATION_JSON })
-	public Response modify(Pack pack, @PathParam("packId") String packId, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
+	public Response modify(Pack pack, @PathParam("packId") String packId) {
 		log.info("Modifying pack with id: {}", packId);
 		EntityManager em = emProvider.get();
+		Pack currentPack = em.find(Pack.class, Integer.parseInt(packId));
 
-		em.persist(pack);
+		Organization org = null;
+		if (pack.getOrgId() != null) {
+			org = em.find(Organization.class, pack.getOrgId());
+			if (org == null) {
+				log.error("Organization pack with id {} not found in DB", pack.getOrgId());
+				return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build();
+			}
+		}
+		LicenseType lt = null;
+		if (pack.getLicTypeId() != null) {
+			lt = em.find(LicenseType.class, pack.getLicTypeId());
+			if (lt == null) {
+				log.error("Pack license type with id {} not found in DB", pack.getLicTypeId());
+				return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build();
+			}
+		}
+		currentPack.setLicenseType(lt);
+		currentPack.setOrganization(org);
+		currentPack.setCode(pack.getCode());
+		currentPack.setComments(pack.getComments());
+		currentPack.setNumLicenses(pack.getNumLicenses());
+
+		em.persist(currentPack);
 
 		return Response.ok(pack).build();
 	}
@@ -165,7 +210,7 @@
 	@Transactional
 	@Produces(
 		{ MediaType.APPLICATION_JSON })
-	public Response delete(@PathParam("packId") String packId, @Context HttpServletRequest request) {
+	public Response delete(@PathParam("packId") String packId) {
 		log.info("Deleting pack with id: {}", packId);
 		EntityManager em = emProvider.get();
 		Pack org = em.find(Pack.class, Integer.parseInt(packId));

--
Gitblit v1.3.2