Joaquín Reñé
2026-03-27 4ee50e257b32f6ec0f72907305d1f2b1212808a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
 */
package net.curisit.securis.services;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.HeaderParam;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.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;
import net.curisit.securis.db.Application;
import net.curisit.securis.db.LicenseType;
import net.curisit.securis.db.LicenseTypeMetadata;
import net.curisit.securis.db.User.Rol;
import net.curisit.securis.ioc.EnsureTransaction;
import net.curisit.securis.security.BasicSecurityContext;
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;
/**
 * 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 MetadataHelper metadataHelper;
   @Context EntityManager em;
   public LicenseTypeResource() { }
   /**
    * 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("/")
   @Produces({ MediaType.APPLICATION_JSON })
   @Securable
   public Response index(@Context BasicSecurityContext bsc) {
       LOG.info("Getting license types list ");
       em.clear();
       TypedQuery<LicenseType> q;
       if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
           q = em.createNamedQuery("list-license_types", LicenseType.class);
       } else {
           if (bsc.getApplicationsIds() == null || bsc.getApplicationsIds().isEmpty()) {
               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();
   }
   /**
    * 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}")
   @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();
       }
       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();
   }
   /**
    * 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)
   @Produces({ MediaType.APPLICATION_JSON })
   @EnsureTransaction
   @Securable(roles = Rol.ADMIN)
   @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
   public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
       LOG.info("Creating new license type");
       try {
           setApplication(lt, lt.getApplicationId(), em);
       } catch (SeCurisException e) {
           return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
       }
       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();
       }
       lt.setCreationTimestamp(new Date());
       em.persist(lt);
       Set<LicenseTypeMetadata> newMD = lt.getMetadata();
       if (newMD != null) {
           for (LicenseTypeMetadata md : newMD) {
               md.setLicenseType(lt);
               em.persist(md);
           }
       }
       lt.setMetadata(newMD);
       return Response.ok(lt).build();
   }
   /**
    * 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}")
   @EnsureTransaction
   @Consumes(MediaType.APPLICATION_JSON)
   @Produces({ MediaType.APPLICATION_JSON })
   @Securable(roles = Rol.ADMIN)
   @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);
       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();
       }
       try {
           setApplication(currentlt, lt.getApplicationId(), em);
       } catch (SeCurisException e) {
           return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
       }
       currentlt.setCode(lt.getCode());
       currentlt.setName(lt.getName());
       currentlt.setDescription(lt.getDescription());
       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);
               }
           }
           if (newMD != null) {
               Set<String> oldMdKeys = getMdKeys(oldMD);
               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);
       }
       return Response.ok(currentlt).build();
   }
   /**
    * 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 req) {
       LOG.info("Deleting app with id: {}", ltid);
       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();
   }
   // ---------------------------------------------------------------------
   // 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);
   }
}