rsanchez
2014-12-01 85e2a65874fcd41771b30ebfff93f86edd4f32b3
securis/src/main/java/net/curisit/securis/services/PackResource.java
....@@ -41,6 +41,7 @@
4141 import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
4242 import net.curisit.securis.services.helpers.LicenseHelper;
4343 import net.curisit.securis.services.helpers.UserHelper;
44
+import net.curisit.securis.utils.LicUtils;
4445 import net.curisit.securis.utils.TokenHelper;
4546
4647 import org.apache.logging.log4j.LogManager;
....@@ -147,9 +148,13 @@
147148 MediaType.APPLICATION_JSON
148149 })
149150 @Transactional
150
- public Response create(Pack pack, @Context BasicSecurityContext bsc) {
151
+ public Response create(Pack pack, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
151152 LOG.info("Creating new pack");
152153 EntityManager em = emProvider.get();
154
+
155
+ if (checkIfCodeExists(pack.getCode(), em)) {
156
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The pack code is already used in an existing pack");
157
+ }
153158
154159 try {
155160 setPackOrganization(pack, pack.getOrgId(), em);
....@@ -181,6 +186,55 @@
181186 return Response.ok(pack).build();
182187 }
183188
189
+ /**
190
+ * Check if there is some pack with the same code
191
+ *
192
+ * @param code
193
+ * Pack code
194
+ * @param em
195
+ * DB session object
196
+ * @return <code>true</code> if code is already used, <code>false</code>
197
+ * otherwise
198
+ */
199
+ private boolean checkIfCodeExists(String code, EntityManager em) {
200
+ TypedQuery<Pack> query = em.createNamedQuery("pack-by-code", Pack.class);
201
+ query.setParameter("code", code);
202
+ int packs = query.getResultList().size();
203
+ return packs > 0;
204
+ }
205
+
206
+ private int getNextCodeSuffix(int packId, EntityManager em) {
207
+ TypedQuery<Integer> query = em.createNamedQuery("last-code-suffix-used-in-pack", Integer.class);
208
+ query.setParameter("packId", packId);
209
+ Integer lastCodeSuffix = query.getSingleResult();
210
+ return lastCodeSuffix == null ? 1 : lastCodeSuffix + 1;
211
+ }
212
+
213
+ /**
214
+ *
215
+ * @return The next available code suffix in pack for license code
216
+ * @throws SeCurisServiceException
217
+ */
218
+ @GET
219
+ @Path("/{packId}/next_license_code")
220
+ @Securable
221
+ @Produces({
222
+ MediaType.TEXT_PLAIN
223
+ })
224
+ public Response getCodeSuffix(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
225
+ EntityManager em = emProvider.get();
226
+
227
+ if (packId == null) {
228
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The pack code is mandatory");
229
+ }
230
+ Integer codeSuffix = getNextCodeSuffix(packId, em);
231
+ Pack pack = em.find(Pack.class, packId);
232
+ ;
233
+
234
+ String licCode = LicUtils.getLicenseCode(pack.getCode(), codeSuffix);
235
+ return Response.ok(licCode).build();
236
+ }
237
+
184238 private void setPackLicenseType(Pack pack, Integer licTypeId, EntityManager em) throws SeCurisException {
185239 LicenseType lt = null;
186240 if (licTypeId != null) {