gcube-cms-suite/geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/UseCaseDescriptors.java

94 lines
3.5 KiB
Java

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.useCaseDescriptor.UseCaseDescriptor;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.service.engine.mongo.UCDManagerI;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path(InterfaceConstants.Methods.PROFILES)
@Slf4j
public class UseCaseDescriptors {
private UCDManagerI profileEngine;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public UseCaseDescriptor createNew(Document toCreate) {
return new GuardedMethod<UseCaseDescriptor>() {
@Override
protected UseCaseDescriptor run() throws Exception, WebApplicationException {
log.info("Creating new UseCaseDescriptor ({})",toCreate);
UseCaseDescriptor toReturn= profileEngine.create(toCreate);
log.info("Created new UseCaseDescriptor (ID {})",toReturn.getId());
return toReturn;
}
}.execute().getResult();
}
@PUT
@Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public UseCaseDescriptor update(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String profileId, Document d) {
return new GuardedMethod<UseCaseDescriptor>() {
@Override
protected UseCaseDescriptor run() throws Exception, WebApplicationException {
log.warn("Updating UseCaseDescriptor ({})",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<Boolean>() {
@Override
protected Boolean run() throws Exception, WebApplicationException {
log.warn("Deleting UseCaseDescriptor (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 UseCaseDescriptor getById(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String id) {
return new GuardedMethod<UseCaseDescriptor>() {
@Override
protected UseCaseDescriptor 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<Iterable<?>>() {
@Override
protected Iterable<?> run() throws Exception, WebApplicationException {
return profileEngine.query(Serialization.parseQuery(queryString));
}
}.execute().getResult();
}
}