package org.gcube.application.geoportal.service.rest; import lombok.extern.slf4j.Slf4j; import org.bson.Document; import org.gcube.application.cms.serialization.Serialization; import org.gcube.application.geoportal.common.model.document.ProfiledDocument; import org.gcube.application.geoportal.common.model.profile.Profile; import org.gcube.application.geoportal.common.rest.InterfaceConstants; import org.gcube.application.geoportal.service.engine.mongo.ProfileManager; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @Path(InterfaceConstants.Methods.PROFILES) @Slf4j public class UseCaseDescriptors { private ProfileManager profileEngine; @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Profile createNew(Document toCreate) { return new GuardedMethod() { @Override protected Profile run() throws Exception, WebApplicationException { log.info("Creating new Profile ({})",toCreate); Profile toReturn= profileEngine.create(toCreate); log.info("Created new Profile (ID {})",toReturn.getId()); return toReturn; } }.execute().getResult(); } @PUT @Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Profile update(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String profileId, Document d) { return new GuardedMethod() { @Override protected Profile run() throws Exception, WebApplicationException { log.warn("Updating Profile ({})",profileId); return profileEngine.update(profileId,d); } }.execute().getResult(); } @DELETE @Produces(MediaType.APPLICATION_JSON) @Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}") public Boolean delete(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String id, @DefaultValue("false") @QueryParam(InterfaceConstants.Parameters.FORCE) Boolean force) { return new GuardedMethod() { @Override protected Boolean run() throws Exception, WebApplicationException { log.warn("Deleting Profile (ID {}). Force is {}",id,force); profileEngine.deleteById(id,force); return true; } }.execute().getResult(); } // BY ID @GET @Produces(MediaType.APPLICATION_JSON) @Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}") public Profile getById(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String id) { return new GuardedMethod() { @Override protected Profile run() throws Exception, WebApplicationException { return profileEngine.getById(id); } }.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 profileEngine.query(Serialization.parseQuery(queryString)); } }.execute().getResult(); } }