| .. | .. |
|---|
| 6 | 6 | import javax.inject.Inject; |
|---|
| 7 | 7 | import javax.inject.Provider; |
|---|
| 8 | 8 | import javax.persistence.EntityManager; |
|---|
| 9 | | -import javax.persistence.TypedQuery; |
|---|
| 9 | +import javax.persistence.Query; |
|---|
| 10 | 10 | import javax.servlet.http.HttpServletRequest; |
|---|
| 11 | 11 | import javax.ws.rs.Consumes; |
|---|
| 12 | 12 | import javax.ws.rs.DELETE; |
|---|
| .. | .. |
|---|
| 23 | 23 | import javax.ws.rs.core.Response.Status; |
|---|
| 24 | 24 | |
|---|
| 25 | 25 | import net.curisit.integrity.commons.Utils; |
|---|
| 26 | +import net.curisit.securis.SecurisErrorHandler; |
|---|
| 27 | +import net.curisit.securis.db.Application; |
|---|
| 26 | 28 | import net.curisit.securis.db.LicenseType; |
|---|
| 27 | 29 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 28 | 30 | |
|---|
| .. | .. |
|---|
| 39 | 41 | @Path("/licensetype") |
|---|
| 40 | 42 | public class LicenseTypeResource { |
|---|
| 41 | 43 | |
|---|
| 44 | + private static final Logger log = LoggerFactory.getLogger(LicenseTypeResource.class); |
|---|
| 45 | + |
|---|
| 42 | 46 | @Inject |
|---|
| 43 | 47 | TokenHelper tokenHelper; |
|---|
| 44 | 48 | |
|---|
| 45 | 49 | @Inject |
|---|
| 46 | 50 | Provider<EntityManager> emProvider; |
|---|
| 47 | | - |
|---|
| 48 | | - private static final Logger log = LoggerFactory.getLogger(LicenseTypeResource.class); |
|---|
| 49 | 51 | |
|---|
| 50 | 52 | public LicenseTypeResource() { |
|---|
| 51 | 53 | } |
|---|
| .. | .. |
|---|
| 62 | 64 | log.info("Getting license types list "); |
|---|
| 63 | 65 | |
|---|
| 64 | 66 | EntityManager em = emProvider.get(); |
|---|
| 65 | | - TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class); |
|---|
| 66 | | - List<LicenseType> list = q.getResultList(); |
|---|
| 67 | + Query q = em.createNamedQuery("list-license_types"); |
|---|
| 68 | + @SuppressWarnings("unchecked") |
|---|
| 69 | + List<Object> list = q.getResultList(); |
|---|
| 67 | 70 | |
|---|
| 68 | 71 | return Response.ok(list).build(); |
|---|
| 69 | 72 | } |
|---|
| .. | .. |
|---|
| 98 | 101 | @Produces( |
|---|
| 99 | 102 | { MediaType.APPLICATION_JSON }) |
|---|
| 100 | 103 | @Transactional |
|---|
| 101 | | - public Response create(LicenseType app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 104 | + public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 102 | 105 | log.info("Creating new license type"); |
|---|
| 103 | 106 | EntityManager em = emProvider.get(); |
|---|
| 104 | | - app.setCreationTimestamp(new Date()); |
|---|
| 105 | | - em.persist(app); |
|---|
| 107 | + Application app = null; |
|---|
| 108 | + if (lt.getApplicationId() != null) { |
|---|
| 109 | + app = em.find(Application.class, lt.getApplicationId()); |
|---|
| 110 | + if (app == null) { |
|---|
| 111 | + log.error("LicenseType application with id {} not found in DB", lt.getApplicationId()); |
|---|
| 112 | + return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "License type's app not found with ID: " + lt.getApplicationId()).build(); |
|---|
| 113 | + } |
|---|
| 114 | + } |
|---|
| 106 | 115 | |
|---|
| 107 | | - return Response.ok(app).build(); |
|---|
| 116 | + lt.setApplication(app); |
|---|
| 117 | + lt.setCreationTimestamp(new Date()); |
|---|
| 118 | + em.persist(lt); |
|---|
| 119 | + |
|---|
| 120 | + return Response.ok(lt).build(); |
|---|
| 108 | 121 | } |
|---|
| 109 | 122 | |
|---|
| 110 | 123 | @PUT |
|---|
| .. | .. |
|---|
| 114 | 127 | @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 115 | 128 | @Produces( |
|---|
| 116 | 129 | { MediaType.APPLICATION_JSON }) |
|---|
| 117 | | - public Response modify(LicenseType app, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 130 | + public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 118 | 131 | log.info("Modifying license type with id: {}", ltid); |
|---|
| 119 | 132 | EntityManager em = emProvider.get(); |
|---|
| 120 | 133 | LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 121 | 134 | if (currentlt == null) { |
|---|
| 122 | 135 | log.error("LicenseType with id {} not found in DB", ltid); |
|---|
| 123 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 136 | + return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "License type not found with ID: " + ltid).build(); |
|---|
| 124 | 137 | } |
|---|
| 125 | | - currentlt.setName(app.getName()); |
|---|
| 126 | | - currentlt.setDescription(app.getDescription()); |
|---|
| 138 | + Application app = null; |
|---|
| 139 | + if (lt.getApplicationId() != null) { |
|---|
| 140 | + app = em.find(Application.class, lt.getApplicationId()); |
|---|
| 141 | + if (app == null) { |
|---|
| 142 | + log.error("LicenseType application with id {} not found in DB", lt.getApplicationId()); |
|---|
| 143 | + return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "License type's app not found with ID: " + lt.getApplicationId()).build(); |
|---|
| 144 | + } |
|---|
| 145 | + } |
|---|
| 146 | + currentlt.setCode(lt.getCode()); |
|---|
| 147 | + currentlt.setName(lt.getName()); |
|---|
| 148 | + currentlt.setDescription(lt.getDescription()); |
|---|
| 149 | + currentlt.setApplication(app); |
|---|
| 127 | 150 | em.persist(currentlt); |
|---|
| 128 | 151 | |
|---|
| 129 | 152 | return Response.ok(currentlt).build(); |
|---|