Merge pull request 'patch_#23400' (#5) from patch_#23400 into master

Reviewed-on: #5
This commit is contained in:
Fabio Sinibaldi 2022-05-26 15:31:03 +02:00
commit b12c4b456f
6 changed files with 158 additions and 3 deletions

View File

@ -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

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.gcube.application</groupId>
<artifactId>geoportal-client</artifactId>
<version>1.0.6</version>
<version>1.0.7</version>
<name>Geoportal Client</name>
@ -34,6 +34,7 @@
<dependency>
<groupId>org.gcube.application.cms</groupId>
<artifactId>geoportal-common</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
@ -71,6 +72,7 @@
<dependency>
<groupId>org.gcube.application.cms</groupId>
<artifactId>cms-test-commons</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>

View File

@ -207,6 +207,9 @@ public class DefaultMongoConcessioni implements MongoConcessioni{
}
protected static<T> T check(Response resp, Class<T> clazz) throws IOException {
String resString=resp.readEntity(String.class);
if(resp.getStatus()<200||resp.getStatus()>=300)

View File

@ -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 <T extends AssociatedContent> 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 extends AssociatedContent> 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;
}
}

View File

@ -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");
}

View File

@ -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());