package net.curisit.securis.services; import java.util.Date; import java.util.List; 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.integrity.exception.CurisException; import net.curisit.securis.SecurisErrorHandler; import net.curisit.securis.db.Pack; import net.curisit.securis.db.User; import net.curisit.securis.utils.TokenHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.inject.persist.Transactional; /** * Pack resource, this service will provide methods to create, modify and delete packs * * @author roberto */ @Path("/pack") public class PackResource { private static final Logger log = LoggerFactory.getLogger(PackResource.class); @Inject TokenHelper tokenHelper; @Inject Provider emProvider; public PackResource() { } /** * * @return the server version in format majorVersion.minorVersion */ @GET @Path("/") @Produces( { MediaType.APPLICATION_JSON }) public Response index() { log.info("Getting packs list "); EntityManager em = emProvider.get(); TypedQuery q = em.createNamedQuery("list-packs-by-orgs", Pack.class); List list = q.getResultList(); return Response.ok(list).build(); } /** * * @return the server version in format majorVersion.minorVersion */ @GET @Path("/{packId}") @Produces( { MediaType.APPLICATION_JSON }) public Response get(@PathParam("packId") String packId, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { log.info("Getting pack data for id: {}: ", packId); if (packId == null || packId.equals("")) { log.error("Pack ID is mandatory"); return Response.status(Status.NOT_FOUND).build(); } EntityManager em = emProvider.get(); Pack lt = em.find(Pack.class, Integer.parseInt(packId)); if (lt == null) { log.error("Pack with id {} not found in DB", packId); return Response.status(Status.NOT_FOUND).build(); } return Response.ok(lt).build(); } @POST @Path("/") @Consumes(MediaType.APPLICATION_JSON) @Produces( { MediaType.APPLICATION_JSON }) @Transactional public Response create(Pack pack, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { log.info("Creating new pack"); EntityManager em = emProvider.get(); pack.setCreationTimestamp(new Date()); em.persist(pack); return Response.ok(pack).build(); } private User getUser(String username, EntityManager em) throws CurisException { User user = null; if (username != null) { user = em.find(User.class, username); if (user == null) { throw new CurisException("User not found"); } } return user; } @PUT @POST @Path("/{packId}") @Transactional @Consumes(MediaType.APPLICATION_JSON) @Produces( { MediaType.APPLICATION_JSON }) public Response modify(Pack pack, @PathParam("packId") String packId, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { log.info("Modifying pack with id: {}", packId); EntityManager em = emProvider.get(); em.persist(pack); return Response.ok(pack).build(); } @DELETE @Path("/{packId}") @Transactional @Produces( { MediaType.APPLICATION_JSON }) public Response delete(@PathParam("packId") String packId, @Context HttpServletRequest request) { log.info("Deleting pack with id: {}", packId); EntityManager em = emProvider.get(); Pack org = em.find(Pack.class, Integer.parseInt(packId)); if (org == null) { log.error("Pack with id {} can not be deleted, It was not found in DB", packId); return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Pack was not found, ID: " + packId).build(); } em.remove(org); return Response.ok(Utils.createMap("success", true, "id", packId)).build(); } }