| .. | .. |
|---|
| 41 | 41 | import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 42 | 42 | import net.curisit.securis.services.helpers.LicenseHelper; |
|---|
| 43 | 43 | import net.curisit.securis.services.helpers.UserHelper; |
|---|
| 44 | +import net.curisit.securis.utils.LicUtils; |
|---|
| 44 | 45 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 45 | 46 | |
|---|
| 46 | 47 | import org.apache.logging.log4j.LogManager; |
|---|
| .. | .. |
|---|
| 147 | 148 | MediaType.APPLICATION_JSON |
|---|
| 148 | 149 | }) |
|---|
| 149 | 150 | @Transactional |
|---|
| 150 | | - public Response create(Pack pack, @Context BasicSecurityContext bsc) { |
|---|
| 151 | + public Response create(Pack pack, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 151 | 152 | LOG.info("Creating new pack"); |
|---|
| 152 | 153 | 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 | + } |
|---|
| 153 | 158 | |
|---|
| 154 | 159 | try { |
|---|
| 155 | 160 | setPackOrganization(pack, pack.getOrgId(), em); |
|---|
| .. | .. |
|---|
| 181 | 186 | return Response.ok(pack).build(); |
|---|
| 182 | 187 | } |
|---|
| 183 | 188 | |
|---|
| 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 | + |
|---|
| 184 | 238 | private void setPackLicenseType(Pack pack, Integer licTypeId, EntityManager em) throws SeCurisException { |
|---|
| 185 | 239 | LicenseType lt = null; |
|---|
| 186 | 240 | if (licTypeId != null) { |
|---|