| .. | .. |
|---|
| 12 | 12 | import javax.persistence.TypedQuery; |
|---|
| 13 | 13 | import javax.ws.rs.Consumes; |
|---|
| 14 | 14 | import javax.ws.rs.DELETE; |
|---|
| 15 | +import javax.ws.rs.FormParam; |
|---|
| 15 | 16 | import javax.ws.rs.GET; |
|---|
| 16 | 17 | import javax.ws.rs.POST; |
|---|
| 17 | 18 | import javax.ws.rs.PUT; |
|---|
| .. | .. |
|---|
| 26 | 27 | import net.curisit.integrity.commons.Utils; |
|---|
| 27 | 28 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 28 | 29 | import net.curisit.securis.SeCurisException; |
|---|
| 30 | +import net.curisit.securis.db.License; |
|---|
| 31 | +import net.curisit.securis.db.LicenseStatus; |
|---|
| 29 | 32 | import net.curisit.securis.db.LicenseType; |
|---|
| 30 | 33 | import net.curisit.securis.db.Organization; |
|---|
| 31 | 34 | import net.curisit.securis.db.Pack; |
|---|
| .. | .. |
|---|
| 36 | 39 | import net.curisit.securis.security.Securable; |
|---|
| 37 | 40 | import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 38 | 41 | import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 42 | +import net.curisit.securis.services.helpers.LicenseHelper; |
|---|
| 43 | +import net.curisit.securis.services.helpers.UserHelper; |
|---|
| 39 | 44 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 40 | 45 | |
|---|
| 41 | 46 | import org.apache.logging.log4j.LogManager; |
|---|
| .. | .. |
|---|
| 60 | 65 | @Inject |
|---|
| 61 | 66 | Provider<EntityManager> emProvider; |
|---|
| 62 | 67 | |
|---|
| 63 | | - public PackResource() { |
|---|
| 64 | | - } |
|---|
| 68 | + @Inject |
|---|
| 69 | + private UserHelper userHelper; |
|---|
| 70 | + |
|---|
| 71 | + @Inject |
|---|
| 72 | + private LicenseHelper licenseHelper; |
|---|
| 65 | 73 | |
|---|
| 66 | 74 | /** |
|---|
| 67 | 75 | * |
|---|
| .. | .. |
|---|
| 297 | 305 | @Produces({ |
|---|
| 298 | 306 | MediaType.APPLICATION_JSON |
|---|
| 299 | 307 | }) |
|---|
| 300 | | - public Response cancel(@PathParam("packId") Integer packId) throws SeCurisServiceException { |
|---|
| 301 | | - LOG.info("Putting On hold pack with id: {}", packId); |
|---|
| 308 | + public Response cancel(@PathParam("packId") Integer packId, @FormParam("reason") String reason, @Context BasicSecurityContext bsc) |
|---|
| 309 | + throws SeCurisServiceException { |
|---|
| 310 | + LOG.info("Cancelling pack with id: {}", packId); |
|---|
| 302 | 311 | EntityManager em = emProvider.get(); |
|---|
| 303 | 312 | |
|---|
| 304 | 313 | Pack currentPack = em.find(Pack.class, packId); |
|---|
| .. | .. |
|---|
| 308 | 317 | throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be cancelled in status: " + currentPack.getStatus().name()); |
|---|
| 309 | 318 | } |
|---|
| 310 | 319 | |
|---|
| 320 | + Set<License> licenses = currentPack.getLicenses(); |
|---|
| 321 | + for (License license : licenses) { |
|---|
| 322 | + if (license.getStatus() == LicenseStatus.ACTIVE || license.getStatus() == LicenseStatus.PRE_ACTIVE) { |
|---|
| 323 | + licenseHelper.cancelLicense(license, "Pack cancellation. " + reason, bsc, em); |
|---|
| 324 | + } |
|---|
| 325 | + } |
|---|
| 311 | 326 | currentPack.setStatus(PackStatus.CANCELLED); |
|---|
| 312 | 327 | em.persist(currentPack); |
|---|
| 313 | 328 | |
|---|
| .. | .. |
|---|
| 334 | 349 | @Produces({ |
|---|
| 335 | 350 | MediaType.APPLICATION_JSON |
|---|
| 336 | 351 | }) |
|---|
| 337 | | - public Response delete(@PathParam("packId") String packId) { |
|---|
| 352 | + public Response delete(@PathParam("packId") String packId) throws SeCurisServiceException { |
|---|
| 338 | 353 | LOG.info("Deleting pack with id: {}", packId); |
|---|
| 339 | 354 | EntityManager em = emProvider.get(); |
|---|
| 340 | 355 | Pack pack = em.find(Pack.class, Integer.parseInt(packId)); |
|---|
| .. | .. |
|---|
| 343 | 358 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack was not found, ID: " + packId) |
|---|
| 344 | 359 | .build(); |
|---|
| 345 | 360 | } |
|---|
| 346 | | - if (pack.getMetadata() != null) { |
|---|
| 347 | | - for (PackMetadata md : pack.getMetadata()) { |
|---|
| 348 | | - em.remove(md); |
|---|
| 361 | + // Pack metadata is removed in cascade automatically. |
|---|
| 362 | + |
|---|
| 363 | + Set<License> licenses = pack.getLicenses(); |
|---|
| 364 | + for (License license : licenses) { |
|---|
| 365 | + if (license.getStatus() == LicenseStatus.ACTIVE || license.getStatus() == LicenseStatus.PRE_ACTIVE) { |
|---|
| 366 | + throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "An active license cannot be deleted. License code: " + license.getCode()); |
|---|
| 349 | 367 | } |
|---|
| 368 | + em.remove(license); |
|---|
| 350 | 369 | } |
|---|
| 351 | 370 | |
|---|
| 352 | 371 | em.remove(pack); |
|---|