rsanchez
2014-10-28 1d9d7b5f03b3e7b6af5600574a0ae6053843b77b
securis/src/main/java/net/curisit/securis/services/PackResource.java
....@@ -12,6 +12,7 @@
1212 import javax.persistence.TypedQuery;
1313 import javax.ws.rs.Consumes;
1414 import javax.ws.rs.DELETE;
15
+import javax.ws.rs.FormParam;
1516 import javax.ws.rs.GET;
1617 import javax.ws.rs.POST;
1718 import javax.ws.rs.PUT;
....@@ -26,6 +27,8 @@
2627 import net.curisit.integrity.commons.Utils;
2728 import net.curisit.securis.DefaultExceptionHandler;
2829 import net.curisit.securis.SeCurisException;
30
+import net.curisit.securis.db.License;
31
+import net.curisit.securis.db.LicenseStatus;
2932 import net.curisit.securis.db.LicenseType;
3033 import net.curisit.securis.db.Organization;
3134 import net.curisit.securis.db.Pack;
....@@ -36,6 +39,8 @@
3639 import net.curisit.securis.security.Securable;
3740 import net.curisit.securis.services.exception.SeCurisServiceException;
3841 import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
42
+import net.curisit.securis.services.helpers.LicenseHelper;
43
+import net.curisit.securis.services.helpers.UserHelper;
3944 import net.curisit.securis.utils.TokenHelper;
4045
4146 import org.apache.logging.log4j.LogManager;
....@@ -60,8 +65,11 @@
6065 @Inject
6166 Provider<EntityManager> emProvider;
6267
63
- public PackResource() {
64
- }
68
+ @Inject
69
+ private UserHelper userHelper;
70
+
71
+ @Inject
72
+ private LicenseHelper licenseHelper;
6573
6674 /**
6775 *
....@@ -297,8 +305,9 @@
297305 @Produces({
298306 MediaType.APPLICATION_JSON
299307 })
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);
302311 EntityManager em = emProvider.get();
303312
304313 Pack currentPack = em.find(Pack.class, packId);
....@@ -308,6 +317,12 @@
308317 throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be cancelled in status: " + currentPack.getStatus().name());
309318 }
310319
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
+ }
311326 currentPack.setStatus(PackStatus.CANCELLED);
312327 em.persist(currentPack);
313328
....@@ -334,7 +349,7 @@
334349 @Produces({
335350 MediaType.APPLICATION_JSON
336351 })
337
- public Response delete(@PathParam("packId") String packId) {
352
+ public Response delete(@PathParam("packId") String packId) throws SeCurisServiceException {
338353 LOG.info("Deleting pack with id: {}", packId);
339354 EntityManager em = emProvider.get();
340355 Pack pack = em.find(Pack.class, Integer.parseInt(packId));
....@@ -343,10 +358,14 @@
343358 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack was not found, ID: " + packId)
344359 .build();
345360 }
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());
349367 }
368
+ em.remove(license);
350369 }
351370
352371 em.remove(pack);