From 7bdf45340c1fbb4f714caac3fcec2590126a0b8c Mon Sep 17 00:00:00 2001 From: Fabio Sinibaldi Date: Tue, 7 Dec 2021 11:16:26 +0100 Subject: [PATCH] Projects REST interface --- .../model/document/ProfiledDocument.java | 1 + .../common/rest/InterfaceConstants.java | 1 + .../geoportal/service/GeoPortalService.java | 4 +- .../service/engine/mongo/MongoManagerI.java | 5 +- .../engine/mongo/ProfiledMongoManager.java | 37 +++- .../model/internal/db/DBConstants.java | 3 + .../geoportal/service/rest/Profiles.java | 5 - .../geoportal/service/rest/Projects.java | 208 ++++++++---------- .../service/BasicServiceTestUnit.java | 34 --- .../geoportal/service/ProjectTests.java | 128 ++++++----- 10 files changed, 211 insertions(+), 215 deletions(-) delete mode 100644 geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/Profiles.java diff --git a/geoportal-common/src/main/java/org/gcube/application/geoportal/common/model/document/ProfiledDocument.java b/geoportal-common/src/main/java/org/gcube/application/geoportal/common/model/document/ProfiledDocument.java index 12e07aa..61d24fd 100644 --- a/geoportal-common/src/main/java/org/gcube/application/geoportal/common/model/document/ProfiledDocument.java +++ b/geoportal-common/src/main/java/org/gcube/application/geoportal/common/model/document/ProfiledDocument.java @@ -34,4 +34,5 @@ public class ProfiledDocument { private TemporalReference temporalReference; private Document theDocument; + } diff --git a/geoportal-common/src/main/java/org/gcube/application/geoportal/common/rest/InterfaceConstants.java b/geoportal-common/src/main/java/org/gcube/application/geoportal/common/rest/InterfaceConstants.java index 3ed42f4..1dac42a 100644 --- a/geoportal-common/src/main/java/org/gcube/application/geoportal/common/rest/InterfaceConstants.java +++ b/geoportal-common/src/main/java/org/gcube/application/geoportal/common/rest/InterfaceConstants.java @@ -13,6 +13,7 @@ public class InterfaceConstants { public static final String PROFILES="profiles"; public static final String SECTIONS="sections"; public static final String PROJECTS="projects"; + public static final String CONCESSIONI="concessioni"; public static final String MONGO_CONCESSIONI="mongo-concessioni"; diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/GeoPortalService.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/GeoPortalService.java index 7fb5902..2edcabe 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/GeoPortalService.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/GeoPortalService.java @@ -3,7 +3,7 @@ package org.gcube.application.geoportal.service; import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; import org.gcube.application.geoportal.common.rest.InterfaceConstants; import org.gcube.application.geoportal.service.rest.ConcessioniOverMongo; -import org.gcube.application.geoportal.service.rest.Profiles; + import org.gcube.application.geoportal.service.rest.Projects; import org.gcube.application.geoportal.service.rest.Sections; import org.gcube.application.geoportal.service.utils.Serialization; @@ -23,7 +23,7 @@ public class GeoPortalService extends ResourceConfig{ registerClasses(ConcessioniOverMongo.class); registerClasses(Projects.class); registerClasses(Sections.class); - registerClasses(Profiles.class); + diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManagerI.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManagerI.java index f5bf0de..87df456 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManagerI.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/MongoManagerI.java @@ -12,7 +12,7 @@ public interface MongoManagerI { // create - public T registerNew(T toRegister) throws IOException; + public T registerNew(Document toRegister) throws IOException; // update public T update(String id,T toSet) throws IOException; @@ -27,7 +27,8 @@ public interface MongoManagerI { // query - public Iterable query(QueryRequest request); + public Iterable query(QueryRequest request); + public Iterable filter(QueryRequest request); // materialize diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java index 2f86ea2..a9d87f6 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java @@ -13,7 +13,10 @@ import org.gcube.application.geoportal.service.model.internal.faults.Configurati import org.gcube.application.geoportal.service.model.internal.faults.DeletionException; import org.gcube.application.geoportal.service.utils.Serialization; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; import java.io.IOException; +import java.security.InvalidParameterException; import java.util.concurrent.LinkedBlockingQueue; import java.util.function.Consumer; @@ -23,7 +26,16 @@ import static org.gcube.application.geoportal.service.engine.mongo.ConcessioniMo public class ProfiledMongoManager extends MongoManager implements MongoManagerI{ - public ProfiledMongoManager() throws ConfigurationException { + String profileId; + + public ProfiledMongoManager(String profileId) throws ConfigurationException { + if(profileId==null) throw new InvalidParameterException("Profile ID cannot be null"); + //check profile existance + //ProfileManager.get(profileId); + // TODO GET FROM PROFILES + if (!profileId.equals("profiled-concessioni")) throw new WebApplicationException("Profile "+profileId+" not registered", Response.Status.NOT_FOUND); + + } private ProfiledDocument onUpdate(ProfiledDocument updatedDocument){ @@ -48,8 +60,13 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< } @Override - public ProfiledDocument registerNew(ProfiledDocument toRegister) throws IOException { - super.insert(asDocument(toRegister),getCollectionName()); + public ProfiledDocument registerNew(Document toRegisterDoc) throws IOException { + ProfiledDocument toRegister = new ProfiledDocument(); + toRegister.setTheDocument(toRegisterDoc); + + //TODO initialize ProfiledDocument values + + insert(asDocument(toRegister),getCollectionName()); log.trace("Going to register {} ",toRegister); toRegister=onUpdate(toRegister); @@ -107,7 +124,19 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< } @Override - public Iterable query(QueryRequest queryRequest) { + public Iterable query(QueryRequest queryRequest) { + log.info("Querying {} ",queryRequest); + LinkedBlockingQueue queue=new LinkedBlockingQueue(); + query(queryRequest,getCollectionName()).forEach( + (Consumer) (Document d)->{try{ + queue.put(d); + }catch(Throwable t){log.warn("Unable to translate "+d);}}); + log.info("Returned {} elements ",queue.size()); + return queue; + } + + @Override + public Iterable filter(QueryRequest queryRequest) { log.info("Searching concessione for filter {} ",queryRequest); LinkedBlockingQueue queue=new LinkedBlockingQueue(); query(queryRequest,getCollectionName()).forEach( diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/model/internal/db/DBConstants.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/model/internal/db/DBConstants.java index 17cd57a..58d243a 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/model/internal/db/DBConstants.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/model/internal/db/DBConstants.java @@ -65,6 +65,9 @@ public class DBConstants { public static final String PAROLE_CHIAVE="parole_chiave"; + // + + public static final ArrayList COLUMNS=new ArrayList(); public static final PostgisTable CENTROIDS=new PostgisTable("centroids_concessioni", diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/Profiles.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/Profiles.java deleted file mode 100644 index 5559c02..0000000 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/Profiles.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.gcube.application.geoportal.service.rest; - -public class Profiles { - -} diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/Projects.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/Projects.java index d6f2acb..3abe341 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/Projects.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/rest/Projects.java @@ -2,133 +2,117 @@ package org.gcube.application.geoportal.service.rest; import lombok.extern.slf4j.Slf4j; import org.bson.Document; -import org.gcube.application.geoportal.common.model.project.Project; +import org.gcube.application.geoportal.common.model.document.ProfiledDocument; +import org.gcube.application.geoportal.common.model.legacy.Concessione; +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.service.engine.mongo.ConcessioniMongoManager; +import org.gcube.application.geoportal.service.engine.mongo.ProfiledMongoManager; +import org.gcube.application.geoportal.service.engine.postgis.PostgisIndex; +import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException; +import org.gcube.application.geoportal.service.utils.Serialization; +import org.json.JSONArray; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; -import java.util.Collections; -import java.util.List; -@Path(InterfaceConstants.Methods.PROJECTS) +@Path(InterfaceConstants.Methods.PROJECTS+"/{"+InterfaceConstants.Parameters.PROFILE_ID+"}") @Slf4j public class Projects { - //***************** GENERIC PROJECTS - // GET ALL - @GET - @Produces(MediaType.APPLICATION_JSON) - public List getAll() { - return new GuardedMethod>() { - protected List run() throws Exception ,WebApplicationException { - return Collections.singletonList(new Project()); - }; - }.execute().getResult(); - } + private ProfiledMongoManager manager; - @POST - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - @Path("/search") - public List getFilteredAll(Document filter){ - return new GuardedMethod>() { - protected List run() throws Exception ,WebApplicationException { - return Collections.singletonList(new Project()); - }; - }.execute().getResult(); - } + public Projects(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String profileID) throws ConfigurationException { + log.debug("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(); + } - //***************** BY PROFILE ID + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public ProfiledDocument createNew(Document d) { + return new GuardedMethod() { + @Override + protected ProfiledDocument run() throws Exception, WebApplicationException { + return manager.registerNew(d); + } + }.execute().getResult(); + } - // Create new Project - @PUT - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - @Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}") - public Project registerNew(@PathParam(InterfaceConstants.Parameters.PROFILE_ID)String profileId, - Document toRegister) { - return new GuardedMethod() { - @Override - protected Project run() throws Exception, WebApplicationException { - return new Project(); - } - }.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(); + } - // GET ALL (Filters apply) - @POST - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - @Path("/search/{"+InterfaceConstants.Parameters.PROFILE_ID+"}") - public List getFilteredAllInProfiles(@PathParam(InterfaceConstants.Parameters.PROFILE_ID)String profileId, - Document filters) { - return new GuardedMethod>() { - protected List run() throws Exception ,WebApplicationException { - return Collections.singletonList(new Project()); - }; - }.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(); + } - // GET ALL - @GET - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - @Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}") - public List getAllinProfile(@PathParam(InterfaceConstants.Parameters.PROFILE_ID)String profileId) { - return new GuardedMethod>() { - protected List run() throws Exception ,WebApplicationException { - return Collections.singletonList(new Project()); - }; - }.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(); + } - //***************** BY PROFILE ID + PROJECT ID - // GET BY ID - @GET - @Produces(MediaType.APPLICATION_JSON) - @Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}") - public Project getByID(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String profile, - @PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) { - Project toReturn=new GuardedMethod() { - @Override - protected Project run() throws Exception ,WebApplicationException{ - return new Project(); - } - }.execute().getResult(); - return toReturn; - } - - - // DELETE BY ID - @DELETE - @Produces(MediaType.APPLICATION_JSON) - @Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}") - public void delete(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String profile, - @PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) { - new GuardedMethod() { - @Override - protected Project run() throws Exception ,WebApplicationException{ - // TODO DELETE - return null; - } - }.execute().getResult(); - } - - @PUT - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - @Path("{"+InterfaceConstants.Parameters.PROFILE_ID+"}/{"+InterfaceConstants.Parameters.PROJECT_ID+"}") - public Project updateDocument(@PathParam(InterfaceConstants.Parameters.PROFILE_ID) String profile, - @PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,Document toSetDocument) { - Project toReturn=new GuardedMethod() { - @Override - protected Project run() throws Exception ,WebApplicationException{ - return new Project(); - } - }.execute().getResult(); - return toReturn; - } } diff --git a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/BasicServiceTestUnit.java b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/BasicServiceTestUnit.java index 3616a78..02990ae 100644 --- a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/BasicServiceTestUnit.java +++ b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/BasicServiceTestUnit.java @@ -33,40 +33,6 @@ public class BasicServiceTestUnit extends JerseyTest { } }); - -/* - ImplementationProvider.get().setStorageProvider(new StorageClientProvider() { - @Override - public IClient getObject() throws ConfigurationException { - TokenSetter.set(scope); - return super.getObject(); - } - }); - - - ImplementationProvider.get().setMongoConnectionProvider(new MongoConnectionProvider() { - @Override - public org.gcube.application.geoportal.service.model.internal.db.MongoConnection getObject() throws ConfigurationException { - TokenSetter.set(scope); - return super.getObject(); - } - }); - - ImplementationProvider.get().setMongoClientProvider(new MongoClientProvider() { - @Override - public MongoClient getObject() throws ConfigurationException { - TokenSetter.set(scope); - return super.getObject(); - } - }); - - ImplementationProvider.get().setDbProvider(new PostgisConnectionProvider() { - @Override - public PostgisDBManager getObject() throws ConfigurationException { - TokenSetter.set(scope); - return super.getObject(); - } - });*/ } diff --git a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/ProjectTests.java b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/ProjectTests.java index cbbdb20..80447b0 100644 --- a/geoportal-service/src/test/java/org/gcube/application/geoportal/service/ProjectTests.java +++ b/geoportal-service/src/test/java/org/gcube/application/geoportal/service/ProjectTests.java @@ -1,75 +1,91 @@ package org.gcube.application.geoportal.service; import org.bson.Document; +import org.gcube.application.cms.tests.TokenSetter; import org.gcube.application.geoportal.common.rest.InterfaceConstants; +import org.junit.Before; import org.junit.Test; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import java.util.Collections; import java.util.List; +import static org.junit.Assert.assertEquals; + public class ProjectTests extends BasicServiceTestUnit{ - String testProfileId="profiled_concessione"; - String projectId="asdlkjgdasfjklgadjhkl"; - + String testProfileId="profiled-concessioni"; + + @Before + public void setContext(){ + TokenSetter.set(scope); + } + + @Test + public void testMissingProfile(){ + Response resp = target(InterfaceConstants.Methods.PROJECTS) + .path("non-existent-profile").request().get(); + assertEquals(resp.getStatus(),404); + } + + @Test public void getAll() { - WebTarget target=target(InterfaceConstants.Methods.PROJECTS); - System.out.println(target.request(MediaType.APPLICATION_JSON).get(List.class)); + System.out.println(target(InterfaceConstants.Methods.PROJECTS).path(testProfileId).request(MediaType.APPLICATION_JSON).get(List.class)); } - @Test - public void getFilteredAll() { - WebTarget target=target(InterfaceConstants.Methods.PROJECTS); - Document document =new Document(Collections.singletonMap("key", "value")); - - System.out.println(target.path("search").request(MediaType.APPLICATION_JSON). - post(Entity.entity(document, MediaType.APPLICATION_JSON))); - - } - - @Test - public void getAllByProfile() { - WebTarget target=target(InterfaceConstants.Methods.PROJECTS); - System.out.println(target.path(testProfileId).request(MediaType.APPLICATION_JSON).get(List.class)); - } - - @Test - public void getFilteredByProfile() { - WebTarget target=target(InterfaceConstants.Methods.PROJECTS); - Document document =new Document(Collections.singletonMap("key", "value")); - - System.out.println(target.path("search").path(testProfileId).request(MediaType.APPLICATION_JSON). - post(Entity.entity(document, MediaType.APPLICATION_JSON))); - - } - - - @Test - public void getById() { - WebTarget target=target(InterfaceConstants.Methods.PROJECTS); - System.out.println(target.path(testProfileId).path(projectId).request(MediaType.APPLICATION_JSON).get().readEntity(String.class)); - } - - - @Test - public void registerNew() { - WebTarget target=target(InterfaceConstants.Methods.PROJECTS); - Document document =new Document(Collections.singletonMap("key", "value")); - - System.out.println(target.path(testProfileId).request(MediaType.APPLICATION_JSON). - put(Entity.entity(document, MediaType.APPLICATION_JSON))); - } - - @Test - public void updateDocument() { - WebTarget target=target(InterfaceConstants.Methods.PROJECTS); - Document document =new Document(Collections.singletonMap("key", "value")); - - System.out.println(target.path(testProfileId).path(projectId).request(MediaType.APPLICATION_JSON). - put(Entity.entity(document, MediaType.APPLICATION_JSON))); - } +// @Test +// public void getFilteredAll() { +// WebTarget target=target(InterfaceConstants.Methods.PROJECTS); +// Document document =new Document(Collections.singletonMap("key", "value")); +// +// System.out.println(target.path("search").request(MediaType.APPLICATION_JSON). +// post(Entity.entity(document, MediaType.APPLICATION_JSON))); +// +// } +// +// @Test +// public void getAllByProfile() { +// WebTarget target=target(InterfaceConstants.Methods.PROJECTS); +// System.out.println(target.path(testProfileId).request(MediaType.APPLICATION_JSON).get(List.class)); +// } +// +// @Test +// public void getFilteredByProfile() { +// WebTarget target=target(InterfaceConstants.Methods.PROJECTS); +// Document document =new Document(Collections.singletonMap("key", "value")); +// +// System.out.println(target.path("search").path(testProfileId).request(MediaType.APPLICATION_JSON). +// post(Entity.entity(document, MediaType.APPLICATION_JSON))); +// +// } +// +// +// @Test +// public void getById() { +// WebTarget target=target(InterfaceConstants.Methods.PROJECTS); +// System.out.println(target.path(testProfileId).path(projectId).request(MediaType.APPLICATION_JSON).get().readEntity(String.class)); +// } +// +// +// @Test +// public void registerNew() { +// WebTarget target=target(InterfaceConstants.Methods.PROJECTS); +// Document document =new Document(Collections.singletonMap("key", "value")); +// +// System.out.println(target.path(testProfileId).request(MediaType.APPLICATION_JSON). +// put(Entity.entity(document, MediaType.APPLICATION_JSON))); +// } +// +// @Test +// public void updateDocument() { +// WebTarget target=target(InterfaceConstants.Methods.PROJECTS); +// Document document =new Document(Collections.singletonMap("key", "value")); +// +// System.out.println(target.path(testProfileId).path(projectId).request(MediaType.APPLICATION_JSON). +// put(Entity.entity(document, MediaType.APPLICATION_JSON))); +// } }