ProfiledDocument Client

This commit is contained in:
Fabio Sinibaldi 2022-02-02 18:34:32 +01:00
parent e2613026dc
commit eadc991b64
14 changed files with 514 additions and 93 deletions

View File

@ -0,0 +1,256 @@
package org.gcube.application.geoportal.client;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.gcube.application.geoportal.client.utils.Serialization;
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.model.rest.RegisterFileSetRequest;
import org.gcube.application.geoportal.common.model.rest.StepExecutionRequest;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.common.rest.ProfiledDocumentsI;
import org.gcube.common.clients.Call;
import org.gcube.common.clients.delegates.ProxyDelegate;
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.io.IOException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Iterator;
@RequiredArgsConstructor
@Slf4j
public abstract class AbstractProfiledDocumentClient<T extends ProfiledDocument> implements ProfiledDocumentsI<T> {
@NonNull
protected final ProxyDelegate<WebTarget> delegate;
@NonNull
protected final String profileID;
protected abstract Class<T> getManagedClass();
@Override
public T createNew(Document toCreate) throws RemoteException {
try {
log.debug("Creating Profiled Document (class {}, profile {}) with content {} ",
getManagedClass(),profileID, toCreate);
Call<WebTarget, T> call = endpoint -> {
return check(endpoint.path(profileID).request(MediaType.APPLICATION_JSON).
post(Entity.entity(toCreate, MediaType.APPLICATION_JSON)),getManagedClass());
};
T toReturn = delegate.make(call);
log.info("Registered {} profiled {} ",toReturn.get_id(),profileID);
return toReturn;
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public void deleteById(String id) throws RemoteException {
deleteById(id,false);
}
@Override
public void deleteById(String id, Boolean force) throws RemoteException {
try {
log.debug("Deleting ID {}  profile {}  force {} ", id, profileID, force);
Call<WebTarget, T> call = endpoint -> {
return check(endpoint.path(profileID).path(id).
queryParam(InterfaceConstants.Parameters.FORCE,force).
request(MediaType.APPLICATION_JSON).delete(),getManagedClass());
};
delegate.make(call);
log.info("Deleted ID {}  profile {}  force {} ", id, profileID, force);
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public T getById(String id) throws RemoteException {
try {
log.info("Loading Document ID {} (class {}, profile {})",
id, getManagedClass(),profileID);
Call<WebTarget, T> call = endpoint -> {
return check(endpoint.path(profileID).path(id).
request(MediaType.APPLICATION_JSON).get(), getManagedClass());
};
return delegate.make(call);
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public Configuration getConfiguration() throws RemoteException {
try {
log.info("Loading Configuration for profile {}", profileID);
Call<WebTarget, Configuration> call = endpoint -> {
return check(endpoint.path(profileID).path(InterfaceConstants.Methods.CONFIGURATION_PATH).
request(MediaType.APPLICATION_JSON).get(), Configuration.class);
};
return delegate.make(call);
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public Iterator<T> query(QueryRequest request) throws RemoteException {
return queryForClass(request,getManagedClass());
}
@Override
public <C> Iterator<C> queryForClass(QueryRequest request,Class<C> clazz) throws RemoteException {
String jsonString=queryForJSON(request);
log.debug("Deserializing query Result as {} ",clazz);
try {
return Serialization.readCollection(jsonString,clazz);
} catch (IOException e) {
log.error("Unable to deserialize result as "+clazz,e);
log.debug("Query request was ",request);
throw new RemoteException("Invalid format for submitted query");
}
}
@Override
public String queryForJSON(QueryRequest request) throws RemoteException {
try {
log.debug("Querying profile {}  for {}",profileID,request);
Call<WebTarget, String> call = endpoint -> {
return check(endpoint.path(profileID).path(InterfaceConstants.Methods.CONFIGURATION_PATH).
request(MediaType.APPLICATION_JSON).get(), String.class);
};
return delegate.make(call);
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public T performStep(String id, StepExecutionRequest request) throws RemoteException{
try {
log.debug("Executing step on {} (class {}, profile {}) with request {} ",
id, getManagedClass(),profileID, request);
Call<WebTarget, T> call = endpoint -> {
return check(endpoint.path(profileID).path(id).request(MediaType.APPLICATION_JSON).
post(Entity.entity(request, MediaType.APPLICATION_JSON)),getManagedClass());
};
T toReturn = delegate.make(call);
log.info("Executed STEP {} on {} [profile {}, class {}] ",request.getStepID(),
id,profileID,getManagedClass());
return toReturn;
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public T registerFileSet(String id, RegisterFileSetRequest req) throws RemoteException {
try {
log.debug("Registering FileSet on {} (class {}, profile {}) with request {} ",
id, getManagedClass(),profileID, req);
Call<WebTarget, T> call = endpoint -> {
return check(endpoint.path(profileID).path(InterfaceConstants.Methods.REGISTER_FILES_PATH)
.path(id).request(MediaType.APPLICATION_JSON).
post(Entity.entity(req, MediaType.APPLICATION_JSON)),getManagedClass());
};
T toReturn = delegate.make(call);
log.info("Registered FileSet on {} [profile {}, class {}]  with {}",
id,profileID,getManagedClass(),req);
return toReturn;
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public T deleteFileSet(String id, String path, Boolean force) throws RemoteException {
try {
log.debug("Deleting Fileset for ID {}  [profile {}  , class {}] at {} (force {} )",
id, profileID,getManagedClass(),path, force);
Call<WebTarget, T> call = endpoint -> {
return check(endpoint.path(profileID).path(InterfaceConstants.Methods.DELETE_FILES_PATH).
path(id).queryParam(InterfaceConstants.Parameters.FORCE,force).
request(MediaType.APPLICATION_JSON).
post(Entity.entity(path, MediaType.APPLICATION_JSON)),getManagedClass());
};
T toReturn=delegate.make(call);
log.info("Deleted ID {}  profile {}  force {} ", id, profileID, force);
return toReturn;
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
@Override
public T updateDocument(String id, Document updatedDocument) throws RemoteException {
try {
log.debug("Updateing {}  [profile {}  , class {}] with ",
id, profileID,getManagedClass(),updatedDocument);
Call<WebTarget, T> call = endpoint -> {
return check(endpoint.path(profileID).path(id).
request(MediaType.APPLICATION_JSON).
put(Entity.entity(updatedDocument, MediaType.APPLICATION_JSON)),getManagedClass());
};
T toReturn=delegate.make(call);
log.info("Updated ID {}  profile {}  ", id, profileID);
return toReturn;
}catch(RemoteException e){
log.error("Unexpected error ",e);
throw e;
}catch(Exception e){
log.error("Unexpected error ",e);
throw new RemoteException("Unexpected Error", e);
}
}
protected static<R> R check(Response resp, Class<R> clazz) throws IOException {
String resString=resp.readEntity(String.class);
if(resp.getStatus()<200||resp.getStatus()>=300)
throw new RemoteException("RESP STATUS IS "+resp.getStatus()+". Message : "+resString);
if(clazz!=null)
return Serialization.read(resString, clazz);
else return null;
}
}

View File

@ -3,60 +3,34 @@ package org.gcube.application.geoportal.client;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document; import org.bson.Document;
import org.gcube.application.geoportal.common.model.document.ProfiledDocument; 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.Configuration;
import org.gcube.application.geoportal.common.model.rest.QueryRequest; import org.gcube.application.geoportal.common.model.rest.QueryRequest;
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
import org.gcube.application.geoportal.common.rest.ProfiledDocumentsI; import org.gcube.application.geoportal.common.rest.ProfiledDocumentsI;
import org.gcube.common.clients.Call;
import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.clients.delegates.ProxyDelegate;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget; import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.Iterator; import java.util.Iterator;
@RequiredArgsConstructor @Slf4j
public class DefaultProfiledDocuments implements ProfiledDocumentsI<ProfiledDocument> { public class DefaultProfiledDocuments extends AbstractProfiledDocumentClient<ProfiledDocument> {
@NonNull
private final ProxyDelegate<WebTarget> delegate;
@Override public DefaultProfiledDocuments(@NonNull ProxyDelegate<WebTarget> delegate, @NonNull String profileID) {
public ProfiledDocument createNew(Document toCreate) throws RemoteException { super(delegate, profileID);
return null;
} }
@Override @Override
public void deleteById(String id) throws RemoteException { protected Class<ProfiledDocument> getManagedClass() {
return ProfiledDocument.class;
}
@Override
public void deleteById(String id, Boolean force) throws RemoteException {
}
@Override
public ProfiledDocument getById(String id) throws RemoteException {
return null;
}
@Override
public Configuration getConfiguration() throws RemoteException {
return null;
}
@Override
public Iterator<ProfiledDocument> query(QueryRequest request) throws RemoteException {
return null;
}
@Override
public String querForJSON(QueryRequest request) throws RemoteException {
return null;
}
@Override
public ProfiledDocument performStep(String id, String step, Document request) {
return null;
} }
} }

View File

@ -1,8 +0,0 @@
package org.gcube.application.geoportal.client;
public class GeoPortalClient {
}

View File

@ -10,6 +10,7 @@ import org.gcube.application.geoportal.client.legacy.StatefulMongoConcessioniPlu
import org.gcube.application.geoportal.common.rest.ConcessioniI; import org.gcube.application.geoportal.common.rest.ConcessioniI;
import org.gcube.application.geoportal.common.rest.InterfaceConstants; import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.common.rest.MongoConcessioni; import org.gcube.application.geoportal.common.rest.MongoConcessioni;
import org.gcube.application.geoportal.common.rest.ProfiledDocumentsI;
import org.gcube.common.clients.Plugin; import org.gcube.common.clients.Plugin;
import org.gcube.common.clients.ProxyBuilder; import org.gcube.common.clients.ProxyBuilder;
import org.gcube.common.clients.ProxyBuilderImpl; import org.gcube.common.clients.ProxyBuilderImpl;
@ -28,6 +29,19 @@ public abstract class GeoportalAbstractPlugin <S, P> implements Plugin<S, P>{
private static StatefulMongoConcessioniPlugin stateful_mongo_concessioni_plugin=new StatefulMongoConcessioniPlugin(); private static StatefulMongoConcessioniPlugin stateful_mongo_concessioni_plugin=new StatefulMongoConcessioniPlugin();
public static ProxyBuilder<ProfiledDocumentsI> profiledDocuments(String profileID) {
ProjectsPlugin plugin=new ProjectsPlugin(profileID);
return new ProxyBuilderImpl<WebTarget, ProfiledDocumentsI>(plugin);
}
public static ProxyBuilder<ProfiledDocumentsI> profiledDocuments(String profileID,
Class<? extends AbstractProfiledDocumentClient<?>> customClient) {
ProjectsPlugin plugin=new ProjectsPlugin(profileID);
plugin.setCustomClientImplementation(customClient);
return new ProxyBuilderImpl<WebTarget, ProfiledDocumentsI>(plugin);
}
public static ProxyBuilder<ConcessioniI> concessioni() { public static ProxyBuilder<ConcessioniI> concessioni() {
return new ProxyBuilderImpl<WebTarget,ConcessioniI>(concessioni_plugin); return new ProxyBuilderImpl<WebTarget,ConcessioniI>(concessioni_plugin);
} }

View File

@ -5,6 +5,11 @@ import javax.xml.namespace.QName;
import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMResult;
import javax.xml.ws.EndpointReference; import javax.xml.ws.EndpointReference;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.gcube.application.geoportal.common.rest.InterfaceConstants; import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.common.rest.ProfiledDocumentsI; import org.gcube.application.geoportal.common.rest.ProfiledDocumentsI;
import org.gcube.common.calls.jaxrs.GcubeService; import org.gcube.common.calls.jaxrs.GcubeService;
@ -13,17 +18,29 @@ import org.gcube.common.clients.config.ProxyConfig;
import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.clients.delegates.ProxyDelegate;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import java.lang.reflect.InvocationTargetException;
@Slf4j
@RequiredArgsConstructor
public class ProjectsPlugin extends GeoportalAbstractPlugin<WebTarget, ProfiledDocumentsI>{ public class ProjectsPlugin extends GeoportalAbstractPlugin<WebTarget, ProfiledDocumentsI>{
@NonNull
private String profileID;
@Setter
private Class<? extends AbstractProfiledDocumentClient<?>>
customClientImplementation=DefaultProfiledDocuments.class;
@Override @Override
public Exception convert(Exception fault, ProxyConfig<?, ?> config) { public Exception convert(Exception fault, ProxyConfig<?, ?> config) {
return fault; return fault;
} }
@SneakyThrows
@Override @Override
public ProfiledDocumentsI newProxy(ProxyDelegate<WebTarget> delegate) { public ProfiledDocumentsI newProxy(ProxyDelegate<WebTarget> delegate) {
return new DefaultProfiledDocuments(delegate); return customClientImplementation.getConstructor(ProxyDelegate.class,String.class).
newInstance(delegate,profileID);
} }
@Override @Override

View File

@ -3,10 +3,13 @@ package org.gcube.application.geoportal.client.legacy;
import java.util.ArrayList; import java.util.ArrayList;
import javax.ws.rs.client.WebTarget; import javax.ws.rs.client.WebTarget;
import javax.xml.ws.soap.Addressing;
import lombok.Getter; import lombok.Getter;
import org.gcube.application.geoportal.client.DefaultMongoConcessioni; import org.gcube.application.geoportal.client.DefaultMongoConcessioni;
import org.gcube.application.geoportal.client.utils.Serialization;
import org.gcube.application.geoportal.common.model.legacy.*; import org.gcube.application.geoportal.common.model.legacy.*;
import org.gcube.application.geoportal.common.model.rest.AddSectionToConcessioneRequest;
import org.gcube.application.geoportal.common.utils.FileSets; import org.gcube.application.geoportal.common.utils.FileSets;
import org.gcube.application.geoportal.common.model.legacy.Concessione.Paths; import org.gcube.application.geoportal.common.model.legacy.Concessione.Paths;
import org.gcube.application.geoportal.common.model.rest.TempFile; import org.gcube.application.geoportal.common.model.rest.TempFile;
@ -64,7 +67,9 @@ public class StatefulMongoConcessioni extends DefaultMongoConcessioni implements
current =super.registerFileSet(current.getMongo_id(), current =super.registerFileSet(current.getMongo_id(),
FileSets.build(Paths.imgByIndex(current.getImmaginiRappresentative().size()-1),f).getTheRequest()); Serialization.convert(
FileSets.build(Paths.imgByIndex(current.getImmaginiRappresentative().size()-1),f).getTheRequest(),
AddSectionToConcessioneRequest.class));
return current; return current;
} }
@ -77,7 +82,8 @@ public class StatefulMongoConcessioni extends DefaultMongoConcessioni implements
current =super.registerFileSet(current.getMongo_id(), current =super.registerFileSet(current.getMongo_id(),
FileSets.build(Paths.piantaByIndex(current.getPianteFineScavo().size()-1),files).getTheRequest()); Serialization.convert(
FileSets.build(Paths.piantaByIndex(current.getPianteFineScavo().size()-1),files).getTheRequest(),AddSectionToConcessioneRequest.class));
return current; return current;
} }
@ -89,7 +95,7 @@ public class StatefulMongoConcessioni extends DefaultMongoConcessioni implements
current =super.registerFileSet(current.getMongo_id(), current =super.registerFileSet(current.getMongo_id(),
FileSets.build(Paths.POSIZIONAMENTO,files).getTheRequest()); Serialization.convert(FileSets.build(Paths.POSIZIONAMENTO,files).getTheRequest(),AddSectionToConcessioneRequest.class));
return current; return current;
} }
@ -101,7 +107,7 @@ public class StatefulMongoConcessioni extends DefaultMongoConcessioni implements
current =super.registerFileSet(current.getMongo_id(), current =super.registerFileSet(current.getMongo_id(),
FileSets.build(Paths.RELAZIONE,files).getTheRequest()); Serialization.convert(FileSets.build(Paths.RELAZIONE,files).getTheRequest(),AddSectionToConcessioneRequest.class));
return current; return current;
} }
@ -112,7 +118,7 @@ public class StatefulMongoConcessioni extends DefaultMongoConcessioni implements
current =replace(current); current =replace(current);
current =super.registerFileSet(current.getMongo_id(), current =super.registerFileSet(current.getMongo_id(),
FileSets.build(Paths.ABSTRACT_RELAZIONE,files).getTheRequest()); Serialization.convert(FileSets.build(Paths.ABSTRACT_RELAZIONE,files).getTheRequest(),AddSectionToConcessioneRequest.class));
return current; return current;
} }

View File

@ -5,37 +5,126 @@ import java.io.InputStream;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Iterator; import java.util.Iterator;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.vdurmont.semver4j.Semver;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.gcube.application.geoportal.common.model.document.ComparableVersion;
import org.gcube.application.geoportal.common.model.document.ProfiledDocument;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
public class Serialization { public class Serialization {
public static final DateTimeFormatter FULL_FORMATTER=DateTimeFormatter.ofPattern("uuuuMMdd_HH-mm-ss"); public static final DateTimeFormatter FULL_FORMATTER=DateTimeFormatter.ofPattern("uuuuMMdd_HH-mm-ss");
public static ObjectMapper mapper; public static ObjectMapper mapper;
static { static {
mapper=new ObjectMapper(); mapper=new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
mapper.registerModule(new JavaTimeModule()); mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
} mapper.registerModule(new JavaTimeModule());
public static <T> T read(String jsonString,Class<T> clazz) throws JsonProcessingException, IOException { SimpleModule s=new SimpleModule();
return mapper.readerFor(clazz).readValue(jsonString); s.addDeserializer(ObjectId.class,new ObjectIdDeserializer());
} s.addSerializer(ObjectId.class,new ObjectIdSerializer());
s.addDeserializer(Semver.class,new SemverDeserializer());
s.addSerializer(Semver.class,new SemverSerializer());
public static String write(Object obj) throws JsonProcessingException, IOException { mapper.registerModule(s);
return mapper.writeValueAsString(obj); }
}
public static <T> T read(String jsonString,Class<T> clazz) throws JsonProcessingException, IOException {
return mapper.readerFor(clazz).readValue(jsonString);
}
public static <T> Iterator<T> readCollection(String jsonString, Class<T> clazz) throws IOException {
return mapper.readerFor(clazz).readValues(jsonString);
}
public static <T> Iterator<T> readCollection(String jsonString, Class<T> clazz) throws IOException {
return mapper.readerFor(clazz).readValues(jsonString);
}
public static <T> Iterator<T> readCollection(InputStream is, Class<T> clazz) throws IOException { public static <T> Iterator<T> readCollection(InputStream is, Class<T> clazz) throws IOException {
return mapper.readerFor(clazz).readValues(is); return mapper.readerFor(clazz).readValues(is);
} }
public static String write(Object toWrite) throws JsonProcessingException {
String toReturn= mapper.writeValueAsString(toWrite);
return toReturn;
}
//**** PROFILED DOCUMENTS
public static final <T> T convert(Object d,Class<T> clazz){
return mapper.convertValue(d,clazz);
}
public static final Document asDocument(Object obj) throws JsonProcessingException {
return Document.parse(mapper.writeValueAsString(obj));
}
// ***** Serialization Exceptions
// OBJECT ID
private static class ObjectIdSerializer extends JsonSerializer<ObjectId> {
@Override
public void serialize(ObjectId objectId, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
if (objectId == null) jsonGenerator.writeNull();
else jsonGenerator.writeString(objectId.toString());
}
@Override
public Class<ObjectId> handledType() {
return ObjectId.class;
}
}
private static class ObjectIdDeserializer extends JsonDeserializer<ObjectId> {
@Override
public ObjectId deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
String value=jsonParser.getValueAsString();
if(value==null || value.isEmpty() || value.equals("null"))
return null;
else return new ObjectId(value);
}
@Override
public Class<ObjectId> handledType() {
return ObjectId.class;
}
}
//Sem Version
private static class SemverSerializer extends JsonSerializer<Semver> {
@Override
public void serialize(Semver semver, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
if (semver == null) jsonGenerator.writeNull();
else jsonGenerator.writeString(semver.getValue());
}
@Override
public Class<Semver> handledType() {
return Semver.class;
}
}
private static class SemverDeserializer extends JsonDeserializer<Semver> {
@Override
public Semver deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
String value=jsonParser.getValueAsString();
if(value==null || value.isEmpty() || value.equals("null"))
return null;
else return new Semver(value);
}
@Override
public Class<Semver> handledType() {
return Semver.class;
}
}
} }

View File

@ -7,9 +7,9 @@ public class BasicVreTests {
@BeforeClass @BeforeClass
public static void setScope(){ public static void setScope(){
TokenSetter.set("/pred4s/preprod/preVRE"); // TokenSetter.set("/pred4s/preprod/preVRE");
// TokenSetter.set("/d4science.research-infrastructures.eu/D4OS/GeoNA-Prototype"); // TokenSetter.set("/d4science.research-infrastructures.eu/D4OS/GeoNA-Prototype");
// TokenSetter.set("/gcube/devsec/devVRE"); TokenSetter.set("/gcube/devsec/devVRE");
} }

View File

@ -0,0 +1,23 @@
package org.gcube.application.geoportal.clients;
import org.gcube.application.geoportal.common.model.document.ProfiledDocument;
import org.gcube.application.geoportal.common.rest.MongoConcessioni;
import org.gcube.application.geoportal.common.rest.ProfiledDocumentsI;
import org.junit.Test;
import static org.gcube.application.geoportal.client.GeoportalAbstractPlugin.mongoConcessioni;
import static org.gcube.application.geoportal.client.GeoportalAbstractPlugin.profiledDocuments;
public class ProfiledDocumentsTest extends BasicVreTests{
private ProfiledDocumentsI<ProfiledDocument> client=
profiledDocuments("profiledConcessioni").build();
@Test
public void getConfiguration() throws Exception {
System.out.println(client.getConfiguration());
}
}

View File

@ -0,0 +1,4 @@
package org.gcube.application.geoportal.clients;
public class ProfiledDocumentsTests {
}

View File

@ -11,6 +11,7 @@ import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import com.sun.xml.internal.ws.api.addressing.AddressingPropertySet;
import org.bson.Document; import org.bson.Document;
import org.gcube.application.cms.tests.model.concessioni.TestConcessioniFilters; import org.gcube.application.cms.tests.model.concessioni.TestConcessioniFilters;
import org.gcube.application.cms.tests.model.concessioni.TestConcessioniModel; import org.gcube.application.cms.tests.model.concessioni.TestConcessioniModel;
@ -118,10 +119,11 @@ public class StatelessClientTests extends BasicVreTests{
public void testUploadFileSet() throws Exception { public void testUploadFileSet() throws Exception {
Concessione testObject= client.createNew(TestConcessioniModel.prepareConcessione()); Concessione testObject= client.createNew(TestConcessioniModel.prepareConcessione());
AddSectionToConcessioneRequest request= AddSectionToConcessioneRequest request=
Serialization.convert(
// FileSets.prepareRequest(new StorageUtils(),Paths.RELAZIONE,new File(TestConcessioniModel.getBaseFolder(),"relazione.pdf")); // FileSets.prepareRequest(new StorageUtils(),Paths.RELAZIONE,new File(TestConcessioniModel.getBaseFolder(),"relazione.pdf"));
FileSets.build(Paths.ABSTRACT_RELAZIONE).add( FileSets.build(Paths.ABSTRACT_RELAZIONE).add(
new StorageUtils().putOntoStorage(new File(TestConcessioniModel.getBaseFolder(),"relazione.pdf"),"San Mauro_drone totale.JPG")) new StorageUtils().putOntoStorage(new File(TestConcessioniModel.getBaseFolder(),"relazione.pdf"),"San Mauro_drone totale.JPG"))
.getTheRequest(); .getTheRequest(),AddSectionToConcessioneRequest.class);
testObject= client.registerFileSet(testObject.getMongo_id(), request); testObject= client.registerFileSet(testObject.getMongo_id(), request);
@ -190,30 +192,37 @@ public class StatelessClientTests extends BasicVreTests{
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy()); assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
c=client.registerFileSet(mongoId, c=client.registerFileSet(mongoId,
FileSets.prepareRequest(storage,Paths.RELAZIONE,new File(TestConcessioniModel.getBaseFolder(),"relazione.pdf"))); Serialization.convert(FileSets.prepareRequest(storage,Paths.RELAZIONE,
new File(TestConcessioniModel.getBaseFolder(),"relazione.pdf")), AddSectionToConcessioneRequest.class));
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy()); assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
c=client.registerFileSet(mongoId, c=client.registerFileSet(mongoId,
FileSets.prepareRequest(storage,Paths.ABSTRACT_RELAZIONE,new File(TestConcessioniModel.getBaseFolder(),"relazione.pdf"))); Serialization.convert(
FileSets.prepareRequest(storage,Paths.ABSTRACT_RELAZIONE,new File(TestConcessioniModel.getBaseFolder(),"relazione.pdf")),AddSectionToConcessioneRequest.class));
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy()); assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
for(int i=0;i<numImgs;i++) for(int i=0;i<numImgs;i++)
c=client.registerFileSet(mongoId, c=client.registerFileSet(mongoId,
FileSets.build(Paths.imgByIndex(i)). Serialization.convert(
FileSets.build(Paths.imgByIndex(i)).
add(storage.putOntoStorage(new File(TestConcessioniModel.getBaseFolder(),"immagine"+(i+1)+".png"), add(storage.putOntoStorage(new File(TestConcessioniModel.getBaseFolder(),"immagine"+(i+1)+".png"),
i+"San Mauro_drone totale.JPG")).getTheRequest()); i+"San Mauro_drone totale.JPG")).getTheRequest(),AddSectionToConcessioneRequest.class));
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy()); assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
c=client.registerFileSet(mongoId, c=client.registerFileSet(mongoId,
FileSets.prepareRequest(storage,Paths.POSIZIONAMENTO,new File(TestConcessioniModel.getBaseFolder(),"pos.shp"))); Serialization.convert(
FileSets.prepareRequest(storage,Paths.POSIZIONAMENTO,new File(TestConcessioniModel.getBaseFolder(),
"pos.shp")),AddSectionToConcessioneRequest.class));
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy()); assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
c=client.registerFileSet(mongoId, c=client.registerFileSet(mongoId,
FileSets.prepareRequest(storage,Paths.piantaByIndex(0),new File(TestConcessioniModel.getBaseFolder(),"pianta.shp"))); Serialization.convert(
FileSets.prepareRequest(storage,Paths.piantaByIndex(0),
new File(TestConcessioniModel.getBaseFolder(),"pianta.shp")),AddSectionToConcessioneRequest.class));
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy()); assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());

View File

@ -4,6 +4,8 @@ import org.bson.Document;
import org.gcube.application.geoportal.common.model.document.ProfiledDocument; 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.Configuration;
import org.gcube.application.geoportal.common.model.rest.QueryRequest; import org.gcube.application.geoportal.common.model.rest.QueryRequest;
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
import org.gcube.application.geoportal.common.model.rest.StepExecutionRequest;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.Iterator; import java.util.Iterator;
@ -12,17 +14,26 @@ public interface ProfiledDocumentsI<P extends ProfiledDocument> {
// CRUD // CRUD
public P createNew(Document toCreate)throws RemoteException; public P createNew(Document toCreate)throws RemoteException;
public P getById(String id) throws RemoteException;
public P updateDocument(String id,Document updatedDocument) throws RemoteException;
public void deleteById(String id) throws RemoteException; public void deleteById(String id) throws RemoteException;
public void deleteById(String id,Boolean force) throws RemoteException; public void deleteById(String id,Boolean force) throws RemoteException;
public P getById(String id) throws RemoteException;
// CONFIG // CONFIG
public Configuration getConfiguration() throws RemoteException; public Configuration getConfiguration() throws RemoteException;
// QUERY // QUERY
public Iterator<P> query (QueryRequest request) throws RemoteException; public Iterator<P> query (QueryRequest request) throws RemoteException;
public String querForJSON(QueryRequest request)throws RemoteException; public <C> Iterator<C> queryForClass (QueryRequest request,Class<C> clazz) throws RemoteException;
public String queryForJSON(QueryRequest request)throws RemoteException;
//Execution //Execution
public P performStep(String id, String step, Document request); public P performStep(String id, StepExecutionRequest request) throws RemoteException;
//FileSets
public P registerFileSet(String id, RegisterFileSetRequest req) throws RemoteException;
//FileSets
public P deleteFileSet(String id, String path, Boolean force) throws RemoteException;
} }

View File

@ -182,6 +182,7 @@ public class ConcessioniMongoManager extends MongoManager{
delete(asId(id), collectionName); delete(asId(id), collectionName);
}catch(DeletionException e) { }catch(DeletionException e) {
//storing updated - partially deleted //storing updated - partially deleted
log.error("Error while trying to delete",e);
concessione=onUpdate(concessione); concessione=onUpdate(concessione);
replace(asDocument(concessione),new ObjectId(concessione.getMongo_id()), collectionName); replace(asDocument(concessione),new ObjectId(concessione.getMongo_id()), collectionName);
throw e; throw e;

View File

@ -1,5 +1,6 @@
package org.gcube.application.geoportal.service; package org.gcube.application.geoportal.service;
import org.bson.Document;
import org.gcube.application.cms.tests.TokenSetter; import org.gcube.application.cms.tests.TokenSetter;
import org.gcube.application.cms.tests.model.concessioni.TestConcessioniFilters; import org.gcube.application.cms.tests.model.concessioni.TestConcessioniFilters;
@ -16,6 +17,7 @@ import org.gcube.application.geoportal.common.utils.FileSets;
import org.gcube.application.geoportal.common.utils.Files; import org.gcube.application.geoportal.common.utils.Files;
import org.gcube.application.geoportal.common.utils.StorageUtils; import org.gcube.application.geoportal.common.utils.StorageUtils;
import org.gcube.application.cms.serialization.Serialization; import org.gcube.application.cms.serialization.Serialization;
import org.geotoolkit.referencing.operation.provider.PolarStereographic;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -26,6 +28,7 @@ import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -208,9 +211,31 @@ public class ConcessioniOverMongoTest extends BasicServiceTestUnit{
public void handlePrecise() throws Exception { public void handlePrecise() throws Exception {
//Republishing //Republishing
WebTarget target=target(PATH); WebTarget target=target(PATH);
String id="610415af02ad3d05b5f81ee3"; //String id="61e6d5212f7b172ac9874879";
publish(target,unpublish(target,id).getMongo_id());
target.path(id).queryParam(InterfaceConstants.Parameters.FORCE,true).request(MediaType.APPLICATION_JSON).delete(); // Query & delete
QueryRequest req = new QueryRequest();
// req.setFilter(Document.parse("{ \"report.objectName\" : { \"$eq\" : \"Unpublish report\" },\n" +
// " \"report.status\" : { \"$eq\" : \"PASSED\" } }"));
req.setFilter(Document.parse("{ \"creationUser\" : { \"$eq\" : \"francesco.mangiacrapa\" }}"));
Iterator<Concessione> it= Serialization.readCollection(check(target.path(InterfaceConstants.Methods.QUERY_PATH).request(MediaType.APPLICATION_JSON_TYPE).post(
Entity.entity(Serialization.write(req),MediaType.APPLICATION_JSON)),String.class),Concessione.class);
AtomicLong totCount=new AtomicLong(0);
List<String> errMessages=new ArrayList<>();
it.forEachRemaining(c->{
try {
totCount.incrementAndGet();
check(target.path(c.getMongo_id()).
queryParam(InterfaceConstants.Parameters.FORCE,false).request(MediaType.APPLICATION_JSON).delete(),String.class);
} catch (Exception e) {
errMessages.add("ERROR with "+c.getMongo_id()+" folder : "+c.getFolderId()+" author : "+c.getCreationUser()+". Message : "+e.getMessage());
}
});
System.out.println(errMessages.size()+" errors out of "+totCount.get()+" found items");
errMessages.forEach(s -> { System.err.println(s); });
//publish(target,unpublish(target,id).getMongo_id());
} }