Merge branch 'master' into #22686

# Conflicts:
#	geoportal-client/CHANGELOG.md
#	geoportal-client/pom.xml
#	geoportal-client/src/test/java/org/gcube/application/geoportal/clients/BasicVreTests.java
#	geoportal-service/CHANGELOG.md
#	geoportal-service/pom.xml
#	geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/postgis/PostgisDBManager.java
#	geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/postgis/PostgisIndex.java
#	geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/providers/PostgisConnectionProvider.java
This commit is contained in:
Fabio Sinibaldi 2022-05-27 13:53:50 +02:00
commit 504311ae80
6 changed files with 203 additions and 43 deletions

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

@ -3,6 +3,7 @@ package org.gcube.application.geoportal.clients.legacy;
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.clients.BasicVreTests;
@ -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 {
assumeTrue(GCubeTest.isTestInfrastructureEnabled());

View File

@ -2,13 +2,13 @@ package org.gcube.application.geoportal.service.engine.postgis;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.gcube.application.cms.implementations.ISInterface;
import org.gcube.application.geoportal.common.model.legacy.BBOX;
import org.gcube.application.geoportal.common.model.rest.DatabaseConnection;
import org.gcube.application.cms.implementations.ImplementationProvider;
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
import org.gcube.application.geoportal.service.model.internal.db.PostgisTable;
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
import org.gcube.application.geoportal.service.model.internal.faults.DataParsingException;
import org.gcube.application.geoportal.service.utils.ISUtils;
import java.sql.*;
import java.util.regex.Matcher;
@ -17,50 +17,57 @@ import java.util.regex.Pattern;
@Slf4j
public class PostgisDBManager implements PostgisDBManagerI {
@Synchronized
public static PostgisDBManager get() throws SQLException, ConfigurationException {
if(config==null) {
log.debug("Looking for Default Configuration.. ");
// @Synchronized
// public static PostgisDBManager get() throws SQLException, ConfigurationException {
// if(config==null) {
// log.debug("Looking for Default Configuration.. ");
//
// // TODO GENERIC
// DatabaseConnection defaultConfiguration=
// ImplementationProvider.get().getIsProvider().getIS().queryForDB("postgis", "Concessioni");
//
// log.debug("Found configuration : "+defaultConfiguration);
// config=defaultConfiguration;
// }
// return new PostgisDBManager();
// }
//
//
// TODO GENERIC
DatabaseConnection defaultConfiguration=
ImplementationProvider.get().getProvidedObjectByClass(ISInterface.class)
.queryForDatabase("Database","postgis", "GNA_DB","Concessioni");
log.debug("Found configuration : "+defaultConfiguration);
config=defaultConfiguration;
public static PostgisDBManagerI get() throws SQLException, ConfigurationException {
return get(false);
}
return new PostgisDBManager();
}
public static PostgisDBManagerI get(boolean autocommit) throws SQLException, ConfigurationException {
PostgisDBManager toReturn=get();
private static PostgisDBManagerI get(boolean autocommit) throws SQLException, ConfigurationException {
DatabaseConnection config=
ImplementationProvider.get().getIsProvider().getIS().queryForDB("postgis", "Concessioni");
log.debug("Found configuration : "+config);
PostgisDBManager toReturn=new PostgisDBManager(config);
toReturn.conn.setAutoCommit(autocommit);
return toReturn;
}
private static DatabaseConnection config;
// private static DatabaseConnection config;
private static Connection getConnection() throws SQLException {
Connection toReturn= DriverManager.getConnection(config.getUrl(),config.getUser(),config.getPwd());
//TODO configure behaviour
toReturn.setAutoCommit(false);
return toReturn;
}
private Connection conn=null;
private DatabaseConnection config;
private PostgisDBManager(DatabaseConnection config) throws SQLException {
this.config=config;
log.info("Opening connection to {}",config);
conn= DriverManager.getConnection(config.getUrl(),config.getUser(),config.getPwd());
private PostgisDBManager() throws SQLException {
conn=getConnection();
//TODO configure behaviour
conn.setAutoCommit(false);
}
@Override

View File

@ -4,16 +4,16 @@ import lombok.extern.slf4j.Slf4j;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.rest.DatabaseConnection;
import org.gcube.application.geoportal.common.model.rest.PostgisIndexDescriptor;
import org.gcube.application.cms.implementations.ImplementationProvider;
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
import org.gcube.application.geoportal.service.engine.materialization.SDIManager;
import org.gcube.application.geoportal.service.model.internal.db.DBConstants;
import org.gcube.application.geoportal.service.model.internal.db.PostgisTable;
import org.gcube.application.geoportal.service.model.internal.db.PostgisTable.Field;
import org.gcube.application.geoportal.service.model.internal.db.PostgisTable.FieldType;
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
import org.gcube.application.geoportal.service.model.internal.faults.PublishException;
import org.gcube.application.geoportal.service.model.internal.faults.SDIInteractionException;
import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.geoportal.service.utils.Serialization;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@ -38,8 +38,8 @@ public class PostgisIndex {
private SDIManager sdiManager;
private String wmsLink=null;
private static PostgisDBManager getDB() throws ConfigurationException {
return ImplementationProvider.get().getProvidedObjectByClass(PostgisDBManager.class);
private static PostgisDBManagerI getDB() throws ConfigurationException {
return ImplementationProvider.get().getDbProvider().getObject();
};
public PostgisIndex() throws SDIInteractionException, SQLException, ConfigurationException {
@ -60,7 +60,7 @@ public class PostgisIndex {
public String init() throws SQLException, ConfigurationException, SDIInteractionException {
log.debug("Contacting postgis DB .. ");
PostgisDBManagerI db=getDB();
PostgisDBManagerI db=ImplementationProvider.get().getDbProvider().getObject();
log.debug("Checking if centroids table exists..");
PostgisTable table=getCentroidsTable();
db.create(table);
@ -77,7 +77,7 @@ public class PostgisIndex {
Map<String,String> centroidRow=evaluateCentroid(record);
log.debug("Contacting postgis DB .. ");
PostgisDBManagerI db=getDB();
PostgisDBManagerI db=ImplementationProvider.get().getDbProvider().getObject();
PostgisTable centroidsTable=getCentroidsTable();
log.debug("Inserting / updated centroid Row {} ",centroidRow);
@ -107,7 +107,7 @@ public class PostgisIndex {
public void removeCentroid(Concessione record) {
try {
PostgisDBManagerI db=getDB();
PostgisDBManagerI db=ImplementationProvider.get().getDbProvider().getObject();
PostgisTable centroidsTable=getCentroidsTable();
log.debug("Deleting centroid if present. ID is "+record.getMongo_id());
int result= db.deleteByFieldValue(centroidsTable, new Field(DBConstants.Concessioni.PRODUCT_ID,FieldType.TEXT), record.getMongo_id());

View File

@ -1,20 +1,23 @@
package org.gcube.application.geoportal.service.engine.providers;
import org.gcube.application.cms.caches.AbstractScopedMap;
import org.gcube.application.geoportal.service.engine.postgis.PostgisDBManager;
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
import org.gcube.application.geoportal.service.engine.postgis.PostgisDBManagerI;
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
import java.sql.SQLException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
public class PostgisConnectionProvider extends AbstractScopedMap<PostgisDBManager> {
public class PostgisConnectionProvider extends AbstractScopedMap<PostgisDBManagerI>{
public PostgisConnectionProvider() {
super("Postgis connection descriptor cache");
setTTL(Duration.of(30, ChronoUnit.SECONDS));
}
@Override
protected PostgisDBManager retrieveObject(String context) throws ConfigurationException {
protected PostgisDBManagerI retrieveObject() throws ConfigurationException {
try {
return PostgisDBManager.get();
} catch (SQLException throwables) {
@ -23,7 +26,7 @@ public class PostgisConnectionProvider extends AbstractScopedMap<PostgisDBManage
}
@Override
protected void dispose(PostgisDBManager toDispose) {
protected void dispose(PostgisDBManagerI toDispose) {
// toDispose.close();
}