| .. | .. |
|---|
| 33 | 33 | import com.google.inject.persist.Transactional; |
|---|
| 34 | 34 | |
|---|
| 35 | 35 | /** |
|---|
| 36 | | - * Application resource, this service will provide methods to create, modify and delete applications |
|---|
| 36 | + * Application resource, this service will provide methods to create, modify and |
|---|
| 37 | + * delete applications |
|---|
| 37 | 38 | * |
|---|
| 38 | 39 | * @author roberto <roberto.sanchez@curisit.net> |
|---|
| 39 | 40 | */ |
|---|
| 40 | 41 | @Path("/application") |
|---|
| 41 | 42 | public class ApplicationResource { |
|---|
| 42 | 43 | |
|---|
| 43 | | - @Inject |
|---|
| 44 | | - TokenHelper tokenHelper; |
|---|
| 44 | + @Inject |
|---|
| 45 | + TokenHelper tokenHelper; |
|---|
| 45 | 46 | |
|---|
| 46 | | - @Inject |
|---|
| 47 | | - Provider<EntityManager> emProvider; |
|---|
| 47 | + @Inject |
|---|
| 48 | + Provider<EntityManager> emProvider; |
|---|
| 48 | 49 | |
|---|
| 49 | | - private static final Logger LOG = LogManager.getLogger(ApplicationResource.class); |
|---|
| 50 | + private static final Logger LOG = LogManager.getLogger(ApplicationResource.class); |
|---|
| 50 | 51 | |
|---|
| 51 | | - public ApplicationResource() { |
|---|
| 52 | | - } |
|---|
| 52 | + public ApplicationResource() {} |
|---|
| 53 | 53 | |
|---|
| 54 | | - /** |
|---|
| 55 | | - * |
|---|
| 56 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 57 | | - */ |
|---|
| 58 | | - @GET |
|---|
| 59 | | - @Path("/") |
|---|
| 60 | | - @Produces( |
|---|
| 61 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 62 | | - public Response index() { |
|---|
| 63 | | - LOG.info("Getting applications list "); |
|---|
| 54 | + /** |
|---|
| 55 | + * |
|---|
| 56 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 57 | + */ |
|---|
| 58 | + @GET |
|---|
| 59 | + @Path("/") |
|---|
| 60 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 61 | + public Response index() { |
|---|
| 62 | + LOG.info("Getting applications list "); |
|---|
| 64 | 63 | |
|---|
| 65 | | - EntityManager em = emProvider.get(); |
|---|
| 66 | | - TypedQuery<Application> q = em.createNamedQuery("list-applications", Application.class); |
|---|
| 67 | | - List<Application> list = q.getResultList(); |
|---|
| 64 | + EntityManager em = emProvider.get(); |
|---|
| 65 | + TypedQuery<Application> q = em.createNamedQuery("list-applications", Application.class); |
|---|
| 66 | + List<Application> list = q.getResultList(); |
|---|
| 68 | 67 | |
|---|
| 69 | | - return Response.ok(list).build(); |
|---|
| 70 | | - } |
|---|
| 68 | + return Response.ok(list).build(); |
|---|
| 69 | + } |
|---|
| 71 | 70 | |
|---|
| 72 | | - /** |
|---|
| 73 | | - * |
|---|
| 74 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 75 | | - */ |
|---|
| 76 | | - @GET |
|---|
| 77 | | - @Path("/{appid}") |
|---|
| 78 | | - @Produces( |
|---|
| 79 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 80 | | - public Response get(@PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 81 | | - LOG.info("Getting application data for id: {}: ", appid); |
|---|
| 82 | | - if (appid == null || appid.equals("")) { |
|---|
| 83 | | - LOG.error("Application ID is mandatory"); |
|---|
| 84 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 85 | | - } |
|---|
| 71 | + /** |
|---|
| 72 | + * |
|---|
| 73 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 74 | + */ |
|---|
| 75 | + @GET |
|---|
| 76 | + @Path("/{appid}") |
|---|
| 77 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 78 | + public Response get(@PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 79 | + LOG.info("Getting application data for id: {}: ", appid); |
|---|
| 80 | + if (appid == null || "".equals(appid)) { |
|---|
| 81 | + LOG.error("Application ID is mandatory"); |
|---|
| 82 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 83 | + } |
|---|
| 86 | 84 | |
|---|
| 87 | | - EntityManager em = emProvider.get(); |
|---|
| 88 | | - Application app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 89 | | - if (app == null) { |
|---|
| 90 | | - LOG.error("Application with id {} not found in DB", appid); |
|---|
| 85 | + EntityManager em = emProvider.get(); |
|---|
| 86 | + Application app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 87 | + if (app == null) { |
|---|
| 88 | + LOG.error("Application with id {} not found in DB", appid); |
|---|
| 91 | 89 | |
|---|
| 92 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid).build(); |
|---|
| 93 | | - } |
|---|
| 94 | | - return Response.ok(app).build(); |
|---|
| 95 | | - } |
|---|
| 90 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid) |
|---|
| 91 | + .build(); |
|---|
| 92 | + } |
|---|
| 93 | + return Response.ok(app).build(); |
|---|
| 94 | + } |
|---|
| 96 | 95 | |
|---|
| 97 | | - @POST |
|---|
| 98 | | - @Path("/") |
|---|
| 99 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 100 | | - @Produces( |
|---|
| 101 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 102 | | - @Transactional |
|---|
| 103 | | - public Response create(Application app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 104 | | - LOG.info("Creating new application"); |
|---|
| 105 | | - EntityManager em = emProvider.get(); |
|---|
| 106 | | - app.setCreationTimestamp(new Date()); |
|---|
| 107 | | - em.persist(app); |
|---|
| 96 | + @POST |
|---|
| 97 | + @Path("/") |
|---|
| 98 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 99 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 100 | + @Transactional |
|---|
| 101 | + public Response create(Application app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 102 | + LOG.info("Creating new application"); |
|---|
| 103 | + EntityManager em = emProvider.get(); |
|---|
| 104 | + app.setCreationTimestamp(new Date()); |
|---|
| 105 | + em.persist(app); |
|---|
| 108 | 106 | |
|---|
| 109 | | - return Response.ok(app).build(); |
|---|
| 110 | | - } |
|---|
| 107 | + return Response.ok(app).build(); |
|---|
| 108 | + } |
|---|
| 111 | 109 | |
|---|
| 112 | | - @PUT |
|---|
| 113 | | - @POST |
|---|
| 114 | | - @Path("/{appid}") |
|---|
| 115 | | - @Transactional |
|---|
| 116 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 117 | | - @Produces( |
|---|
| 118 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 119 | | - public Response modify(Application app, @PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 120 | | - LOG.info("Modifying application with id: {}", appid); |
|---|
| 121 | | - EntityManager em = emProvider.get(); |
|---|
| 122 | | - Application currentapp = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 123 | | - if (currentapp == null) { |
|---|
| 124 | | - LOG.error("Application with id {} not found in DB", appid); |
|---|
| 125 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid).build(); |
|---|
| 126 | | - } |
|---|
| 127 | | - currentapp.setName(app.getName()); |
|---|
| 128 | | - currentapp.setDescription(app.getDescription()); |
|---|
| 129 | | - em.persist(currentapp); |
|---|
| 110 | + @PUT |
|---|
| 111 | + @POST |
|---|
| 112 | + @Path("/{appid}") |
|---|
| 113 | + @Transactional |
|---|
| 114 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 115 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 116 | + public Response modify(Application app, @PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 117 | + LOG.info("Modifying application with id: {}", appid); |
|---|
| 118 | + EntityManager em = emProvider.get(); |
|---|
| 119 | + Application currentapp = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 120 | + if (currentapp == null) { |
|---|
| 121 | + LOG.error("Application with id {} not found in DB", appid); |
|---|
| 122 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid) |
|---|
| 123 | + .build(); |
|---|
| 124 | + } |
|---|
| 125 | + currentapp.setName(app.getName()); |
|---|
| 126 | + currentapp.setDescription(app.getDescription()); |
|---|
| 127 | + em.persist(currentapp); |
|---|
| 130 | 128 | |
|---|
| 131 | | - return Response.ok(currentapp).build(); |
|---|
| 132 | | - } |
|---|
| 129 | + return Response.ok(currentapp).build(); |
|---|
| 130 | + } |
|---|
| 133 | 131 | |
|---|
| 134 | | - @DELETE |
|---|
| 135 | | - @Path("/{appid}") |
|---|
| 136 | | - @Transactional |
|---|
| 137 | | - @Produces( |
|---|
| 138 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 139 | | - public Response delete(@PathParam("appid") String appid, @Context HttpServletRequest request) { |
|---|
| 140 | | - LOG.info("Deleting app with id: {}", appid); |
|---|
| 141 | | - EntityManager em = emProvider.get(); |
|---|
| 142 | | - Application app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 143 | | - if (app == null) { |
|---|
| 144 | | - LOG.error("Application with id {} can not be deleted, It was not found in DB", appid); |
|---|
| 145 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid).build(); |
|---|
| 146 | | - } |
|---|
| 132 | + @DELETE |
|---|
| 133 | + @Path("/{appid}") |
|---|
| 134 | + @Transactional |
|---|
| 135 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 136 | + public Response delete(@PathParam("appid") String appid, @Context HttpServletRequest request) { |
|---|
| 137 | + LOG.info("Deleting app with id: {}", appid); |
|---|
| 138 | + EntityManager em = emProvider.get(); |
|---|
| 139 | + Application app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 140 | + if (app == null) { |
|---|
| 141 | + LOG.error("Application with id {} can not be deleted, It was not found in DB", appid); |
|---|
| 142 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid) |
|---|
| 143 | + .build(); |
|---|
| 144 | + } |
|---|
| 147 | 145 | |
|---|
| 148 | | - if (app.getLicenseTypes() != null && app.getLicenseTypes().size() > 0) { |
|---|
| 149 | | - return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application can not be deleted becasue has assigned one or more License types, ID: " + appid).build(); |
|---|
| 150 | | - } |
|---|
| 146 | + if (app.getLicenseTypes() != null && app.getLicenseTypes().size() > 0) { |
|---|
| 147 | + return Response |
|---|
| 148 | + .status(Status.FORBIDDEN) |
|---|
| 149 | + .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, |
|---|
| 150 | + "Application can not be deleted becasue has assigned one or more License types, ID: " + appid).build(); |
|---|
| 151 | + } |
|---|
| 151 | 152 | |
|---|
| 152 | | - em.remove(app); |
|---|
| 153 | | - return Response.ok(Utils.createMap("success", true, "id", appid)).build(); |
|---|
| 154 | | - } |
|---|
| 153 | + em.remove(app); |
|---|
| 154 | + return Response.ok(Utils.createMap("success", true, "id", appid)).build(); |
|---|
| 155 | + } |
|---|
| 155 | 156 | |
|---|
| 156 | 157 | } |
|---|