package net.curisit.securis.services; import java.util.Date; import java.util.List; import java.util.Set; import javax.annotation.security.RolesAllowed; import javax.inject.Inject; import javax.inject.Provider; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import net.curisit.integrity.commons.Utils; import net.curisit.securis.DefaultExceptionHandler; import net.curisit.securis.SeCurisException; import net.curisit.securis.db.Application; import net.curisit.securis.db.LicenseType; import net.curisit.securis.db.LicenseTypeMetadata; import net.curisit.securis.security.BasicSecurityContext; import net.curisit.securis.security.Securable; import net.curisit.securis.services.exception.SeCurisServiceException; import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; import net.curisit.securis.utils.TokenHelper; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.google.inject.persist.Transactional; /** * LicenseType resource, this service will provide methods to create, modify and * delete license types * * @author roberto */ @Path("/licensetype") public class LicenseTypeResource { private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class); @Inject TokenHelper tokenHelper; @Inject Provider emProvider; public LicenseTypeResource() { } /** * * @return the server version in format majorVersion.minorVersion */ @GET @Path("/") @Produces({ MediaType.APPLICATION_JSON }) @Securable public Response index() { LOG.info("Getting license types list "); EntityManager em = emProvider.get(); em.clear(); TypedQuery q = em.createNamedQuery("list-license_types", LicenseType.class); List list = q.getResultList(); return Response.ok(list).build(); } /** * * @return the server version in format majorVersion.minorVersion * @throws SeCurisServiceException */ @GET @Path("/{ltid}") @Produces({ MediaType.APPLICATION_JSON }) @Securable public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) throws SeCurisServiceException { LOG.info("Getting license type data for id: {}: ", ltid); if (ltid == null || "".equals(ltid)) { LOG.error("LicenseType ID is mandatory"); return Response.status(Status.NOT_FOUND).build(); } EntityManager em = emProvider.get(); em.clear(); LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid)); if (lt == null) { LOG.error("LicenseType with id {} not found in DB", ltid); throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "LicenseType was not found in DB"); } return Response.ok(lt).build(); } @POST @Path("/") @Consumes(MediaType.APPLICATION_JSON) @Produces({ MediaType.APPLICATION_JSON }) @Transactional @Securable @RolesAllowed(BasicSecurityContext.ROL_ADMIN) public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { LOG.info("Creating new license type"); EntityManager em = emProvider.get(); try { setApplication(lt, lt.getApplicationId(), em); } catch (SeCurisException e) { return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); } if (lt.getApplicationId() == null) { LOG.error("Application is missing for current license type data"); return Response.status(Status.NOT_FOUND) .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application is missing for current license type data").build(); } lt.setCreationTimestamp(new Date()); em.persist(lt); Set newMD = lt.getMetadata(); if (newMD != null) { for (LicenseTypeMetadata md : newMD) { md.setLicenseType(lt); em.persist(md); } } lt.setMetadata(newMD); return Response.ok(lt).build(); } @PUT @POST @Path("/{ltid}") @Transactional @Consumes(MediaType.APPLICATION_JSON) @Produces({ MediaType.APPLICATION_JSON }) @Securable @RolesAllowed(BasicSecurityContext.ROL_ADMIN) public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { LOG.info("Modifying license type with id: {}", ltid); EntityManager em = emProvider.get(); LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid)); if (currentlt == null) { LOG.error("LicenseType with id {} not found in DB", ltid); return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type not found with ID: " + ltid) .build(); } try { setApplication(currentlt, lt.getApplicationId(), em); } catch (SeCurisException e) { return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); } currentlt.setCode(lt.getCode()); currentlt.setName(lt.getName()); currentlt.setDescription(lt.getDescription()); Set newMD = lt.getMetadata(); for (LicenseTypeMetadata currentMd : currentlt.getMetadata()) { if (newMD == null || !newMD.contains(currentMd)) { em.remove(currentMd); LOG.info("Removing MD: {}", currentMd); } } if (newMD != null) { Set oldMD = currentlt.getMetadata(); for (LicenseTypeMetadata md : newMD) { if (oldMD.contains(md)) { em.merge(md); } else { md.setLicenseType(currentlt); em.persist(md); } } } currentlt.setMetadata(newMD); em.merge(currentlt); return Response.ok(currentlt).build(); } private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException { Application app = null; if (applicationId != null) { app = em.find(Application.class, applicationId); if (app == null) { LOG.error("LicenseType application with id {} not found in DB", applicationId); throw new SecurityException("License type's app not found with ID: " + applicationId); } } licType.setApplication(app); } @DELETE @Path("/{ltid}") @Transactional @Produces({ MediaType.APPLICATION_JSON }) @Securable @RolesAllowed(BasicSecurityContext.ROL_ADMIN) public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) { LOG.info("Deleting app with id: {}", ltid); EntityManager em = emProvider.get(); LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid)); if (app == null) { LOG.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid); return Response.status(Status.NOT_FOUND).build(); } em.remove(app); return Response.ok(Utils.createMap("success", true, "id", ltid)).build(); } }