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/ApplicationResource.java | 337 +++++++++++++++++++++++++++----------------------------
1 files changed, 166 insertions(+), 171 deletions(-)
diff --git a/securis/src/main/java/net/curisit/securis/services/ApplicationResource.java b/securis/src/main/java/net/curisit/securis/services/ApplicationResource.java
index 5ce62d6..e3fcced 100644
--- a/securis/src/main/java/net/curisit/securis/services/ApplicationResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/ApplicationResource.java
@@ -25,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.db.Application;
@@ -34,10 +37,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;
/**
* Application resource, this service will provide methods to create, modify and
@@ -48,191 +49,185 @@
@Path("/application")
public class ApplicationResource {
- @Inject
- TokenHelper tokenHelper;
+ @Inject
+ TokenHelper tokenHelper;
- @Context
- EntityManager em;
+ @Inject
+ MetadataHelper metadataHelper;
- private static final Logger LOG = LogManager.getLogger(ApplicationResource.class);
+ @Context
+ EntityManager em;
- public ApplicationResource() {
- }
+ private static final Logger LOG = LogManager.getLogger(ApplicationResource.class);
- /**
- *
- * @return the server version in format majorVersion.minorVersion
- */
- @GET
- @Path("/")
- @Produces({
- MediaType.APPLICATION_JSON
- })
- @Securable
- public Response index() {
- LOG.info("Getting applications list ");
+ public ApplicationResource() {
+ }
- // EntityManager em = emProvider.get();
- em.clear();
- TypedQuery<Application> q = em.createNamedQuery("list-applications", Application.class);
- List<Application> list = q.getResultList();
+ /**
+ *
+ * @return the server version in format majorVersion.minorVersion
+ */
+ @GET
+ @Path("/")
+ @Produces({ MediaType.APPLICATION_JSON })
+ @Securable
+ public Response index() {
+ LOG.info("Getting applications list ");
- return Response.ok(list).build();
- }
+ // EntityManager em = emProvider.get();
+ em.clear();
+ TypedQuery<Application> q = em.createNamedQuery("list-applications", Application.class);
+ List<Application> list = q.getResultList();
- /**
- *
- * @return the server version in format majorVersion.minorVersion
- * @throws SeCurisServiceException
- */
- @GET
- @Path("/{appid}")
- @Produces({
- MediaType.APPLICATION_JSON
- })
- @Securable
- public Response get(@PathParam("appid") String appid) throws SeCurisServiceException {
- LOG.info("Getting application data for id: {}: ", appid);
- if (appid == null || "".equals(appid)) {
- LOG.error("Application ID is mandatory");
- return Response.status(Status.NOT_FOUND).build();
- }
+ return Response.ok(list).build();
+ }
- em.clear();
+ /**
+ *
+ * @return the server version in format majorVersion.minorVersion
+ * @throws SeCurisServiceException
+ */
+ @GET
+ @Path("/{appid}")
+ @Produces({ MediaType.APPLICATION_JSON })
+ @Securable
+ public Response get(@PathParam("appid") String appid) throws SeCurisServiceException {
+ LOG.info("Getting application data for id: {}: ", appid);
+ if (appid == null || "".equals(appid)) {
+ LOG.error("Application ID is mandatory");
+ return Response.status(Status.NOT_FOUND).build();
+ }
- Application app = null;
- try {
- LOG.info("READY to GET app: {}", appid);
- app = em.find(Application.class, Integer.parseInt(appid));
- } catch (Exception e) {
- LOG.info("ERROR GETTING app: {}", e);
- }
- if (app == null) {
- LOG.error("Application with id {} not found in DB", appid);
- throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Application not found with ID: " + appid);
- }
+ em.clear();
- return Response.ok(app).build();
- }
+ Application app = null;
+ try {
+ LOG.info("READY to GET app: {}", appid);
+ app = em.find(Application.class, Integer.parseInt(appid));
+ } catch (Exception e) {
+ LOG.info("ERROR GETTING app: {}", e);
+ }
+ if (app == null) {
+ LOG.error("Application with id {} not found in DB", appid);
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Application not found with ID: " + appid);
+ }
- @POST
- @Path("/")
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces({
- MediaType.APPLICATION_JSON
- })
- @EnsureTransaction
- @Securable
- @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
- public Response create(Application app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
- LOG.info("Creating new application");
- // EntityManager em = emProvider.get();
- app.setCreationTimestamp(new Date());
- em.persist(app);
+ return Response.ok(app).build();
+ }
- if (app.getApplicationMetadata() != null) {
- for (ApplicationMetadata md : app.getApplicationMetadata()) {
- md.setApplication(app);
- md.setCreationTimestamp(new Date());
- em.persist(md);
- }
- }
- LOG.info("Creating application ({}) with date: {}", app.getId(), app.getCreationTimestamp());
+ @POST
+ @Path("/")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces({ MediaType.APPLICATION_JSON })
+ @EnsureTransaction
+ @Securable
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+ public Response create(Application app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
+ LOG.info("Creating new application");
+ // EntityManager em = emProvider.get();
+ app.setCreationTimestamp(new Date());
+ em.persist(app);
- return Response.ok(app).build();
- }
+ if (app.getApplicationMetadata() != null) {
+ for (ApplicationMetadata md : app.getApplicationMetadata()) {
+ md.setApplication(app);
+ md.setCreationTimestamp(new Date());
+ em.persist(md);
+ }
+ }
+ LOG.info("Creating application ({}) with date: {}", app.getId(), app.getCreationTimestamp());
- @PUT
- @POST
- @Path("/{appid}")
- @EnsureTransaction
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces({
- MediaType.APPLICATION_JSON
- })
- @Securable
- @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
- public Response modify(Application app, @PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
- LOG.info("Modifying application with id: {}", appid);
- // EntityManager em = emProvider.get();
- Application currentapp = em.find(Application.class, Integer.parseInt(appid));
- if (currentapp == null) {
- LOG.error("Application with id {} not found in DB", appid);
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid)
- .build();
- }
- currentapp.setCode(app.getCode());
- currentapp.setName(app.getName());
- currentapp.setLicenseFilename(app.getLicenseFilename());
- currentapp.setDescription(app.getDescription());
+ return Response.ok(app).build();
+ }
- Set<ApplicationMetadata> newMD = app.getApplicationMetadata();
- Set<ApplicationMetadata> oldMD = currentapp.getApplicationMetadata();
- Map<String, ApplicationMetadata> directOldMD = getMapMD(oldMD);
- Map<String, ApplicationMetadata> directNewMD = getMapMD(newMD);
- for (ApplicationMetadata currentMd : oldMD) {
- if (newMD == null || !directNewMD.containsKey(currentMd.getKey())) {
- em.remove(currentMd);
- }
- }
+ @PUT
+ @POST
+ @Path("/{appid}")
+ @EnsureTransaction
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces({ MediaType.APPLICATION_JSON })
+ @Securable
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+ public Response modify(Application app, @PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
+ LOG.info("Modifying application with id: {}", appid);
+ // EntityManager em = emProvider.get();
+ Application currentapp = em.find(Application.class, Integer.parseInt(appid));
+ if (currentapp == null) {
+ LOG.error("Application with id {} not found in DB", appid);
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid).build();
+ }
+ currentapp.setCode(app.getCode());
+ currentapp.setName(app.getName());
+ currentapp.setLicenseFilename(app.getLicenseFilename());
+ currentapp.setDescription(app.getDescription());
- if (newMD != null) {
- for (ApplicationMetadata md : newMD) {
- if (directOldMD.containsKey(md.getKey())) {
- ApplicationMetadata amd = directOldMD.get(md.getKey());
- amd.setValue(md.getValue());
- amd.setMandatory(md.isMandatory());
- em.merge(amd);
- } else {
- md.setApplication(currentapp);
- if (md.getCreationTimestamp() == null) {
- md.setCreationTimestamp(app.getCreationTimestamp());
- }
- em.persist(md);
- }
- }
- }
- currentapp.setApplicationMetadata(app.getApplicationMetadata());
- em.merge(currentapp);
- return Response.ok(currentapp).build();
- }
+ Set<ApplicationMetadata> newMD = app.getApplicationMetadata();
+ Set<ApplicationMetadata> oldMD = currentapp.getApplicationMetadata();
+ boolean metadataChanges = !metadataHelper.match(newMD, oldMD);
+ if (metadataChanges) {
+ Map<String, ApplicationMetadata> directOldMD = getMapMD(oldMD);
+ Map<String, ApplicationMetadata> directNewMD = getMapMD(newMD);
+ for (ApplicationMetadata currentMd : oldMD) {
+ if (newMD == null || !directNewMD.containsKey(currentMd.getKey())) {
+ em.remove(currentMd);
+ }
+ }
- private Map<String, ApplicationMetadata> getMapMD(Set<ApplicationMetadata> amd) {
- Map<String, ApplicationMetadata> map = new HashMap<String, ApplicationMetadata>();
- if (amd != null) {
- for (ApplicationMetadata applicationMetadata : amd) {
- map.put(applicationMetadata.getKey(), applicationMetadata);
- }
- }
- return map;
- }
+ if (newMD != null) {
+ for (ApplicationMetadata md : newMD) {
+ if (directOldMD.containsKey(md.getKey())) {
+ em.merge(md);
+ } else {
+ md.setApplication(currentapp);
+ if (md.getCreationTimestamp() == null) {
+ md.setCreationTimestamp(app.getCreationTimestamp());
+ }
+ em.persist(md);
+ }
+ }
+ }
+ currentapp.setApplicationMetadata(app.getApplicationMetadata());
+ }
+ em.merge(currentapp);
+ if (metadataChanges) {
+ metadataHelper.propagateMetadata(em, currentapp);
+ }
+ return Response.ok(currentapp).build();
+ }
- @DELETE
- @Path("/{appid}")
- @EnsureTransaction
- @Produces({
- MediaType.APPLICATION_JSON
- })
- @Securable
- @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
- public Response delete(@PathParam("appid") String appid, @Context HttpServletRequest request) {
- LOG.info("Deleting app with id: {}", appid);
- // EntityManager em = emProvider.get();
- Application app = em.find(Application.class, Integer.parseInt(appid));
- if (app == null) {
- LOG.error("Application with id {} can not be deleted, It was not found in DB", appid);
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid)
- .build();
- }
- /*
- * if (app.getLicenseTypes() != null &&
- * !app.getLicenseTypes().isEmpty()) { throw new
- * SeCurisServiceException(ErrorCodes.NOT_FOUND,
- * "Application can not be deleted becasue has assigned one or more License types, ID: "
- * + appid); }
- */
- em.remove(app);
- return Response.ok(Utils.createMap("success", true, "id", appid)).build();
- }
+ private Map<String, ApplicationMetadata> getMapMD(Set<ApplicationMetadata> amd) {
+ Map<String, ApplicationMetadata> map = new HashMap<String, ApplicationMetadata>();
+ if (amd != null) {
+ for (ApplicationMetadata applicationMetadata : amd) {
+ map.put(applicationMetadata.getKey(), applicationMetadata);
+ }
+ }
+ return map;
+ }
+
+ @DELETE
+ @Path("/{appid}")
+ @EnsureTransaction
+ @Produces({ MediaType.APPLICATION_JSON })
+ @Securable
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
+ public Response delete(@PathParam("appid") String appid, @Context HttpServletRequest request) {
+ LOG.info("Deleting app with id: {}", appid);
+ // EntityManager em = emProvider.get();
+ Application app = em.find(Application.class, Integer.parseInt(appid));
+ if (app == null) {
+ LOG.error("Application with id {} can not be deleted, It was not found in DB", appid);
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid).build();
+ }
+ /*
+ * if (app.getLicenseTypes() != null &&
+ * !app.getLicenseTypes().isEmpty()) { throw new
+ * SeCurisServiceException(ErrorCodes.NOT_FOUND,
+ * "Application can not be deleted becasue has assigned one or more License types, ID: "
+ * + appid); }
+ */
+ em.remove(app);
+ return Response.ok(Utils.createMap("success", true, "id", appid)).build();
+ }
}
--
Gitblit v1.3.2