From 8d5386be38db25a2a41c3bf6c876adee21ca26cc Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Fri, 19 Sep 2014 08:26:02 +0000
Subject: [PATCH] #396 fix - Fixed more SonarQube issues

---
 securis/src/main/java/net/curisit/securis/services/PackResource.java |  327 ++++++++++++++++++++++++++---------------------------
 1 files changed, 161 insertions(+), 166 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 03978de..fa4291a 100644
--- a/securis/src/main/java/net/curisit/securis/services/PackResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/PackResource.java
@@ -40,202 +40,197 @@
 import com.google.inject.persist.Transactional;
 
 /**
- * Pack resource, this service will provide methods to create, modify and delete packs
+ * Pack resource, this service will provide methods to create, modify and delete
+ * packs
  * 
  * @author roberto <roberto.sanchez@curisit.net>
  */
 @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;
+    @Inject
+    TokenHelper tokenHelper;
 
-	@Inject
-	Provider<EntityManager> emProvider;
+    @Inject
+    Provider<EntityManager> emProvider;
 
-	public PackResource() {
-	}
+    public PackResource() {}
 
-	/**
-	 * 
-	 * @return the server version in format majorVersion.minorVersion
-	 */
-	@GET
-	@Path("/")
-	@Securable
-	@Produces(
-		{ MediaType.APPLICATION_JSON })
-	public Response index(@Context BasicSecurityContext bsc) {
-		LOG.info("Getting packs list ");
+    /**
+     * 
+     * @return the server version in format majorVersion.minorVersion
+     */
+    @GET
+    @Path("/")
+    @Securable
+    @Produces({ MediaType.APPLICATION_JSON })
+    public Response index(@Context BasicSecurityContext bsc) {
+        LOG.info("Getting packs list ");
 
-		EntityManager em = emProvider.get();
-		// TypedQuery<Pack> q = em.createNamedQuery("list-packs-by-orgs", Pack.class);
+        EntityManager em = emProvider.get();
 
-		TypedQuery<Pack> q;
-		if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
-			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);
-			if (bsc.getOrganizationsIds() == null)
-				Response.ok().build();
-			q.setParameter("list_ids", bsc.getOrganizationsIds());
-		}
+        TypedQuery<Pack> q;
+        if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
+            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);
+            if (bsc.getOrganizationsIds() == null) {
+                Response.ok().build();
+            }
+            q.setParameter("list_ids", bsc.getOrganizationsIds());
+        }
 
-		List<Pack> list = q.getResultList();
+        List<Pack> list = q.getResultList();
 
-		return Response.ok(list).build();
-	}
+        return Response.ok(list).build();
+    }
 
-	private Response generateErrorUnathorizedAccess(Pack pack, Principal 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();
-	}
+    private Response generateErrorUnathorizedAccess(Pack pack, Principal 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();
+    }
 
-	/**
-	 * 
-	 * @return the server version in format majorVersion.minorVersion
-	 */
-	@GET
-	@Path("/{packId}")
-	@Securable
-	@Produces(
-		{ MediaType.APPLICATION_JSON })
-	public Response get(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) {
-		LOG.info("Getting pack data for id: {}: ", packId);
-		if (packId == null || packId.equals("")) {
-			LOG.error("Pack ID is mandatory");
-			return Response.status(Status.NOT_FOUND).build();
-		}
+    /**
+     * 
+     * @return the server version in format majorVersion.minorVersion
+     */
+    @GET
+    @Path("/{packId}")
+    @Securable
+    @Produces({ MediaType.APPLICATION_JSON })
+    public Response get(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) {
+        LOG.info("Getting pack data for id: {}: ", packId);
+        if (packId == null || "".equals(packId)) {
+            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);
-			return Response.status(Status.NOT_FOUND).build();
-		}
-		if (bsc.isUserInRole(BasicSecurityContext.ROL_ADVANCE)) {
-			if (bsc.getOrganizationsIds() == null || !bsc.getOrganizationsIds().contains(pack.getOrgId())) {
-				return generateErrorUnathorizedAccess(pack, bsc.getUserPrincipal());
-			}
-		}
-		return Response.ok(pack).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);
+            return Response.status(Status.NOT_FOUND).build();
+        }
+        if (bsc.isUserInRole(BasicSecurityContext.ROL_ADVANCE)) {
+            if (bsc.getOrganizationsIds() == null || !bsc.getOrganizationsIds().contains(pack.getOrgId())) {
+                return generateErrorUnathorizedAccess(pack, bsc.getUserPrincipal());
+            }
+        }
+        return Response.ok(pack).build();
+    }
 
