| .. | .. |
|---|
| 40 | 40 | import com.google.inject.persist.Transactional; |
|---|
| 41 | 41 | |
|---|
| 42 | 42 | /** |
|---|
| 43 | | - * Pack resource, this service will provide methods to create, modify and delete packs |
|---|
| 43 | + * Pack resource, this service will provide methods to create, modify and delete |
|---|
| 44 | + * packs |
|---|
| 44 | 45 | * |
|---|
| 45 | 46 | * @author roberto <roberto.sanchez@curisit.net> |
|---|
| 46 | 47 | */ |
|---|
| 47 | 48 | @Path("/pack") |
|---|
| 48 | 49 | public class PackResource { |
|---|
| 49 | 50 | |
|---|
| 50 | | - private static final Logger LOG = LogManager.getLogger(PackResource.class); |
|---|
| 51 | + private static final Logger LOG = LogManager.getLogger(PackResource.class); |
|---|
| 51 | 52 | |
|---|
| 52 | | - @Inject |
|---|
| 53 | | - TokenHelper tokenHelper; |
|---|
| 53 | + @Inject |
|---|
| 54 | + TokenHelper tokenHelper; |
|---|
| 54 | 55 | |
|---|
| 55 | | - @Inject |
|---|
| 56 | | - Provider<EntityManager> emProvider; |
|---|
| 56 | + @Inject |
|---|
| 57 | + Provider<EntityManager> emProvider; |
|---|
| 57 | 58 | |
|---|
| 58 | | - public PackResource() { |
|---|
| 59 | | - } |
|---|
| 59 | + public PackResource() {} |
|---|
| 60 | 60 | |
|---|
| 61 | | - /** |
|---|
| 62 | | - * |
|---|
| 63 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 64 | | - */ |
|---|
| 65 | | - @GET |
|---|
| 66 | | - @Path("/") |
|---|
| 67 | | - @Securable |
|---|
| 68 | | - @Produces( |
|---|
| 69 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 70 | | - public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 71 | | - LOG.info("Getting packs list "); |
|---|
| 61 | + /** |
|---|
| 62 | + * |
|---|
| 63 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 64 | + */ |
|---|
| 65 | + @GET |
|---|
| 66 | + @Path("/") |
|---|
| 67 | + @Securable |
|---|
| 68 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 69 | + public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 70 | + LOG.info("Getting packs list "); |
|---|
| 72 | 71 | |
|---|
| 73 | | - EntityManager em = emProvider.get(); |
|---|
| 74 | | - // TypedQuery<Pack> q = em.createNamedQuery("list-packs-by-orgs", Pack.class); |
|---|
| 72 | + EntityManager em = emProvider.get(); |
|---|
| 75 | 73 | |
|---|
| 76 | | - TypedQuery<Pack> q; |
|---|
| 77 | | - if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 78 | | - LOG.info("Getting all packs for user: " + bsc.getUserPrincipal()); |
|---|
| 79 | | - q = em.createNamedQuery("list-packs", Pack.class); |
|---|
| 80 | | - } else { |
|---|
| 81 | | - q = em.createNamedQuery("list-packs-by-orgs", Pack.class); |
|---|
| 82 | | - if (bsc.getOrganizationsIds() == null) |
|---|
| 83 | | - Response.ok().build(); |
|---|
| 84 | | - q.setParameter("list_ids", bsc.getOrganizationsIds()); |
|---|
| 85 | | - } |
|---|
| 74 | + TypedQuery<Pack> q; |
|---|
| 75 | + if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 76 | + LOG.info("Getting all packs for user: " + bsc.getUserPrincipal()); |
|---|
| 77 | + q = em.createNamedQuery("list-packs", Pack.class); |
|---|
| 78 | + } else { |
|---|
| 79 | + q = em.createNamedQuery("list-packs-by-orgs", Pack.class); |
|---|
| 80 | + if (bsc.getOrganizationsIds() == null) { |
|---|
| 81 | + Response.ok().build(); |
|---|
| 82 | + } |
|---|
| 83 | + q.setParameter("list_ids", bsc.getOrganizationsIds()); |
|---|
| 84 | + } |
|---|
| 86 | 85 | |
|---|
| 87 | | - List<Pack> list = q.getResultList(); |
|---|
| 86 | + List<Pack> list = q.getResultList(); |
|---|
| 88 | 87 | |
|---|
| 89 | | - return Response.ok(list).build(); |
|---|
| 90 | | - } |
|---|
| 88 | + return Response.ok(list).build(); |
|---|
| 89 | + } |
|---|
| 91 | 90 | |
|---|
| 92 | | - private Response generateErrorUnathorizedAccess(Pack pack, Principal user) { |
|---|
| 93 | | - LOG.error("Pack with id {} not accesible by user {}", pack, user); |
|---|
| 94 | | - return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack").build(); |
|---|
| 95 | | - } |
|---|
| 91 | + private Response generateErrorUnathorizedAccess(Pack pack, Principal user) { |
|---|
| 92 | + LOG.error("Pack with id {} not accesible by user {}", pack, user); |
|---|
| 93 | + return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack").build(); |
|---|
| 94 | + } |
|---|
| 96 | 95 | |
|---|
| 97 | | - /** |
|---|
| 98 | | - * |
|---|
| 99 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 100 | | - */ |
|---|
| 101 | | - @GET |
|---|
| 102 | | - @Path("/{packId}") |
|---|
| 103 | | - @Securable |
|---|
| 104 | | - @Produces( |
|---|
| 105 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 106 | | - public Response get(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) { |
|---|
| 107 | | - LOG.info("Getting pack data for id: {}: ", packId); |
|---|
| 108 | | - if (packId == null || packId.equals("")) { |
|---|
| 109 | | - LOG.error("Pack ID is mandatory"); |
|---|
| 110 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 111 | | - } |
|---|
| 96 | + /** |
|---|
| 97 | + * |
|---|
| 98 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 99 | + */ |
|---|
| 100 | + @GET |
|---|
| 101 | + @Path("/{packId}") |
|---|
| 102 | + @Securable |
|---|
| 103 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 104 | + public Response get(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) { |
|---|
| 105 | + LOG.info("Getting pack data for id: {}: ", packId); |
|---|
| 106 | + if (packId == null || "".equals(packId)) { |
|---|
| 107 | + LOG.error("Pack ID is mandatory"); |
|---|
| 108 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 109 | + } |
|---|
| 112 | 110 | |
|---|
| 113 | | - EntityManager em = emProvider.get(); |
|---|
| 114 | | - Pack pack = em.find(Pack.class, packId); |
|---|
| 115 | | - if (pack == null) { |
|---|
| 116 | | - LOG.error("Pack with id {} not found in DB", packId); |
|---|
| 117 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 118 | | - } |
|---|
| 119 | | - if (bsc.isUserInRole(BasicSecurityContext.ROL_ADVANCE)) { |
|---|
| 120 | | - if (bsc.getOrganizationsIds() == null || !bsc.getOrganizationsIds().contains(pack.getOrgId())) { |
|---|
| 121 | | - return generateErrorUnathorizedAccess(pack, bsc.getUserPrincipal()); |
|---|
| 122 | | - } |
|---|
| 123 | | - } |
|---|
| 124 | | - return Response.ok(pack).build(); |
|---|
| 125 | | - } |
|---|
| 111 | + EntityManager em = emProvider.get(); |
|---|
| 112 | + Pack pack = em.find(Pack.class, packId); |
|---|
| 113 | + if (pack == null) { |
|---|
| 114 | + LOG.error("Pack with id {} not found in DB", packId); |
|---|
| 115 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 116 | + } |
|---|
| 117 | + if (bsc.isUserInRole(BasicSecurityContext.ROL_ADVANCE)) { |
|---|
| 118 | + if (bsc.getOrganizationsIds() == null || !bsc.getOrganizationsIds().contains(pack.getOrgId())) { |
|---|
| 119 | + return generateErrorUnathorizedAccess(pack, bsc.getUserPrincipal()); |
|---|
| 120 | + } |
|---|
| 121 | + } |
|---|
| 122 | + return Response.ok(pack).build(); |
|---|
| 123 | + } |
|---|
| 126 | 124 | |
|---|
| 127 | | - @POST |
|---|
| 128 | | - @Path("/") |
|---|
| 129 | | - @Securable |
|---|
| 130 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 131 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 132 | | - @Produces( |
|---|
| 133 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 134 | | - @Transactional |
|---|
| 135 | | - public Response create(Pack pack, @Context BasicSecurityContext bsc) { |
|---|
| 136 | | - LOG.info("Creating new pack"); |
|---|
| 137 | | - EntityManager em = emProvider.get(); |
|---|
| 125 | + @POST |
|---|
| 126 | + @Path("/") |
|---|
| 127 | + @Securable |
|---|
| 128 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 129 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 130 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 131 | + @Transactional |
|---|
| 132 | + public Response create(Pack pack, @Context BasicSecurityContext bsc) { |
|---|
| 133 | + LOG.info("Creating new pack"); |
|---|
| 134 | + EntityManager em = emProvider.get(); |
|---|
| 138 | 135 | |
|---|
| 139 | | - try { |
|---|
| 140 | | - setPackOrganization(pack, pack.getOrgId(), em); |
|---|
| 141 | | - } catch (SeCurisException e) { |
|---|
| 142 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 143 | | - } |
|---|
| 136 | + try { |
|---|
| 137 | + setPackOrganization(pack, pack.getOrgId(), em); |
|---|
| 138 | + } catch (SeCurisException e) { |
|---|
| 139 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 140 | + } |
|---|
| 144 | 141 | |
|---|
| 145 | | - try { |
|---|
| 146 | | - setPackLicenseType(pack, pack.getLicTypeId(), em); |
|---|
| 147 | | - } catch (SeCurisException e) { |
|---|
| 148 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 149 | | - } |
|---|
| 142 | + try { |
|---|
| 143 | + setPackLicenseType(pack, pack.getLicTypeId(), em); |
|---|
| 144 | + } catch (SeCurisException e) { |
|---|
| 145 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 146 | + } |
|---|
| 150 | 147 | |
|---|
| 151 | | - User user = em.find(User.class, bsc.getUserPrincipal().getName()); |
|---|
| 148 | + User user = em.find(User.class, bsc.getUserPrincipal().getName()); |
|---|
| 152 | 149 | |
|---|
| 153 | | - pack.setCreatedBy(user); |
|---|
| 154 | | - pack.setCreationTimestamp(new Date()); |
|---|
| 155 | | - em.persist(pack); |
|---|
| 150 | + pack.setCreatedBy(user); |
|---|
| 151 | + pack.setCreationTimestamp(new Date()); |
|---|
| 152 | + em.persist(pack); |
|---|
| 156 | 153 | |
|---|
| 157 | | - return Response.ok(pack).build(); |
|---|
| 158 | | - } |
|---|
| 159 | | - |
|---|
| 160 | | - private void setPackLicenseType(Pack pack, Integer licTypeId, EntityManager em) throws SeCurisException { |
|---|
| 161 | | - LicenseType lt = null; |
|---|
| 162 | | - if (licTypeId != null) { |
|---|
| 163 | | - lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 164 | | - if (lt == null) { |
|---|
| 165 | | - LOG.error("Pack license type with id {} not found in DB", licTypeId); |
|---|
| 166 | | - throw new SeCurisException("Pack license type not found with ID: " + licTypeId); |
|---|
| 167 | | -// return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build(); |
|---|
| 168 | | - } |
|---|
| 169 | | - } |
|---|
| 170 | | - pack.setLicenseType(lt); |
|---|
| 171 | | - } |
|---|
| 154 | + return Response.ok(pack).build(); |
|---|
| 155 | + } |
|---|
| 172 | 156 | |
|---|
| 173 | | - @PUT |
|---|
| 174 | | - @POST |
|---|
| 175 | | - @Path("/{packId}") |
|---|
| 176 | | - @Transactional |
|---|
| 177 | | - @Securable |
|---|
| 178 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 179 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 180 | | - @Produces( |
|---|
| 181 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 182 | | - public Response modify(Pack pack, @PathParam("packId") Integer packId) { |
|---|
| 183 | | - LOG.info("Modifying pack with id: {}", packId); |
|---|
| 184 | | - EntityManager em = emProvider.get(); |
|---|
| 185 | | - Pack currentPack = em.find(Pack.class, packId); |
|---|
| 157 | + private void setPackLicenseType(Pack pack, Integer licTypeId, EntityManager em) throws SeCurisException { |
|---|
| 158 | + LicenseType lt = null; |
|---|
| 159 | + if (licTypeId != null) { |
|---|
| 160 | + lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 161 | + if (lt == null) { |
|---|
| 162 | + LOG.error("Pack license type with id {} not found in DB", licTypeId); |
|---|
| 163 | + throw new SeCurisException("Pack license type not found with ID: " + licTypeId); |
|---|
| 164 | + } |
|---|
| 165 | + } |
|---|
| 166 | + pack.setLicenseType(lt); |
|---|
| 167 | + } |
|---|
| 186 | 168 | |
|---|
| 187 | | - try { |
|---|
| 188 | | - setPackOrganization(currentPack, pack.getOrgId(), em); |
|---|
| 189 | | - } catch (SeCurisException e) { |
|---|
| 190 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 191 | | - } |
|---|
| 169 | + @PUT |
|---|
| 170 | + @POST |
|---|
| 171 | + @Path("/{packId}") |
|---|
| 172 | + @Transactional |
|---|
| 173 | + @Securable |
|---|
| 174 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 175 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 176 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 177 | + public Response modify(Pack pack, @PathParam("packId") Integer packId) { |
|---|
| 178 | + LOG.info("Modifying pack with id: {}", packId); |
|---|
| 179 | + EntityManager em = emProvider.get(); |
|---|
| 180 | + Pack currentPack = em.find(Pack.class, packId); |
|---|
| 192 | 181 | |
|---|
| 193 | | - try { |
|---|
| 194 | | - setPackLicenseType(currentPack, pack.getLicTypeId(), em); |
|---|
| 195 | | - } catch (SeCurisException e) { |
|---|
| 196 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 197 | | - } |
|---|
| 198 | | - |
|---|
| 199 | | - currentPack.setLicensePreactivation(pack.isLicensePreactivation()); |
|---|
| 200 | | - currentPack.setCode(pack.getCode()); |
|---|
| 201 | | - currentPack.setComments(pack.getComments()); |
|---|
| 202 | | - currentPack.setNumLicenses(pack.getNumLicenses()); |
|---|
| 182 | + try { |
|---|
| 183 | + setPackOrganization(currentPack, pack.getOrgId(), em); |
|---|
| 184 | + } catch (SeCurisException e) { |
|---|
| 185 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 186 | + } |
|---|
| 203 | 187 | |
|---|
| 204 | | - em.persist(currentPack); |
|---|
| 188 | + try { |
|---|
| 189 | + setPackLicenseType(currentPack, pack.getLicTypeId(), em); |
|---|
| 190 | + } catch (SeCurisException e) { |
|---|
| 191 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 192 | + } |
|---|
| 205 | 193 | |
|---|
| 206 | | - return Response.ok(pack).build(); |
|---|
| 207 | | - } |
|---|
| 194 | + currentPack.setLicensePreactivation(pack.isLicensePreactivation()); |
|---|
| 195 | + currentPack.setCode(pack.getCode()); |
|---|
| 196 | + currentPack.setComments(pack.getComments()); |
|---|
| 197 | + currentPack.setNumLicenses(pack.getNumLicenses()); |
|---|
| 208 | 198 | |
|---|
| 209 | | - private void setPackOrganization(Pack currentPack, Integer orgId, EntityManager em) throws SeCurisException { |
|---|
| 210 | | - Organization org = null; |
|---|
| 211 | | - if (orgId != null) { |
|---|
| 212 | | - org = em.find(Organization.class, orgId); |
|---|
| 213 | | - if (org == null) { |
|---|
| 214 | | - LOG.error("Organization pack with id {} not found in DB", orgId); |
|---|
| 215 | | - throw new SeCurisException("Pack organization not found with ID: " + orgId); |
|---|
| 216 | | - } |
|---|
| 217 | | - } |
|---|
| 218 | | - currentPack.setOrganization(org); |
|---|
| 219 | | - } |
|---|
| 199 | + em.persist(currentPack); |
|---|
| 220 | 200 | |
|---|
| 221 | | - @DELETE |
|---|
| 222 | | - @Path("/{packId}") |
|---|
| 223 | | - @Securable |
|---|
| 224 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 225 | | - @Transactional |
|---|
| 226 | | - @Produces( |
|---|
| 227 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 228 | | - public Response delete(@PathParam("packId") String packId) { |
|---|
| 229 | | - LOG.info("Deleting pack with id: {}", packId); |
|---|
| 230 | | - EntityManager em = emProvider.get(); |
|---|
| 231 | | - Pack org = em.find(Pack.class, Integer.parseInt(packId)); |
|---|
| 232 | | - if (org == null) { |
|---|
| 233 | | - LOG.error("Pack with id {} can not be deleted, It was not found in DB", packId); |
|---|
| 234 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack was not found, ID: " + packId).build(); |
|---|
| 235 | | - } |
|---|
| 201 | + return Response.ok(pack).build(); |
|---|
| 202 | + } |
|---|
| 236 | 203 | |
|---|
| 237 | | - em.remove(org); |
|---|
| 238 | | - return Response.ok(Utils.createMap("success", true, "id", packId)).build(); |
|---|
| 239 | | - } |
|---|
| 204 | + private void setPackOrganization(Pack currentPack, Integer orgId, EntityManager em) throws SeCurisException { |
|---|
| 205 | + Organization org = null; |
|---|
| 206 | + if (orgId != null) { |
|---|
| 207 | + org = em.find(Organization.class, orgId); |
|---|
| 208 | + if (org == null) { |
|---|
| 209 | + LOG.error("Organization pack with id {} not found in DB", orgId); |
|---|
| 210 | + throw new SeCurisException("Pack organization not found with ID: " + orgId); |
|---|
| 211 | + } |
|---|
| 212 | + } |
|---|
| 213 | + currentPack.setOrganization(org); |
|---|
| 214 | + } |
|---|
| 215 | + |
|---|
| 216 | + @DELETE |
|---|
| 217 | + @Path("/{packId}") |
|---|
| 218 | + @Securable |
|---|
| 219 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 220 | + @Transactional |
|---|
| 221 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 222 | + public Response delete(@PathParam("packId") String packId) { |
|---|
| 223 | + LOG.info("Deleting pack with id: {}", packId); |
|---|
| 224 | + EntityManager em = emProvider.get(); |
|---|
| 225 | + Pack org = em.find(Pack.class, Integer.parseInt(packId)); |
|---|
| 226 | + if (org == null) { |
|---|
| 227 | + LOG.error("Pack with id {} can not be deleted, It was not found in DB", packId); |
|---|
| 228 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack was not found, ID: " + packId) |
|---|
| 229 | + .build(); |
|---|
| 230 | + } |
|---|
| 231 | + |
|---|
| 232 | + em.remove(org); |
|---|
| 233 | + return Response.ok(Utils.createMap("success", true, "id", packId)).build(); |
|---|
| 234 | + } |
|---|
| 240 | 235 | |
|---|
| 241 | 236 | } |
|---|