| .. | .. |
|---|
| 24 | 24 | import javax.ws.rs.core.Response; |
|---|
| 25 | 25 | import javax.ws.rs.core.Response.Status; |
|---|
| 26 | 26 | |
|---|
| 27 | +import org.apache.logging.log4j.LogManager; |
|---|
| 28 | +import org.apache.logging.log4j.Logger; |
|---|
| 29 | + |
|---|
| 27 | 30 | import net.curisit.integrity.commons.Utils; |
|---|
| 28 | 31 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 29 | 32 | import net.curisit.securis.SeCurisException; |
|---|
| .. | .. |
|---|
| 44 | 47 | import net.curisit.securis.utils.LicUtils; |
|---|
| 45 | 48 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 46 | 49 | |
|---|
| 47 | | -import org.apache.logging.log4j.LogManager; |
|---|
| 48 | | -import org.apache.logging.log4j.Logger; |
|---|
| 49 | | - |
|---|
| 50 | 50 | /** |
|---|
| 51 | 51 | * Pack resource, this service will provide methods to create, modify and delete |
|---|
| 52 | 52 | * packs |
|---|
| .. | .. |
|---|
| 56 | 56 | @Path("/pack") |
|---|
| 57 | 57 | public class PackResource { |
|---|
| 58 | 58 | |
|---|
| 59 | | - private static final Logger LOG = LogManager.getLogger(PackResource.class); |
|---|
| 59 | + private static final Logger LOG = LogManager.getLogger(PackResource.class); |
|---|
| 60 | 60 | |
|---|
| 61 | | - @Inject |
|---|
| 62 | | - TokenHelper tokenHelper; |
|---|
| 61 | + @Inject |
|---|
| 62 | + TokenHelper tokenHelper; |
|---|
| 63 | 63 | |
|---|
| 64 | | - @Context |
|---|
| 65 | | - EntityManager em; |
|---|
| 64 | + @Context |
|---|
| 65 | + EntityManager em; |
|---|
| 66 | 66 | |
|---|
| 67 | | - @Inject |
|---|
| 68 | | - private LicenseHelper licenseHelper; |
|---|
| 67 | + @Inject |
|---|
| 68 | + private LicenseHelper licenseHelper; |
|---|
| 69 | 69 | |
|---|
| 70 | | - /** |
|---|
| 71 | | - * |
|---|
| 72 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 73 | | - */ |
|---|
| 74 | | - @GET |
|---|
| 75 | | - @Path("/") |
|---|
| 76 | | - @Securable |
|---|
| 77 | | - @Produces({ |
|---|
| 78 | | - MediaType.APPLICATION_JSON |
|---|
| 79 | | - }) |
|---|
| 80 | | - public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 81 | | - LOG.info("Getting packs list "); |
|---|
| 70 | + /** |
|---|
| 71 | + * |
|---|
| 72 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 73 | + */ |
|---|
| 74 | + @GET |
|---|
| 75 | + @Path("/") |
|---|
| 76 | + @Securable |
|---|
| 77 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 78 | + public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 79 | + LOG.info("Getting packs list "); |
|---|
| 82 | 80 | |
|---|
| 83 | | - // EntityManager em = emProvider.get(); |
|---|
| 84 | | - em.clear(); |
|---|
| 81 | + // EntityManager em = emProvider.get(); |
|---|
| 82 | + em.clear(); |
|---|
| 85 | 83 | |
|---|
| 86 | | - TypedQuery<Pack> q; |
|---|
| 87 | | - if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 88 | | - LOG.info("Getting all packs for user: " + bsc.getUserPrincipal()); |
|---|
| 89 | | - q = em.createNamedQuery("list-packs", Pack.class); |
|---|
| 90 | | - } else { |
|---|
| 91 | | - q = em.createNamedQuery("list-packs-by-orgs", Pack.class); |
|---|
| 92 | | - if (bsc.getOrganizationsIds() == null) { |
|---|
| 93 | | - Response.ok().build(); |
|---|
| 94 | | - } |
|---|
| 95 | | - q.setParameter("list_ids", bsc.getOrganizationsIds()); |
|---|
| 96 | | - } |
|---|
| 84 | + TypedQuery<Pack> q; |
|---|
| 85 | + if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 86 | + LOG.info("Getting all packs for user: " + bsc.getUserPrincipal()); |
|---|
| 87 | + q = em.createNamedQuery("list-packs", Pack.class); |
|---|
| 88 | + } else { |
|---|
| 89 | + q = em.createNamedQuery("list-packs-by-orgs", Pack.class); |
|---|
| 90 | + if (bsc.getOrganizationsIds() == null) { |
|---|
| 91 | + Response.ok().build(); |
|---|
| 92 | + } |
|---|
| 93 | + q.setParameter("list_ids", bsc.getOrganizationsIds()); |
|---|
| 94 | + } |
|---|
| 97 | 95 | |
|---|
| 98 | | - List<Pack> list = q.getResultList(); |
|---|
| 96 | + List<Pack> list = q.getResultList(); |
|---|
| 99 | 97 | |
|---|
| 100 | | - return Response.ok(list).build(); |
|---|
| 101 | | - } |
|---|
| 98 | + return Response.ok(list).build(); |
|---|
| 99 | + } |
|---|
| 102 | 100 | |
|---|
| 103 | | - private Response generateErrorUnathorizedAccess(Pack pack, Principal user) { |
|---|
| 104 | | - LOG.error("Pack with id {} not accesible by user {}", pack, user); |
|---|
| 105 | | - return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack").build(); |
|---|
| 106 | | - } |
|---|
| 101 | + private Response generateErrorUnathorizedAccess(Pack pack, Principal user) { |
|---|
| 102 | + LOG.error("Pack with id {} not accesible by user {}", pack, user); |
|---|
| 103 | + return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack").build(); |
|---|
| 104 | + } |
|---|
| 107 | 105 | |
|---|
| 108 | | - /** |
|---|
| 109 | | - * |
|---|
| 110 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 111 | | - */ |
|---|
| 112 | | - @GET |
|---|
| 113 | | - @Path("/{packId}") |
|---|
| 114 | | - @Securable |
|---|
| 115 | | - @Produces({ |
|---|
| 116 | | - MediaType.APPLICATION_JSON |
|---|
| 117 | | - }) |
|---|
| 118 | | - public Response get(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) { |
|---|
| 119 | | - LOG.info("Getting pack data for id: {}: ", packId); |
|---|
| 120 | | - if (packId == null || "".equals(packId)) { |
|---|
| 121 | | - LOG.error("Pack ID is mandatory"); |
|---|
| 122 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 123 | | - } |
|---|
| 106 | + /** |
|---|
| 107 | + * |
|---|
| 108 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 109 | + */ |
|---|
| 110 | + @GET |
|---|
| 111 | + @Path("/{packId}") |
|---|
| 112 | + @Securable |
|---|
| 113 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 114 | + public Response get(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) { |
|---|
| 115 | + LOG.info("Getting pack data for id: {}: ", packId); |
|---|
| 116 | + if (packId == null || "".equals(packId)) { |
|---|
| 117 | + LOG.error("Pack ID is mandatory"); |
|---|
| 118 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 119 | + } |
|---|
| 124 | 120 | |
|---|
| 125 | | - // EntityManager em = emProvider.get(); |
|---|
| 126 | | - em.clear(); |
|---|
| 127 | | - Pack pack = em.find(Pack.class, packId); |
|---|
| 128 | | - if (pack == null) { |
|---|
| 129 | | - LOG.error("Pack with id {} not found in DB", packId); |
|---|
| 130 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 131 | | - } |
|---|
| 132 | | - if (bsc.isUserInRole(BasicSecurityContext.ROL_ADVANCE) |
|---|
| 133 | | - && (bsc.getOrganizationsIds() == null || !bsc.getOrganizationsIds().contains(pack.getOrgId()))) { |
|---|
| 134 | | - return generateErrorUnathorizedAccess(pack, bsc.getUserPrincipal()); |
|---|
| 135 | | - } |
|---|
| 136 | | - return Response.ok(pack).build(); |
|---|
| 137 | | - } |
|---|
| 121 | + // EntityManager em = emProvider.get(); |
|---|
| 122 | + em.clear(); |
|---|
| 123 | + Pack pack = em.find(Pack.class, packId); |
|---|
| 124 | + if (pack == null) { |
|---|
| 125 | + LOG.error("Pack with id {} not found in DB", packId); |
|---|
| 126 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 127 | + } |
|---|
| 128 | + if (bsc.isUserInRole(BasicSecurityContext.ROL_ADVANCE) && (bsc.getOrganizationsIds() == null || !bsc.getOrganizationsIds().contains(pack.getOrgId()))) { |
|---|
| 129 | + return generateErrorUnathorizedAccess(pack, bsc.getUserPrincipal()); |
|---|
| 130 | + } |
|---|
| 131 | + return Response.ok(pack).build(); |
|---|
| 132 | + } |
|---|
| 138 | 133 | |
|---|
| 139 | | - @POST |
|---|
| 140 | | - @Path("/") |
|---|
| 141 | | - @Securable |
|---|
| 142 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 143 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 144 | | - @Produces({ |
|---|
| 145 | | - MediaType.APPLICATION_JSON |
|---|
| 146 | | - }) |
|---|
| 147 | | - @EnsureTransaction |
|---|
| 148 | | - public Response create(Pack pack, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 149 | | - LOG.info("Creating new pack"); |
|---|
| 150 | | - // EntityManager em = emProvider.get(); |
|---|
| 134 | + @POST |
|---|
| 135 | + @Path("/") |
|---|
| 136 | + @Securable |
|---|
| 137 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 138 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 139 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 140 | + @EnsureTransaction |
|---|
| 141 | + public Response create(Pack pack, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 142 | + LOG.info("Creating new pack"); |
|---|
| 143 | + // EntityManager em = emProvider.get(); |
|---|
| 151 | 144 | |
|---|
| 152 | | - if (checkIfCodeExists(pack.getCode(), em)) { |
|---|
| 153 | | - throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The pack code is already used in an existing pack"); |
|---|
| 154 | | - } |
|---|
| 145 | + if (checkIfCodeExists(pack.getCode(), em)) { |
|---|
| 146 | + throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The pack code is already used in an existing pack"); |
|---|
| 147 | + } |
|---|
| 155 | 148 | |
|---|
| 156 | | - try { |
|---|
| 157 | | - setPackOrganization(pack, pack.getOrgId(), em); |
|---|
| 158 | | - } catch (SeCurisException e) { |
|---|
| 159 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 160 | | - } |
|---|
| 149 | + try { |
|---|
| 150 | + setPackOrganization(pack, pack.getOrgId(), em); |
|---|
| 151 | + } catch (SeCurisException e) { |
|---|
| 152 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 153 | + } |
|---|
| 161 | 154 | |
|---|
| 162 | | - try { |
|---|
| 163 | | - setPackLicenseType(pack, pack.getLicTypeId(), em); |
|---|
| 164 | | - } catch (SeCurisException e) { |
|---|
| 165 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 166 | | - } |
|---|
| 155 | + try { |
|---|
| 156 | + setPackLicenseType(pack, pack.getLicTypeId(), em); |
|---|
| 157 | + } catch (SeCurisException e) { |
|---|
| 158 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 159 | + } |
|---|
| 167 | 160 | |
|---|
| 168 | | - User user = em.find(User.class, bsc.getUserPrincipal().getName()); |
|---|
| 161 | + User user = em.find(User.class, bsc.getUserPrincipal().getName()); |
|---|
| 169 | 162 | |
|---|
| 170 | | - pack.setStatus(PackStatus.CREATED); |
|---|
| 171 | | - pack.setCreatedBy(user); |
|---|
| 172 | | - pack.setCreationTimestamp(new Date()); |
|---|
| 173 | | - em.persist(pack); |
|---|
| 174 | | - Set<PackMetadata> newMD = pack.getMetadata(); |
|---|
| 163 | + pack.setStatus(PackStatus.CREATED); |
|---|
| 164 | + pack.setCreatedBy(user); |
|---|
| 165 | + pack.setCreationTimestamp(new Date()); |
|---|
| 166 | + em.persist(pack); |
|---|
| 167 | + Set<PackMetadata> newMD = pack.getMetadata(); |
|---|
| 175 | 168 | |
|---|
| 176 | | - if (newMD != null) { |
|---|
| 177 | | - for (PackMetadata md : newMD) { |
|---|
| 178 | | - md.setPack(pack); |
|---|
| 179 | | - em.persist(md); |
|---|
| 180 | | - } |
|---|
| 181 | | - } |
|---|
| 182 | | - pack.setMetadata(newMD); |
|---|
| 183 | | - return Response.ok(pack).build(); |
|---|
| 184 | | - } |
|---|
| 169 | + if (newMD != null) { |
|---|
| 170 | + for (PackMetadata md : newMD) { |
|---|
| 171 | + md.setPack(pack); |
|---|
| 172 | + em.persist(md); |
|---|
| 173 | + } |
|---|
| 174 | + } |
|---|
| 175 | + pack.setMetadata(newMD); |
|---|
| 176 | + return Response.ok(pack).build(); |
|---|
| 177 | + } |
|---|
| 185 | 178 | |
|---|
| 186 | | - /** |
|---|
| 187 | | - * Check if there is some pack with the same code |
|---|
| 188 | | - * |
|---|
| 189 | | - * @param code |
|---|
| 190 | | - * Pack code |
|---|
| 191 | | - * @param em |
|---|
| 192 | | - * DB session object |
|---|
| 193 | | - * @return <code>true</code> if code is already used, <code>false</code> |
|---|
| 194 | | - * otherwise |
|---|
| 195 | | - */ |
|---|
| 196 | | - private boolean checkIfCodeExists(String code, EntityManager em) { |
|---|
| 197 | | - TypedQuery<Pack> query = em.createNamedQuery("pack-by-code", Pack.class); |
|---|
| 198 | | - query.setParameter("code", code); |
|---|
| 199 | | - int packs = query.getResultList().size(); |
|---|
| 200 | | - return packs > 0; |
|---|
| 201 | | - } |
|---|
| 179 | + /** |
|---|
| 180 | + * Check if there is some pack with the same code |
|---|
| 181 | + * |
|---|
| 182 | + * @param code |
|---|
| 183 | + * Pack code |
|---|
| 184 | + * @param em |
|---|
| 185 | + * DB session object |
|---|
| 186 | + * @return <code>true</code> if code is already used, <code>false</code> |
|---|
| 187 | + * otherwise |
|---|
| 188 | + */ |
|---|
| 189 | + private boolean checkIfCodeExists(String code, EntityManager em) { |
|---|
| 190 | + TypedQuery<Pack> query = em.createNamedQuery("pack-by-code", Pack.class); |
|---|
| 191 | + query.setParameter("code", code); |
|---|
| 192 | + int packs = query.getResultList().size(); |
|---|
| 193 | + return packs > 0; |
|---|
| 194 | + } |
|---|
| 202 | 195 | |
|---|
| 203 | | - /** |
|---|
| 204 | | - * |
|---|
| 205 | | - * @return The next available code suffix in pack for license code |
|---|
| 206 | | - * @throws SeCurisServiceException |
|---|
| 207 | | - */ |
|---|
| 208 | | - @GET |
|---|
| 209 | | - @Path("/{packId}/next_license_code") |
|---|
| 210 | | - @Securable |
|---|
| 211 | | - @Produces({ |
|---|
| 212 | | - MediaType.TEXT_PLAIN |
|---|
| 213 | | - }) |
|---|
| 214 | | - public Response getCodeSuffix(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 215 | | - // EntityManager em = emProvider.get(); |
|---|
| 196 | + /** |
|---|
| 197 | + * |
|---|
| 198 | + * @return The next available code suffix in pack for license code |
|---|
| 199 | + * @throws SeCurisServiceException |
|---|
| 200 | + */ |
|---|
| 201 | + @GET |
|---|
| 202 | + @Path("/{packId}/next_license_code") |
|---|
| 203 | + @Securable |
|---|
| 204 | + @Produces({ MediaType.TEXT_PLAIN }) |
|---|
| 205 | + public Response getCodeSuffix(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 206 | + // EntityManager em = emProvider.get(); |
|---|
| 216 | 207 | |
|---|
| 217 | | - if (packId == null) { |
|---|
| 218 | | - throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The pack code is mandatory"); |
|---|
| 219 | | - } |
|---|
| 220 | | - Integer codeSuffix = licenseHelper.getNextCodeSuffix(packId, em); |
|---|
| 221 | | - Pack pack = em.find(Pack.class, packId); |
|---|
| 222 | | - ; |
|---|
| 208 | + if (packId == null) { |
|---|
| 209 | + throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The pack code is mandatory"); |
|---|
| 210 | + } |
|---|
| 211 | + Integer codeSuffix = licenseHelper.getNextCodeSuffix(packId, em); |
|---|
| 212 | + Pack pack = em.find(Pack.class, packId); |
|---|
| 213 | + ; |
|---|
| 223 | 214 | |
|---|
| 224 | | - String licCode = LicUtils.getLicenseCode(pack.getCode(), codeSuffix); |
|---|
| 225 | | - return Response.ok(licCode).build(); |
|---|
| 226 | | - } |
|---|
| 215 | + String licCode = LicUtils.getLicenseCode(pack.getCode(), codeSuffix); |
|---|
| 216 | + return Response.ok(licCode).build(); |
|---|
| 217 | + } |
|---|
| 227 | 218 | |
|---|
| 228 | | - private void setPackLicenseType(Pack pack, Integer licTypeId, EntityManager em) throws SeCurisException { |
|---|
| 229 | | - LicenseType lt = null; |
|---|
| 230 | | - if (licTypeId != null) { |
|---|
| 231 | | - lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 232 | | - if (lt == null) { |
|---|
| 233 | | - LOG.error("Pack license type with id {} not found in DB", licTypeId); |
|---|
| 234 | | - throw new SeCurisException("Pack license type not found with ID: " + licTypeId); |
|---|
| 235 | | - } |
|---|
| 236 | | - } |
|---|
| 237 | | - pack.setLicenseType(lt); |
|---|
| 238 | | - } |
|---|
| 219 | + private void setPackLicenseType(Pack pack, Integer licTypeId, EntityManager em) throws SeCurisException { |
|---|
| 220 | + LicenseType lt = null; |
|---|
| 221 | + if (licTypeId != null) { |
|---|
| 222 | + lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 223 | + if (lt == null) { |
|---|
| 224 | + LOG.error("Pack license type with id {} not found in DB", licTypeId); |
|---|
| 225 | + throw new SeCurisException("Pack license type not found with ID: " + licTypeId); |
|---|
| 226 | + } |
|---|
| 227 | + } |
|---|
| 228 | + pack.setLicenseType(lt); |
|---|
| 229 | + } |
|---|
| 239 | 230 | |
|---|
| 240 | | - private Set<String> getMdKeys(Set<PackMetadata> mds) { |
|---|
| 241 | | - Set<String> ids = new HashSet<String>(); |
|---|
| 242 | | - if (mds != null) { |
|---|
| 243 | | - for (PackMetadata md : mds) { |
|---|
| 244 | | - ids.add(md.getKey()); |
|---|
| 245 | | - } |
|---|
| 246 | | - } |
|---|
| 247 | | - return ids; |
|---|
| 248 | | - } |
|---|
| 231 | + private Set<String> getMdKeys(Set<PackMetadata> mds) { |
|---|
| 232 | + Set<String> ids = new HashSet<String>(); |
|---|
| 233 | + if (mds != null) { |
|---|
| 234 | + for (PackMetadata md : mds) { |
|---|
| 235 | + ids.add(md.getKey()); |
|---|
| 236 | + } |
|---|
| 237 | + } |
|---|
| 238 | + return ids; |
|---|
| 239 | + } |
|---|
| 249 | 240 | |
|---|
| 250 | | - @PUT |
|---|
| 251 | | - @POST |
|---|
| 252 | | - @Path("/{packId}") |
|---|
| 253 | | - @EnsureTransaction |
|---|
| 254 | | - @Securable |
|---|
| 255 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 256 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 257 | | - @Produces({ |
|---|
| 258 | | - MediaType.APPLICATION_JSON |
|---|
| 259 | | - }) |
|---|
| 260 | | - public Response modify(Pack pack, @PathParam("packId") Integer packId) { |
|---|
| 261 | | - LOG.info("Modifying pack with id: {}", packId); |
|---|
| 262 | | - // EntityManager em = emProvider.get(); |
|---|
| 263 | | - Pack currentPack = em.find(Pack.class, packId); |
|---|
| 241 | + @PUT |
|---|
| 242 | + @POST |
|---|
| 243 | + @Path("/{packId}") |
|---|
| 244 | + @EnsureTransaction |
|---|
| 245 | + @Securable |
|---|
| 246 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 247 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 248 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 249 | + public Response modify(Pack pack, @PathParam("packId") Integer packId) { |
|---|
| 250 | + LOG.info("Modifying pack with id: {}", packId); |
|---|
| 251 | + // EntityManager em = emProvider.get(); |
|---|
| 252 | + Pack currentPack = em.find(Pack.class, packId); |
|---|
| 264 | 253 | |
|---|
| 265 | | - try { |
|---|
| 266 | | - setPackOrganization(currentPack, pack.getOrgId(), em); |
|---|
| 267 | | - } catch (SeCurisException e) { |
|---|
| 268 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 269 | | - } |
|---|
| 254 | + try { |
|---|
| 255 | + setPackOrganization(currentPack, pack.getOrgId(), em); |
|---|
| 256 | + } catch (SeCurisException e) { |
|---|
| 257 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 258 | + } |
|---|
| 270 | 259 | |
|---|
| 271 | | - try { |
|---|
| 272 | | - setPackLicenseType(currentPack, pack.getLicTypeId(), em); |
|---|
| 273 | | - } catch (SeCurisException e) { |
|---|
| 274 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 275 | | - } |
|---|
| 260 | + try { |
|---|
| 261 | + setPackLicenseType(currentPack, pack.getLicTypeId(), em); |
|---|
| 262 | + } catch (SeCurisException e) { |
|---|
| 263 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 264 | + } |
|---|
| 276 | 265 | |
|---|
| 277 | | - currentPack.setLicensePreactivation(pack.isLicensePreactivation()); |
|---|
| 278 | | - currentPack.setCode(pack.getCode()); |
|---|
| 279 | | - currentPack.setComments(pack.getComments()); |
|---|
| 280 | | - currentPack.setNumLicenses(pack.getNumLicenses()); |
|---|
| 281 | | - currentPack.setPreactivationValidPeriod(pack.getPreactivationValidPeriod()); |
|---|
| 282 | | - currentPack.setRenewValidPeriod(pack.getRenewValidPeriod()); |
|---|
| 266 | + currentPack.setLicensePreactivation(pack.isLicensePreactivation()); |
|---|
| 267 | + currentPack.setCode(pack.getCode()); |
|---|
| 268 | + currentPack.setComments(pack.getComments()); |
|---|
| 269 | + currentPack.setNumLicenses(pack.getNumLicenses()); |
|---|
| 270 | + currentPack.setPreactivationValidPeriod(pack.getPreactivationValidPeriod()); |
|---|
| 271 | + currentPack.setRenewValidPeriod(pack.getRenewValidPeriod()); |
|---|
| 272 | + currentPack.setInitValidDate(pack.getInitValidDate()); |
|---|
| 273 | + currentPack.setEndValidDate(pack.getEndValidDate()); |
|---|
| 283 | 274 | |
|---|
| 284 | | - Set<PackMetadata> newMD = pack.getMetadata(); |
|---|
| 285 | | - Set<String> newMdKeys = getMdKeys(newMD); |
|---|
| 286 | | - for (PackMetadata currentMd : currentPack.getMetadata()) { |
|---|
| 287 | | - if (!newMdKeys.contains(currentMd.getKey())) { |
|---|
| 288 | | - em.remove(currentMd); |
|---|
| 289 | | - } |
|---|
| 290 | | - } |
|---|
| 275 | + Set<PackMetadata> newMD = pack.getMetadata(); |
|---|
| 276 | + Set<String> newMdKeys = getMdKeys(newMD); |
|---|
| 277 | + for (PackMetadata currentMd : currentPack.getMetadata()) { |
|---|
| 278 | + if (!newMdKeys.contains(currentMd.getKey())) { |
|---|
| 279 | + em.remove(currentMd); |
|---|
| 280 | + } |
|---|
| 281 | + } |
|---|
| 291 | 282 | |
|---|
| 292 | | - if (newMD != null) { |
|---|
| 293 | | - Set<PackMetadata> oldMD = currentPack.getMetadata(); |
|---|
| 294 | | - Set<String> oldMdKeys = getMdKeys(newMD); |
|---|
| 295 | | - for (PackMetadata md : newMD) { |
|---|
| 296 | | - if (oldMdKeys.contains(md.getKey())) { |
|---|
| 297 | | - em.merge(md); |
|---|
| 298 | | - } else { |
|---|
| 299 | | - md.setPack(currentPack); |
|---|
| 300 | | - em.persist(md); |
|---|
| 301 | | - } |
|---|
| 302 | | - } |
|---|
| 303 | | - } |
|---|
| 304 | | - currentPack.setMetadata(newMD); |
|---|
| 305 | | - em.merge(currentPack); |
|---|
| 283 | + if (newMD != null) { |
|---|
| 284 | + Set<String> oldMdKeys = getMdKeys(newMD); |
|---|
| 285 | + for (PackMetadata md : newMD) { |
|---|
| 286 | + if (oldMdKeys.contains(md.getKey())) { |
|---|
| 287 | + em.merge(md); |
|---|
| 288 | + } else { |
|---|
| 289 | + md.setPack(currentPack); |
|---|
| 290 | + em.persist(md); |
|---|
| 291 | + } |
|---|
| 292 | + } |
|---|
| 293 | + } |
|---|
| 294 | + currentPack.setMetadata(newMD); |
|---|
| 295 | + em.merge(currentPack); |
|---|
| 306 | 296 | |
|---|
| 307 | | - return Response.ok(currentPack).build(); |
|---|
| 308 | | - } |
|---|
| 297 | + return Response.ok(currentPack).build(); |
|---|
| 298 | + } |
|---|
| 309 | 299 | |
|---|
| 310 | | - @POST |
|---|
| 311 | | - @Path("/{packId}/activate") |
|---|
| 312 | | - @EnsureTransaction |
|---|
| 313 | | - @Securable |
|---|
| 314 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 315 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 316 | | - @Produces({ |
|---|
| 317 | | - MediaType.APPLICATION_JSON |
|---|
| 318 | | - }) |
|---|
| 319 | | - public Response activate(@PathParam("packId") Integer packId) throws SeCurisServiceException { |
|---|
| 320 | | - LOG.info("Activating pack with id: {}", packId); |
|---|
| 321 | | - // EntityManager em = emProvider.get(); |
|---|
| 300 | + @POST |
|---|
| 301 | + @Path("/{packId}/activate") |
|---|
| 302 | + @EnsureTransaction |
|---|
| 303 | + @Securable |
|---|
| 304 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 305 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 306 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 307 | + public Response activate(@PathParam("packId") Integer packId) throws SeCurisServiceException { |
|---|
| 308 | + LOG.info("Activating pack with id: {}", packId); |
|---|
| 309 | + // EntityManager em = emProvider.get(); |
|---|
| 322 | 310 | |
|---|
| 323 | | - Pack currentPack = em.find(Pack.class, packId); |
|---|
| 311 | + Pack currentPack = em.find(Pack.class, packId); |
|---|
| 324 | 312 | |
|---|
| 325 | | - if (!Pack.Status.isActionValid(Pack.Action.ACTIVATION, currentPack.getStatus())) { |
|---|
| 326 | | - LOG.error("Pack with id {} cannot be activaed from status {}", packId, currentPack.getStatus().name()); |
|---|
| 327 | | - throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be activated in status: " + currentPack.getStatus().name()); |
|---|
| 328 | | - } |
|---|
| 313 | + if (!Pack.Status.isActionValid(Pack.Action.ACTIVATION, currentPack.getStatus())) { |
|---|
| 314 | + LOG.error("Pack with id {} cannot be activaed from status {}", packId, currentPack.getStatus().name()); |
|---|
| 315 | + throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be activated in status: " + currentPack.getStatus().name()); |
|---|
| 316 | + } |
|---|
| 329 | 317 | |
|---|
| 330 | | - currentPack.setStatus(PackStatus.ACTIVE); |
|---|
| 331 | | - em.persist(currentPack); |
|---|
| 318 | + currentPack.setStatus(PackStatus.ACTIVE); |
|---|
| 319 | + em.persist(currentPack); |
|---|
| 332 | 320 | |
|---|
| 333 | | - return Response.ok(currentPack).build(); |
|---|
| 334 | | - } |
|---|
| 321 | + return Response.ok(currentPack).build(); |
|---|
| 322 | + } |
|---|
| 335 | 323 | |
|---|
| 336 | | - @POST |
|---|
| 337 | | - @Path("/{packId}/putonhold") |
|---|
| 338 | | - @EnsureTransaction |
|---|
| 339 | | - @Securable |
|---|
| 340 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 341 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 342 | | - @Produces({ |
|---|
| 343 | | - MediaType.APPLICATION_JSON |
|---|
| 344 | | - }) |
|---|
| 345 | | - public Response onhold(@PathParam("packId") Integer packId) throws SeCurisServiceException { |
|---|
| 346 | | - LOG.info("Putting On hold pack with id: {}", packId); |
|---|
| 347 | | - // EntityManager em = emProvider.get(); |
|---|
| 324 | + @POST |
|---|
| 325 | + @Path("/{packId}/putonhold") |
|---|
| 326 | + @EnsureTransaction |
|---|
| 327 | + @Securable |
|---|
| 328 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 329 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 330 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 331 | + public Response onhold(@PathParam("packId") Integer packId) throws SeCurisServiceException { |
|---|
| 332 | + LOG.info("Putting On hold pack with id: {}", packId); |
|---|
| 333 | + // EntityManager em = emProvider.get(); |
|---|
| 348 | 334 | |
|---|
| 349 | | - Pack currentPack = em.find(Pack.class, packId); |
|---|
| 335 | + Pack currentPack = em.find(Pack.class, packId); |
|---|
| 350 | 336 | |
|---|
| 351 | | - if (!Pack.Status.isActionValid(Pack.Action.PUT_ONHOLD, currentPack.getStatus())) { |
|---|
| 352 | | - LOG.error("Pack with id {} cannot be put on hold from status {}", packId, currentPack.getStatus().name()); |
|---|
| 353 | | - throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be put on hold in status: " + currentPack.getStatus().name()); |
|---|
| 354 | | - } |
|---|
| 337 | + if (!Pack.Status.isActionValid(Pack.Action.PUT_ONHOLD, currentPack.getStatus())) { |
|---|
| 338 | + LOG.error("Pack with id {} cannot be put on hold from status {}", packId, currentPack.getStatus().name()); |
|---|
| 339 | + throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be put on hold in status: " + currentPack.getStatus().name()); |
|---|
| 340 | + } |
|---|
| 355 | 341 | |
|---|
| 356 | | - currentPack.setStatus(PackStatus.ON_HOLD); |
|---|
| 357 | | - em.persist(currentPack); |
|---|
| 342 | + currentPack.setStatus(PackStatus.ON_HOLD); |
|---|
| 343 | + em.persist(currentPack); |
|---|
| 358 | 344 | |
|---|
| 359 | | - return Response.ok(currentPack).build(); |
|---|
| 360 | | - } |
|---|
| 345 | + return Response.ok(currentPack).build(); |
|---|
| 346 | + } |
|---|
| 361 | 347 | |
|---|
| 362 | | - @POST |
|---|
| 363 | | - @Path("/{packId}/cancel") |
|---|
| 364 | | - @EnsureTransaction |
|---|
| 365 | | - @Securable |
|---|
| 366 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 367 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 368 | | - @Produces({ |
|---|
| 369 | | - MediaType.APPLICATION_JSON |
|---|
| 370 | | - }) |
|---|
| 371 | | - public Response cancel(@PathParam("packId") Integer packId, @FormParam("reason") String reason, @Context BasicSecurityContext bsc) |
|---|
| 372 | | - throws SeCurisServiceException { |
|---|
| 373 | | - LOG.info("Cancelling pack with id: {}", packId); |
|---|
| 374 | | - // EntityManager em = emProvider.get(); |
|---|
| 348 | + @POST |
|---|
| 349 | + @Path("/{packId}/cancel") |
|---|
| 350 | + @EnsureTransaction |
|---|
| 351 | + @Securable |
|---|
| 352 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 353 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 354 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 355 | + public Response cancel(@PathParam("packId") Integer packId, @FormParam("reason") String reason, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 356 | + LOG.info("Cancelling pack with id: {}", packId); |
|---|
| 357 | + // EntityManager em = emProvider.get(); |
|---|
| 375 | 358 | |
|---|
| 376 | | - Pack currentPack = em.find(Pack.class, packId); |
|---|
| 359 | + Pack currentPack = em.find(Pack.class, packId); |
|---|
| 377 | 360 | |
|---|
| 378 | | - if (!Pack.Status.isActionValid(Pack.Action.CANCEL, currentPack.getStatus())) { |
|---|
| 379 | | - LOG.error("Pack with id {} cannot cancelled from status {}", packId, currentPack.getStatus().name()); |
|---|
| 380 | | - throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be cancelled in status: " + currentPack.getStatus().name()); |
|---|
| 381 | | - } |
|---|
| 361 | + if (!Pack.Status.isActionValid(Pack.Action.CANCEL, currentPack.getStatus())) { |
|---|
| 362 | + LOG.error("Pack with id {} cannot cancelled from status {}", packId, currentPack.getStatus().name()); |
|---|
| 363 | + throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Pack cannot be cancelled in status: " + currentPack.getStatus().name()); |
|---|
| 364 | + } |
|---|
| 382 | 365 | |
|---|
| 383 | | - Set<License> licenses = currentPack.getLicenses(); |
|---|
| 384 | | - for (License license : licenses) { |
|---|
| 385 | | - if (license.getStatus() == LicenseStatus.ACTIVE || license.getStatus() == LicenseStatus.PRE_ACTIVE) { |
|---|
| 386 | | - licenseHelper.cancelLicense(license, "Pack cancellation. " + reason, bsc, em); |
|---|
| 387 | | - } |
|---|
| 388 | | - } |
|---|
| 389 | | - currentPack.setStatus(PackStatus.CANCELLED); |
|---|
| 390 | | - em.persist(currentPack); |
|---|
| 366 | + Set<License> licenses = currentPack.getLicenses(); |
|---|
| 367 | + for (License license : licenses) { |
|---|
| 368 | + if (license.getStatus() == LicenseStatus.ACTIVE || license.getStatus() == LicenseStatus.PRE_ACTIVE) { |
|---|
| 369 | + licenseHelper.cancelLicense(license, "Pack cancellation. " + reason, bsc, em); |
|---|
| 370 | + } |
|---|
| 371 | + } |
|---|
| 372 | + currentPack.setStatus(PackStatus.CANCELLED); |
|---|
| 373 | + em.persist(currentPack); |
|---|
| 391 | 374 | |
|---|
| 392 | | - return Response.ok(currentPack).build(); |
|---|
| 393 | | - } |
|---|
| 375 | + return Response.ok(currentPack).build(); |
|---|
| 376 | + } |
|---|
| 394 | 377 | |
|---|
| 395 | | - private void setPackOrganization(Pack currentPack, Integer orgId, EntityManager em) throws SeCurisException { |
|---|
| 396 | | - Organization org = null; |
|---|
| 397 | | - if (orgId != null) { |
|---|
| 398 | | - org = em.find(Organization.class, orgId); |
|---|
| 399 | | - if (org == null) { |
|---|
| 400 | | - LOG.error("Organization pack with id {} not found in DB", orgId); |
|---|
| 401 | | - throw new SeCurisException("Pack organization not found with ID: " + orgId); |
|---|
| 402 | | - } |
|---|
| 403 | | - } |
|---|
| 404 | | - currentPack.setOrganization(org); |
|---|
| 405 | | - } |
|---|
| 378 | + private void setPackOrganization(Pack currentPack, Integer orgId, EntityManager em) throws SeCurisException { |
|---|
| 379 | + Organization org = null; |
|---|
| 380 | + if (orgId != null) { |
|---|
| 381 | + org = em.find(Organization.class, orgId); |
|---|
| 382 | + if (org == null) { |
|---|
| 383 | + LOG.error("Organization pack with id {} not found in DB", orgId); |
|---|
| 384 | + throw new SeCurisException("Pack organization not found with ID: " + orgId); |
|---|
| 385 | + } |
|---|
| 386 | + } |
|---|
| 387 | + currentPack.setOrganization(org); |
|---|
| 388 | + } |
|---|
| 406 | 389 | |
|---|
| 407 | | - @DELETE |
|---|
| 408 | | - @Path("/{packId}") |
|---|
| 409 | | - @Securable |
|---|
| 410 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 411 | | - @EnsureTransaction |
|---|
| 412 | | - @Produces({ |
|---|
| 413 | | - MediaType.APPLICATION_JSON |
|---|
| 414 | | - }) |
|---|
| 415 | | - public Response delete(@PathParam("packId") String packId) throws SeCurisServiceException { |
|---|
| 416 | | - LOG.info("Deleting pack with id: {}", packId); |
|---|
| 417 | | - // EntityManager em = emProvider.get(); |
|---|
| 418 | | - Pack pack = em.find(Pack.class, Integer.parseInt(packId)); |
|---|
| 419 | | - if (pack == null) { |
|---|
| 420 | | - LOG.error("Pack with id {} can not be deleted, It was not found in DB", packId); |
|---|
| 421 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack was not found, ID: " + packId) |
|---|
| 422 | | - .build(); |
|---|
| 423 | | - } |
|---|
| 424 | | - // Pack metadata is removed in cascade automatically. |
|---|
| 390 | + @DELETE |
|---|
| 391 | + @Path("/{packId}") |
|---|
| 392 | + @Securable |
|---|
| 393 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 394 | + @EnsureTransaction |
|---|
| 395 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 396 | + public Response delete(@PathParam("packId") String packId) throws SeCurisServiceException { |
|---|
| 397 | + LOG.info("Deleting pack with id: {}", packId); |
|---|
| 398 | + // EntityManager em = emProvider.get(); |
|---|
| 399 | + Pack pack = em.find(Pack.class, Integer.parseInt(packId)); |
|---|
| 400 | + if (pack == null) { |
|---|
| 401 | + LOG.error("Pack with id {} can not be deleted, It was not found in DB", packId); |
|---|
| 402 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack was not found, ID: " + packId).build(); |
|---|
| 403 | + } |
|---|
| 404 | + // Pack metadata is removed in cascade automatically. |
|---|
| 425 | 405 | |
|---|
| 426 | | - Set<License> licenses = pack.getLicenses(); |
|---|
| 427 | | - for (License license : licenses) { |
|---|
| 428 | | - if (license.getStatus() == LicenseStatus.ACTIVE || license.getStatus() == LicenseStatus.PRE_ACTIVE) { |
|---|
| 429 | | - throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "An active license cannot be deleted. License code: " + license.getCode()); |
|---|
| 430 | | - } |
|---|
| 431 | | - em.remove(license); |
|---|
| 432 | | - } |
|---|
| 406 | + Set<License> licenses = pack.getLicenses(); |
|---|
| 407 | + for (License license : licenses) { |
|---|
| 408 | + if (license.getStatus() == LicenseStatus.ACTIVE || license.getStatus() == LicenseStatus.PRE_ACTIVE) { |
|---|
| 409 | + throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "An active license cannot be deleted. License code: " + license.getCode()); |
|---|
| 410 | + } |
|---|
| 411 | + em.remove(license); |
|---|
| 412 | + } |
|---|
| 433 | 413 | |
|---|
| 434 | | - em.remove(pack); |
|---|
| 435 | | - return Response.ok(Utils.createMap("success", true, "id", packId)).build(); |
|---|
| 436 | | - } |
|---|
| 414 | + em.remove(pack); |
|---|
| 415 | + return Response.ok(Utils.createMap("success", true, "id", packId)).build(); |
|---|
| 416 | + } |
|---|
| 437 | 417 | |
|---|
| 438 | 418 | } |
|---|