| .. | .. |
|---|
| 9 | 9 | import javax.inject.Provider; |
|---|
| 10 | 10 | import javax.persistence.EntityManager; |
|---|
| 11 | 11 | import javax.persistence.TypedQuery; |
|---|
| 12 | | -import javax.servlet.http.HttpServletRequest; |
|---|
| 13 | 12 | import javax.ws.rs.Consumes; |
|---|
| 14 | 13 | import javax.ws.rs.DELETE; |
|---|
| 15 | 14 | import javax.ws.rs.GET; |
|---|
| 16 | | -import javax.ws.rs.HeaderParam; |
|---|
| 17 | 15 | import javax.ws.rs.POST; |
|---|
| 18 | 16 | import javax.ws.rs.PUT; |
|---|
| 19 | 17 | import javax.ws.rs.Path; |
|---|
| .. | .. |
|---|
| 26 | 24 | |
|---|
| 27 | 25 | import net.curisit.integrity.commons.Utils; |
|---|
| 28 | 26 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 27 | +import net.curisit.securis.db.LicenseType; |
|---|
| 28 | +import net.curisit.securis.db.Organization; |
|---|
| 29 | 29 | import net.curisit.securis.db.Pack; |
|---|
| 30 | +import net.curisit.securis.db.User; |
|---|
| 30 | 31 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| 31 | 32 | import net.curisit.securis.security.Securable; |
|---|
| 32 | 33 | import net.curisit.securis.utils.TokenHelper; |
|---|
| .. | .. |
|---|
| 78 | 79 | q = em.createNamedQuery("list-packs-by-orgs", Pack.class); |
|---|
| 79 | 80 | if (bsc.getOrganizationsIds() == null) |
|---|
| 80 | 81 | Response.ok().build(); |
|---|
| 81 | | - // log.info("Getting only {} orgs for user: {}", securityContext.getOrganizationsIds(), securityContext.getUserPrincipal()); |
|---|
| 82 | 82 | q.setParameter("list_ids", bsc.getOrganizationsIds()); |
|---|
| 83 | 83 | } |
|---|
| 84 | 84 | |
|---|
| .. | .. |
|---|
| 130 | 130 | @Produces( |
|---|
| 131 | 131 | { MediaType.APPLICATION_JSON }) |
|---|
| 132 | 132 | @Transactional |
|---|
| 133 | | - public Response create(Pack pack, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 133 | + public Response create(Pack pack, @Context BasicSecurityContext bsc) { |
|---|
| 134 | 134 | log.info("Creating new pack"); |
|---|
| 135 | 135 | EntityManager em = emProvider.get(); |
|---|
| 136 | 136 | |
|---|
| 137 | + Organization org = null; |
|---|
| 138 | + if (pack.getOrgId() != null) { |
|---|
| 139 | + org = em.find(Organization.class, pack.getOrgId()); |
|---|
| 140 | + if (org == null) { |
|---|
| 141 | + log.error("Organization pack with id {} not found in DB", pack.getOrgId()); |
|---|
| 142 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build(); |
|---|
| 143 | + } |
|---|
| 144 | + } |
|---|
| 145 | + LicenseType lt = null; |
|---|
| 146 | + if (pack.getLicTypeId() != null) { |
|---|
| 147 | + lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 148 | + if (lt == null) { |
|---|
| 149 | + log.error("Pack license type with id {} not found in DB", pack.getLicTypeId()); |
|---|
| 150 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build(); |
|---|
| 151 | + } |
|---|
| 152 | + } |
|---|
| 153 | + |
|---|
| 154 | + User user = em.find(User.class, bsc.getUserPrincipal().getName()); |
|---|
| 155 | + |
|---|
| 156 | + pack.setCreatedBy(user); |
|---|
| 157 | + pack.setLicenseType(lt); |
|---|
| 158 | + pack.setOrganization(org); |
|---|
| 137 | 159 | pack.setCreationTimestamp(new Date()); |
|---|
| 138 | 160 | em.persist(pack); |
|---|
| 139 | 161 | |
|---|
| .. | .. |
|---|
| 149 | 171 | @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 150 | 172 | @Produces( |
|---|
| 151 | 173 | { MediaType.APPLICATION_JSON }) |
|---|
| 152 | | - public Response modify(Pack pack, @PathParam("packId") String packId, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 174 | + public Response modify(Pack pack, @PathParam("packId") String packId) { |
|---|
| 153 | 175 | log.info("Modifying pack with id: {}", packId); |
|---|
| 154 | 176 | EntityManager em = emProvider.get(); |
|---|
| 177 | + Pack currentPack = em.find(Pack.class, Integer.parseInt(packId)); |
|---|
| 155 | 178 | |
|---|
| 156 | | - em.persist(pack); |
|---|
| 179 | + Organization org = null; |
|---|
| 180 | + if (pack.getOrgId() != null) { |
|---|
| 181 | + org = em.find(Organization.class, pack.getOrgId()); |
|---|
| 182 | + if (org == null) { |
|---|
| 183 | + log.error("Organization pack with id {} not found in DB", pack.getOrgId()); |
|---|
| 184 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build(); |
|---|
| 185 | + } |
|---|
| 186 | + } |
|---|
| 187 | + LicenseType lt = null; |
|---|
| 188 | + if (pack.getLicTypeId() != null) { |
|---|
| 189 | + lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 190 | + if (lt == null) { |
|---|
| 191 | + log.error("Pack license type with id {} not found in DB", pack.getLicTypeId()); |
|---|
| 192 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build(); |
|---|
| 193 | + } |
|---|
| 194 | + } |
|---|
| 195 | + currentPack.setLicenseType(lt); |
|---|
| 196 | + currentPack.setOrganization(org); |
|---|
| 197 | + currentPack.setCode(pack.getCode()); |
|---|
| 198 | + currentPack.setComments(pack.getComments()); |
|---|
| 199 | + currentPack.setNumLicenses(pack.getNumLicenses()); |
|---|
| 200 | + |
|---|
| 201 | + em.persist(currentPack); |
|---|
| 157 | 202 | |
|---|
| 158 | 203 | return Response.ok(pack).build(); |
|---|
| 159 | 204 | } |
|---|
| .. | .. |
|---|
| 165 | 210 | @Transactional |
|---|
| 166 | 211 | @Produces( |
|---|
| 167 | 212 | { MediaType.APPLICATION_JSON }) |
|---|
| 168 | | - public Response delete(@PathParam("packId") String packId, @Context HttpServletRequest request) { |
|---|
| 213 | + public Response delete(@PathParam("packId") String packId) { |
|---|
| 169 | 214 | log.info("Deleting pack with id: {}", packId); |
|---|
| 170 | 215 | EntityManager em = emProvider.get(); |
|---|
| 171 | 216 | Pack org = em.find(Pack.class, Integer.parseInt(packId)); |
|---|