From 52ce72b22ef8d92a1f35b4993bcddaaa66d67350 Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Thu, 18 Sep 2014 17:55:35 +0000
Subject: [PATCH] #396 fix - Fixed some SonarQube issues

---
 securis/src/main/java/net/curisit/securis/services/PackResource.java |  100 ++++++++++++++++++++++++++++---------------------
 1 files changed, 57 insertions(+), 43 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 82c2dd8..03978de 100644
--- a/securis/src/main/java/net/curisit/securis/services/PackResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/PackResource.java
@@ -5,6 +5,7 @@
 import java.util.List;
 
 import javax.annotation.security.RolesAllowed;
+import javax.crypto.SealedObject;
 import javax.inject.Inject;
 import javax.inject.Provider;
 import javax.persistence.EntityManager;
@@ -24,6 +25,7 @@
 
 import net.curisit.integrity.commons.Utils;
 import net.curisit.securis.DefaultExceptionHandler;
+import net.curisit.securis.SeCurisException;
 import net.curisit.securis.db.LicenseType;
 import net.curisit.securis.db.Organization;
 import net.curisit.securis.db.Pack;
@@ -45,7 +47,7 @@
 @Path("/pack")
 public class PackResource {
 
-	private static final Logger log = LogManager.getLogger(PackResource.class);
+	private static final Logger LOG = LogManager.getLogger(PackResource.class);
 
 	@Inject
 	TokenHelper tokenHelper;
@@ -66,14 +68,14 @@
 	@Produces(
 		{ MediaType.APPLICATION_JSON })
 	public Response index(@Context BasicSecurityContext bsc) {
-		log.info("Getting packs list ");
+		LOG.info("Getting packs list ");
 
 		EntityManager em = emProvider.get();
 		// TypedQuery<Pack> q = em.createNamedQuery("list-packs-by-orgs", Pack.class);
 
 		TypedQuery<Pack> q;
 		if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
-			log.info("Getting all packs for user: " + bsc.getUserPrincipal());
+			LOG.info("Getting all packs for user: " + bsc.getUserPrincipal());
 			q = em.createNamedQuery("list-packs", Pack.class);
 		} else {
 			q = em.createNamedQuery("list-packs-by-orgs", Pack.class);
@@ -88,7 +90,7 @@
 	}
 
 	private Response generateErrorUnathorizedAccess(Pack pack, Principal user) {
-		log.error("Pack with id {} not accesible by user {}", pack, user);
+		LOG.error("Pack with id {} not accesible by user {}", pack, user);
 		return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack").build();
 	}
 
@@ -102,16 +104,16 @@
 	@Produces(
 		{ MediaType.APPLICATION_JSON })
 	public Response get(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) {
-		log.info("Getting pack data for id: {}: ", packId);
+		LOG.info("Getting pack data for id: {}: ", packId);
 		if (packId == null || packId.equals("")) {
-			log.error("Pack ID is mandatory");
+			LOG.error("Pack ID is mandatory");
 			return Response.status(Status.NOT_FOUND).build();
 		}
 
 		EntityManager em = emProvider.get();
 		Pack pack = em.find(Pack.class, packId);
 		if (pack == null) {
-			log.error("Pack with id {} not found in DB", packId);
+			LOG.error("Pack with id {} not found in DB", packId);
 			return Response.status(Status.NOT_FOUND).build();
 		}
 		if (bsc.isUserInRole(BasicSecurityContext.ROL_ADVANCE)) {
@@ -131,35 +133,41 @@
 		{ MediaType.APPLICATION_JSON })
 	@Transactional
 	public Response create(Pack pack, @Context BasicSecurityContext bsc) {
-		log.info("Creating new pack");
+		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();
-			}
+		try {
+			setPackOrganization(pack, pack.getOrgId(), em);
+		} catch (SeCurisException e) {
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).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();
-			}
+
+		try {
+			setPackLicenseType(pack, pack.getLicTypeId(), em);
+		} catch (SeCurisException e) {
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).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);
 
 		return Response.ok(pack).build();
+	}
+	
+	private void setPackLicenseType(Pack pack, Integer licTypeId, EntityManager em) throws SeCurisException {
+		LicenseType lt = null;
+		if (licTypeId != null) {
+			lt = em.find(LicenseType.class, pack.getLicTypeId());
+			if (lt == null) {
+				LOG.error("Pack license type with id {} not found in DB", licTypeId);
+				throw new SeCurisException("Pack license type not found with ID: " + licTypeId);
+//				return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build();
+			}
+		}
+		pack.setLicenseType(lt);
 	}
 
 	@PUT
@@ -172,29 +180,23 @@
 	@Produces(
 		{ MediaType.APPLICATION_JSON })
 	public Response modify(Pack pack, @PathParam("packId") Integer packId) {
-		log.info("Modifying pack with id: {}", packId);
+		LOG.info("Modifying pack with id: {}", packId);
 		EntityManager em = emProvider.get();
 		Pack currentPack = em.find(Pack.class, packId);
 
-		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();
-			}
+		try {
+			setPackOrganization(currentPack, pack.getOrgId(), em);
+		} catch (SeCurisException e) {
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).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();
-			}
+
+		try {
+			setPackLicenseType(currentPack, pack.getLicTypeId(), em);
+		} catch (SeCurisException e) {
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
 		}
+		
 		currentPack.setLicensePreactivation(pack.isLicensePreactivation());
-		currentPack.setLicenseType(lt);
-		currentPack.setOrganization(org);
 		currentPack.setCode(pack.getCode());
 		currentPack.setComments(pack.getComments());
 		currentPack.setNumLicenses(pack.getNumLicenses());
@@ -202,6 +204,18 @@
 		em.persist(currentPack);
 
 		return Response.ok(pack).build();
+	}
+
+	private void setPackOrganization(Pack currentPack, Integer orgId, EntityManager em) throws SeCurisException {
+		Organization org = null;
+		if (orgId != null) {
+			org = em.find(Organization.class, orgId);
+			if (org == null) {
+				LOG.error("Organization pack with id {} not found in DB", orgId);
+				throw new SeCurisException("Pack organization not found with ID: " + orgId);
+			}
+		}
+		currentPack.setOrganization(org);
 	}
 
 	@DELETE
@@ -212,11 +226,11 @@
 	@Produces(
 		{ MediaType.APPLICATION_JSON })
 	public Response delete(@PathParam("packId") String packId) {
-		log.info("Deleting pack with id: {}", packId);
+		LOG.info("Deleting pack with id: {}", packId);
 		EntityManager em = emProvider.get();
 		Pack org = em.find(Pack.class, Integer.parseInt(packId));
 		if (org == null) {
-			log.error("Pack with id {} can not be deleted, It was not found in DB", packId);
+			LOG.error("Pack with id {} can not be deleted, It was not found in DB", packId);
 			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack was not found, ID: " + packId).build();
 		}
 

--
Gitblit v1.3.2