| .. | .. |
|---|
| 35 | 35 | import com.google.inject.persist.Transactional; |
|---|
| 36 | 36 | |
|---|
| 37 | 37 | /** |
|---|
| 38 | | - * LicenseType resource, this service will provide methods to create, modify and delete license types |
|---|
| 38 | + * LicenseType resource, this service will provide methods to create, modify and |
|---|
| 39 | + * delete license types |
|---|
| 39 | 40 | * |
|---|
| 40 | 41 | * @author roberto <roberto.sanchez@curisit.net> |
|---|
| 41 | 42 | */ |
|---|
| 42 | 43 | @Path("/licensetype") |
|---|
| 43 | 44 | public class LicenseTypeResource { |
|---|
| 44 | 45 | |
|---|
| 45 | | - private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class); |
|---|
| 46 | + private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class); |
|---|
| 46 | 47 | |
|---|
| 47 | | - @Inject |
|---|
| 48 | | - TokenHelper tokenHelper; |
|---|
| 48 | + @Inject |
|---|
| 49 | + TokenHelper tokenHelper; |
|---|
| 49 | 50 | |
|---|
| 50 | | - @Inject |
|---|
| 51 | | - Provider<EntityManager> emProvider; |
|---|
| 51 | + @Inject |
|---|
| 52 | + Provider<EntityManager> emProvider; |
|---|
| 52 | 53 | |
|---|
| 53 | | - public LicenseTypeResource() { |
|---|
| 54 | | - } |
|---|
| 54 | + public LicenseTypeResource() {} |
|---|
| 55 | 55 | |
|---|
| 56 | | - /** |
|---|
| 57 | | - * |
|---|
| 58 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 59 | | - */ |
|---|
| 60 | | - @GET |
|---|
| 61 | | - @Path("/") |
|---|
| 62 | | - @Produces( |
|---|
| 63 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 64 | | - public Response index() { |
|---|
| 65 | | - LOG.info("Getting license types list "); |
|---|
| 56 | + /** |
|---|
| 57 | + * |
|---|
| 58 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 59 | + */ |
|---|
| 60 | + @GET |
|---|
| 61 | + @Path("/") |
|---|
| 62 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 63 | + public Response index() { |
|---|
| 64 | + LOG.info("Getting license types list "); |
|---|
| 66 | 65 | |
|---|
| 67 | | - EntityManager em = emProvider.get(); |
|---|
| 68 | | - TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class); |
|---|
| 69 | | - List<LicenseType> list = q.getResultList(); |
|---|
| 66 | + EntityManager em = emProvider.get(); |
|---|
| 67 | + TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class); |
|---|
| 68 | + List<LicenseType> list = q.getResultList(); |
|---|
| 70 | 69 | |
|---|
| 71 | | - return Response.ok(list).build(); |
|---|
| 72 | | - } |
|---|
| 70 | + return Response.ok(list).build(); |
|---|
| 71 | + } |
|---|
| 73 | 72 | |
|---|
| 74 | | - /** |
|---|
| 75 | | - * |
|---|
| 76 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 77 | | - */ |
|---|
| 78 | | - @GET |
|---|
| 79 | | - @Path("/{ltid}") |
|---|
| 80 | | - @Produces( |
|---|
| 81 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 82 | | - public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 83 | | - LOG.info("Getting license type data for id: {}: ", ltid); |
|---|
| 84 | | - if (ltid == null || ltid.equals("")) { |
|---|
| 85 | | - LOG.error("LicenseType ID is mandatory"); |
|---|
| 86 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 87 | | - } |
|---|
| 73 | + /** |
|---|
| 74 | + * |
|---|
| 75 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 76 | + */ |
|---|
| 77 | + @GET |
|---|
| 78 | + @Path("/{ltid}") |
|---|
| 79 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 80 | + public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 81 | + LOG.info("Getting license type data for id: {}: ", ltid); |
|---|
| 82 | + if (ltid == null || "".equals(ltid)) { |
|---|
| 83 | + LOG.error("LicenseType ID is mandatory"); |
|---|
| 84 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 85 | + } |
|---|
| 88 | 86 | |
|---|
| 89 | | - EntityManager em = emProvider.get(); |
|---|
| 90 | | - LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 91 | | - if (lt == null) { |
|---|
| 92 | | - LOG.error("LicenseType with id {} not found in DB", ltid); |
|---|
| 93 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 94 | | - } |
|---|
| 95 | | - return Response.ok(lt).build(); |
|---|
| 96 | | - } |
|---|
| 87 | + EntityManager em = emProvider.get(); |
|---|
| 88 | + LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 89 | + if (lt == null) { |
|---|
| 90 | + LOG.error("LicenseType with id {} not found in DB", ltid); |
|---|
| 91 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 92 | + } |
|---|
| 93 | + return Response.ok(lt).build(); |
|---|
| 94 | + } |
|---|
| 97 | 95 | |
|---|
| 98 | | - @POST |
|---|
| 99 | | - @Path("/") |
|---|
| 100 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 101 | | - @Produces( |
|---|
| 102 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 103 | | - @Transactional |
|---|
| 104 | | - public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 105 | | - LOG.info("Creating new license type"); |
|---|
| 106 | | - EntityManager em = emProvider.get(); |
|---|
| 96 | + @POST |
|---|
| 97 | + @Path("/") |
|---|
| 98 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 99 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 100 | + @Transactional |
|---|
| 101 | + public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 102 | + LOG.info("Creating new license type"); |
|---|
| 103 | + EntityManager em = emProvider.get(); |
|---|
| 107 | 104 | |
|---|
| 108 | | - try { |
|---|
| 109 | | - setApplication(lt, lt.getApplicationId(), em); |
|---|
| 110 | | - } catch (SeCurisException e) { |
|---|
| 111 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 112 | | - } |
|---|
| 113 | | - |
|---|
| 114 | | - if (lt.getApplicationId() == null) { |
|---|
| 115 | | - LOG.error("Application is missing for current license type data"); |
|---|
| 116 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application is missing for current license type data").build(); |
|---|
| 117 | | - } |
|---|
| 105 | + try { |
|---|
| 106 | + setApplication(lt, lt.getApplicationId(), em); |
|---|
| 107 | + } catch (SeCurisException e) { |
|---|
| 108 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 109 | + } |
|---|
| 118 | 110 | |
|---|
| 119 | | - lt.setCreationTimestamp(new Date()); |
|---|
| 120 | | - em.persist(lt); |
|---|
| 111 | + if (lt.getApplicationId() == null) { |
|---|
| 112 | + LOG.error("Application is missing for current license type data"); |
|---|
| 113 | + return Response.status(Status.NOT_FOUND) |
|---|
| 114 | + .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application is missing for current license type data").build(); |
|---|
| 115 | + } |
|---|
| 121 | 116 | |
|---|
| 122 | | - return Response.ok(lt).build(); |
|---|
| 123 | | - } |
|---|
| 117 | + lt.setCreationTimestamp(new Date()); |
|---|
| 118 | + em.persist(lt); |
|---|
| 124 | 119 | |
|---|
| 125 | | - @PUT |
|---|
| 126 | | - @POST |
|---|
| 127 | | - @Path("/{ltid}") |
|---|
| 128 | | - @Transactional |
|---|
| 129 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 130 | | - @Produces( |
|---|
| 131 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 132 | | - public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 133 | | - LOG.info("Modifying license type with id: {}", ltid); |
|---|
| 134 | | - EntityManager em = emProvider.get(); |
|---|
| 135 | | - LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 136 | | - if (currentlt == null) { |
|---|
| 137 | | - LOG.error("LicenseType with id {} not found in DB", ltid); |
|---|
| 138 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type not found with ID: " + ltid).build(); |
|---|
| 139 | | - } |
|---|
| 120 | + return Response.ok(lt).build(); |
|---|
| 121 | + } |
|---|
| 140 | 122 | |
|---|
| 141 | | - try { |
|---|
| 142 | | - setApplication(currentlt, lt.getApplicationId(), em); |
|---|
| 143 | | - } catch (SeCurisException e) { |
|---|
| 144 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 145 | | - } |
|---|
| 146 | | - |
|---|
| 147 | | - currentlt.setCode(lt.getCode()); |
|---|
| 148 | | - currentlt.setName(lt.getName()); |
|---|
| 149 | | - currentlt.setDescription(lt.getDescription()); |
|---|
| 150 | | - em.persist(currentlt); |
|---|
| 123 | + @PUT |
|---|
| 124 | + @POST |
|---|
| 125 | + @Path("/{ltid}") |
|---|
| 126 | + @Transactional |
|---|
| 127 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 128 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 129 | + public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 130 | + LOG.info("Modifying license type with id: {}", ltid); |
|---|
| 131 | + EntityManager em = emProvider.get(); |
|---|
| 132 | + LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 133 | + if (currentlt == null) { |
|---|
| 134 | + LOG.error("LicenseType with id {} not found in DB", ltid); |
|---|
| 135 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type not found with ID: " + ltid) |
|---|
| 136 | + .build(); |
|---|
| 137 | + } |
|---|
| 151 | 138 | |
|---|
| 152 | | - return Response.ok(currentlt).build(); |
|---|
| 153 | | - } |
|---|
| 139 | + try { |
|---|
| 140 | + setApplication(currentlt, lt.getApplicationId(), em); |
|---|
| 141 | + } catch (SeCurisException e) { |
|---|
| 142 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 143 | + } |
|---|
| 154 | 144 | |
|---|
| 155 | | - private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException { |
|---|
| 156 | | - Application app = null; |
|---|
| 157 | | - if (applicationId != null) { |
|---|
| 158 | | - app = em.find(Application.class, applicationId); |
|---|
| 159 | | - if (app == null) { |
|---|
| 160 | | - LOG.error("LicenseType application with id {} not found in DB", applicationId); |
|---|
| 161 | | - |
|---|
| 162 | | - throw new SecurityException("License type's app not found with ID: " + applicationId); |
|---|
| 163 | | - } |
|---|
| 164 | | - } |
|---|
| 165 | | - licType.setApplication(app); |
|---|
| 166 | | - } |
|---|
| 145 | + currentlt.setCode(lt.getCode()); |
|---|
| 146 | + currentlt.setName(lt.getName()); |
|---|
| 147 | + currentlt.setDescription(lt.getDescription()); |
|---|
| 148 | + em.persist(currentlt); |
|---|
| 167 | 149 | |
|---|
| 168 | | - @DELETE |
|---|
| 169 | | - @Path("/{ltid}") |
|---|
| 170 | | - @Transactional |
|---|
| 171 | | - @Produces( |
|---|
| 172 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 173 | | - public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) { |
|---|
| 174 | | - LOG.info("Deleting app with id: {}", ltid); |
|---|
| 175 | | - EntityManager em = emProvider.get(); |
|---|
| 176 | | - LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 177 | | - if (app == null) { |
|---|
| 178 | | - LOG.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid); |
|---|
| 179 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 180 | | - } |
|---|
| 150 | + return Response.ok(currentlt).build(); |
|---|
| 151 | + } |
|---|
| 181 | 152 | |
|---|
| 182 | | - em.remove(app); |
|---|
| 183 | | - return Response.ok(Utils.createMap("success", true, "id", ltid)).build(); |
|---|
| 184 | | - } |
|---|
| 153 | + private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException { |
|---|
| 154 | + Application app = null; |
|---|
| 155 | + if (applicationId != null) { |
|---|
| 156 | + app = em.find(Application.class, applicationId); |
|---|
| 157 | + if (app == null) { |
|---|
| 158 | + LOG.error("LicenseType application with id {} not found in DB", applicationId); |
|---|
| 159 | + |
|---|
| 160 | + throw new SecurityException("License type's app not found with ID: " + applicationId); |
|---|
| 161 | + } |
|---|
| 162 | + } |
|---|
| 163 | + licType.setApplication(app); |
|---|
| 164 | + } |
|---|
| 165 | + |
|---|
| 166 | + @DELETE |
|---|
| 167 | + @Path("/{ltid}") |
|---|
| 168 | + @Transactional |
|---|
| 169 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 170 | + public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) { |
|---|
| 171 | + LOG.info("Deleting app with id: {}", ltid); |
|---|
| 172 | + EntityManager em = emProvider.get(); |
|---|
| 173 | + LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 174 | + if (app == null) { |
|---|
| 175 | + LOG.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid); |
|---|
| 176 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 177 | + } |
|---|
| 178 | + |
|---|
| 179 | + em.remove(app); |
|---|
| 180 | + return Response.ok(Utils.createMap("success", true, "id", ltid)).build(); |
|---|
| 181 | + } |
|---|
| 185 | 182 | |
|---|
| 186 | 183 | } |
|---|