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/OrganizationResource.java |  124 +++++++++++++++++++++++-----------------
 1 files changed, 71 insertions(+), 53 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/services/OrganizationResource.java b/securis/src/main/java/net/curisit/securis/services/OrganizationResource.java
index b956327..8a51b35 100644
--- a/securis/src/main/java/net/curisit/securis/services/OrganizationResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/OrganizationResource.java
@@ -26,6 +26,7 @@
 
 import net.curisit.integrity.commons.Utils;
 import net.curisit.securis.DefaultExceptionHandler;
+import net.curisit.securis.SeCurisException;
 import net.curisit.securis.db.Organization;
 import net.curisit.securis.db.User;
 import net.curisit.securis.security.BasicSecurityContext;
@@ -46,7 +47,7 @@
 @Path("/organization")
 public class OrganizationResource {
 
-	private static final Logger log = LogManager.getLogger(OrganizationResource.class);
+	private static final Logger LOG = LogManager.getLogger(OrganizationResource.class);
 
 	@Inject
 	private Provider<EntityManager> emProvider;
@@ -65,24 +66,24 @@
 	@Securable
 	// @RolesAllowed(SecurityContextWrapper.ROL_ADVANCE)
 	public Response index(@Context BasicSecurityContext bsc) {
-		log.info("Getting organizations list ");
+		LOG.info("Getting organizations list ");
 
-		// log.info("User orgs: {}", request.getAttribute("oser_orgs"));
+		// LOG.info("User orgs: {}", request.getAttribute("oser_orgs"));
 		BasicSecurityContext bsc2 = ResteasyProviderFactory.getContextData(BasicSecurityContext.class);
-		log.info("bsc: {}", bsc);
-		log.info("bsc2: {}", bsc2);
-		// log.info("securityContext: {}", scw);
-		log.info("securityContext ROL_ADMIN?: {}", bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN));
+		LOG.info("bsc: {}", bsc);
+		LOG.info("bsc2: {}", bsc2);
+		// LOG.info("securityContext: {}", scw);
+		LOG.info("securityContext ROL_ADMIN?: {}", bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN));
 		EntityManager em = emProvider.get();
 		TypedQuery<Organization> q;
 		if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
-			log.info("GEtting all orgs for user: " + bsc.getUserPrincipal());
+			LOG.info("GEtting all orgs for user: " + bsc.getUserPrincipal());
 			q = em.createNamedQuery("list-organizations", Organization.class);
 		} else {
 			q = em.createNamedQuery("list-organizations", Organization.class);
 			// if (securityContext.getOrganizationsIds() == null)
 			// Response.ok().build();
-			// log.info("Getting only {} orgs for user: {}", securityContext.getOrganizationsIds(), securityContext.getUserPrincipal());
+			// LOG.info("Getting only {} orgs for user: {}", securityContext.getOrganizationsIds(), securityContext.getUserPrincipal());
 			// q = em.createNamedQuery("list-organizations-by-ids", Organization.class);
 			// q.setParameter("list_ids", securityContext.getOrganizationsIds());
 		}
