From 85e2a65874fcd41771b30ebfff93f86edd4f32b3 Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Mon, 01 Dec 2014 14:59:34 +0000
Subject: [PATCH] #396 fix - Added automated license code generation and other minor fixes
---
securis/src/main/java/net/curisit/securis/services/PackResource.java | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 55 insertions(+), 1 deletions(-)
diff --git a/securis/src/main/java/net/curisit/securis/services/PackResource.java b/securis/src/main/java/net/curisit/securis/services/PackResource.java
index 8a29756..a584779 100644
--- a/securis/src/main/java/net/curisit/securis/services/PackResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/PackResource.java
@@ -41,6 +41,7 @@
import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
import net.curisit.securis.services.helpers.LicenseHelper;
import net.curisit.securis.services.helpers.UserHelper;
+import net.curisit.securis.utils.LicUtils;
import net.curisit.securis.utils.TokenHelper;
import org.apache.logging.log4j.LogManager;
@@ -147,9 +148,13 @@
MediaType.APPLICATION_JSON
})
@Transactional
- public Response create(Pack pack, @Context BasicSecurityContext bsc) {
+ public Response create(Pack pack, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
LOG.info("Creating new pack");
EntityManager em = emProvider.get();
+
+ if (checkIfCodeExists(pack.getCode(), em)) {
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The pack code is already used in an existing pack");
+ }
try {
setPackOrganization(pack, pack.getOrgId(), em);
@@ -181,6 +186,55 @@
return Response.ok(pack).build();
}
+ /**
+ * Check if there is some pack with the same code
+ *
+ * @param code
+ * Pack code
+ * @param em
+ * DB session object
+ * @return <code>true</code> if code is already used, <code>false</code>
+ * otherwise
+ */
+ private boolean checkIfCodeExists(String code, EntityManager em) {
+ TypedQuery<Pack> query = em.createNamedQuery("pack-by-code", Pack.class);
+ query.setParameter("code", code);
+ int packs = query.getResultList().size();
+ return packs > 0;
+ }
+
+ private 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;
+ }
+
+ /**
+ *
+ * @return The next available code suffix in pack for license code
+ * @throws SeCurisServiceException
+ */
+ @GET
+ @Path("/{packId}/next_license_code")
+ @Securable
+ @Produces({
+ MediaType.TEXT_PLAIN
+ })
+ public Response getCodeSuffix(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
+ EntityManager em = emProvider.get();
+
+ if (packId == null) {
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The pack code is mandatory");
+ }
+ Integer codeSuffix = getNextCodeSuffix(packId, em);
+ Pack pack = em.find(Pack.class, packId);
+ ;
+
+ String licCode = LicUtils.getLicenseCode(pack.getCode(), codeSuffix);
+ return Response.ok(licCode).build();
+ }
+
private void setPackLicenseType(Pack pack, Integer licTypeId, EntityManager em) throws SeCurisException {
LicenseType lt = null;
if (licTypeId != null) {
--
Gitblit v1.3.2