Compare commits

...

9 Commits

13 changed files with 1308 additions and 67 deletions

View File

@ -4,6 +4,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v2.3.0]
- Read countByPhase from configuration [#25598]
- Reduced/Optimized some LOGs [#25539]
## [v2.2.0]
- Integrated the cross-filtering configuration [#25074]

View File

@ -10,7 +10,7 @@
<groupId>org.gcube.application</groupId>
<artifactId>geoportal-data-common</artifactId>
<version>2.2.0</version>
<version>2.3.0</version>
<description>GeoPortal Data Common is common library used by GUI components developed for GeoNA</description>
<scm>

View File

@ -66,6 +66,7 @@ import org.gcube.application.geoportalcommon.shared.geoportal.ucd.HandlerDeclara
import org.gcube.application.geoportalcommon.shared.geoportal.ucd.RelationshipDefinitionDV;
import org.gcube.application.geoportalcommon.shared.geoportal.ucd.UseCaseDescriptorDV;
import org.gcube.application.geoportalcommon.util.DateUtils;
import org.gcube.application.geoportalcommon.util.StringUtil;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
@ -94,6 +95,8 @@ import com.jayway.jsonpath.Option;
*/
public class ConvertToDataValueObjectModel {
public static final int _MAX_LENGHT_STRING_AT_INFO = 200;
private static Logger LOG = LoggerFactory.getLogger(ConvertToDataValueObjectModel.class);
private static final String NO_TIME = "T00:00";
@ -174,7 +177,9 @@ public class ConvertToDataValueObjectModel {
}
ucdVO.setHandlers(listHandlersDV);
LOG.info("returning {}", ucdVO);
LOG.info("returning {}", StringUtil.ellipsize(ucdVO.toString(), _MAX_LENGHT_STRING_AT_INFO));
if (LOG.isDebugEnabled())
LOG.debug("returning {}", ucdVO);
return ucdVO;
}
@ -313,7 +318,10 @@ public class ConvertToDataValueObjectModel {
dDV.setConfiguration(listGcubeProfiles);
dDV.setConfigurationType(geoportalConfigType); // -> GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles
hdDV.setConfiguration(dDV);
LOG.info("returning {}", hdDV);
LOG.info("Found config {}", GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles);
LOG.info("returning {}", StringUtil.ellipsize(hdDV.toString(), _MAX_LENGHT_STRING_AT_INFO));
if (LOG.isDebugEnabled())
LOG.debug("returning {}", hdDV);
return hdDV;
}
case item_fields: {
@ -332,7 +340,10 @@ public class ConvertToDataValueObjectModel {
dDV.setConfiguration(listItemFields);
dDV.setConfigurationType(geoportalConfigType); // -> GEOPORTAL_CONFIGURATION_TYPE.item_fields
hdDV.setConfiguration(dDV);
LOG.info("returning {}", hdDV);
LOG.info("Found config {}", GEOPORTAL_CONFIGURATION_TYPE.item_fields);
LOG.info("returning {}", StringUtil.ellipsize(hdDV.toString(), _MAX_LENGHT_STRING_AT_INFO));
if (LOG.isDebugEnabled())
LOG.debug("returning {}", hdDV);
return hdDV;
}
@ -375,7 +386,10 @@ public class ConvertToDataValueObjectModel {
dDV.setConfiguration(listActionsDef);
dDV.setConfigurationType(geoportalConfigType); // -> GEOPORTAL_CONFIGURATION_TYPE.actions_definition
hdDV.setConfiguration(dDV);
LOG.info("returning {}", hdDV);
LOG.info("Found config {}", GEOPORTAL_CONFIGURATION_TYPE.actions_definition);
LOG.info("returning {}", StringUtil.ellipsize(hdDV.toString(), _MAX_LENGHT_STRING_AT_INFO));
if (LOG.isDebugEnabled())
LOG.debug("returning {}", hdDV);
return hdDV;
}
@ -388,7 +402,8 @@ public class ConvertToDataValueObjectModel {
ObjectMapper mapper = new ObjectMapper();
for (String asJSONString : jsonConfigurations) {
LOG.debug(++i + ") the " + GroupedLayersDV.class.getSimpleName() + " is: " + asJSONString);
TypeReference<GroupedLayersDV<ConfiguredLayerDV>> typeRef = new TypeReference<GroupedLayersDV<ConfiguredLayerDV>>() {};
TypeReference<GroupedLayersDV<ConfiguredLayerDV>> typeRef = new TypeReference<GroupedLayersDV<ConfiguredLayerDV>>() {
};
GroupedLayersDV<ConfiguredLayerDV> profile = mapper.readValue(asJSONString, typeRef);
// GroupedLayersDV<ConfiguredLayerDV> profile = org.gcube.application.geoportal.client.utils.Serialization
// .read(asJSONString, typeRef);
@ -401,7 +416,10 @@ public class ConvertToDataValueObjectModel {
dDV.setConfiguration(listGroupedCL);
dDV.setConfigurationType(geoportalConfigType);
hdDV.setConfiguration(dDV);
LOG.info("returning {}", hdDV);
LOG.info("Found config {}", GEOPORTAL_CONFIGURATION_TYPE.grouped_overlay_layers);
LOG.info("returning {}", StringUtil.ellipsize(hdDV.toString(), _MAX_LENGHT_STRING_AT_INFO));
if (LOG.isDebugEnabled())
LOG.debug("returning {}", hdDV);
return hdDV;
}
@ -414,7 +432,8 @@ public class ConvertToDataValueObjectModel {
ObjectMapper mapper = new ObjectMapper();
for (String asJSONString : jsonConfigurations) {
LOG.debug(++i + ") the " + GroupedLayersDV.class.getSimpleName() + " is: " + asJSONString);
TypeReference<GroupedLayersDV<CrossFilteringLayerDV>> typeRef = new TypeReference<GroupedLayersDV<CrossFilteringLayerDV>>() {};
TypeReference<GroupedLayersDV<CrossFilteringLayerDV>> typeRef = new TypeReference<GroupedLayersDV<CrossFilteringLayerDV>>() {
};
GroupedLayersDV<CrossFilteringLayerDV> profile = mapper.readValue(asJSONString, typeRef);
// GroupedLayersDV<CrossFilteringLayerDV> profile = org.gcube.application.geoportal.client.utils.Serialization
// .read(asJSONString, GroupedLayersDV.class);
@ -427,7 +446,10 @@ public class ConvertToDataValueObjectModel {
dDV.setConfiguration(listGroupedCL);
dDV.setConfigurationType(geoportalConfigType);
hdDV.setConfiguration(dDV);
LOG.info("returning {}", hdDV);
LOG.info("Found config {}", GEOPORTAL_CONFIGURATION_TYPE.grouped_cross_filtering_layers);
LOG.info("returning {}", StringUtil.ellipsize(hdDV.toString(), _MAX_LENGHT_STRING_AT_INFO));
if (LOG.isDebugEnabled())
LOG.debug("returning {}", hdDV);
return hdDV;
}
@ -457,7 +479,7 @@ public class ConvertToDataValueObjectModel {
LOG.trace("toGcubeProfileDV called");
if (gCubeProfile == null) {
LOG.warn(GcubeProfile.class.getSimpleName() + " is null");
LOG.info(GcubeProfile.class.getSimpleName() + " is null");
return null;
}
@ -481,7 +503,9 @@ public class ConvertToDataValueObjectModel {
gpVO.setFilePaths(filePathsVO);
}
LOG.info("returning: " + gpVO);
LOG.info("returning {}", StringUtil.ellipsize(gpVO.toString(), _MAX_LENGHT_STRING_AT_INFO));
if (LOG.isDebugEnabled())
LOG.debug("returning {}", gpVO);
return gpVO;
}
@ -495,7 +519,7 @@ public class ConvertToDataValueObjectModel {
LOG.trace("toItemFieldDV called");
if (itemField == null) {
LOG.warn(ItemField.class.getSimpleName() + " is null");
LOG.info(ItemField.class.getSimpleName() + " is null");
return null;
}
@ -507,7 +531,7 @@ public class ConvertToDataValueObjectModel {
ifDV.setSearchable(itemField.isSearchable());
ifDV.setSortable(itemField.isSortable());
LOG.info("returning: " + ifDV);
LOG.debug("returning: " + ifDV);
return ifDV;
}
@ -534,7 +558,9 @@ public class ConvertToDataValueObjectModel {
actDef.setTitle(actionDefinition.getTitle());
actDef.setRoles(roles);
LOG.info("returning: " + actionDefinition);
LOG.info("returning {}", StringUtil.ellipsize(actionDefinition.toString(), _MAX_LENGHT_STRING_AT_INFO));
if (LOG.isDebugEnabled())
LOG.debug("returning {}", actionDefinition);
return actDef;
}
@ -548,7 +574,7 @@ public class ConvertToDataValueObjectModel {
LOG.trace("toFilePathDV called");
if (filePath == null) {
LOG.warn("List of " + FilePath.class.getSimpleName() + " is null");
LOG.info("List of " + FilePath.class.getSimpleName() + " is null for {}", filePath);
return null;
}
@ -557,7 +583,9 @@ public class ConvertToDataValueObjectModel {
fpVO.setFieldDefinition(filePath.getFieldDefinition());
fpVO.setGcubeProfileFieldName(filePath.getGcubeProfileFieldName());
LOG.info("returning: " + fpVO);
LOG.info("returning {}", StringUtil.ellipsize(fpVO.toString(), _MAX_LENGHT_STRING_AT_INFO));
if (LOG.isDebugEnabled())
LOG.debug("returning {}", fpVO);
return fpVO;
}

View File

@ -97,7 +97,7 @@ public class SerializerUtil {
@Override
public GeoServerPlatformInfoDV[] deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
LOG.info("Sono qui: " +jp.getValueAsString());
LOG.info("deserialize: " +jp.getValueAsString());
return jp.readValueAs(GeoServerPlatformInfoDV[].class);
//return jp.readValueAs(GeoServerPlatformInfoDV[].class);
//List<GeoServerPlatformInfoDV> listPlatform = jp.readValueAs(new TypeReference<List<GeoServerPlatformInfoDV>>() {});

View File

@ -57,7 +57,7 @@ public class GeoportalConfigUtil {
JSONArray arrayRoles = new JSONArray(data.get(0).toString());
for (int i = 0; i < arrayRoles.length(); i++) {
String role = arrayRoles.get(i).toString();
LOG.info("for STEP_ID {} read role {}", stepID, role);
LOG.debug("for STEP_ID {} read role {}", stepID, role);
listdata.add(role);
}
}catch (Exception e) {

View File

@ -58,6 +58,7 @@ import com.mongodb.BasicDBObjectBuilder;
*/
public class ProjectsCaller {
public static final String DOCUMENT_STORE_COLLECTION = "DOCUMENT-STORE-COLLECTION";
private static Logger LOG = LoggerFactory.getLogger(GeoportalClientCaller.class);
/**
@ -208,7 +209,7 @@ public class ProjectsCaller {
for (Archive archive : listArchives) {
String theType = archive.getString("_type");
if (theType.equalsIgnoreCase("DOCUMENT-STORE-COLLECTION")) {
if (theType.equalsIgnoreCase(DOCUMENT_STORE_COLLECTION)) {
String totalDocumentAre = archive.get("count").toString();
int total = Integer.parseInt(totalDocumentAre);
LOG.info("total docs for profileID: {}, are: {}", profileID, total);
@ -234,7 +235,7 @@ public class ProjectsCaller {
for (Archive archive : listArchives) {
String theType = archive.getString("_type");
if (theType.equalsIgnoreCase("DOCUMENT-STORE-COLLECTION")) {
if (theType.equalsIgnoreCase(DOCUMENT_STORE_COLLECTION)) {
com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder()
.jsonProvider(new JsonOrgJsonProvider()).build();
String toJSON = archive.toJson();
@ -255,6 +256,36 @@ public class ProjectsCaller {
return null;
}
public Integer getCountByPhaseFor(String profileID, String phase, String status) throws Exception {
LOG.info("getCountByPhaseFor called for profileID: {}, phase: {}, status: {}", profileID, phase, status);
Projects<Project> client = (Projects<Project>) getClient(profileID);
Configuration config = client.getConfiguration();
List<Archive> listArchives = config.getArchives();
Integer count = null;
for (Archive archive : listArchives) {
String theType = archive.getString("_type");
if (theType.equalsIgnoreCase(DOCUMENT_STORE_COLLECTION)) {
com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder()
.jsonProvider(new JsonOrgJsonProvider()).build();
String toJSON = archive.toJson();
JSONObject jObject = new JSONObject(toJSON);
String query = String.format("$.countByPhase[*][?(@._id.phase == '%s' && @._id.status == '%s')].count",
phase, status);
LOG.debug("Performing query: " + query);
JsonPath jsonPath = JsonPath.compile(query);
JSONArray counts = jsonPath.read(jObject, configuration);
try {
count = counts.getInt(0);
} catch (Exception e) {
LOG.warn("getCountByPhaseFor error: " + e.getLocalizedMessage());
}
}
}
LOG.info("getCountByPhaseFor returning: " + count);
return count;
}
/**
* Gets the phases into document store collection.
*
@ -270,7 +301,7 @@ public class ProjectsCaller {
for (Archive archive : listArchives) {
String theType = archive.getString("_type");
if (theType.equalsIgnoreCase("DOCUMENT-STORE-COLLECTION")) {
if (theType.equalsIgnoreCase(DOCUMENT_STORE_COLLECTION)) {
com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder()
.jsonProvider(new JsonOrgJsonProvider()).build();
String toJSON = archive.toJson();
@ -398,19 +429,19 @@ public class ProjectsCaller {
* @param projectID the project ID
* @param jsonPathToFileset the json path to fileset
* @param force the force
* @param ignoreErrors the ignore errors
* @param ignoreErrors the ignore errors
* @return the project
* @throws RemoteException the remote exception
*/
public Project deleteFileset(String profileID, String projectID, String jsonPathToFileset, Boolean force, Boolean ignoreErrors)
throws RemoteException {
public Project deleteFileset(String profileID, String projectID, String jsonPathToFileset, Boolean force,
Boolean ignoreErrors) throws RemoteException {
LOG.info("deleteFileset called for profileID {} and projectID {}, fileset path: {}", profileID, projectID,
jsonPathToFileset);
Projects<Project> client = (Projects<Project>) getClient(profileID);
ignoreErrors = ignoreErrors==null?false:ignoreErrors;
ignoreErrors = ignoreErrors == null ? false : ignoreErrors;
Project project = client.deleteFileSet(projectID, jsonPathToFileset, force, ignoreErrors);
LOG.info("fileset {} deleted", jsonPathToFileset);
LOG.debug("returning new project: {} ", project.getTheDocument());

View File

@ -0,0 +1,27 @@
package org.gcube.application.geoportalcommon.util;
/**
* The Class StringUtil.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Aug 31, 2023
*/
public class StringUtil {
/**
* Ellipsize.
*
* @param input the input
* @param maxLength the max length
* @return the string
*/
public static String ellipsize(String input, int maxLength) {
String ellip = "...";
if (input == null || input.length() <= maxLength || input.length() < ellip.length()) {
return input;
}
return input.substring(0, maxLength - ellip.length()).concat(ellip);
}
}

View File

@ -0,0 +1,43 @@
package org.gcube.application;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ContextConfigTest {
private static final String GCUBE_CONFIG_PROPERTIES_FILENAME = "gcube_config.properties";
// APP Working Directory + /src/test/resources must be the location of
// gcube_config.properties
private static String gcube_config_path = String.format("%s/%s",
System.getProperty("user.dir") + "/src/test/resources", GCUBE_CONFIG_PROPERTIES_FILENAME);
public static String CONTEXT;
public static String TOKEN;
/**
* Read context settings.
*/
public static void readContextSettings() {
try (InputStream input = new FileInputStream(gcube_config_path)) {
Properties prop = new Properties();
// load a properties file
prop.load(input);
CONTEXT = prop.getProperty("CONTEXT");
TOKEN = prop.getProperty("TOKEN");
// get the property value and print it out
System.out.println("CONTEXT: " + CONTEXT);
System.out.println("TOKEN: " + TOKEN);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

View File

@ -34,17 +34,22 @@ import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResol
public class CreateGeoportalGisLinkInTheCentroidLayers {
private static String CONTEXT = "/gcube/devsec/devVRE";
private static String TOKEN = ""; // devVRE
// private static String CONTEXT = "/gcube/devsec/devVRE";
// private static String TOKEN = ""; // devVRE
// private static final String CONTEXT = "/pred4s/preprod/preVRE";
// private static final String TOKEN = ""; //preVRE
// private static final String CONTEXT = "/d4science.research-infrastructures.eu/D4OS/GNA";
// private static final String TOKEN = ""; //GNA
//Esquiline
private static final String CONTEXT = "/d4science.research-infrastructures.eu/gCubeApps/Esquiline";
private static final String TOKEN = ""; //Esquiline
private static String PROFILE_ID = "esquilino";
// GEOPORTAL
private static String PROFILE_ID = "profiledConcessioni";
//private static String PROFILE_ID = "profiledConcessioni";
// private static final String PROFILE_ID = "concessioni-estere";
private static final String JSON_KEY_DATA_FINE_PROGETTO = "dataFineProgetto";
private static final String JSON_KEY_DATA_INIZIO_PROGETTO = "dataInizioProgetto";
@ -61,7 +66,7 @@ public class CreateGeoportalGisLinkInTheCentroidLayers {
private static ProjectsCaller client = null;
public static final boolean READ_ONLY_MODE = false;
public static final boolean READ_ONLY_MODE = true;
/**
* Gets the client.
@ -85,9 +90,9 @@ public class CreateGeoportalGisLinkInTheCentroidLayers {
initGeoportalClient();
//devVRE
PROFILE_ID = "profiledConcessioni";
String tableName = "profiledconcessioni_devvre_centroids";
tableName = "profiledconcessioni_internal__devvre_centroids";
// PROFILE_ID = "profiledConcessioni";
// String tableName = "profiledconcessioni_devvre_centroids";
// tableName = "profiledconcessioni_internal__devvre_centroids";
// PROFILE_ID = "concessioni-estere";
// String tableName = "concessioni_estere_devvre_centroids";
// tableName = "concessioni_estere_internal__devvre_centroids";
@ -104,9 +109,14 @@ public class CreateGeoportalGisLinkInTheCentroidLayers {
// PROFILE_ID = "concessioni-estere";
// String tableName = "concessioni_estere_internal__gna_centroids";
// String tableName = "concessioni_estere_gna_centroids";
PROFILE_ID = "profiledConcessioni";
// PROFILE_ID = "profiledConcessioni";
// String tableName = "profiledconcessioni_internal__gna_centroids";
// String tableName = "profiledconcessioni_gna_centroids";
// Esquiline
PROFILE_ID = "esquilino";
String tableName = "esquilino_esquiline_centroids";
long startTime = System.currentTimeMillis();

View File

@ -0,0 +1,538 @@
package org.gcube.application;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.gcube.application.geoportalcommon.geoportal.GeoportalClientCaller;
import org.gcube.application.geoportalcommon.geoportal.ProjectsCaller;
import org.gcube.application.se.RuntimeResourceReader;
import org.gcube.application.se.ServiceEndpointBean;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.scope.api.ScopeProvider;
public class GNA_New_DataModel_IndexTable_25925 {
private static String PROFILE_ID = "profiledConcessioni";
private static final String platformName = "postgis";
private static final String category = "Database";
// These are defined via ContextConfigTest.readContextSettings();
private static String CONTEXT = "";
private static String TOKEN = "";
// #### DEV
// DB
// private static final String resourceName = "GNA-POSTGIS-DB"; // devVRE
// #### PRE
// DB
private static final String resourceName = "Geoserver-t postgis"; // preVRE
// #### PROD
// DB
// private static final String resourceName = "GNA-postgis"; // GNA
// Esquiline
// private static final String CONTEXT = "/d4science.research-infrastructures.eu/gCubeApps/Esquiline";
// private static final String TOKEN = ""; //Esquiline
// private static String PROFILE_ID = "esquilino";
// GEOPORTAL
// private static String PROFILE_ID = "profiledConcessioni";
// private static final String PROFILE_ID = "concessioni-estere";
private static final Map<String, String> ADD_TABLE_COLUMNS = new LinkedHashMap<>();
// static {
// ADD_TABLE_COLUMNS.put("ufficio_mic_competente", "TEXT");
// ADD_TABLE_COLUMNS.put("funzionario_responsabile", "TEXT");
// ADD_TABLE_COLUMNS.put("modalita_individuazione", "TEXT");
// ADD_TABLE_COLUMNS.put("contesto_indagine", "TEXT");
// ADD_TABLE_COLUMNS.put("denominazione", "TEXT");
// ADD_TABLE_COLUMNS.put("stato_attuale", "TEXT");
// ADD_TABLE_COLUMNS.put("accessibilita", "TEXT");
// ADD_TABLE_COLUMNS.put("cronologia_macrofase", "TEXT");
// ADD_TABLE_COLUMNS.put("specifiche_cronologia", "TEXT");
// ADD_TABLE_COLUMNS.put("quota_max", "TEXT");
// ADD_TABLE_COLUMNS.put("quota_min", "TEXT");
// }
private static final Map<String, String> DROP_TABLE_COLUMNS = new HashMap<>();
// static {
// DROP_TABLE_COLUMNS.put("autore", "");
// DROP_TABLE_COLUMNS.put("titolare", "");
// DROP_TABLE_COLUMNS.put("titolare_licenza", "");
// }
private static final Map<String, String> RENAME_TABLE_COLUMNS = new HashMap<>();
//Rename from column to column
// static {
// RENAME_TABLE_COLUMNS.put("ufficio_mic_competente", "ufficio_competente");
// }
private static ProjectsCaller client = null;
public static final boolean READ_ONLY_MODE = true;
public static List<String> listTable = new ArrayList<String>();
/**
* Gets the client.
*
* @return the client
*/
// @Before
public static void initGeoportalClient() {
// assumeTrue(GCubeTest.isTestInfrastructureEnabled());
ContextConfigTest.readContextSettings();
CONTEXT = ContextConfigTest.CONTEXT;
TOKEN = ContextConfigTest.TOKEN;
ScopeProvider.instance.set(CONTEXT);
SecurityTokenProvider.instance.set(TOKEN);
client = GeoportalClientCaller.projects();
}
public static void main(String[] args) {
initGeoportalClient();
System.out.println("READ_ONLY_MODE ENABLED: " + READ_ONLY_MODE);
System.out.println("CONTEXT IS: " + CONTEXT);
System.out.println("PROFILE_ID: " + PROFILE_ID);
// devVRE
// PROFILE_ID = "profiledConcessioni";
// String tableName = "profiledconcessioni_devvre_centroids";
// tableName = "profiledconcessioni_internal__devvre_centroids";
// PROFILE_ID = "concessioni-estere";
// String tableName = "concessioni_estere_devvre_centroids";
// tableName = "concessioni_estere_internal__devvre_centroids";
// preVRE
// PROFILE_ID = "profiledConcessioni";
// listTable.add("profiledconcessioni_internal__prevre_centroids");
// listTable.add("profiledconcessioni_prevre_centroids");
PROFILE_ID = "concessioni-estere";
listTable.add("concessioni_estere_internal__prevre_centroids");
listTable.add("concessioni_estere_prevre_centroids");
// GNA
// PROFILE_ID = "concessioni-estere";
// String tableName = "concessioni_estere_internal__gna_centroids";
// String tableName = "concessioni_estere_gna_centroids";
// PROFILE_ID = "profiledConcessioni";
// String tableName = "profiledconcessioni_internal__gna_centroids";
// String tableName = "profiledconcessioni_gna_centroids";
// Esquiline
// PROFILE_ID = "esquilino";
// String tableName = "esquilino_esquiline_centroids";
long startTime = System.currentTimeMillis();
try {
RuntimeResourceReader rrr = new RuntimeResourceReader(CONTEXT, resourceName, platformName, category, null);
ServiceEndpointBean se = rrr.getListSE().get(0);
AccessPoint ap = se.getListAP().get(0);
System.out.println(ap.address());
System.out.println(ap.username());
String pwdDB = RuntimeResourceReader.dectryptPassword(CONTEXT, ap);
System.out.println(pwdDB);
Connection dbConnection = getDatabaseConnection(ap.address(), ap.username(), pwdDB);
for (String tableName : listTable) {
System.out.println("\n\n# SOURCE TABLE");
Boolean tableExists = checkTableExists(dbConnection, tableName);
if (!tableExists) {
throw new Exception("Table '" + tableName + "' does not exits in the DB!!! Exit");
}
printTableColumn(dbConnection, tableName);
System.out.println("\n\n### ADDING COLUMNS...\n");
// ADD NEW COLUMNS
for (String columnName : ADD_TABLE_COLUMNS.keySet()) {
try {
boolean columnExists = checkColumnExists(dbConnection, tableName, columnName);
if (!columnExists) {
addColumnTable(dbConnection, tableName, columnName, ADD_TABLE_COLUMNS.get(columnName));
int sleeping = 500;
System.out.println("... sleeping " + sleeping + " ...");
Thread.sleep(sleeping);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
System.out.println("\n\n### END ADD COLUMNS");
System.out.println("\n\n### DROPPING COLUMNS...\n");
// REMOVE OLD COLUMNS
for (String columnName : DROP_TABLE_COLUMNS.keySet()) {
try {
boolean columnExists = checkColumnExists(dbConnection, tableName, columnName);
if (columnExists) {
dropColumnTable(dbConnection, tableName, columnName, DROP_TABLE_COLUMNS.get(columnName));
int sleeping = 1000;
System.out.println("... sleeping " + sleeping + " ...");
Thread.sleep(sleeping);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
System.out.println("\n\n### END DROP COLUMNS");
System.out.println("\n\n### RENAMING COLUMNS...\n");
// RENAME_TABLE_COLUMNS
for (String columnName : RENAME_TABLE_COLUMNS.keySet()) {
try {
boolean columnExists = checkColumnExists(dbConnection, tableName, columnName);
if (columnExists) {
renameColumnTable(dbConnection, tableName, columnName, RENAME_TABLE_COLUMNS.get(columnName));
int sleeping = 1000;
System.out.println("... sleeping " + sleeping + " ...");
Thread.sleep(sleeping);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
System.out.println("\n\n### END RENAME COLUMNS");
System.out.println("\n\n# UPDATED TABLE");
printTableColumn(dbConnection, tableName);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("\n\nSCRIPT END at: " + endTime);
double toSec = (endTime - startTime) / 1000;
System.out.println("SCRIPT TERMINATED in " + toSec + "sec");
}
public static void printTableColumn(Connection con, String tablename) {
try {
DatabaseMetaData databaseMetaData = con.getMetaData();
// Print TABLE_TYPE "TABLE"
ResultSet columns = databaseMetaData.getColumns(null, null, tablename, null);
System.out.println("\n==== TABLE " + tablename + " ");
System.out.println("[COLUMN_NAME - TYPE_NAME ( COLUMN_SIZE )]");
System.out.println("-----------------------------------------------");
while (columns.next()) {
String columnName = columns.getString("COLUMN_NAME");
String columnType = columns.getString("TYPE_NAME");
int columnSize = columns.getInt("COLUMN_SIZE");
System.out.println("\t" + columnName + " - " + columnType + " (" + columnSize + ")");
}
System.out.println("-----------------------------------------------");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Gets the database connection.
*
* @param dbURL the db url
* @param user the user
* @param pwd the pwd
* @return the database connection
*/
public static Connection getDatabaseConnection(String dbURL, String user, String pwd) {
System.out.println("dbURL: " + dbURL);
Connection c = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection(dbURL, user, pwd);
c.setAutoCommit(false);
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
return c;
}
public static void addColumnTable(Connection con, String tableName, String newColumn, String type)
throws SQLException {
String alterTableString = String.format("ALTER TABLE %s ADD COLUMN %s %s", tableName, newColumn, type);
System.out.println("Executing: " + alterTableString);
try {
con.setAutoCommit(false);
if (!READ_ONLY_MODE) {
con.createStatement().execute(alterTableString);
con.commit();
}
} catch (SQLException e) {
e.printStackTrace();
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.rollback();
} catch (SQLException excep) {
e.printStackTrace();
}
}
}
System.out.println("Executed: " + alterTableString);
}
public static void renameColumnTable(Connection con, String tableName, String oldColumn, String newColumn)
throws SQLException {
String alterTableString = String.format("ALTER TABLE %s RENAME COLUMN %s TO %s", tableName, oldColumn,
newColumn);
System.out.println("Executing: " + alterTableString);
try {
con.setAutoCommit(false);
if (!READ_ONLY_MODE) {
con.createStatement().execute(alterTableString);
con.commit();
}
} catch (SQLException e) {
e.printStackTrace();
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.rollback();
} catch (SQLException excep) {
e.printStackTrace();
}
}
}
System.out.println("Executed: " + alterTableString);
}
public static void dropColumnTable(Connection con, String tableName, String oldColumn, String type)
throws SQLException {
String alterTableString = String.format("ALTER TABLE %s DROP COLUMN %s", tableName, oldColumn);
System.out.println("Executing: " + alterTableString);
try {
con.setAutoCommit(false);
if (!READ_ONLY_MODE) {
con.createStatement().execute(alterTableString);
con.commit();
}
} catch (SQLException e) {
e.printStackTrace();
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.rollback();
} catch (SQLException excep) {
e.printStackTrace();
}
}
}
System.out.println("Executed: " + alterTableString);
}
public static Boolean checkColumnExists(Connection con, String tableName, String columnName) throws SQLException {
String columnExistsLabel = "COLUMN_EXISTS";
String sql = String.format(
"SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='%s' AND column_name='%s') as %s",
tableName, columnName, columnExistsLabel);
System.out.println("\n+++ " + columnExistsLabel + " checking '" + tableName + "': " + sql);
PreparedStatement p;
ResultSet resultSet;
try {
p = con.prepareStatement(sql);
resultSet = p.executeQuery();
// Expected 1 row
resultSet.next();
Boolean columnExists = resultSet.getBoolean(columnExistsLabel);
System.out.println("\t RESP --> Column '" + columnName + "' exists: " + columnExists + "\n");
return columnExists;
} catch (SQLException e) {
System.err.println(e);
return false;
}
}
public static Boolean checkTableExists(Connection con, String tableName) throws SQLException {
String tableExistsLabel = "TABLE_EXISTS";
String sql = String.format(
"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='%s' AND table_type LIKE 'BASE TABLE' AND table_schema LIKE 'public') as %s",
tableName, tableExistsLabel);
System.out.println("\n+++ " + tableExistsLabel + " checking '" + tableName + "': " + sql);
PreparedStatement p;
ResultSet resultSet;
try {
p = con.prepareStatement(sql);
resultSet = p.executeQuery();
// Expected 1 row
resultSet.next();
Boolean tableExists = resultSet.getBoolean(tableExistsLabel);
System.out.println("\t RESP --> Table '" + tableName + "' exists: " + tableExists + "\n");
return tableExists;
} catch (SQLException e) {
System.err.println(e);
return false;
}
}
public static void updateTable(Connection con, String tableName, String whereCondition,
LinkedHashMap<String, String> mapColumnValue) throws SQLException {
StringBuilder updateSQL = new StringBuilder();
updateSQL.append("UPDATE " + tableName + " SET");
int i = 1;
for (String column : mapColumnValue.keySet()) {
updateSQL.append(" " + column + "=?");
if (i < mapColumnValue.size())
updateSQL.append(", ");
i++;
}
updateSQL.append(" WHERE " + whereCondition);
String update = updateSQL.toString();
// System.out.println("Executing: " + update);
try (PreparedStatement updatePS = con.prepareStatement(update);) {
int index = 1;
for (String column : mapColumnValue.keySet()) {
updatePS.setString(index, mapColumnValue.get(column));
index++;
}
con.setAutoCommit(false);
System.out.println("Executing: " + updatePS.toString());
if (!READ_ONLY_MODE) {
updatePS.executeUpdate();
con.commit();
}
} catch (SQLException e) {
e.printStackTrace();
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.rollback();
} catch (SQLException excep) {
e.printStackTrace();
}
}
}
}
// Step1: Main driver method
public static List<String> readTableIDs(Connection con, String tableName, String columnNameProjectId) {
PreparedStatement p = null;
ResultSet resultSet = null;
// Try block to catch exception/s
List<String> listProjectIds = new ArrayList<String>();
try {
// SQL command data stored in String datatype
String sql = String.format("SELECT * FROM %s", tableName);
p = con.prepareStatement(sql);
resultSet = p.executeQuery();
ResultSetMetaData rsmd = resultSet.getMetaData();
int columnsNumber = rsmd.getColumnCount();
int row = 1;
System.out.println("\n####TABLE: " + tableName + " content..\n");
while (resultSet.next()) {
System.out.print("" + row + "] ");
for (int i = 1; i <= columnsNumber; i++) {
String columnValue = resultSet.getString(i);
System.out.print("(" + rsmd.getColumnName(i) + ") " + columnValue + " | ");
}
row++;
System.out.println("\n");
String projectId = resultSet.getString(columnNameProjectId);
listProjectIds.add(projectId);
}
System.out.println("####TABLE: " + tableName + " end content\n");
}
// Catch block to handle exception
catch (SQLException e) {
// Print exception pop-up on screen
System.err.println(e);
}
System.out.println("returning list IDs: " + listProjectIds);
return listProjectIds;
}
}

View File

@ -0,0 +1,511 @@
package org.gcube.application;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.gcube.application.geoportalcommon.geoportal.GeoportalClientCaller;
import org.gcube.application.geoportalcommon.geoportal.ProjectsCaller;
import org.gcube.application.se.RuntimeResourceReader;
import org.gcube.application.se.ServiceEndpointBean;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.scope.api.ScopeProvider;
public class GNA_New_DataModel_IndexTable_PROD_25926 {
private static String PROFILE_ID = "profiledConcessioni";
private static final String platformName = "postgis";
private static final String category = "Database";
// These are defined via ContextConfigTest.readContextSettings();
private static String CONTEXT = "";
private static String TOKEN = "";
// #### PROD
// DB
private static final String resourceName = "GNA-postgis"; // GNA
// Esquiline
// private static final String CONTEXT = "/d4science.research-infrastructures.eu/gCubeApps/Esquiline";
// private static final String TOKEN = ""; //Esquiline
// private static String PROFILE_ID = "esquilino";
private static final Map<String, String> ADD_TABLE_COLUMNS = new LinkedHashMap<>();
// static {
// ADD_TABLE_COLUMNS.put("ufficio_mic_competente", "TEXT");
// ADD_TABLE_COLUMNS.put("funzionario_responsabile", "TEXT");
// ADD_TABLE_COLUMNS.put("modalita_individuazione", "TEXT");
// ADD_TABLE_COLUMNS.put("contesto_indagine", "TEXT");
// ADD_TABLE_COLUMNS.put("denominazione", "TEXT");
// ADD_TABLE_COLUMNS.put("stato_attuale", "TEXT");
// ADD_TABLE_COLUMNS.put("accessibilita", "TEXT");
// ADD_TABLE_COLUMNS.put("cronologia_macrofase", "TEXT");
// ADD_TABLE_COLUMNS.put("specifiche_cronologia", "TEXT");
// ADD_TABLE_COLUMNS.put("quota_max", "TEXT");
// ADD_TABLE_COLUMNS.put("quota_min", "TEXT");
// }
//
private static final Map<String, String> DROP_TABLE_COLUMNS = new HashMap<>();
// static {
// DROP_TABLE_COLUMNS.put("autore", "");
// DROP_TABLE_COLUMNS.put("titolare", "");
// DROP_TABLE_COLUMNS.put("titolare_licenza", "");
// }
private static final Map<String, String> RENAME_TABLE_COLUMNS = new HashMap<>();
// Rename from column to column
static {
RENAME_TABLE_COLUMNS.put("ufficio_mic_competente", "ufficio_competente");
}
private static ProjectsCaller client = null;
public static final boolean READ_ONLY_MODE = true;
public static List<String> listTable = new ArrayList<String>();
/**
* Gets the client.
*
* @return the client
*/
// @Before
public static void initGeoportalClient() {
// assumeTrue(GCubeTest.isTestInfrastructureEnabled());
ContextConfigTest.readContextSettings();
CONTEXT = ContextConfigTest.CONTEXT;
TOKEN = ContextConfigTest.TOKEN;
ScopeProvider.instance.set(CONTEXT);
SecurityTokenProvider.instance.set(TOKEN);
client = GeoportalClientCaller.projects();
}
public static void main(String[] args) {
initGeoportalClient();
System.out.println("READ_ONLY_MODE ENABLED: " + READ_ONLY_MODE);
System.out.println("CONTEXT IS: " + CONTEXT);
System.out.println("PROFILE_ID: " + PROFILE_ID);
// GNA
// PROFILE_ID = "profiledConcessioni";
// listTable.add("profiledconcessioni_internal__gna_centroids");
// listTable.add("profiledconcessioni_gna_centroids");
// PROFILE_ID = "concessioni-estere";
// listTable.add("concessioni_estere_internal__gna_centroids");
// listTable.add("concessioni_estere_gna_centroids");
// Esquiline
// PROFILE_ID = "esquilino";
// String tableName = "esquilino_esquiline_centroids";
long startTime = System.currentTimeMillis();
try {
RuntimeResourceReader rrr = new RuntimeResourceReader(CONTEXT, resourceName, platformName, category, null);
ServiceEndpointBean se = rrr.getListSE().get(0);
AccessPoint ap = se.getListAP().get(0);
System.out.println(ap.address());
System.out.println(ap.username());
String pwdDB = RuntimeResourceReader.dectryptPassword(CONTEXT, ap);
System.out.println(pwdDB);
Connection dbConnection = getDatabaseConnection(ap.address(), ap.username(), pwdDB);
for (String tableName : listTable) {
System.out.println("\n\n# SOURCE TABLE");
Boolean tableExists = checkTableExists(dbConnection, tableName);
if (!tableExists) {
throw new Exception("Table '" + tableName + "' does not exits in the DB!!! Exit");
}
printTableColumn(dbConnection, tableName);
System.out.println("\n\n### ADDING COLUMNS...\n");
// ADD NEW COLUMNS
for (String columnName : ADD_TABLE_COLUMNS.keySet()) {
try {
boolean columnExists = checkColumnExists(dbConnection, tableName, columnName);
if (!columnExists) {
addColumnTable(dbConnection, tableName, columnName, ADD_TABLE_COLUMNS.get(columnName));
int sleeping = 500;
System.out.println("... sleeping " + sleeping + " ...");
Thread.sleep(sleeping);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
System.out.println("\n\n### END ADD COLUMNS");
System.out.println("\n\n### DROPPING COLUMNS...\n");
// REMOVE OLD COLUMNS
for (String columnName : DROP_TABLE_COLUMNS.keySet()) {
try {
boolean columnExists = checkColumnExists(dbConnection, tableName, columnName);
if (columnExists) {
dropColumnTable(dbConnection, tableName, columnName, DROP_TABLE_COLUMNS.get(columnName));
int sleeping = 1000;
System.out.println("... sleeping " + sleeping + " ...");
Thread.sleep(sleeping);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
System.out.println("\n\n### END DROP COLUMNS");
System.out.println("\n\n### RENAMING COLUMNS...\n");
// RENAME_TABLE_COLUMNS
for (String columnName : RENAME_TABLE_COLUMNS.keySet()) {
try {
boolean columnExists = checkColumnExists(dbConnection, tableName, columnName);
if (columnExists) {
renameColumnTable(dbConnection, tableName, columnName,
RENAME_TABLE_COLUMNS.get(columnName));
int sleeping = 1000;
System.out.println("... sleeping " + sleeping + " ...");
Thread.sleep(sleeping);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
System.out.println("\n\n### END RENAME COLUMNS");
System.out.println("\n\n# UPDATED TABLE");
printTableColumn(dbConnection, tableName);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("\n\nSCRIPT END at: " + endTime);
double toSec = (endTime - startTime) / 1000;
System.out.println("SCRIPT TERMINATED in " + toSec + "sec");
}
public static void printTableColumn(Connection con, String tablename) {
try {
DatabaseMetaData databaseMetaData = con.getMetaData();
// Print TABLE_TYPE "TABLE"
ResultSet columns = databaseMetaData.getColumns(null, null, tablename, null);
System.out.println("\n==== TABLE " + tablename + " ");
System.out.println("[COLUMN_NAME - TYPE_NAME ( COLUMN_SIZE )]");
System.out.println("-----------------------------------------------");
while (columns.next()) {
String columnName = columns.getString("COLUMN_NAME");
String columnType = columns.getString("TYPE_NAME");
int columnSize = columns.getInt("COLUMN_SIZE");
System.out.println("\t" + columnName + " - " + columnType + " (" + columnSize + ")");
}
System.out.println("-----------------------------------------------");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Gets the database connection.
*
* @param dbURL the db url
* @param user the user
* @param pwd the pwd
* @return the database connection
*/
public static Connection getDatabaseConnection(String dbURL, String user, String pwd) {
System.out.println("dbURL: " + dbURL);
Connection c = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection(dbURL, user, pwd);
c.setAutoCommit(false);
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
return c;
}
public static void addColumnTable(Connection con, String tableName, String newColumn, String type)
throws SQLException {
String alterTableString = String.format("ALTER TABLE %s ADD COLUMN %s %s", tableName, newColumn, type);
System.out.println("Executing: " + alterTableString);
try {
con.setAutoCommit(false);
if (!READ_ONLY_MODE) {
con.createStatement().execute(alterTableString);
con.commit();
}
} catch (SQLException e) {
e.printStackTrace();
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.rollback();
} catch (SQLException excep) {
e.printStackTrace();
}
}
}
System.out.println("Executed: " + alterTableString);
}
public static void renameColumnTable(Connection con, String tableName, String oldColumn, String newColumn)
throws SQLException {
String alterTableString = String.format("ALTER TABLE %s RENAME COLUMN %s TO %s", tableName, oldColumn,
newColumn);
System.out.println("Executing: " + alterTableString);
try {
con.setAutoCommit(false);
if (!READ_ONLY_MODE) {
con.createStatement().execute(alterTableString);
con.commit();
}
} catch (SQLException e) {
e.printStackTrace();
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.rollback();
} catch (SQLException excep) {
e.printStackTrace();
}
}
}
System.out.println("Executed: " + alterTableString);
}
public static void dropColumnTable(Connection con, String tableName, String oldColumn, String type)
throws SQLException {
String alterTableString = String.format("ALTER TABLE %s DROP COLUMN %s", tableName, oldColumn);
System.out.println("Executing: " + alterTableString);
try {
con.setAutoCommit(false);
if (!READ_ONLY_MODE) {
con.createStatement().execute(alterTableString);
con.commit();
}
} catch (SQLException e) {
e.printStackTrace();
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.rollback();
} catch (SQLException excep) {
e.printStackTrace();
}
}
}
System.out.println("Executed: " + alterTableString);
}
public static Boolean checkColumnExists(Connection con, String tableName, String columnName) throws SQLException {
String columnExistsLabel = "COLUMN_EXISTS";
String sql = String.format(
"SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='%s' AND column_name='%s') as %s",
tableName, columnName, columnExistsLabel);
System.out.println("\n+++ " + columnExistsLabel + " checking '" + tableName + "': " + sql);
PreparedStatement p;
ResultSet resultSet;
try {
p = con.prepareStatement(sql);
resultSet = p.executeQuery();
// Expected 1 row
resultSet.next();
Boolean columnExists = resultSet.getBoolean(columnExistsLabel);
System.out.println("\t RESP --> Column '" + columnName + "' exists: " + columnExists + "\n");
return columnExists;
} catch (SQLException e) {
System.err.println(e);
return false;
}
}
public static Boolean checkTableExists(Connection con, String tableName) throws SQLException {
String tableExistsLabel = "TABLE_EXISTS";
String sql = String.format(
"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='%s' AND table_type LIKE 'BASE TABLE' AND table_schema LIKE 'public') as %s",
tableName, tableExistsLabel);
System.out.println("\n+++ " + tableExistsLabel + " checking '" + tableName + "': " + sql);
PreparedStatement p;
ResultSet resultSet;
try {
p = con.prepareStatement(sql);
resultSet = p.executeQuery();
// Expected 1 row
resultSet.next();
Boolean tableExists = resultSet.getBoolean(tableExistsLabel);
System.out.println("\t RESP --> Table '" + tableName + "' exists: " + tableExists + "\n");
return tableExists;
} catch (SQLException e) {
System.err.println(e);
return false;
}
}
public static void updateTable(Connection con, String tableName, String whereCondition,
LinkedHashMap<String, String> mapColumnValue) throws SQLException {
StringBuilder updateSQL = new StringBuilder();
updateSQL.append("UPDATE " + tableName + " SET");
int i = 1;
for (String column : mapColumnValue.keySet()) {
updateSQL.append(" " + column + "=?");
if (i < mapColumnValue.size())
updateSQL.append(", ");
i++;
}
updateSQL.append(" WHERE " + whereCondition);
String update = updateSQL.toString();
// System.out.println("Executing: " + update);
try (PreparedStatement updatePS = con.prepareStatement(update);) {
int index = 1;
for (String column : mapColumnValue.keySet()) {
updatePS.setString(index, mapColumnValue.get(column));
index++;
}
con.setAutoCommit(false);
System.out.println("Executing: " + updatePS.toString());
if (!READ_ONLY_MODE) {
updatePS.executeUpdate();
con.commit();
}
} catch (SQLException e) {
e.printStackTrace();
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.rollback();
} catch (SQLException excep) {
e.printStackTrace();
}
}
}
}
// Step1: Main driver method
public static List<String> readTableIDs(Connection con, String tableName, String columnNameProjectId) {
PreparedStatement p = null;
ResultSet resultSet = null;
// Try block to catch exception/s
List<String> listProjectIds = new ArrayList<String>();
try {
// SQL command data stored in String datatype
String sql = String.format("SELECT * FROM %s", tableName);
p = con.prepareStatement(sql);
resultSet = p.executeQuery();
ResultSetMetaData rsmd = resultSet.getMetaData();
int columnsNumber = rsmd.getColumnCount();
int row = 1;
System.out.println("\n####TABLE: " + tableName + " content..\n");
while (resultSet.next()) {
System.out.print("" + row + "] ");
for (int i = 1; i <= columnsNumber; i++) {
String columnValue = resultSet.getString(i);
System.out.print("(" + rsmd.getColumnName(i) + ") " + columnValue + " | ");
}
row++;
System.out.println("\n");
String projectId = resultSet.getString(columnNameProjectId);
listProjectIds.add(projectId);
}
System.out.println("####TABLE: " + tableName + " end content\n");
}
// Catch block to handle exception
catch (SQLException e) {
// Print exception pop-up on screen
System.err.println(e);
}
System.out.println("returning list IDs: " + listProjectIds);
return listProjectIds;
}
}

View File

@ -1,8 +1,6 @@
package org.gcube.application;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
@ -13,7 +11,6 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@ -38,6 +35,7 @@ import org.gcube.application.geoportalcommon.shared.geoportal.project.PhaseDV;
import org.gcube.application.geoportalcommon.shared.geoportal.project.ProjectDV;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.junit.Before;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@ -63,7 +61,7 @@ public class Project_Tests {
private static ProjectsCaller clientPrj = null;
private static String PROFILE_ID = "profiledConcessioni";
private static String PROJECT_ID = "644a66e944aad51c80409a3b";
private static String PROJECT_ID = "6384aaac308f5c28c5ee0888";
private static String MY_LOGIN = "francesco.mangiacrapa";
@ -77,32 +75,11 @@ public class Project_Tests {
}
/**
* Read context settings.
*/
public static void readContextSettings() {
try (InputStream input = new FileInputStream(gcube_config_path)) {
Properties prop = new Properties();
// load a properties file
prop.load(input);
CONTEXT = prop.getProperty("CONTEXT");
TOKEN = prop.getProperty("TOKEN");
// get the property value and print it out
System.out.println("CONTEXT: " + CONTEXT);
System.out.println("TOKEN: " + TOKEN);
} catch (IOException ex) {
ex.printStackTrace();
}
}
// @Before
@Before
public void init() {
readContextSettings();
ContextConfigTest.readContextSettings();
CONTEXT = ContextConfigTest.CONTEXT;
TOKEN = ContextConfigTest.TOKEN;
ScopeProvider.instance.set(CONTEXT);
SecurityTokenProvider.instance.set(TOKEN);
clientPrj = GeoportalClientCaller.projects();
@ -119,7 +96,7 @@ public class Project_Tests {
}
}
// @Test
//@Test
public void getByID() throws Exception {
Project project = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID);
ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true);
@ -132,7 +109,9 @@ public class Project_Tests {
System.out.println("The key: " + key + " has value: " + theValue);
}
System.out.println(projectDV.getSpatialReference());
System.out.println("JSON: " + projectDV.getTheDocument().getDocumentAsJSON());
System.out.println("Spatial reference: " + projectDV.getSpatialReference());
}
// @Test
@ -172,8 +151,68 @@ public class Project_Tests {
e.printStackTrace();
}
}
//@Test
public void getListProjectsDVFilteredJSONDocument() throws Exception {
// List<Project> listOfProjects = client.getListForProfileID(PROFILE_ID);
// @Test
SearchingFilter filter = new SearchingFilter();
// Where Clause
List<WhereClause> conditions = new ArrayList<WhereClause>();
Map<String, Object> searchInto = new HashMap<String, Object>();
//searchInto.put("_id", "6384aaac308f5c28c5ee0888");
searchInto.put("_theDocument.nome", "della Civita di Tarquinia ");
WhereClause whereClause = new WhereClause(LOGICAL_OP.OR, searchInto);
conditions.add(whereClause);
filter.setConditions(conditions);
/* ORDER BY */
// List<ItemFieldDV> orderByFields = new ArrayList<ItemFieldDV>();
// List<String> jsonFields = Arrays.asList("_theDocument.dataInizioProgetto");
// ItemFieldDV itemField = new ItemFieldDV("dataInizioProgetto", jsonFields, "$or", false, false, false);
// orderByFields.add(itemField);
// filter.setOrderByFields(orderByFields);
LinkedHashMap<String, Object> projection = new LinkedHashMap<String, Object>();
// default
// PROJECTION
projection.put(Project.ID, 1);
// projection.put("_theDocument.nome", 1);
// projection.put("_profileID", 1);
// projection.put("_profileVersion", 1);
// projection.put("_version", 1);
// projection.put("_theDocument", 1);
//
// projection.put("_theDocument.paroleChiaveLibere", 1);
// projection.put("_theDocument.editore", 1);
// projection.put("_theDocument.paroleChiaveICCD", 1);
// projection.put("_theDocument.responsabile", 1);
//
// projection.put("_theDocument.introduzione", 1);
// projection.put("_theDocument.authors", 1);
// projection.put("_theDocument.dataInizioProgetto", 1);
//filter.setProjection(projection);
Integer totalDocs = clientPrj.getTotalDocument(PROFILE_ID);
Iterator<Project> projects = clientPrj.queryOnMongo(PROFILE_ID, 10, 0, null, filter);
//Iterable<Project> itP = () -> projects;
//Stream<Project> targetStream = StreamSupport.stream(itP.spliterator(), false);
//List<String> listProjectIDs = targetStream.map(Project::getId).collect(Collectors.toList());
List<ResultDocumentDV> results = ConvertToDataValueObjectModel.toListResultDocument(projects);
int i = 0;
for (ResultDocumentDV projectDV : results) {
System.out.println(++i + ") " + projectDV.getId() + " JSON: "
+ projectDV.getDocumentAsJSON());
}
}
//@Test
public void getListProjectsDVFiltered() throws Exception {
// List<Project> listOfProjects = client.getListForProfileID(PROFILE_ID);
@ -314,6 +353,16 @@ public class Project_Tests {
}
// @Test
public void getCountByPhase() throws Exception {
Integer integer = clientPrj.getCountByPhaseFor(PROFILE_ID, "Published", "OK");
System.out.println(integer);
integer = clientPrj.getCountByPhaseFor(PROFILE_ID, "Pending Approval", "OK");
System.out.println(integer);
}
// @Test
public void getRelationshipsChain() throws Exception {
System.out.println("getRelationshipsChain test");

View File

@ -32,7 +32,6 @@ import org.gcube.common.scope.api.ScopeProvider;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider;