From 4f5711b8ec555ab8307516ce178b454445d3833f Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Fri, 24 Mar 2017 10:03:47 +0000
Subject: [PATCH] #3535 - Apply metadata changes in cascade
---
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java | 356 +++++++++++++++++++++++++++++-----------------------------
1 files changed, 178 insertions(+), 178 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 aea9199..ea63e1b 100644
--- a/securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
@@ -4,6 +4,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.stream.Collectors;
import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
@@ -24,6 +25,9 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
import net.curisit.integrity.commons.Utils;
import net.curisit.securis.DefaultExceptionHandler;
import net.curisit.securis.SeCurisException;
@@ -35,10 +39,8 @@
import net.curisit.securis.security.Securable;
import net.curisit.securis.services.exception.SeCurisServiceException;
import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
+import net.curisit.securis.services.helpers.MetadataHelper;
import net.curisit.securis.utils.TokenHelper;
-
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
/**
* LicenseType resource, this service will provide methods to create, modify and
@@ -49,206 +51,204 @@
@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;
+ @Inject
+ TokenHelper tokenHelper;
- @Context
- EntityManager em;
+ @Inject
+ MetadataHelper metadataHelper;
- public LicenseTypeResource() {
- }
+ @Context
+ EntityManager em;
- /**
- *
- * @return the server version in format majorVersion.minorVersion
- */
- @GET
- @Path("/")
- @Produces({
- MediaType.APPLICATION_JSON
- })
- @Securable
- public Response index() {
- LOG.info("Getting license types list ");
+ public LicenseTypeResource() {
+ }
- // EntityManager em = emProvider.get();
- em.clear();
- TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
- List<LicenseType> list = q.getResultList();
+ /**
+ *
+ * @return the server version in format majorVersion.minorVersion
+ */
+ @GET
+ @Path("/")
+ @Produces({ MediaType.APPLICATION_JSON })
+ @Securable
+ public Response index() {
+ LOG.info("Getting license types list ");
- return Response.ok(list).build();
- }
+ // EntityManager em = emProvider.get();
+ em.clear();
+ TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
+ List<LicenseType> list = q.getResultList();
- /**
- *
- * @return the server version in format majorVersion.minorVersion
- * @throws SeCurisServiceException
- */
- @GET
- @Path("/{ltid}")
- @Produces({
- MediaType.APPLICATION_JSON
- })
- @Securable
- public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) throws SeCurisServiceException {
- LOG.info("Getting license type data for id: {}: ", ltid);
- if (ltid == null || "".equals(ltid)) {
- LOG.error("LicenseType ID is mandatory");
- return Response.status(Status.NOT_FOUND).build();
- }
+ return Response.ok(list).build();
+ }
- // EntityManager em = emProvider.get();
- em.clear();
- LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
- if (lt == null) {
- LOG.error("LicenseType with id {} not found in DB", ltid);
- throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "LicenseType was not found in DB");
- }
- return Response.ok(lt).build();
- }
+ /**
+ *
+ * @return the server version in format majorVersion.minorVersion
+ * @throws SeCurisServiceException
+ */
+ @GET
+ @Path("/{ltid}")
+ @Produces({ MediaType.APPLICATION_JSON })
+ @Securable
+ public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) throws SeCurisServiceException {
+ LOG.info("Getting license type data for id: {}: ", ltid);
+ if (ltid == null || "".equals(ltid)) {
+ LOG.error("LicenseType ID is mandatory");
+ return Response.status(Status.NOT_FOUND).build();
+ }
- @POST
- @Path("/")
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces({
- MediaType.APPLICATION_JSON
- })
- @EnsureTransaction
- @Securable
- @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
- public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
- LOG.info("Creating new license type");
- // EntityManager em = emProvider.get();
+ // EntityManager em = emProvider.get();
+ em.clear();
+ LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
+ if (lt == null) {
+ LOG.error("LicenseType with id {} not found in DB", ltid);
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "LicenseType was not found in DB");
+ }
+ return Response.ok(lt).build();
+ }
- try {
- setApplication(lt, lt.getApplicationId(), em);
- } catch (SeCurisException e) {
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
- }
+ @POST
+ @Path("/")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces({ MediaType.APPLICATION_JSON })
+ @EnsureTransaction
+ @Securable
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+ public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
+ LOG.info("Creating new license type");
+ // EntityManager em = emProvider.get();
- 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();
- }
+ try {
+ setApplication(lt, lt.getApplicationId(), em);
+ } catch (SeCurisException e) {
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
+ }
- lt.setCreationTimestamp(new Date());
- em.persist(lt);
- Set<LicenseTypeMetadata> newMD = lt.getMetadata();
+ 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();
+ }
- if (newMD != null) {
- for (LicenseTypeMetadata md : newMD) {
- md.setLicenseType(lt);
- em.persist(md);
- }
- }
- lt.setMetadata(newMD);
+ lt.setCreationTimestamp(new Date());
+ em.persist(lt);
+ Set<LicenseTypeMetadata> newMD = lt.getMetadata();
- return Response.ok(lt).build();
- }
+ if (newMD != null) {
+ for (LicenseTypeMetadata md : newMD) {
+ md.setLicenseType(lt);
+ em.persist(md);
+ }
+ }
+ lt.setMetadata(newMD);
- private Set<String> getMdKeys(Set<LicenseTypeMetadata> mds) {
- Set<String> ids = new HashSet<String>();
- if (mds != null) {
- for (LicenseTypeMetadata md : mds) {
- ids.add(md.getKey());
- }
- }
- return ids;
- }
+ return Response.ok(lt).build();
+ }
- @PUT
- @POST
- @Path("/{ltid}")
- @EnsureTransaction
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces({
- MediaType.APPLICATION_JSON
- })
- @Securable
- @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
- 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);
- // 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);
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type not found with ID: " + ltid)
- .build();
- }
+ private Set<String> getMdKeys(Set<LicenseTypeMetadata> mds) {
+ Set<String> ids = new HashSet<String>();
+ if (mds != null) {
+ for (LicenseTypeMetadata md : mds) {
+ ids.add(md.getKey());
+ }
+ }
+ return ids;
+ }
- try {
- setApplication(currentlt, lt.getApplicationId(), em);
- } catch (SeCurisException e) {
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
- }
+ @PUT
+ @POST
+ @Path("/{ltid}")
+ @EnsureTransaction
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces({ MediaType.APPLICATION_JSON })
+ @Securable
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+ 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);
+ // 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);
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type not found with ID: " + ltid).build();
+ }
- currentlt.setCode(lt.getCode());
- currentlt.setName(lt.getName());
- currentlt.setDescription(lt.getDescription());
+ try {
+ setApplication(currentlt, lt.getApplicationId(), em);
+ } catch (SeCurisException e) {
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
+ }
- Set<LicenseTypeMetadata> newMD = lt.getMetadata();
- Set<String> newMdKeys = getMdKeys(newMD);
- for (LicenseTypeMetadata currentMd : currentlt.getMetadata()) {
- if (!newMdKeys.contains(currentMd.getKey())) {
- em.remove(currentMd);
- LOG.info("Removing MD: {}", currentMd);
- }
- }
+ currentlt.setCode(lt.getCode());
+ currentlt.setName(lt.getName());
+ currentlt.setDescription(lt.getDescription());
- if (newMD != null) {
- Set<LicenseTypeMetadata> oldMD = currentlt.getMetadata();
- Set<String> oldMdKeys = getMdKeys(oldMD);
+ Set<LicenseTypeMetadata> newMD = lt.getMetadata();
+ Set<LicenseTypeMetadata> oldMD = currentlt.getMetadata();
+ boolean metadataChanges = !metadataHelper.match(newMD, oldMD);
+ if (metadataChanges) {
+ Set<String> newMdKeys = getMdKeys(newMD);
+ for (LicenseTypeMetadata currentMd : oldMD) {
+ if (!newMdKeys.contains(currentMd.getKey())) {
+ em.remove(currentMd);
+ LOG.info("Removing MD: {}", currentMd);
+ }
+ }
- for (LicenseTypeMetadata md : newMD) {
- if (oldMdKeys.contains(md.getKey())) {
- em.merge(md);
- } else {
- md.setLicenseType(currentlt);
- em.persist(md);
- }
- }
- }
- currentlt.setMetadata(newMD);
- em.merge(currentlt);
+ if (newMD != null) {
+ Set<String> oldMdKeys = getMdKeys(oldMD);
- return Response.ok(currentlt).build();
- }
+ for (LicenseTypeMetadata md : newMD) {
+ if (oldMdKeys.contains(md.getKey())) {
+ em.merge(md);
+ } else {
+ md.setLicenseType(currentlt);
+ em.persist(md);
+ }
+ }
+ }
+ currentlt.setMetadata(newMD);
+ }
+ em.merge(currentlt);
+ if (metadataChanges) {
+ Set<String> keys = newMD.parallelStream().map(md -> md.getKey()).collect(Collectors.toSet());
+ metadataHelper.propagateMetadata(em, currentlt, keys);
+ }
- 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);
+ return Response.ok(currentlt).build();
+ }
- throw new SecurityException("License type's app not found with ID: " + applicationId);
- }
- }
- licType.setApplication(app);
- }
+ 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);
- @DELETE
- @Path("/{ltid}")
- @EnsureTransaction
- @Produces({
- MediaType.APPLICATION_JSON
- })
- @Securable
- @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
- public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
- 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);
- return Response.status(Status.NOT_FOUND).build();
- }
+ throw new SecurityException("License type's app not found with ID: " + applicationId);
+ }
+ }
+ licType.setApplication(app);
+ }
- em.remove(app);
- return Response.ok(Utils.createMap("success", true, "id", ltid)).build();
- }
+ @DELETE
+ @Path("/{ltid}")
+ @EnsureTransaction
+ @Produces({ MediaType.APPLICATION_JSON })
+ @Securable
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+ public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
+ 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);
+ return Response.status(Status.NOT_FOUND).build();
+ }
+
+ em.remove(app);
+ return Response.ok(Utils.createMap("success", true, "id", ltid)).build();
+ }
}
--
Gitblit v1.3.2