Joaquín Reñé
2025-10-07 146a0fb8b0e90f9196e569152f649baf60d6cc8f
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
....@@ -1,3 +1,6 @@
1
+/*
2
+ * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
3
+ */
14 package net.curisit.securis.services;
25
36 import java.util.Date;
....@@ -44,31 +47,34 @@
4447 import net.curisit.securis.utils.TokenHelper;
4548
4649 /**
47
- * LicenseType resource, this service will provide methods to create, modify and
48
- * delete license types
49
- *
50
- * @author roberto <roberto.sanchez@curisit.net>
50
+ * LicenseTypeResource
51
+ * <p>
52
+ * CRUD for license types. Non-admin queries are scoped to the applications
53
+ * accessible by the caller. Metadata changes are reconciled and, when keys
54
+ * change, can be propagated to dependent entities via {@link MetadataHelper}.
55
+ *
56
+ * @author JRA
57
+ * Last reviewed by JRA on Oct 5, 2025.
5158 */
5259 @Path("/licensetype")
5360 public class LicenseTypeResource {
5461
5562 private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class);
5663
57
- @Inject
58
- TokenHelper tokenHelper;
64
+ @Inject TokenHelper tokenHelper;
65
+ @Inject MetadataHelper metadataHelper;
5966
60
- @Inject
61
- MetadataHelper metadataHelper;
67
+ @Context EntityManager em;
6268
63
- @Context
64
- EntityManager em;
65
-
66
- public LicenseTypeResource() {
67
- }
69
+ public LicenseTypeResource() { }
6870
6971 /**
70
- *
71
- * @return the server version in format majorVersion.minorVersion
72
+ * index
73
+ * <p>
74
+ * List license types. Non-admin users get only types for their allowed apps.
75
+ *
76
+ * @param bsc security context.
77
+ * @return 200 OK with list (possibly empty).
7278 */
7379 @GET
7480 @Path("/")
....@@ -76,8 +82,6 @@
7682 @Securable
7783 public Response index(@Context BasicSecurityContext bsc) {
7884 LOG.info("Getting license types list ");
79
-
80
- // EntityManager em = emProvider.get();
8185 em.clear();
8286 TypedQuery<LicenseType> q;
8387 if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
....@@ -87,18 +91,21 @@
8791 return Response.ok().build();
8892 }
8993 q = em.createNamedQuery("list-license_types-by_apps-id", LicenseType.class);
90
-
9194 q.setParameter("list_ids", bsc.getApplicationsIds());
9295 }
9396 List<LicenseType> list = q.getResultList();
94
-
9597 return Response.ok(list).build();
9698 }
9799
98100 /**
99
- *
100
- * @return the server version in format majorVersion.minorVersion
101
- * @throws SeCurisServiceException
101
+ * get
102
+ * <p>
103
+ * Fetch a license type by id.
104
+ *
105
+ * @param ltid LicenseType id (string form).
106
+ * @param token (unused) header token.
107
+ * @return 200 OK with the entity.
108
+ * @throws SeCurisServiceException 404 if not found.
102109 */
103110 @GET
104111 @Path("/{ltid}")
....@@ -111,7 +118,6 @@
111118 return Response.status(Status.NOT_FOUND).build();
112119 }
113120
114
- // EntityManager em = emProvider.get();
115121 em.clear();
116122 LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
117123 if (lt == null) {
....@@ -121,6 +127,16 @@
121127 return Response.ok(lt).build();
122128 }
123129
130
+ /**
131
+ * create
132
+ * <p>
133
+ * Create a new license type. Requires ADMIN. Sets application reference,
134
+ * persists metadata entries and stamps creation time.
135
+ *
136
+ * @param lt Payload.
137
+ * @param token (unused) token header.
138
+ * @return 200 OK with created entity, or 404 if app missing.
139
+ */
124140 @POST
125141 @Path("/")
126142 @Consumes(MediaType.APPLICATION_JSON)
....@@ -130,7 +146,6 @@
130146 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
131147 public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
132148 LOG.info("Creating new license type");
133
- // EntityManager em = emProvider.get();
134149
135150 try {
136151 setApplication(lt, lt.getApplicationId(), em);
....@@ -158,16 +173,18 @@
158173 return Response.ok(lt).build();
159174 }
160175
161
- private Set<String> getMdKeys(Set<LicenseTypeMetadata> mds) {
162
- Set<String> ids = new HashSet<String>();
163
- if (mds != null) {
164
- for (LicenseTypeMetadata md : mds) {
165
- ids.add(md.getKey());
166
- }
167
- }
168
- return ids;
169
- }
170
-
176
+ /**
177
+ * modify
178
+ * <p>
179
+ * Update an existing license type. Reconciles metadata:
180
+ * removes keys not present in the new set; merges existing; persists new ones.
181
+ * If keys changed, {@link MetadataHelper#propagateMetadata} is invoked.
182
+ *
183
+ * @param lt New values.
184
+ * @param ltid LicenseType id.
185
+ * @param token (unused) token.
186
+ * @return 200 OK with updated entity; 404 if not found or app missing.
187
+ */
171188 @PUT
172189 @POST
173190 @Path("/{ltid}")
....@@ -178,7 +195,6 @@
178195 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
179196 public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
180197 LOG.info("Modifying license type with id: {}", ltid);
181
- // EntityManager em = emProvider.get();
182198 LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid));
183199 if (currentlt == null) {
184200 LOG.error("LicenseType with id {} not found in DB", ltid);
....@@ -230,28 +246,23 @@
230246 return Response.ok(currentlt).build();
231247 }
232248
233
- private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException {
234
- Application app = null;
235
- if (applicationId != null) {
236
- app = em.find(Application.class, applicationId);
237
- if (app == null) {
238
- LOG.error("LicenseType application with id {} not found in DB", applicationId);
239
-
240
- throw new SecurityException("License type's app not found with ID: " + applicationId);
241
- }
242
- }
243
- licType.setApplication(app);
244
- }
245
-
249
+ /**
250
+ * delete
251
+ * <p>
252
+ * Delete a license type by id. Requires ADMIN.
253
+ *
254
+ * @param ltid LicenseType id.
255
+ * @param req request (unused).
256
+ * @return 200 OK on success; 404 if not found.
257
+ */
246258 @DELETE
247259 @Path("/{ltid}")
248260 @EnsureTransaction
249261 @Produces({ MediaType.APPLICATION_JSON })
250262 @Securable(roles = Rol.ADMIN)
251263 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
252
- public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
264
+ public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest req) {
253265 LOG.info("Deleting app with id: {}", ltid);
254
- // EntityManager em = emProvider.get();
255266 LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid));
256267 if (app == null) {
257268 LOG.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid);
....@@ -262,4 +273,39 @@
262273 return Response.ok(Utils.createMap("success", true, "id", ltid)).build();
263274 }
264275
276
+ // ---------------------------------------------------------------------
277
+ // Helpers
278
+ // ---------------------------------------------------------------------
279
+
280
+ private Set<String> getMdKeys(Set<LicenseTypeMetadata> mds) {
281
+ Set<String> ids = new HashSet<String>();
282
+ if (mds != null) {
283
+ for (LicenseTypeMetadata md : mds) {
284
+ ids.add(md.getKey());
285
+ }
286
+ }
287
+ return ids;
288
+ }
289
+
290
+ /**
291
+ * setApplication<p>
292
+ * Resolve and set the application of a license type.
293
+ *
294
+ * @param licType target LicenseType.
295
+ * @param applicationId id of the application (nullable).
296
+ * @param em entity manager.
297
+ * @throws SeCurisException if id provided but not found.
298
+ */
299
+ private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException {
300
+ Application app = null;
301
+ if (applicationId != null) {
302
+ app = em.find(Application.class, applicationId);
303
+ if (app == null) {
304
+ LOG.error("LicenseType application with id {} not found in DB", applicationId);
305
+ throw new SecurityException("License type's app not found with ID: " + applicationId);
306
+ }
307
+ }
308
+ licType.setApplication(app);
309
+ }
265310 }
311
+