From b33557d3faf3e45eff3d3e19f4a1549ffe907b4c Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Tue, 18 Nov 2014 10:52:52 +0000
Subject: [PATCH] #396 fix - Corrected actions: "Download" and "Send email"

---
 securis/src/main/java/net/curisit/securis/services/LicenseResource.java |   17 +++++++++++------
 1 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/services/LicenseResource.java b/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
index 2428bb1..d4a1c04 100644
--- a/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
@@ -165,7 +165,7 @@
             LOG.error("License with id {} has not license file generated", licId, bsc.getUserPrincipal());
             throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License has not contain data to generate license file");
         }
-        if (License.Status.isActionValid(License.Action.DOWNLOAD, lic.getStatus())) {
+        if (!License.Status.isActionValid(License.Action.DOWNLOAD, lic.getStatus())) {
             LOG.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal());
             throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License is not active, so It can not be downloaded");
         }
@@ -198,7 +198,7 @@
         EntityManager em = emProvider.get();
         License lic = getCurrentLicense(licId, bsc, em);
 
-        if (License.Status.isActionValid(License.Action.ACTIVATION, lic.getStatus())) {
+        if (!License.Status.isActionValid(License.Action.ACTIVATION, lic.getStatus())) {
             LOG.error("License with id {} can not be activated from current license status", licId);
             throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId
                     + " can not be activated from the current license status");
@@ -250,10 +250,14 @@
             throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "There is no license file available");
         }
 
+        if (lic.getFullName() == null) {
+            throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "Please add an user name in license data to send it the license file");
+        }
+
         User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
         try {
             String subject = MessageFormat.format(Params.get(Params.KEYS.EMAIL_LIC_DEFAULT_SUBJECT), lic.getPack().getAppName());
-            String email_tpl = IOUtils.toString(this.getClass().getResourceAsStream("/lic_email_template.en"));
+            String email_tpl = IOUtils.toString(this.getClass().getResourceAsStream("/lic_email.template.en"));
             String body = MessageFormat.format(email_tpl, lic.getFullName(), app.getName());
             licFile = licenseHelper.createTemporaryLicenseFile(lic, app.getLicenseFilename());
 
@@ -264,11 +268,12 @@
         } finally {
             if (licFile != null) {
                 licFile.delete();
+                licFile.getParentFile().delete();
             }
         }
 
         lic.setModificationTimestamp(new Date());
-        em.persist(lic);
+        em.merge(lic);
         em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail()));
         return Response.ok(lic).build();
     }
@@ -296,7 +301,7 @@
         EntityManager em = emProvider.get();
         License lic = getCurrentLicense(licId, bsc, em);
 
-        if (License.Status.isActionValid(License.Action.CANCEL, lic.getStatus())) {
+        if (!License.Status.isActionValid(License.Action.CANCEL, lic.getStatus())) {
             LOG.error("License with id {} can not be canceled from current license status", licId);
             throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId
                     + " can not be canceled from the current license status");
@@ -499,7 +504,7 @@
         EntityManager em = emProvider.get();
         License lic = getCurrentLicense(licId, bsc, em);
 
-        if (License.Status.isActionValid(License.Action.DELETE, lic.getStatus())) {
+        if (!License.Status.isActionValid(License.Action.DELETE, lic.getStatus())) {
             LOG.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus());
             throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License can not be deleted in current status: " + lic.getStatus().name());
         }

--
Gitblit v1.3.2