gcube-cms-suite/sdi-plugins/src/main/java/org/gcube/application/cms/sdi/engine/PostgisIndexer.java

130 lines
4.6 KiB
Java
Raw Normal View History

2022-02-18 18:11:12 +01:00
package org.gcube.application.cms.sdi.engine;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
2022-03-11 18:11:32 +01:00
import org.gcube.application.cms.plugins.requests.BaseRequest;
2022-02-18 18:11:12 +01:00
import org.gcube.application.cms.sdi.faults.SDIInteractionException;
2022-03-17 17:54:00 +01:00
import org.gcube.application.cms.sdi.model.CrossReferencedLayer;
2022-03-11 18:11:32 +01:00
import org.gcube.application.geoportal.common.model.configuration.Index;
2022-03-17 17:54:00 +01:00
import org.gcube.application.geoportal.common.model.document.filesets.GCubeSDILayer;
2022-03-11 18:11:32 +01:00
import org.gcube.application.geoportal.common.model.legacy.SDILayerDescriptor;
2022-03-04 14:23:20 +01:00
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
2022-02-18 18:11:12 +01:00
import org.gcube.application.geoportal.common.model.rest.DatabaseConnection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
2022-03-11 18:11:32 +01:00
import java.util.ArrayList;
import java.util.HashMap;
2022-02-18 18:11:12 +01:00
import java.util.List;
@Slf4j
public class PostgisIndexer {
2022-03-17 17:54:00 +01:00
public static final String INDEX_TYPE="GIS-CENTROIDS";
2022-02-18 18:11:12 +01:00
public static void init() throws ClassNotFoundException {
Class.forName("org.postgresql.Driver");
Class.forName("org.postgis.DriverWrapper");
};
@NonNull
SDIManagerWrapper manager;
@NonNull
2022-03-04 14:23:20 +01:00
UseCaseDescriptor useCaseDescriptor;
2022-02-18 18:11:12 +01:00
@NonNull
DatabaseConnection connectionParameters;
PostgisDBManagerI dbManager=null;
2022-03-17 17:54:00 +01:00
PostgisTable table = null;
GCubeSDILayer indexLayer = null;
String indexName = null;
private List<CrossReferencedLayer> crossReferenceableLayers= new ArrayList<>();
2022-02-18 18:11:12 +01:00
2022-03-04 14:23:20 +01:00
public PostgisIndexer(SDIManagerWrapper manager, UseCaseDescriptor useCaseDescriptor,
2022-02-18 18:11:12 +01:00
DatabaseConnection postgisConnection) throws SQLException {
2022-03-04 14:23:20 +01:00
log.info("POSTGIS Index for {} Connecting to {} ", useCaseDescriptor.getId(),postgisConnection);
2022-02-18 18:11:12 +01:00
this.connectionParameters=postgisConnection;
dbManager=new PostgisDBManager(DriverManager.
getConnection(connectionParameters.getUrl(),
connectionParameters.getUser(),
connectionParameters.getPwd()));
this.manager=manager;
2022-03-04 14:23:20 +01:00
this.useCaseDescriptor = useCaseDescriptor;
2022-02-18 18:11:12 +01:00
}
2022-03-11 18:11:32 +01:00
2022-02-18 18:11:12 +01:00
public void initIndex(String indexName, List<PostgisTable.Field> fields, String workspace,String storeName) throws SQLException, SDIInteractionException {
2022-03-04 14:23:20 +01:00
log.info("Check/init index for {} ", useCaseDescriptor.getId());
2022-02-18 18:11:12 +01:00
table = new PostgisTable(indexName,fields, PostgisTable.GeometryType.POINT);
log.trace("Index Postgis Table is {} ",table);
log.debug("Create if missing..");
// Check if table exists
dbManager.create(table);
log.debug("Checking/ registering index layer in GS ");
2022-03-18 15:38:24 +01:00
this.indexName = indexName;
2022-03-17 17:54:00 +01:00
indexLayer = manager.configureCentroidLayer(indexName,workspace,storeName,table,connectionParameters);
2022-02-18 18:11:12 +01:00
// TODO Additional layers
// Create layer
2022-03-17 17:54:00 +01:00
// register cross related layers
2022-02-18 18:11:12 +01:00
}
2022-03-17 17:54:00 +01:00
HashMap<String,GCubeSDILayer> crossReferenced = new HashMap<>();
2022-02-18 18:11:12 +01:00
2022-03-11 18:11:32 +01:00
public Index getIndexConfiguration(){
2022-03-17 17:54:00 +01:00
Index toReturn = new Index(INDEX_TYPE);
2022-03-11 18:11:32 +01:00
// SDI Layers
2022-03-17 17:54:00 +01:00
toReturn.put("layer",indexLayer);
toReturn.put("indexName",indexName);
try {
toReturn.put("records", dbManager.count(table));
}catch (SQLException e) {
log.warn("Unable to count records for index " + indexName, e);
}
toReturn.put("crossReferencedLayers",crossReferenced);
2022-03-11 18:11:32 +01:00
return toReturn;
}
2022-02-18 18:11:12 +01:00
public void insert(Document toInsertRecord)throws SDIInteractionException {
log.info("Inserting {} in index {}",toInsertRecord,table.getTablename());
try {
PreparedStatement ps = dbManager.prepareInsertStatement(table, false, true);
table.fillObjectsPreparedStatement(toInsertRecord, ps);
ps.execute();
}catch (Throwable t ){
log.error("Unable to insert {} into {} ",toInsertRecord,table.getTablename(),t);
throw new SDIInteractionException("Unable to insert record in postgis index "+table.getTablename(),t);
}
}
public void deleteByStringValue(PostgisTable.Field field, String id) throws SDIInteractionException {
log.info("Deleting {}={} from index {}",field.getName(), id,table.getTablename());
try {
dbManager.deleteByFieldValue(table,field,id);
}catch (Throwable t ){
log.error("Unable to delete {}={} from index {}",field.getName(), id,table.getTablename(),t);
throw new SDIInteractionException("Unable to delete record in postgis index "+table.getTablename(),t);
}
}
}