From 523edc2956a42bda7d33884fcbbb6018addda4b0 Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Tue, 18 Apr 2017 17:07:34 +0000
Subject: [PATCH] #3582 feature - Changed API to take in account pack data, (valid end date, status, ...) and some minor fixes

---
 securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java |  223 ++++++++++++++++++++++++++++---------------------------
 1 files changed, 114 insertions(+), 109 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java b/securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java
index 512957b..d8d6e20 100644
--- a/securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java
+++ b/securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java
@@ -13,6 +13,11 @@
 import javax.persistence.EntityManager;
 import javax.persistence.TypedQuery;
 
+import org.apache.commons.io.FileUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import net.curisit.integrity.exception.CurisRuntimeException;
 import net.curisit.securis.beans.LicenseBean;
 import net.curisit.securis.db.License;
 import net.curisit.securis.db.LicenseHistory;
@@ -24,128 +29,128 @@
 import net.curisit.securis.services.exception.SeCurisServiceException;
 import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
 
-import org.apache.commons.io.FileUtils;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-
 @ApplicationScoped
 public class LicenseHelper {
 
-    @SuppressWarnings("unused")
-    private static final Logger LOG = LogManager.getLogger(LicenseHelper.class);
-    private static final long MS_PER_DAY = 24L * 3600L * 1000L;
-    private static final int DEFAULT_VALID_LIC_PERIOD = 7;
+	@SuppressWarnings("unused")
+	private static final Logger LOG = LogManager.getLogger(LicenseHelper.class);
+	private static final long MS_PER_DAY = 24L * 3600L * 1000L;
 
-    @Inject
-    private UserHelper userHelper;
+	@Inject
+	private UserHelper userHelper;
 
-    /**
-     * Cancel the license
-     * 
-     * @param lic
-     * @param em
-     */
-    public void cancelLicense(License lic, String reason, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
-        lic.setStatus(LicenseStatus.CANCELLED);
-        lic.setCancelledById(bsc.getUserPrincipal().getName());
-        lic.setModificationTimestamp(new Date());
-        em.persist(lic);
+	/**
+	 * Cancel the license
+	 * 
+	 * @param lic
+	 * @param em
+	 */
+	public void cancelLicense(License lic, String reason, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
+		lic.setStatus(LicenseStatus.CANCELLED);
+		lic.setCancelledById(bsc.getUserPrincipal().getName());
+		lic.setModificationTimestamp(new Date());
+		em.persist(lic);
 
-        em.persist(createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.CANCEL, "Cancellation reason: " + reason));
-    }
+		em.persist(createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.CANCEL, "Cancellation reason: " + reason));
+	}
 
-    /**
-     * Validates that the passed license is still valid
-     * 
-     * @param lic
-     * @param reason
-     * @param bsc
-     * @param em
-     * @throws SeCurisServiceException
-     */
-    public void assertLicenseStatusIsActive(LicenseBean licBean, EntityManager em) throws SeCurisServiceException {
-        License lic = License.findLicenseByCode(licBean.getLicenseCode(), em);
-        if (lic == null) {
-            throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Current license code doesn't exist");
-        }
-        if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
-            throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "Current license in not active");
-        }
-    }
+	/**
+	 * Validates that the passed license exists and is still valid
+	 * 
+	 * @param licBean
+	 * @param em
+	 * @return The License instance in DB
+	 * @throws SeCurisServiceException
+	 */
+	public License getActiveLicenseFromDB(LicenseBean licBean, EntityManager em) throws SeCurisServiceException {
+		License lic = License.findLicenseByCode(licBean.getLicenseCode(), em);
+		if (lic == null) {
+			throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "Current license code doesn't exist");
+		}
+		if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
+			throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "Current license in not active");
+		}
+		return lic;
+	}
 
-    public LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) {
-        LicenseHistory lh = new LicenseHistory();
-        lh.setLicense(lic);
-        lh.setUser(user);
-        lh.setCreationTimestamp(new Date());
-        lh.setAction(action);
-        lh.setComments(comments);
-        return lh;
-    }
+	public LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) {
+		LicenseHistory lh = new LicenseHistory();
+		lh.setLicense(lic);
+		lh.setUser(user);
+		lh.setCreationTimestamp(new Date());
+		lh.setAction(action);
+		lh.setComments(comments);
+		return lh;
+	}
 
-    public LicenseHistory createLicenseHistoryAction(License lic, User user, String action) {
-        return createLicenseHistoryAction(lic, user, action, null);
-    }
+	public LicenseHistory createLicenseHistoryAction(License lic, User user, String action) {
+		return createLicenseHistoryAction(lic, user, action, null);
+	}
 
