From 146a0fb8b0e90f9196e569152f649baf60d6cc8f Mon Sep 17 00:00:00 2001
From: Joaquín Reñé <jrene@curisit.net>
Date: Tue, 07 Oct 2025 14:52:57 +0000
Subject: [PATCH] #4410 - Comments on classes

---
 securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java |  146 ++++++++++++++++++++++++++++++++----------------
 1 files changed, 96 insertions(+), 50 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 28b5c7d..04c15a2 100644
--- a/securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
@@ -1,3 +1,6 @@
+/*
+ * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
+ */
 package net.curisit.securis.services;
 
 import java.util.Date;
@@ -44,31 +47,34 @@
 import net.curisit.securis.utils.TokenHelper;
 
 /**
- * LicenseType resource, this service will provide methods to create, modify and
- * delete license types
- * 
- * @author roberto <roberto.sanchez@curisit.net>
+ * LicenseTypeResource
+ * <p>
+ * CRUD for license types. Non-admin queries are scoped to the applications
+ * accessible by the caller. Metadata changes are reconciled and, when keys
+ * change, can be propagated to dependent entities via {@link MetadataHelper}.
+ *
+ * @author JRA
+ * Last reviewed by JRA on Oct 5, 2025.
  */
 @Path("/licensetype")
 public class LicenseTypeResource {
 
 	private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class);
 
-	@Inject
-	TokenHelper tokenHelper;
+	@Inject TokenHelper tokenHelper;
+	@Inject MetadataHelper metadataHelper;
 
-	@Inject
-	MetadataHelper metadataHelper;
+	@Context EntityManager em;
 
-	@Context
-	EntityManager em;
-
-	public LicenseTypeResource() {
-	}
+	public LicenseTypeResource() { }
 
 	/**
-	 * 
-	 * @return the server version in format majorVersion.minorVersion
+	 * index
+	 * <p>
+	 * List license types. Non-admin users get only types for their allowed apps.
+	 *
+	 * @param bsc security context.
+	 * @return 200 OK with list (possibly empty).
 	 */
 	@GET
 	@Path("/")
@@ -76,8 +82,6 @@
 	@Securable
 	public Response index(@Context BasicSecurityContext bsc) {
 		LOG.info("Getting license types list ");
-
-		// EntityManager em = emProvider.get();
 		em.clear();
 		TypedQuery<LicenseType> q;
 		if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
@@ -87,18 +91,21 @@
 				return Response.ok().build();
 			}
 			q = em.createNamedQuery("list-license_types-by_apps-id", LicenseType.class);
-
 			q.setParameter("list_ids", bsc.getApplicationsIds());
 		}
 		List<LicenseType> list = q.getResultList();
-
 		return Response.ok(list).build();
 	}
 
 	/**
-	 * 
-	 * @return the server version in format majorVersion.minorVersion
-	 * @throws SeCurisServiceException
+	 * get
+	 * <p>
+	 * Fetch a license type by id.
+	 *
+	 * @param ltid  LicenseType id (string form).
+	 * @param token (unused) header token.
+	 * @return 200 OK with the entity.
+	 * @throws SeCurisServiceException 404 if not found.
 	 */
 	@GET
 	@Path("/{ltid}")
@@ -111,7 +118,6 @@
 			return Response.status(Status.NOT_FOUND).build();
 		}
 
-		// EntityManager em = emProvider.get();
 		em.clear();
 		LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
 		if (lt == null) {
@@ -121,6 +127,16 @@
 		return Response.ok(lt).build();
 	}
 
+	/**
+	 * create
+	 * <p>
+	 * Create a new license type. Requires ADMIN. Sets application reference,
+	 * persists metadata entries and stamps creation time.
+	 *
+	 * @param lt    Payload.
+	 * @param token (unused) token header.
+	 * @return 200 OK with created entity, or 404 if app missing.
+	 */
 	@POST
 	@Path("/")
 	@Consumes(MediaType.APPLICATION_JSON)
@@ -130,7 +146,6 @@
 	@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();
 
 		try {
 			setApplication(lt, lt.getApplicationId(), em);
@@ -158,16 +173,18 @@
 		return Response.ok(lt).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;
-	}
-
+	/**
+	 * modify
+	 * <p>
+	 * Update an existing license type. Reconciles metadata:
+	 * removes keys not present in the new set; merges existing; persists new ones.
+	 * If keys changed, {@link MetadataHelper#propagateMetadata} is invoked.
+	 *
+	 * @param lt    New values.
+	 * @param ltid  LicenseType id.
+	 * @param token (unused) token.
+	 * @return 200 OK with updated entity; 404 if not found or app missing.
+	 */
 	@PUT
 	@POST
 	@Path("/{ltid}")
@@ -178,7 +195,6 @@
 	@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);
@@ -230,28 +246,23 @@
 		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
+	 * <p>
+	 * Delete a license type by id. Requires ADMIN.
+	 *
+	 * @param ltid LicenseType id.
+	 * @param req  request (unused).
+	 * @return 200 OK on success; 404 if not found.
+	 */
 	@DELETE
 	@Path("/{ltid}")
 	@EnsureTransaction
 	@Produces({ MediaType.APPLICATION_JSON })
 	@Securable(roles = Rol.ADMIN)
 	@RolesAllowed(BasicSecurityContext.ROL_ADMIN)
-	public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
+	public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest req) {
 		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);
@@ -262,4 +273,39 @@
 		return Response.ok(Utils.createMap("success", true, "id", ltid)).build();
 	}
 
+	// ---------------------------------------------------------------------
+	// Helpers
+	// ---------------------------------------------------------------------
+
+	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;
+	}
+
+	/**
+	 * setApplication<p>
+	 * Resolve and set the application of a license type.
+	 *
+	 * @param licType       target LicenseType.
+	 * @param applicationId id of the application (nullable).
+	 * @param em            entity manager.
+	 * @throws SeCurisException if id provided but not found.
+	 */
+	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);
+	}
 }
+

--
Gitblit v1.3.2