Added test classes

This commit is contained in:
Francesco Mangiacrapa 2023-03-17 16:41:33 +01:00
parent 4e19c5cdd0
commit 026fd35fd6
6 changed files with 905 additions and 64 deletions

View File

@ -120,6 +120,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -4,6 +4,8 @@ import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
@ -13,6 +15,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.bson.Document;
import org.gcube.application.geoportal.common.model.configuration.Configuration;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
@ -41,19 +44,37 @@ import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Project_Tests {
private ProjectsCaller client = null;
private static ProjectsCaller client = null;
// private static String CONTEXT = "/pred4s/preprod/preVRE";
// private static String TOKEN = ""; //preVRE
//private static String CONTEXT = "/gcube/devsec/devVRE";
//private static String TOKEN = ""; // devVRE
private static String PROFILE_ID = "profiledConcessioni";
private static String PROJECT_ID = "6365485fa8d67f4c82794cc4";
// private static String CONTEXT = "/gcube/devsec/devVRE";
// private static String TOKEN = "c41a00c0-7897-48d2-a67a-05190d6ce5e6-98187548"; // devVRE
// private static String PROFILE_ID = "concessioni-estere";
// private static String PROJECT_ID = "6399de68a0a4545420373257";
private static String CONTEXT = "/pred4s/preprod/preVRE";
private static String TOKEN = ""; //preVRE
private static String TOKEN = "5c3c7551-870d-4259-bd77-a7e603fe4949-980114272"; // preVRE
private static String PROFILE_ID = "profiledConcessioni";
private static String PROJECT_ID = "638488d4308f5c28c5ee074d";
// private static String CONTEXT = "/pred4s/preprod/preVRE";
// private static String TOKEN = ""; // preVRE
private static final String FOLLOWS = "follows";
private static final String PRECEDES = "precedes";
protected static class Phases {
public static final String PENDING_APPROVAL = "Pending Approval";
public static final String PUBLISHED = "Published";
public static final String UNPUBLISHED = "UnPublished";
}
@Before
public void getClient() {
@ -73,7 +94,7 @@ public class Project_Tests {
}
}
//@Test
// @Test
public void getByID() throws Exception {
Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID);
ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true);
@ -85,11 +106,11 @@ public class Project_Tests {
Object theValue = theMap.get(key);
System.out.println("The key: " + key + " has value: " + theValue);
}
System.out.println(projectDV.getSpatialReference());
}
//@Test
// @Test
public void getListProjectsDV() throws Exception {
List<Project> listOfProjects = client.getListForProfileID(PROFILE_ID);
ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true);
@ -101,7 +122,7 @@ public class Project_Tests {
}
//@Test
// @Test
public void getResultDocumentForID() {
try {
ProjectsCaller projects = GeoportalClientCaller.projects();
@ -114,11 +135,10 @@ public class Project_Tests {
}
}
//@Test
// @Test
public void getListProjectsDVFiltered() throws Exception {
// List<Project> listOfProjects = client.getListForProfileID(PROFILE_ID);
SearchingFilter filter = new SearchingFilter();
// Where Clause
@ -131,13 +151,13 @@ public class Project_Tests {
// searchInto.put("_theDocument.editore", "scavo");
// searchInto.put("_theDocument.paroleChiaveICCD", "scavo");
// searchInto.put("_theDocument.responsabile", "scavo");
searchInto.put("_theDocument.dataInizioProgetto", "2019");
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");
@ -147,7 +167,7 @@ public class Project_Tests {
LinkedHashMap<String, Object> projection = new LinkedHashMap<String, Object>();
// default
//PROJECTION
// PROJECTION
projection.put(Project.ID, 1);
// projection.put("_theDocument.nome", 1);
// projection.put("_profileID", 1);
@ -171,14 +191,13 @@ public class Project_Tests {
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() + " dataInizioProgetto: "+projectDV.getDocumentAsMap().get("dataInizioProgetto"));
System.out.println(++i + ") " + projectDV.getId() + " dataInizioProgetto: "
+ projectDV.getDocumentAsMap().get("dataInizioProgetto"));
}
// TEST TO PROJECT DV
/*
@ -190,46 +209,37 @@ public class Project_Tests {
* for (ProjectDV projectDV : listProjects) { System.out.println(++i + ") " +
* projectDV); }
*/
/*int limit = 2;
LinkedHashMap<String, Object> documentAsMap = new LinkedHashMap<String, Object>(limit);
try {
// Project theProject =
// GeoportalClientCaller.projects().getProjectByID(profileID, projectID);
QueryRequest request = new QueryRequest();
request.setFilter(Document.parse("{\"" + Project.ID + "\" : " + "{\"$eq\" : \"" + PROJECT_ID + "\"}}"));
request.setProjection(Document.parse("{\"" + Project.ID + "\" : " + "1}"));
// should be one
projects(PROFILE_ID).build().f
projects(PROFILE_ID).build().query(request).forEachRemaining(p -> {
try {
System.out.println("p is: "+p);
Iterator<Entry<String, Object>> entrySetsIt = p.getTheDocument().entrySet().iterator();
int i = 0;
while (entrySetsIt.hasNext()) {
if (i > limit)
break;
Entry<String, Object> entry = entrySetsIt.next();
documentAsMap.put(entry.getKey(), entry.getValue());
i++;
}
} catch (Exception e) {
throw e;
}
});
System.out.println("getEntrySetsDocumentForProjectID returning map: "+documentAsMap);
} catch (Exception e) {
String erroMsg = "Error occurred on loading EntrySets document for profileID " + PROFILE_ID
+ " and projectID " + PROJECT_ID;
e.printStackTrace();
}*/
/*
* int limit = 2; LinkedHashMap<String, Object> documentAsMap = new
* LinkedHashMap<String, Object>(limit);
*
* try { // Project theProject = //
* GeoportalClientCaller.projects().getProjectByID(profileID, projectID);
*
* QueryRequest request = new QueryRequest();
* request.setFilter(Document.parse("{\"" + Project.ID + "\" : " +
* "{\"$eq\" : \"" + PROJECT_ID + "\"}}"));
* request.setProjection(Document.parse("{\"" + Project.ID + "\" : " + "1}"));
* // should be one projects(PROFILE_ID).build().f
* projects(PROFILE_ID).build().query(request).forEachRemaining(p -> { try {
* System.out.println("p is: "+p); Iterator<Entry<String, Object>> entrySetsIt =
* p.getTheDocument().entrySet().iterator(); int i = 0; while
* (entrySetsIt.hasNext()) { if (i > limit) break;
*
* Entry<String, Object> entry = entrySetsIt.next();
* documentAsMap.put(entry.getKey(), entry.getValue()); i++; }
*
* } catch (Exception e) { throw e; } });
*
* System.out.println("getEntrySetsDocumentForProjectID returning map: "
* +documentAsMap);
*
*
* } catch (Exception e) { String erroMsg =
* "Error occurred on loading EntrySets document for profileID " + PROFILE_ID +
* " and projectID " + PROJECT_ID; e.printStackTrace(); }
*/
}
// @Test
@ -266,7 +276,7 @@ public class Project_Tests {
}
//@Test
// @Test
public void getRelationshipsChain() throws Exception {
System.out.println("getRelationshipsChain test");
Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID);
@ -274,10 +284,10 @@ public class Project_Tests {
List<Relationship> relations = project.getRelationships();
for (Relationship relationship : relations) {
System.out.println("\n\ngetRelationshipsChain for "+relationship);
System.out.println("\n\ngetRelationshipsChain for " + relationship);
Iterator<RelationshipNavigationObject> iterator = client.getRelationshipChain(PROFILE_ID, PROJECT_ID,
relationship.getRelationshipName(), true);
while (iterator.hasNext()) {
RelationshipNavigationObject nav = (RelationshipNavigationObject) iterator.next();
visitRelationshipsChain(nav, relationship.getRelationshipName());
@ -287,9 +297,10 @@ public class Project_Tests {
}
public void visitRelationshipsChain(RelationshipNavigationObject nav, String relationshipName) {
System.out.println("visitRelationshipsChain of target: "+nav.getTarget().getId() +", relationshipName: "+relationshipName+", doc: "+nav.getTarget().getTheDocument());
System.out.println("visitRelationshipsChain children "+nav.getChildren());
System.out.println("visitRelationshipsChain of target: " + nav.getTarget().getId() + ", relationshipName: "
+ relationshipName + ", doc: " + nav.getTarget().getTheDocument());
System.out.println("visitRelationshipsChain children " + nav.getChildren());
if (nav == null)
return;
@ -359,4 +370,219 @@ public class Project_Tests {
return LocalDate.parse(p.getValueAsString(), FULL_FORMATTER);
}
}
@Test
public void testEvaluateRelationshipsChain() throws Exception {
System.out.println("testEvaluateRelationshipsChain test");
// PROJECT_ID = "6399de3ca0a4545420373251";
//
// PROJECT_ID = "6399de68a0a4545420373257";
//
//
// PROJECT_ID = "6399de96a0a4545420373258"; //no Relation
//
PROJECT_ID = "63c80aebacb8c7657b858741";
Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID);
evaluateAdditionalIndexParameters(project);
}
public static Document evaluateAdditionalIndexParameters(Project project) throws Exception {
Document toReturn = null;
if (toReturn == null)
toReturn = new Document();
Project indexingProject = project;
// Evaluate to display project IDs
log.debug("Evaluating Last ID in relationship chain. Current Concessione ID is {}", indexingProject.getId());
try {
ArrayList<Project> projects = new ArrayList<>();
// get Last ID in relation chain
projects.add(indexingProject);
if (!indexingProject.getRelationshipsByName(PRECEDES).isEmpty()) {
List<RelationshipNavigationObject> relChain = getRelationshipChain(indexingProject.getProfileID(),
indexingProject, PRECEDES, true);
scanRelation(projects, relChain.get(0), false);
}
if (!indexingProject.getRelationshipsByName(FOLLOWS).isEmpty()) {
List<RelationshipNavigationObject> relChain = getRelationshipChain(indexingProject.getProfileID(),
indexingProject, FOLLOWS, true);
scanRelation(projects, relChain.get(0), false);
}
log.debug("Produced full chain [size : {}] from {}, evaluating last available for PHASE {} ",
projects.size(), indexingProject.getId(), indexingProject.getLifecycleInformation().getPhase());
List<String> toDisplayId = new ArrayList<>();
List<String> toHideIds = new ArrayList<>();
log.info("projects are: " + projects.size());
// Reverse Order means from the last FOLLOW to the first one (temporal reverse
// order)
Collections.sort(projects, Collections.reverseOrder(new ProjectRelationComparator()));
int j = 0;
for (Project theProject : projects) {
log.info(++j + ") " + theProject.getId() + " data inizio: "
+ theProject.getTheDocument().get("dataInizioProgetto"));
}
log.trace("Checking from LAST.. ");
for (int i = 0; i < projects.size(); i++) {
Project p = projects.get(i);
String phase = p.getLifecycleInformation().getPhase();
// IS TO DISPLAY EMPTY? Step into only once
if (toDisplayId.isEmpty()) {
// IF PHASE IS PENDING APPROVAL OR PUBLISHED
if ((phase.equals(Phases.PENDING_APPROVAL) || phase.equals(Phases.PUBLISHED))) {
toDisplayId.add(p.getId());
}
} else {
switch (phase) {
case Phases.PENDING_APPROVAL:
case Phases.PUBLISHED: {
if ((p.getLifecycleInformation().getPhase().equals(Phases.PENDING_APPROVAL)
|| p.getLifecycleInformation().getPhase().equals(Phases.PUBLISHED)))
toHideIds.add(p.getId());
break;
}
}
}
}
toReturn.put("_toHideIds", toHideIds);
toReturn.put("_toDisplayIds", toDisplayId);
log.info("Indexing request for Concessione [ID {}] with to HIDE {} and toDisplay {} ",
indexingProject.getId(), toHideIds, toDisplayId);
return toReturn;
} catch (Exception e) {
log.error("Unable to evaluate to Hide and Display Ids ", e);
throw new Exception("Unable to evaluate chain ids to hide / display", e);
}
}
public static class ProjectRelationComparator implements Comparator<Project> {
@Override
public int compare(Project p1, Project p2) {
if (p1 == null)
return 1;
if (p2 == null)
return -1;
Integer compareResult = null;
compareResult = compareProjectAgainstRelations(p1, p2);
log.trace("p1 & p2, comparator result: {}", compareResult);
if (compareResult == null) {
log.debug("No relations beetween p1 & p2, checking inverted relations");
compareResult = compareProjectAgainstRelations(p2, p1);
log.trace("p2 & p1, comparator result: {}", compareResult);
if (compareResult == null) {
log.trace("p1 & p2, are not comparable, returning 0");
compareResult = 0;
}
}
log.debug("p1 & p2, comparator result, returns: {}", compareResult);
return compareResult;
}
}
public static Integer compareProjectAgainstRelations(Project source, Project target) {
log.debug("comparing source {} and target {}", source.getId(), target.getId());
List<Relationship> listRel = source.getRelationships();
log.debug("relationships of {} are : {}", source.getId(), listRel);
if (listRel != null) {
String targetId = target.getId();
for (Relationship relationship : listRel) {
String relTargetId = relationship.getTargetID();
if (targetId.compareTo(relTargetId) == 0) {
String relationName = relationship.getRelationshipName();
if (relationName.equals(PRECEDES)) {
log.debug("source {} [rel {} ] target {}, so source < target ", source.getId(), PRECEDES,
target.getId());
// source < target
return -1;
} else if (relationName.equals(FOLLOWS)) {
log.debug("source {} [rel {} ] target {}, so source > target ", source.getId(), FOLLOWS,
target.getId());
// source > target
return 1;
}
}
}
}
log.debug("No relations beetween a & b");
return null;
}
private static void scanRelation(ArrayList chain, RelationshipNavigationObject obj, boolean putBefore) {
if (putBefore)
chain.add(0, obj.getTarget());
else
chain.add(obj.getTarget());
if (obj.getChildren() != null)
obj.getChildren().forEach(r -> scanRelation(chain, r, putBefore));
}
public static List<RelationshipNavigationObject> getRelationshipChain(String ucid, Project current,
String relationshipId, Boolean deep) throws Exception {
// recursive
// ProfiledMongoManager manager = new ProfiledMongoManager(ucid);
log.info("UCD {} : Getting Relationships List for {} [rel : {}, recurse {}]", ucid, current.getId(),
relationshipId, deep);
// Project current = manager.getByID(id);
long startTime = System.currentTimeMillis();
List<RelationshipNavigationObject> toReturn = getLinked(current, relationshipId, deep);
log.info("Got {} relationship elements in {}ms", toReturn.size(), (System.currentTimeMillis() - startTime));
return toReturn;
}
private static List<RelationshipNavigationObject> getLinked(Project current, String relationName, Boolean recurse) {
log.debug("Getting Relationships Lists for {} [rel : {}, recurse {}]", current.getId(), relationName, recurse);
ArrayList<RelationshipNavigationObject> toReturn = new ArrayList<>();
List<Relationship> existing = current.getRelationshipsByName(relationName);
for (Relationship relationship : existing) {
try {
log.debug("Navigating from {} : {} to[rel {} ] {} : {}", relationship.getTargetUCD(),
relationship.getTargetID(), relationship.getRelationshipName(), current.getProfileID(),
current.getId());
RelationshipNavigationObject linkedProject = new RelationshipNavigationObject();
// linkedProject.setTarget(new
// ProfiledMongoManager(relationship.getTargetUCD()).getByID(relationship.getTargetID()));
// Project target = new Project();
// target.setProfileID(relationship.getTargetUCD());
// target.setId(relationship.getTargetID());
Project target = client.getProjectByID(relationship.getTargetUCD(), relationship.getTargetID());
linkedProject.setTarget(target);
if (recurse) {
List<RelationshipNavigationObject> linked = getLinked(linkedProject.getTarget(), relationName,
recurse);
linkedProject.setChildren(linked);
}
toReturn.add(linkedProject);
} catch (Exception e) {
log.warn("Unable to navigate from {} : {} to[rel {} ] {} : {}", relationship.getTargetUCD(),
relationship.getTargetID(), relationship.getRelationshipName(), current.getProfileID(),
current.getId(), e);
}
}
return toReturn;
}
}

View File

@ -0,0 +1,355 @@
package org.gcube.application;
import java.sql.Connection;
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.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportalcommon.ConvertToDataValueObjectModel;
import org.gcube.application.geoportalcommon.ProjectDVBuilder;
import org.gcube.application.geoportalcommon.geoportal.GeoportalClientCaller;
import org.gcube.application.geoportalcommon.geoportal.ProjectsCaller;
import org.gcube.application.geoportalcommon.shared.SearchingFilter;
import org.gcube.application.geoportalcommon.shared.SearchingFilter.LOGICAL_OP;
import org.gcube.application.geoportalcommon.shared.WhereClause;
import org.gcube.application.geoportalcommon.shared.geoportal.ResultDocumentDV;
import org.gcube.application.geoportalcommon.shared.geoportal.project.ProjectDV;
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 ReSyncCentroidsMetadataOnDB {
private static final String CONTEXT = "/gcube/devsec/devVRE";
private static final String TOKEN = ""; // devVRE
// GEOPORTAL
private static final String PROFILE_ID = "profiledConcessioni";
private static final String JSON_KEY_DATA_FINE_PROGETTO = "dataFineProgetto";
private static final String JSON_KEY_DATA_INIZIO_PROGETTO = "dataInizioProgetto";
private static final String JSON_KEY_INTRODUZIONE = "introduzione";
// DB
private static final String platformName = "postgis";
private static final String category = "Database";
private static final String resourceName = "GNA-POSTGIS-DB";
private static final String TABLE_COLUMN_DATA_FINE_PROGETTO = "data_fine_progetto";
private static final String TABLE_COLUMN_DATA_INIZIO_PROGETTO = "data_inizio_progetto";
private static final String TABLE_COLUMN_DESCRIZIONE = "descrizione";
private static ProjectsCaller client = null;
/**
* Gets the client.
*
* @return the client
*/
// @Before
public static void initGeoportalClient() {
// assumeTrue(GCubeTest.isTestInfrastructureEnabled());
ScopeProvider.instance.set(CONTEXT);
SecurityTokenProvider.instance.set(TOKEN);
client = GeoportalClientCaller.projects();
}
public static void main(String[] args) {
initGeoportalClient();
String tableName = "profiledconcessioni_internal__devvre_centroids";
tableName = "profiledconcessioni_devvre_centroids";
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);
// MUST BE DONE JUST ONCE
try {
// renameColumnTable(dbConnection, tableName, "anno", TABLE_COLUMN_DATA_INIZIO_PROGETTO);
// renameColumnTable(dbConnection, tableName, "date_scavo", TABLE_COLUMN_DATA_FINE_PROGETTO);
// addColumnTable(dbConnection, tableName, "gna_url", "TEXT"); //Could be a
// VARCHAR(2048). Is it manageable by service?
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
List<String> listProjectIdsIntoDB = readTableIDs(dbConnection, tableName, "projectid");
ScopeProvider.instance.set(CONTEXT);
SecurityTokenProvider.instance.set(TOKEN);
// List<ResultDocumentDV> listPublishedProjectsIntoService =
// getListProjectsDVFiltered();
for (String projectId : listProjectIdsIntoDB) {
Project project = null;
try {
project = client.getProjectByID(PROFILE_ID, projectId);
} catch (Exception e) {
System.err.println("Project id not found: " + projectId);
}
try {
if (project == null)
continue;
System.out.println("\n###Trying to update Centroid TABLE for project id: " + projectId);
ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true);
ProjectDV projectDV = ConvertToDataValueObjectModel.toProjectDV(project, projectBuilder);
LinkedHashMap<String, Object> projectMap = projectDV.getTheDocument().getDocumentAsMap();
LinkedHashMap<String, String> hashMap = new LinkedHashMap<String, String>();
hashMap.put(TABLE_COLUMN_DATA_INIZIO_PROGETTO, projectMap.get(JSON_KEY_DATA_INIZIO_PROGETTO) + "");
hashMap.put(TABLE_COLUMN_DATA_FINE_PROGETTO, projectMap.get(JSON_KEY_DATA_FINE_PROGETTO) + "");
hashMap.put(TABLE_COLUMN_DESCRIZIONE, projectMap.get(JSON_KEY_INTRODUZIONE) + "");
updateTable(dbConnection, tableName, "projectId='" + projectId + "'", hashMap);
System.out.println("###UPDATED project id: " + projectId);
} catch (Exception e) {
System.err.println("Error on updating the table for the project id: " + projectId);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static List<ResultDocumentDV> getListProjectsDVFiltered() throws Exception {
// List<Project> listOfProjects = client.getListForProfileID(PROFILE_ID);
SearchingFilter filter = new SearchingFilter();
// Where Clause
List<WhereClause> conditions = new ArrayList<WhereClause>();
Map<String, Object> searchInto = new HashMap<String, Object>();
searchInto.put("_lifecycleInformation._phase", "Published");
WhereClause whereClause = new WhereClause(LOGICAL_OP.OR, searchInto);
conditions.add(whereClause);
filter.setConditions(conditions);
LinkedHashMap<String, Object> projection = new LinkedHashMap<String, Object>();
// default
// PROJECTION
String theDoc = Project.THE_DOCUMENT + ".";
projection.put(Project.ID, 1);
projection.put(theDoc + "nome", 1);
projection.put(theDoc + JSON_KEY_DATA_INIZIO_PROGETTO, 1);
projection.put(theDoc + JSON_KEY_DATA_FINE_PROGETTO, 1);
filter.setProjection(projection);
Integer totalDocs = client.getTotalDocument(PROFILE_ID);
Iterator<Project> projects = client.queryOnMongo(PROFILE_ID, totalDocs, 0, null, filter);
List<ResultDocumentDV> results = ConvertToDataValueObjectModel.toListResultDocument(projects);
int i = 0;
for (ResultDocumentDV projectDV : results) {
System.out.println(++i + ") " + projectDV.getId() + " " + JSON_KEY_DATA_INIZIO_PROGETTO + ": "
+ projectDV.getDocumentAsMap().get(JSON_KEY_DATA_INIZIO_PROGETTO) + " "
+ JSON_KEY_DATA_FINE_PROGETTO + ": " + projectDV.getDocumentAsMap().get(JSON_KEY_DATA_FINE_PROGETTO)
+ " " + JSON_KEY_INTRODUZIONE + ": " + projectDV.getDocumentAsMap().get(JSON_KEY_INTRODUZIONE));
}
return results;
}
/**
* 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 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);
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 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);
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 updateTable(Connection con, String tableName, String whereCondition,
LinkedHashMap<String, String> mapColumnValue) throws SQLException {
// String alterTableString = String.format("UPDATE TABLE %s SET COLUMN %s TO %s", tableName, oldColumn,
// newColumn);
//
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());
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,181 @@
package org.gcube.application.se;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.ArrayList;
import java.util.List;
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class RuntimeResourceReader.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Mar 10, 2017
*/
public class RuntimeResourceReader {
public static final Logger logger = LoggerFactory.getLogger(RuntimeResourceReader.class);
private List<ServiceEndpointBean> listSE = new ArrayList<ServiceEndpointBean>();
/**
* Instantiates a new runtime resource reader.
*
* @param scope the scope
* @param resourceName the resource name
* @param platformName the platform name
* @param category the category
* @param endPoint the end point
* @throws Exception the exception
*/
public RuntimeResourceReader(String scope, String resourceName, String platformName, String category,
String endPoint) throws Exception {
read(scope, resourceName, platformName, category, endPoint);
}
/**
* Read.
*
* @param scope the scope
* @param platformName the platform name
* @param endPoint the end point
* @return the server parameters
* @throws Exception the exception
*/
private List<ServiceEndpointBean> read(String scope, String resourceName, String platformName, String category,
String endPoint) throws Exception {
String originalScope = null;
try {
originalScope = ScopeProvider.instance.get();
ScopeProvider.instance.set(scope);
SimpleQuery query = queryFor(ServiceEndpoint.class);
if (resourceName != null)
query.addCondition("$resource/Profile/Name/text() eq '" + resourceName + "'");
query.addCondition("$resource/Profile/Platform/Name/text() eq '" + platformName + "'");
query.addCondition("$resource/Profile/Category/text() eq '" + category + "'");
if (endPoint != null && !endPoint.isEmpty())
query.addCondition("$resource/Profile/AccessPoint/Interface/Endpoint/text() eq '" + endPoint + "'");
// query.addVariable("$prop", "$resource/Profile/AccessPoint/Properties/Property")
// .addCondition("$prop/Name/text() eq 'priority'")
// .addCondition("$prop/Value/text() eq '1'");
logger.info("GeoRuntimeReader, using scope: " + scope + ", to get resource: " + platformName);
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> listServiceEndpoint = client.submit(query);
if (listServiceEndpoint == null || listServiceEndpoint.isEmpty())
throw new Exception("Cannot retrieve the runtime resource: " + platformName);
for (ServiceEndpoint serviceEndpoint : listServiceEndpoint) {
ServiceEndpointBean seb = new ServiceEndpointBean();
seb.setRuntime(serviceEndpoint.profile().runtime());
List<AccessPoint> listAp = new ArrayList<ServiceEndpoint.AccessPoint>();
try {
for (AccessPoint accessPoint : serviceEndpoint.profile().accessPoints()) {
listAp.add(accessPoint);
}
} catch (Exception e) {
System.err.println("Error on reading Access point not found");
}
seb.setListAP(listAp);
listSE.add(seb);
}
} catch (Exception e) {
logger.error("Sorry, an error occurred on reading parameters in Runtime Resources", e);
} finally {
if (originalScope != null && !originalScope.isEmpty()) {
ScopeProvider.instance.set(originalScope);
logger.info("scope provider setted to orginal scope: " + originalScope);
} else {
ScopeProvider.instance.reset();
logger.info("scope provider reset");
}
}
logger.info("returning list: " + listSE);
return listSE;
}
public List<ServiceEndpointBean> getListSE() {
return listSE;
}
public static String dectryptPassword(String scope, AccessPoint ap) {
ScopeProvider.instance.set(scope);
logger.info("username: " + ap.username());
logger.info("password: " + ap.password());
String decryptedPassword = null;
try {
decryptedPassword = StringEncrypter.getEncrypter().decrypt(ap.password());
logger.info("Decrypted Password: " + decryptedPassword);
} catch (Exception e) {
logger.info("ignoring exception during pwd decrypting");
}
return decryptedPassword;
}
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
String scope = "/gcube/devsec/devVRE";
String platformName = "GeoServer";
// scope = "/pred4s/preprod/preVRE";
RuntimeResourceReader reader;
try {
ScopeProvider.instance.set(scope);
reader = new RuntimeResourceReader(scope, null, "postgis", "Database", null);
for (ServiceEndpointBean seb : reader.getListSE()) {
System.out.println("Found: " + seb);
List<AccessPoint> listAp = seb.getListAP();
for (AccessPoint ap : listAp) {
System.out.println("username: " + ap.username());
System.out.println("password: " + ap.password());
try {
String decryptedPassword = StringEncrypter.getEncrypter().decrypt(ap.password());
System.out.println("Decrypted Password: " + decryptedPassword);
} catch (Exception e) {
System.out.println("ignoring exception during pwd decrypting");
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,60 @@
package org.gcube.application.se;
import java.io.Serializable;
import java.util.List;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
/**
* The Class ServerParameters.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it May 16, 2017
*/
public class ServiceEndpointBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2459971193655529274L;
protected org.gcube.common.resources.gcore.ServiceEndpoint.Runtime runtime;
protected List<AccessPoint> listAP;
public ServiceEndpointBean() {
// TODO Auto-generated constructor stub
}
public ServiceEndpointBean(org.gcube.common.resources.gcore.ServiceEndpoint.Runtime runtime, List<AccessPoint> listAP) {
super();
this.runtime = runtime;
this.listAP = listAP;
}
public org.gcube.common.resources.gcore.ServiceEndpoint.Runtime getRuntime() {
return runtime;
}
public List<AccessPoint> getListAP() {
return listAP;
}
public void setRuntime(org.gcube.common.resources.gcore.ServiceEndpoint.Runtime runtime2) {
this.runtime = runtime2;
}
public void setListAP(List<AccessPoint> listAP) {
this.listAP = listAP;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ServiceEndpoint [runtime=");
builder.append(runtime);
builder.append(", listAP=");
builder.append(listAP);
builder.append("]");
return builder.toString();
}
}

View File

@ -6,3 +6,15 @@
/preprod.gcubekey
/geoportal-config.json
/devVRE_TOKEN.txt
/CNR.it.gcubekey
/D4OS.gcubekey
/D4Research.gcubekey
/ISTI.gcubekey
/OpenAIRE.gcubekey
/ParthenosVO.gcubekey
/SmartArea.gcubekey
/SoBigData.gcubekey
/d4science.research-infrastructures.eu.gcubekey
/FARM.gcubekey
/gCubeApps.gcubekey
/devVRE.gcubekey