@@ -102,20 +103,20 @@
 		{ MediaType.APPLICATION_JSON })
 	@Securable
 	public Response get(@PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
-		log.info("Getting organization data for id: {}: ", orgid);
+		LOG.info("Getting organization data for id: {}: ", orgid);
 		if (orgid == null || orgid.equals("")) {
-			log.error("Organization ID is mandatory");
+			LOG.error("Organization ID is mandatory");
 			return Response.status(Status.NOT_FOUND).build();
 		}
 		// if (!securityContext.isOrgAccesible(Integer.parseInt(orgid))) {
-		// log.error("Organization with id {} not accessible for user: {}", orgid, securityContext.getUserPrincipal());
+		// LOG.error("Organization with id {} not accessible for user: {}", orgid, securityContext.getUserPrincipal());
 		// return Response.status(Status.UNAUTHORIZED).build();
 		// }
 
 		EntityManager em = emProvider.get();
 		Organization org = em.find(Organization.class, Integer.parseInt(orgid));
 		if (org == null) {
-			log.error("Organization with id {} not found in DB", orgid);
+			LOG.error("Organization with id {} not found in DB", orgid);
 			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found, id: " + orgid).build();
 		}
 		return Response.ok(org).build();
@@ -139,16 +140,15 @@
 	@Securable
 	@RolesAllowed(BasicSecurityContext.ROL_ADMIN)
 	public Response create(Organization org, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
-		log.info("Creating new organization");
+		LOG.info("Creating new organization");
 		EntityManager em = emProvider.get();
-		Organization parentOrg = null;
-		if (org.getParentOrgId() != null) {
-			parentOrg = em.find(Organization.class, org.getParentOrgId());
-			if (parentOrg == null) {
-				log.error("Organization parent with id {} not found in DB", org.getParentOrgId());
-				return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's parent not found with ID: " + org.getParentOrgId()).build();
-			}
+		
+		try {
+			this.setParentOrg(org, org.getParentOrgId(), em);
+		} catch (SeCurisException e) {
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
 		}
+		
 		List<User> users = null;
 		List<String> usersIds = org.getUsersIds();
 		if (usersIds != null && usersIds.size() > 0) {
@@ -156,7 +156,7 @@
 			for (String username : usersIds) {
 				User user = em.find(User.class, username);
 				if (user == null) {
-					log.error("Organization user with id {} not found in DB", username);
+					LOG.error("Organization user with id {} not found in DB", username);
 					return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's user not found with ID: " + username).build();
 				}
 				users.add(user);
@@ -164,11 +164,40 @@
 		}
 
 		org.setUsers(users);
-		org.setParentOrganization(parentOrg);
 		org.setCreationTimestamp(new Date());
 		em.persist(org);
 
 		return Response.ok(org).build();
+	}
+	
+	private void setParentOrg(Organization org, Integer parentOrgId, EntityManager em) throws SeCurisException {
+		Organization parentOrg = null;
+		if (parentOrgId != null) {
+			parentOrg = em.find(Organization.class, parentOrgId);
+			if (parentOrg == null) {
+				LOG.error("Organization parent with id {} not found in DB", org.getParentOrgId());
+				throw new SecurityException("Organization's parent not found with ID: " + org.getParentOrgId());
+			}
+		}
+
+		org.setParentOrganization(parentOrg);
+	}
+
+	private void setOrgUsers(Organization org, List<String> usersIds, EntityManager em) throws SeCurisException {
+		List<User> users = null;
+		if (usersIds != null && usersIds.size() > 0) {
+			users = new ArrayList<>();
+			for (String username : usersIds) {
+				User user = em.find(User.class, username);
+				if (user == null) {
+					LOG.error("Organization user with id '{}' not found in DB", username);
+					throw new SecurityException("Organization's user not found with ID: " + username);
+				}
+				users.add(user);
+			}
+		}
+
+		org.setUsers(users);
 	}
 
 	@PUT
@@ -181,42 +210,31 @@
 	@Securable
 	@RolesAllowed(BasicSecurityContext.ROL_ADMIN)
 	public Response modify(Organization org, @PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
-		log.info("Modifying organization with id: {}", orgid);
+		LOG.info("Modifying organization with id: {}", orgid);
 		EntityManager em = emProvider.get();
 		Organization currentOrg = em.find(Organization.class, Integer.parseInt(orgid));
 		if (currentOrg == null) {
-			log.error("Organization with id {} not found in DB", orgid);
+			LOG.error("Organization with id {} not found in DB", orgid);
 			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found with ID: " + orgid).build();
 		}
-		Organization parentOrg = null;
-		if (org.getParentOrgId() != null) {
-			parentOrg = em.find(Organization.class, org.getParentOrgId());
-			if (parentOrg == null) {
-				log.error("Organization parent with id {} not found in DB", org.getParentOrgId());
-				return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's parent not found with ID: " + org.getParentOrgId()).build();
-			}
-			if (isCyclicalRelationship(currentOrg.getId(), parentOrg)) {
-				log.error("Organization parent generate a cyclical relationship, parent id {}, current id: {}", org.getParentOrgId(), currentOrg.getId());
-				return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Cyclical relationships are not allowed, please change the parent organization, current Parent: " + parentOrg.getName()).build();
+		try {
+			this.setParentOrg(currentOrg, org.getParentOrgId(), em);
+		} catch (SeCurisException e) {
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
+		}
+		if (org.getParentOrganization() != null) {
+			if (isCyclicalRelationship(currentOrg.getId(), org.getParentOrganization())) {
+				LOG.error("Organization parent generate a cyclical relationship, parent id {}, current id: {}", org.getParentOrgId(), currentOrg.getId());
+				return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Cyclical relationships are not allowed, please change the parent organization, current Parent: " + org.getParentOrganization().getName()).build();
 			}
 		}
-
-		List<User> users = null;
-		List<String> usersIds = org.getUsersIds();
-		if (usersIds != null && usersIds.size() > 0) {
-			users = new ArrayList<>();
-			for (String username : usersIds) {
-				User user = em.find(User.class, username);
-				if (user == null) {
-					log.error("Organization user with id '{}' not found in DB", username);
-					return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's user not found with ID: " + username).build();
-				}
-				users.add(user);
-			}
+		
+		try {
+			setOrgUsers(currentOrg, org.getUsersIds(), em);
+		} catch (SeCurisException e) {
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
 		}
 
-		currentOrg.setUsers(users);
-		currentOrg.setParentOrganization(parentOrg);
 		currentOrg.setCode(org.getCode());
 		currentOrg.setName(org.getName());
 		currentOrg.setDescription(org.getDescription());
@@ -233,15 +251,15 @@
 	@Securable
 	@RolesAllowed(BasicSecurityContext.ROL_ADMIN)
 	public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest request) {
-		log.info("Deleting organization with id: {}", orgid);
+		LOG.info("Deleting organization with id: {}", orgid);
 		EntityManager em = emProvider.get();
 		Organization org = em.find(Organization.class, Integer.parseInt(orgid));
 		if (org == null) {
-			log.error("Organization with id {} can not be deleted, It was not found in DB", orgid);
+			LOG.error("Organization with id {} can not be deleted, It was not found in DB", orgid);
 			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization was not found, ID: " + orgid).build();
 		}
 		if (org.getChildOrganizations() != null && org.getChildOrganizations().size() > 0) {
-			log.error("Organization has children and can not be deleted, ID: " + orgid);
+			LOG.error("Organization has children and can not be deleted, ID: " + orgid);
 			return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization has children and can not be deleted, ID: " + orgid).build();
 		}
 

--
Gitblit v1.3.2