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/LicenseTypeResource.java |   67 +++++++++++++++++++--------------
 1 files changed, 39 insertions(+), 28 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java b/securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
index 45dedbe..9218b96 100644
--- a/securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
@@ -24,6 +24,7 @@
 
 import net.curisit.integrity.commons.Utils;
 import net.curisit.securis.DefaultExceptionHandler;
+import net.curisit.securis.SeCurisException;
 import net.curisit.securis.db.Application;
 import net.curisit.securis.db.LicenseType;
 import net.curisit.securis.utils.TokenHelper;
@@ -41,7 +42,7 @@
 @Path("/licensetype")
 public class LicenseTypeResource {
 
-	private static final Logger log = LogManager.getLogger(LicenseTypeResource.class);
+	private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class);
 
 	@Inject
 	TokenHelper tokenHelper;
@@ -61,7 +62,7 @@
 	@Produces(
 		{ MediaType.APPLICATION_JSON })
 	public Response index() {
-		log.info("Getting license types list ");
+		LOG.info("Getting license types list ");
 
 		EntityManager em = emProvider.get();
 		TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
@@ -79,16 +80,16 @@
 	@Produces(
 		{ MediaType.APPLICATION_JSON })
 	public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
-		log.info("Getting license type data for id: {}: ", ltid);
+		LOG.info("Getting license type data for id: {}: ", ltid);
 		if (ltid == null || ltid.equals("")) {
-			log.error("LicenseType ID is mandatory");
+			LOG.error("LicenseType ID is mandatory");
 			return Response.status(Status.NOT_FOUND).build();
 		}
 
 		EntityManager em = emProvider.get();
 		LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
 		if (lt == null) {
-			log.error("LicenseType with id {} not found in DB", ltid);
+			LOG.error("LicenseType with id {} not found in DB", ltid);
 			return Response.status(Status.NOT_FOUND).build();
 		}
 		return Response.ok(lt).build();
@@ -101,21 +102,20 @@
 		{ MediaType.APPLICATION_JSON })
 	@Transactional
 	public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
-		log.info("Creating new license type");
+		LOG.info("Creating new license type");
 		EntityManager em = emProvider.get();
-		Application app = null;
-		if (lt.getApplicationId() != null) {
-			app = em.find(Application.class, lt.getApplicationId());
-			if (app == null) {
-				log.error("LicenseType application with id {} not found in DB", lt.getApplicationId());
-				return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type's app not found with ID: " + lt.getApplicationId()).build();
-			}
-		} else {
-			log.error("Application is missing for current license type data");
+
+		try {
+			setApplication(lt, lt.getApplicationId(), em);
+		} catch (SeCurisException e) {
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
+		}
+		
+		if (lt.getApplicationId() == null) {
+			LOG.error("Application is missing for current license type data");
 			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application is missing for current license type data").build();
 		}
 
-		lt.setApplication(app);
 		lt.setCreationTimestamp(new Date());
 		em.persist(lt);
 
@@ -130,28 +130,39 @@
 	@Produces(
 		{ MediaType.APPLICATION_JSON })
 	public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
-		log.info("Modifying license type with id: {}", ltid);
+		LOG.info("Modifying license type with id: {}", ltid);
 		EntityManager em = emProvider.get();
 		LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid));
 		if (currentlt == null) {
-			log.error("LicenseType with id {} not found in DB", ltid);
+			LOG.error("LicenseType with id {} not found in DB", ltid);
 			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type not found with ID: " + ltid).build();
 		}
-		Application app = null;
-		if (lt.getApplicationId() != null) {
-			app = em.find(Application.class, lt.getApplicationId());
-			if (app == null) {
-				log.error("LicenseType application with id {} not found in DB", lt.getApplicationId());
-				return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type's app not found with ID: " + lt.getApplicationId()).build();
-			}
+
+		try {
+			setApplication(currentlt, lt.getApplicationId(), em);
+		} catch (SeCurisException e) {
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
 		}
+		
 		currentlt.setCode(lt.getCode());
 		currentlt.setName(lt.getName());
 		currentlt.setDescription(lt.getDescription());
-		currentlt.setApplication(app);
 		em.persist(currentlt);
 
 		return Response.ok(currentlt).build();
+	}
+
+	private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException {
+		Application app = null;
+		if (applicationId != null) {
+			app = em.find(Application.class, applicationId);
+			if (app == null) {
+				LOG.error("LicenseType application with id {} not found in DB", applicationId);
+				
+				throw new SecurityException("License type's app not found with ID: " + applicationId);
+			}
+		}
+		licType.setApplication(app);		
 	}
 
 	@DELETE
@@ -160,11 +171,11 @@
 	@Produces(
 		{ MediaType.APPLICATION_JSON })
 	public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
-		log.info("Deleting app with id: {}", ltid);
+		LOG.info("Deleting app with id: {}", ltid);
 		EntityManager em = emProvider.get();
 		LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid));
 		if (app == null) {
-			log.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid);
+			LOG.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid);
 			return Response.status(Status.NOT_FOUND).build();
 		}
 

--
Gitblit v1.3.2