-	@POST
-	@Path("/")
-	@Securable
-	@RolesAllowed(BasicSecurityContext.ROL_ADMIN)
-	@Consumes(MediaType.APPLICATION_JSON)
-	@Produces(
-		{ MediaType.APPLICATION_JSON })
-	@Transactional
-	public Response create(Pack pack, @Context BasicSecurityContext bsc) {
-		LOG.info("Creating new pack");
-		EntityManager em = emProvider.get();
+    @POST
+    @Path("/")
+    @Securable
+    @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces({ MediaType.APPLICATION_JSON })
+    @Transactional
+    public Response create(Pack pack, @Context BasicSecurityContext bsc) {
+        LOG.info("Creating new pack");
+        EntityManager em = emProvider.get();
 
-		try {
-			setPackOrganization(pack, pack.getOrgId(), em);
-		} catch (SeCurisException e) {
-			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
-		}
+        try {
+            setPackOrganization(pack, pack.getOrgId(), em);
+        } catch (SeCurisException e) {
+            return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
+        }
 
-		try {
-			setPackLicenseType(pack, pack.getLicTypeId(), em);
-		} catch (SeCurisException e) {
-			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).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());
+        User user = em.find(User.class, bsc.getUserPrincipal().getName());
 
-		pack.setCreatedBy(user);
-		pack.setCreationTimestamp(new Date());
-		em.persist(pack);
+        pack.setCreatedBy(user);
+        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);
-	}
+        return Response.ok(pack).build();
+    }
 
-	@PUT
-	@POST
-	@Path("/{packId}")
-	@Transactional
-	@Securable
-	@RolesAllowed(BasicSecurityContext.ROL_ADMIN)
-	@Consumes(MediaType.APPLICATION_JSON)
-	@Produces(
-		{ MediaType.APPLICATION_JSON })
-	public Response modify(Pack pack, @PathParam("packId") Integer packId) {
-		LOG.info("Modifying pack with id: {}", packId);
-		EntityManager em = emProvider.get();
-		Pack currentPack = em.find(Pack.class, packId);
+    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);
+            }
+        }
+        pack.setLicenseType(lt);
+    }
 
-		try {
-			setPackOrganization(currentPack, pack.getOrgId(), em);
-		} catch (SeCurisException e) {
-			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
-		}
+    @PUT
+    @POST
+    @Path("/{packId}")
+    @Transactional
+    @Securable
+    @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces({ MediaType.APPLICATION_JSON })
+    public Response modify(Pack pack, @PathParam("packId") Integer packId) {
+        LOG.info("Modifying pack with id: {}", packId);
+        EntityManager em = emProvider.get();
+        Pack currentPack = em.find(Pack.class, packId);
 
-		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.setCode(pack.getCode());
-		currentPack.setComments(pack.getComments());
-		currentPack.setNumLicenses(pack.getNumLicenses());
+        try {
+            setPackOrganization(currentPack, pack.getOrgId(), em);
+        } catch (SeCurisException e) {
+            return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
+        }
 
-		em.persist(currentPack);
+        try {
+            setPackLicenseType(currentPack, pack.getLicTypeId(), em);
+        } catch (SeCurisException e) {
+            return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
+        }
 
-		return Response.ok(pack).build();
-	}
+        currentPack.setLicensePreactivation(pack.isLicensePreactivation());
+        currentPack.setCode(pack.getCode());
+        currentPack.setComments(pack.getComments());
+        currentPack.setNumLicenses(pack.getNumLicenses());
 
-	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);
-	}
+        em.persist(currentPack);
 
-	@DELETE
-	@Path("/{packId}")
-	@Securable
-	@RolesAllowed(BasicSecurityContext.ROL_ADMIN)
-	@Transactional
-	@Produces(
-		{ MediaType.APPLICATION_JSON })
-	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));
-		if (org == null) {
-			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();
-		}
+        return Response.ok(pack).build();
+    }
 
-		em.remove(org);
-		return Response.ok(Utils.createMap("success", true, "id", packId)).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
+    @Path("/{packId}")
+    @Securable
+    @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+    @Transactional
+    @Produces({ MediaType.APPLICATION_JSON })
+    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));
+        if (org == null) {
+            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();
+        }
+
+        em.remove(org);
+        return Response.ok(Utils.createMap("success", true, "id", packId)).build();
+    }
 
 }

--
Gitblit v1.3.2