-    /**
-     * Create a license file in a temporary directory
-     * 
-     * @param lic
-     * @param licFileName
-     * @return
-     * @throws IOException
-     */
-    public File createTemporaryLicenseFile(License lic, String licFileName) throws IOException {
-        File f = Files.createTempDirectory("securis-server").toFile();
-        f = new File(f, licFileName);
-        FileUtils.writeStringToFile(f, lic.getLicenseData());
-        return f;
-    }
+	/**
+	 * Create a license file in a temporary directory
+	 * 
+	 * @param lic
+	 * @param licFileName
+	 * @return
+	 * @throws IOException
+	 */
+	public File createTemporaryLicenseFile(License lic, String licFileName) throws IOException {
+		File f = Files.createTempDirectory("securis-server").toFile();
+		f = new File(f, licFileName);
+		FileUtils.writeStringToFile(f, lic.getLicenseData());
+		return f;
+	}
 
-    public Map<String, Object> extractPackMetadata(Set<PackMetadata> packMetadata) {
-        Map<String, Object> metadata = new HashMap<>();
-        for (PackMetadata md : packMetadata) {
-            metadata.put(md.getKey(), md.getValue());
-        }
+	public Map<String, Object> extractPackMetadata(Set<PackMetadata> packMetadata) {
+		Map<String, Object> metadata = new HashMap<>();
+		for (PackMetadata md : packMetadata) {
+			metadata.put(md.getKey(), md.getValue());
+		}
 
-        return metadata;
-    }
+		return metadata;
+	}
 
-    /**
-     * If the action is a renew the expiration date is got form pack end valid
-     * date, if the action is a pre-activation the expiration date is calculated
-     * using the pack default valid period
-     * 
-     * @param pack
-     * @param isPreActivation
-     * @return
-     */
-    public Date getExpirationDateFromPack(Pack pack, boolean isPreActivation) {
-        Long validPeriod;
-        if (isPreActivation) {
-            validPeriod = pack.getPreactivationValidPeriod() * MS_PER_DAY;
-        } else {
-            if (pack.getRenewValidPeriod() <= 0) {
-                return pack.getEndValidDate();
-            }
-            validPeriod = pack.getRenewValidPeriod() * MS_PER_DAY;
-        }
-        Date expirationDate = new Date(new Date().getTime() + validPeriod);
-        return expirationDate;
-    }
+	/**
+	 * If the action is a renew the expiration date is got form pack end valid
+	 * date, if the action is a pre-activation the expiration date is calculated
+	 * using the pack default valid period
+	 * 
+	 * @param pack
+	 * @param isPreActivation
+	 * @return
+	 */
+	public Date getExpirationDateFromPack(Pack pack, boolean isPreActivation) {
+		Long validPeriod;
+		if (pack.getEndValidDate().before(new Date())) {
+			throw new CurisRuntimeException("Pack end valid period is reached, no new licenses can be activated.");
+		}
+		if (isPreActivation) {
+			validPeriod = pack.getPreactivationValidPeriod() * MS_PER_DAY;
+		} else {
+			if (pack.getRenewValidPeriod() <= 0) {
+				return pack.getEndValidDate();
+			}
+			long renewPeriod = pack.getRenewValidPeriod() * MS_PER_DAY;
+			long expirationPeriod = pack.getEndValidDate().getTime() - new Date().getTime();
+			validPeriod = renewPeriod < expirationPeriod ? renewPeriod : expirationPeriod;
+		}
+		Date expirationDate = new Date(new Date().getTime() + validPeriod);
+		return expirationDate;
+	}
 
-    /**
-     * Get the next free code suffis for a given Pack
-     * 
-     * @param packId
-     * @param em
-     * @return
-     */
-    public int getNextCodeSuffix(int packId, EntityManager em) {
-        TypedQuery<Integer> query = em.createNamedQuery("last-code-suffix-used-in-pack", Integer.class);
-        query.setParameter("packId", packId);
-        Integer lastCodeSuffix = query.getSingleResult();
-        return lastCodeSuffix == null ? 1 : lastCodeSuffix + 1;
-    }
+	/**
+	 * Get the next free code suffis for a given Pack
+	 * 
+	 * @param packId
+	 * @param em
+	 * @return
+	 */
+	public int getNextCodeSuffix(int packId, EntityManager em) {
+		TypedQuery<Integer> query = em.createNamedQuery("last-code-suffix-used-in-pack", Integer.class);
+		query.setParameter("packId", packId);
+		Integer lastCodeSuffix = query.getSingleResult();
+		return lastCodeSuffix == null ? 1 : lastCodeSuffix + 1;
+	}
 
 }

--
Gitblit v1.3.2