package org.gcube.application.geoportal.service.rest; import lombok.extern.slf4j.Slf4j; import org.bson.Document; import org.gcube.application.geoportal.common.model.document.ProfiledDocument; import org.gcube.application.geoportal.common.model.rest.Configuration; import org.gcube.application.geoportal.common.model.rest.QueryRequest; import org.gcube.application.geoportal.common.rest.InterfaceConstants; import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest; import org.gcube.application.geoportal.common.model.rest.StepExecutionRequest; import org.gcube.application.geoportal.service.engine.mongo.ProfiledMongoManager; import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException; import org.gcube.application.cms.serialization.Serialization; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @Path(InterfaceConstants.Methods.PROJECTS+"/{"+InterfaceConstants.Parameters.PROFILE_ID+"}") @Slf4j public class ProfiledDocuments { private ProfiledMongoManager manager; public ProfiledDocuments(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String profileID) throws ConfigurationException { log.info("Accessing profiles "+profileID); manager=new GuardedMethod(){ @Override protected ProfiledMongoManager run() throws Exception { return new ProfiledMongoManager(profileID); } }.execute().getResult(); } @GET @Path(InterfaceConstants.Methods.CONFIGURATION_PATH) @Produces(MediaType.APPLICATION_JSON) public Configuration getConfiguration(){ return new GuardedMethod(){ @Override protected Configuration run() throws Exception, WebApplicationException { //manager.getConfiguration(); throw new Exception("Implement This Method"); } }.execute().getResult(); } @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public ProfiledDocument createNew(Document d) { return new GuardedMethod() { @Override protected ProfiledDocument run() throws Exception, WebApplicationException { log.info("Creating new ProfiledDocument ({})",manager.getProfile().getId()); ProfiledDocument toReturn= manager.registerNew(d); log.info("Created new ProfiledDocument ({}, ID {})",manager.getProfile().getId(),toReturn.get_id()); return toReturn; } }.execute().getResult(); } @PUT @Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public ProfiledDocument update(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String documentId, Document d) { return new GuardedMethod() { @Override protected ProfiledDocument run() throws Exception, WebApplicationException { log.info("Updating ProfiledDocument ({}, ID {})",manager.getProfile().getId(),documentId); return manager.update(documentId,d); } }.execute().getResult(); } @DELETE @Produces(MediaType.APPLICATION_JSON) @Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}") public Boolean delete(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id, @DefaultValue("false") @QueryParam(InterfaceConstants.Parameters.FORCE) Boolean force) { return new GuardedMethod() { @Override protected Boolean run() throws Exception, WebApplicationException { log.info("Deleting ProfiledDocument ({}, ID {}). Force is {}",manager.getProfile().getId(),id,force); manager.delete(id,force); return true; } }.execute().getResult(); } @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("/"+InterfaceConstants.Methods.REGISTER_FILES_PATH+"/{"+InterfaceConstants.Parameters.PROJECT_ID+"}") public ProfiledDocument registerFileSet(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id, RegisterFileSetRequest request) { return new GuardedMethod() { @Override protected ProfiledDocument run() throws Exception, WebApplicationException { log.info("Registering {} file(s) for ProfiledDocument ({}, ID {}) at path {}", request.getStreams().size(), manager.getProfile().getId(), id,request.getDestinationPath()); request.validate(); return manager.registerFileSet(id,request); } }.execute().getResult(); } @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("/"+InterfaceConstants.Methods.DELETE_FILES_PATH+"/{"+InterfaceConstants.Parameters.PROJECT_ID+"}") public ProfiledDocument deleteFileSet( @PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id, @DefaultValue("false") @QueryParam(InterfaceConstants.Parameters.FORCE) Boolean force, String path) { return new GuardedMethod() { @Override protected ProfiledDocument run() throws Exception, WebApplicationException { log.info("Deleting FileSet of ProfiledDocument ({}, ID {}) at path {}. Force is {}", manager.getProfile().getId(), id,path,force); return manager.deleteFileSet(id,path,force); } }.execute().getResult(); } @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("/"+InterfaceConstants.Methods.STEP+"/{"+InterfaceConstants.Parameters.PROJECT_ID+"}") public ProfiledDocument performStep( @PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id, StepExecutionRequest request) { return new GuardedMethod() { @Override protected ProfiledDocument run() throws Exception, WebApplicationException { log.info("Executing step {} on ProfiledDocument ({},ID,{}) with options {}", request.getStepID(), manager.getProfile().getId(), id,request.getOptions()); return manager.performStep(id,request.getStepID(),request.getOptions()); } }.execute().getResult(); } //********************************** READ @GET @Produces(MediaType.APPLICATION_JSON) public Iterable list() { return new GuardedMethod>() { protected Iterable run() throws Exception ,WebApplicationException { return manager.query(new QueryRequest()); }; }.execute().getResult(); } // BY ID @GET @Produces(MediaType.APPLICATION_JSON) @Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}") public ProfiledDocument getById(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) { return new GuardedMethod() { @Override protected ProfiledDocument run() throws Exception, WebApplicationException { return manager.getByID(id); } }.execute().getResult(); } @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("/"+InterfaceConstants.Methods.SEARCH_PATH) public String search(String filter){ return new GuardedMethod() { @Override protected String run() throws Exception, WebApplicationException { QueryRequest req=new QueryRequest(); req.setFilter(Document.parse(filter)); return Serialization.write(manager.query(req)); } }.execute().getResult(); } @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("/"+InterfaceConstants.Methods.QUERY_PATH) public Iterable query(String queryString){ return new GuardedMethod>() { @Override protected Iterable run() throws Exception, WebApplicationException { return manager.query(Serialization.parseQuery(queryString)); } }.execute().getResult(); } }