diff --git a/geoportal-client/CHANGELOG.md b/geoportal-client/CHANGELOG.md index 5b1585a..0faac6e 100644 --- a/geoportal-client/CHANGELOG.md +++ b/geoportal-client/CHANGELOG.md @@ -2,6 +2,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm # Changelog for org.gcube.application.geoportal-client +## [v1.0.7] - 2022-05-25 +- Clone Concessione + ## [v1.0.6] - 2021-09-20 - Changed artifact dependencies - Default Profiled Documents client diff --git a/geoportal-client/pom.xml b/geoportal-client/pom.xml index 35d70b8..f626cf7 100644 --- a/geoportal-client/pom.xml +++ b/geoportal-client/pom.xml @@ -4,7 +4,7 @@ 4.0.0 org.gcube.application geoportal-client - 1.0.6 + 1.0.7 Geoportal Client @@ -34,6 +34,7 @@ org.gcube.application.cms geoportal-common + 1.0.8 @@ -71,6 +72,7 @@ org.gcube.application.cms cms-test-commons + 1.0.1 test diff --git a/geoportal-client/src/main/java/org/gcube/application/geoportal/client/DefaultMongoConcessioni.java b/geoportal-client/src/main/java/org/gcube/application/geoportal/client/DefaultMongoConcessioni.java index 9e8d971..bce1e87 100644 --- a/geoportal-client/src/main/java/org/gcube/application/geoportal/client/DefaultMongoConcessioni.java +++ b/geoportal-client/src/main/java/org/gcube/application/geoportal/client/DefaultMongoConcessioni.java @@ -207,6 +207,9 @@ public class DefaultMongoConcessioni implements MongoConcessioni{ } + + + protected static T check(Response resp, Class clazz) throws IOException { String resString=resp.readEntity(String.class); if(resp.getStatus()<200||resp.getStatus()>=300) diff --git a/geoportal-client/src/main/java/org/gcube/application/geoportal/client/utils/ConcessioniUtils.java b/geoportal-client/src/main/java/org/gcube/application/geoportal/client/utils/ConcessioniUtils.java new file mode 100644 index 0000000..df3eb99 --- /dev/null +++ b/geoportal-client/src/main/java/org/gcube/application/geoportal/client/utils/ConcessioniUtils.java @@ -0,0 +1,132 @@ +package org.gcube.application.geoportal.client.utils; + +import com.sun.corba.se.spi.orbutil.threadpool.Work; +import lombok.extern.slf4j.Slf4j; +import org.gcube.application.geoportal.common.model.legacy.*; +import org.gcube.application.geoportal.common.model.rest.AddSectionToConcessioneRequest; +import org.gcube.application.geoportal.common.rest.MongoConcessioni; +import org.gcube.application.geoportal.common.rest.TempFile; +import org.gcube.application.geoportal.common.utils.StorageUtils; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; + +@Slf4j +public class ConcessioniUtils { + + public static final Concessione clone(MongoConcessioni client,String toCloneId) throws Exception { + StorageUtils storage=new StorageUtils(); + log.info("Cloning {}",toCloneId); + Concessione src = client.getById(toCloneId); + // copy + Concessione copied = Serialization.read(Serialization.write(src),Concessione.class); + // cleanup + copied.setMongo_id(null); + copied.setLastUpdateTime(null); + copied.setLastUpdateUser(null); + copied.setFolderId(null); + copied.setReport(null); + copied.setCreationTime(null); + copied.setCreationUser(null); + copied.setId(0); + copied.setVersion("1.0.0"); + + copied.setRelazioneScavo(null); + copied.setAbstractRelazione(null); + copied.setPosizionamentoScavo(null); + copied.setImmaginiRappresentative(null); + copied.setPianteFineScavo(null); + + + copied = client.createNew(copied); + + + //Handle Files + + copied.setRelazioneScavo(clean(src.getRelazioneScavo())); + copied=client.replace(copied); + AddSectionToConcessioneRequest req=asCopyRequest(src.getRelazioneScavo(),storage); + if(req.getStreams()!=null&&!req.getStreams().isEmpty()) + copied=client.registerFileSet(copied.getMongo_id(),req); + + copied.setAbstractRelazione(clean(src.getAbstractRelazione())); + copied=client.replace(copied); + req=asCopyRequest(src.getAbstractRelazione(),storage); + if(req.getStreams()!=null&&!req.getStreams().isEmpty()) + copied=client.registerFileSet(copied.getMongo_id(),req); + + + copied.setPosizionamentoScavo(clean(src.getPosizionamentoScavo())); + copied=client.replace(copied); + req=asCopyRequest(src.getPosizionamentoScavo(),storage); + if(req.getStreams()!=null&&!req.getStreams().isEmpty()) + copied=client.registerFileSet(copied.getMongo_id(),req); + + if(src.getImmaginiRappresentative()!=null&&src.getImmaginiRappresentative().size()>0) { + copied.setImmaginiRappresentative(new ArrayList<>()); + for (int i = 0; i < src.getImmaginiRappresentative().size(); i++) { + copied.getImmaginiRappresentative().add(clean(src.getImmaginiRappresentative().get(i))); + copied=client.replace(copied); + req=asCopyRequest(src.getImmaginiRappresentative().get(i),storage); + req.setDestinationPath(Concessione.Paths.imgByIndex(i)); + if(req.getStreams()!=null&&!req.getStreams().isEmpty()) + copied=client.registerFileSet(copied.getMongo_id(),req); + } + } + + if(src.getPianteFineScavo()!=null&&src.getPianteFineScavo().size()>0) { + copied.setPianteFineScavo(new ArrayList<>()); + for (int i = 0; i < src.getPianteFineScavo().size(); i++) { + copied.getPianteFineScavo().add(clean(src.getPianteFineScavo().get(i))); + copied=client.replace(copied); + req=asCopyRequest(src.getPianteFineScavo().get(i),storage); + req.setDestinationPath(Concessione.Paths.imgByIndex(i)); + if(req.getStreams()!=null&&!req.getStreams().isEmpty()) + copied=client.registerFileSet(copied.getMongo_id(),req); + } + } + + log.info("Cloned {} into {} ",src.getMongo_id(),copied.getMongo_id()); + return copied; + } + + private static AddSectionToConcessioneRequest asCopyRequest(T content, StorageUtils storage) throws IOException { + AddSectionToConcessioneRequest request = new AddSectionToConcessioneRequest(); + request.setStreams(new ArrayList<>()); + if(content.getActualContent()!=null) { + for (PersistedContent persistedContent : content.getActualContent()) { + if (persistedContent instanceof WorkspaceContent) { + WorkspaceContent wc = (WorkspaceContent) persistedContent; + request.getStreams().add(storage.putOntoStorage(new URL(wc.getLink()).openStream(), wc.getName())); + } + } + } + + String path = null; + if(content instanceof LayerConcessione) path = Concessione.Paths.POSIZIONAMENTO; + else if (content instanceof RelazioneScavo) path = Concessione.Paths.RELAZIONE; + else if (content instanceof AbstractRelazione) path = Concessione.Paths.ABSTRACT_RELAZIONE; + request.setDestinationPath(path); + return request; + } + + private static T clean(T content) throws IOException { + T toReturn = (T) Serialization.read(Serialization.write(content),content.getClass()); + toReturn.setMongo_id(null); + toReturn.setCreationTime(null); + toReturn.setId(0); + toReturn.setActualContent(new ArrayList<>()); + + if(content instanceof LayerConcessione) { + ((LayerConcessione)toReturn).setLayerName(null); + ((LayerConcessione)toReturn).setLayerID(null); + ((LayerConcessione)toReturn).setBbox(null); + ((LayerConcessione)toReturn).setLayerUUID(null); + ((LayerConcessione)toReturn).setWorkspace(null); + ((LayerConcessione)toReturn).setWmsLink(null); + } + return toReturn; + } +} diff --git a/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/BasicVreTests.java b/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/BasicVreTests.java index 4016eb1..a780e2d 100644 --- a/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/BasicVreTests.java +++ b/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/BasicVreTests.java @@ -7,9 +7,9 @@ public class BasicVreTests { @BeforeClass 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("/gcube/devsec/devVRE"); +// TokenSetter.set("/gcube/devsec/devVRE"); } diff --git a/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/StatelessClientTests.java b/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/StatelessClientTests.java index c9f953b..578c8e3 100644 --- a/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/StatelessClientTests.java +++ b/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/StatelessClientTests.java @@ -15,6 +15,7 @@ import org.bson.Document; 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.TestConcessioniQueries; +import org.gcube.application.geoportal.client.utils.ConcessioniUtils; import org.gcube.application.geoportal.client.utils.Queries; import org.gcube.application.geoportal.client.utils.Serialization; import org.gcube.application.geoportal.common.model.legacy.*; @@ -105,6 +106,20 @@ public class StatelessClientTests extends BasicVreTests{ assertNotNull(c.getMongo_id()); } + + + @Test + public void testClone() throws Exception { + Concessione c= prepare(); + Concessione copied = ConcessioniUtils.clone(client,c.getMongo_id()); + assertEquals(copied.getNome(),c.getNome()); + assertEquals(copied.getPianteFineScavo().size(),c.getPianteFineScavo().size()); + } + + + + + @Test public void testReplace() throws Exception { Concessione testObject= client.createNew(TestConcessioniModel.prepareEmptyConcessione());