This repository has been archived on 2021-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/ConcessioniOverMongo.java

248 lines
8.7 KiB
Java
Raw Normal View History

2020-12-17 18:27:45 +01:00
package org.gcube.application.geoportal.service.rest;
import lombok.extern.slf4j.Slf4j;
2021-09-09 13:35:09 +02:00
import org.bson.Document;
2020-12-17 18:27:45 +01:00
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.rest.AddSectionToConcessioneRequest;
import org.gcube.application.geoportal.common.model.rest.Configuration;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
2020-12-17 18:27:45 +01:00
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.service.engine.mongo.ConcessioniMongoManager;
import org.gcube.application.geoportal.service.engine.postgis.PostgisIndex;
2021-08-03 12:50:02 +02:00
import org.gcube.application.geoportal.service.model.internal.faults.DeletionException;
2021-09-09 13:35:09 +02:00
import org.gcube.application.geoportal.service.utils.Serialization;
2020-12-17 18:27:45 +01:00
import org.json.JSONArray;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
2021-08-03 12:50:02 +02:00
import javax.ws.rs.core.Response;
2020-12-17 18:27:45 +01:00
2021-01-04 16:58:40 +01:00
@Path(InterfaceConstants.Methods.MONGO_CONCESSIONI)
2020-12-17 18:27:45 +01:00
@Slf4j
public class ConcessioniOverMongo {
2021-08-04 16:22:29 +02:00
@GET
@Path(InterfaceConstants.Methods.CONFIGURATION_PATH)
2021-08-04 16:22:29 +02:00
@Produces(MediaType.APPLICATION_JSON)
2021-09-02 18:05:59 +02:00
public Configuration getConfiguration(){
return new GuardedMethod<Configuration>(){
2021-08-04 16:22:29 +02:00
@Override
2021-09-02 18:05:59 +02:00
protected Configuration run() throws Exception, WebApplicationException {
2021-08-04 16:22:29 +02:00
Configuration toReturn = new Configuration();
toReturn.setIndex(new PostgisIndex().getInfo());
log.info("Returning configuration {} ",toReturn);
2021-09-02 18:05:59 +02:00
return toReturn;
2021-08-04 16:22:29 +02:00
}
}.execute().getResult();
}
2020-12-17 18:27:45 +01:00
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
2021-09-02 18:05:59 +02:00
public Concessione replace(Concessione c) {
return new GuardedMethod<Concessione> () {
2020-12-17 18:27:45 +01:00
@Override
2021-09-02 18:05:59 +02:00
protected Concessione run() throws Exception, WebApplicationException {
//Concessione c=Serialization.read(jsonString, Concessione.class);
2020-12-17 18:27:45 +01:00
ConcessioniMongoManager manager=new ConcessioniMongoManager();
2020-12-22 18:22:56 +01:00
manager.replace(c);
2020-12-17 18:27:45 +01:00
2021-09-02 18:05:59 +02:00
// return Serialization.write(manager.getById(c.getMongo_id()));
return manager.getById(c.getMongo_id());
2020-12-17 18:27:45 +01:00
}
}.execute().getResult();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
2021-09-02 18:05:59 +02:00
public Concessione createNew(Concessione c) {
return new GuardedMethod<Concessione>() {
2020-12-17 18:27:45 +01:00
@Override
2021-09-02 18:05:59 +02:00
protected Concessione run() throws Exception, WebApplicationException {
2020-12-17 18:27:45 +01:00
ConcessioniMongoManager manager=new ConcessioniMongoManager();
2021-09-02 18:05:59 +02:00
return manager.registerNew(c);
2020-12-17 18:27:45 +01:00
}
}.execute().getResult();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
2021-09-02 18:05:59 +02:00
public Iterable<Concessione> list() {
return new GuardedMethod<Iterable<Concessione>>() {
protected Iterable<Concessione> run() throws Exception ,WebApplicationException {
2020-12-17 18:27:45 +01:00
ConcessioniMongoManager manager=new ConcessioniMongoManager();
JSONArray toReturn=new JSONArray();
2021-09-02 18:05:59 +02:00
return manager.list();
2020-12-17 18:27:45 +01:00
};
}.execute().getResult();
}
// BY ID
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
2021-09-02 18:05:59 +02:00
public Concessione getById(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
return new GuardedMethod<Concessione>() {
2020-12-17 18:27:45 +01:00
@Override
2021-09-02 18:05:59 +02:00
protected Concessione run() throws Exception, WebApplicationException {
2020-12-17 18:27:45 +01:00
ConcessioniMongoManager manager=new ConcessioniMongoManager();
2021-09-02 18:05:59 +02:00
return manager.getById(id);
2020-12-17 18:27:45 +01:00
}
}.execute().getResult();
}
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
2021-08-02 17:43:30 +02:00
public void deleteById(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,
@QueryParam(InterfaceConstants.Parameters.FORCE) Boolean forceOption) {
2020-12-17 18:27:45 +01:00
new GuardedMethod<Concessione> () {
@Override
protected Concessione run() throws Exception, WebApplicationException {
2021-08-03 12:50:02 +02:00
try{
Boolean force=(forceOption!=null)?forceOption:false;
ConcessioniMongoManager manager=new ConcessioniMongoManager();
manager.deleteById(id,force);
return null;
}catch(DeletionException e){
throw new WebApplicationException("Unable to delete "+id,e, Response.Status.EXPECTATION_FAILED);
}
2020-12-17 18:27:45 +01:00
}
}.execute();
}
2020-12-18 18:24:20 +01:00
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
2021-09-02 18:05:59 +02:00
public Concessione update(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,Concessione c) {
return new GuardedMethod<Concessione>() {
2020-12-18 18:24:20 +01:00
@Override
2021-09-02 18:05:59 +02:00
protected Concessione run() throws Exception, WebApplicationException {
2021-08-03 16:24:49 +02:00
ConcessioniMongoManager manager=new ConcessioniMongoManager();
c.setMongo_id(id);
2021-09-02 18:05:59 +02:00
return manager.replace(c);
2020-12-18 18:24:20 +01:00
}
}.execute().getResult();
}
2020-12-17 18:27:45 +01:00
@PUT
@Produces(MediaType.APPLICATION_JSON)
2021-01-04 16:58:40 +01:00
@Path("/{"+InterfaceConstants.Methods.PUBLISH_PATH+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
2021-09-02 18:05:59 +02:00
public Concessione publish(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
return new GuardedMethod<Concessione>() {
2020-12-17 18:27:45 +01:00
@Override
2021-09-02 18:05:59 +02:00
protected Concessione run() throws Exception, WebApplicationException {
2020-12-17 18:27:45 +01:00
ConcessioniMongoManager manager=new ConcessioniMongoManager();
2021-09-02 18:05:59 +02:00
return manager.publish(id);
2020-12-17 18:27:45 +01:00
}
}.execute().getResult();
}
2021-08-03 16:24:49 +02:00
@DELETE
@Path("/{"+InterfaceConstants.Methods.PUBLISH_PATH+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
2021-09-02 18:05:59 +02:00
public Concessione unpublish(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
2021-08-03 17:44:53 +02:00
log.info("Unpublishing {} ",id);
2021-09-02 18:05:59 +02:00
return new GuardedMethod<Concessione>() {
2021-08-03 16:24:49 +02:00
@Override
2021-09-02 18:05:59 +02:00
protected Concessione run() throws Exception, WebApplicationException {
2021-08-03 16:24:49 +02:00
ConcessioniMongoManager manager=new ConcessioniMongoManager();
2021-09-02 18:05:59 +02:00
return manager.unpublish(id);
2021-08-03 16:24:49 +02:00
}
}.execute().getResult();
2021-08-03 17:44:53 +02:00
2021-08-03 16:24:49 +02:00
}
2020-12-17 18:34:53 +01:00
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
2021-01-04 16:58:40 +01:00
@Path("/"+InterfaceConstants.Methods.REGISTER_FILES_PATH+"/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
2021-09-02 18:05:59 +02:00
public Concessione registerFile(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id, AddSectionToConcessioneRequest request) {
return new GuardedMethod<Concessione>() {
2020-12-17 18:34:53 +01:00
@Override
2021-09-02 18:05:59 +02:00
protected Concessione run() throws Exception, WebApplicationException {
2020-12-18 18:24:20 +01:00
log.info("Registering {} file(s) for {} Concessione ID {}",
request.getStreams().size(),
request.getDestinationPath(),id);
ConcessioniMongoManager manager=new ConcessioniMongoManager();
2021-09-02 18:05:59 +02:00
return manager.persistContent(id, request.getDestinationPath(), request.getStreams());
2020-12-17 18:34:53 +01:00
}
}.execute().getResult();
}
2021-08-06 16:17:24 +02:00
2021-09-03 15:28:50 +02:00
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/"+InterfaceConstants.Methods.DELETE_FILES_PATH+"/{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public Concessione clearFileset(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id, String path) {
return new GuardedMethod<Concessione>() {
@Override
protected Concessione run() throws Exception, WebApplicationException {
log.info("Clearing files of {} Concessione ID {}",path,id);
ConcessioniMongoManager manager=new ConcessioniMongoManager();
return manager.unregisterFileset(id,path);
}
}.execute().getResult();
}
2021-08-06 16:17:24 +02:00
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/"+InterfaceConstants.Methods.SEARCH_PATH)
2021-08-06 18:33:34 +02:00
public Iterable<Concessione> search(String filter){
return new GuardedMethod<Iterable<Concessione>>() {
2021-08-06 16:17:24 +02:00
@Override
2021-08-06 18:33:34 +02:00
protected Iterable<Concessione> run() throws Exception, WebApplicationException {
2021-08-06 16:17:24 +02:00
ConcessioniMongoManager manager=new ConcessioniMongoManager();
2021-09-09 13:35:09 +02:00
return manager.search(Document.parse(filter));
2021-08-06 16:17:24 +02:00
}
}.execute().getResult();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/"+InterfaceConstants.Methods.QUERY_PATH)
2021-09-09 13:35:09 +02:00
public String query(String queryString){
return new GuardedMethod<String>() {
2021-08-06 16:17:24 +02:00
@Override
2021-09-09 13:35:09 +02:00
protected String run() throws Exception, WebApplicationException {
2021-08-06 18:33:34 +02:00
ConcessioniMongoManager manager=new ConcessioniMongoManager();
2021-09-09 13:35:09 +02:00
Document queryDocument=Document.parse(queryString);
QueryRequest req=new QueryRequest();
if(queryDocument.containsKey("ordering"))
req.setOrdering(Serialization.read(((Document)queryDocument.get("ordering")).toJson(),QueryRequest.OrderedRequest.class));
if(queryDocument.containsKey("paging"))
req.setPaging(Serialization.read(((Document)queryDocument.get("paging")).toJson(),QueryRequest.PagedRequest.class));
req.setProjection(queryDocument.get("projection",Document.class));
req.setFilter(queryDocument.get("filter",Document.class));
StringBuilder builder=new StringBuilder("[");
manager.query(req).forEach(d->{builder.append(d.toJson()+",");});
builder.deleteCharAt(builder.length()-1);
builder.append("]");
return builder.toString();
2021-08-06 16:17:24 +02:00
}
}.execute().getResult();
}
2021-09-09 13:35:09 +02:00
2020-12-17 18:27:45 +01:00
}