From 026fd35fd6a16f40a110b383f2617d9c0c60f2d7 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Fri, 17 Mar 2023 16:41:33 +0100 Subject: [PATCH 01/14] Added test classes --- pom.xml | 7 + .../org/gcube/application/Project_Tests.java | 354 +++++++++++++---- .../ReSyncCentroidsMetadataOnDB.java | 355 ++++++++++++++++++ .../application/se/RuntimeResourceReader.java | 181 +++++++++ .../application/se/ServiceEndpointBean.java | 60 +++ src/test/resources/.gitignore | 12 + 6 files changed, 905 insertions(+), 64 deletions(-) create mode 100644 src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java create mode 100644 src/test/java/org/gcube/application/se/RuntimeResourceReader.java create mode 100644 src/test/java/org/gcube/application/se/ServiceEndpointBean.java diff --git a/pom.xml b/pom.xml index ff9e5ab..fd8e482 100644 --- a/pom.xml +++ b/pom.xml @@ -120,6 +120,13 @@ test + + org.postgresql + postgresql + 9.4.1212 + test + + diff --git a/src/test/java/org/gcube/application/Project_Tests.java b/src/test/java/org/gcube/application/Project_Tests.java index c4b8414..49dcf16 100644 --- a/src/test/java/org/gcube/application/Project_Tests.java +++ b/src/test/java/org/gcube/application/Project_Tests.java @@ -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 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 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 orderByFields = new ArrayList(); // List jsonFields = Arrays.asList("_theDocument.dataInizioProgetto"); @@ -147,7 +167,7 @@ public class Project_Tests { LinkedHashMap projection = new LinkedHashMap(); // 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 itP = () -> projects; Stream targetStream = StreamSupport.stream(itP.spliterator(), false); List listProjectIDs = targetStream.map(Project::getId).collect(Collectors.toList()); - List 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 documentAsMap = new LinkedHashMap(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> entrySetsIt = p.getTheDocument().entrySet().iterator(); - int i = 0; - while (entrySetsIt.hasNext()) { - if (i > limit) - break; - - Entry 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 documentAsMap = new + * LinkedHashMap(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> entrySetsIt = + * p.getTheDocument().entrySet().iterator(); int i = 0; while + * (entrySetsIt.hasNext()) { if (i > limit) break; + * + * Entry 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 relations = project.getRelationships(); for (Relationship relationship : relations) { - System.out.println("\n\ngetRelationshipsChain for "+relationship); + System.out.println("\n\ngetRelationshipsChain for " + relationship); Iterator 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 projects = new ArrayList<>(); + + // get Last ID in relation chain + projects.add(indexingProject); + + if (!indexingProject.getRelationshipsByName(PRECEDES).isEmpty()) { + List relChain = getRelationshipChain(indexingProject.getProfileID(), + indexingProject, PRECEDES, true); + scanRelation(projects, relChain.get(0), false); + } + if (!indexingProject.getRelationshipsByName(FOLLOWS).isEmpty()) { + List 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 toDisplayId = new ArrayList<>(); + List 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 { + @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 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 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 toReturn = getLinked(current, relationshipId, deep); + log.info("Got {} relationship elements in {}ms", toReturn.size(), (System.currentTimeMillis() - startTime)); + return toReturn; + } + + private static List getLinked(Project current, String relationName, Boolean recurse) { + log.debug("Getting Relationships Lists for {} [rel : {}, recurse {}]", current.getId(), relationName, recurse); + ArrayList toReturn = new ArrayList<>(); + List 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 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; + } + } diff --git a/src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java b/src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java new file mode 100644 index 0000000..e70eb19 --- /dev/null +++ b/src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java @@ -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 listProjectIdsIntoDB = readTableIDs(dbConnection, tableName, "projectid"); + ScopeProvider.instance.set(CONTEXT); + SecurityTokenProvider.instance.set(TOKEN); + // List 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 projectMap = projectDV.getTheDocument().getDocumentAsMap(); + + LinkedHashMap hashMap = new LinkedHashMap(); + 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 getListProjectsDVFiltered() throws Exception { + // List listOfProjects = client.getListForProfileID(PROFILE_ID); + + SearchingFilter filter = new SearchingFilter(); + + // Where Clause + List conditions = new ArrayList(); + Map searchInto = new HashMap(); + searchInto.put("_lifecycleInformation._phase", "Published"); + WhereClause whereClause = new WhereClause(LOGICAL_OP.OR, searchInto); + + conditions.add(whereClause); + filter.setConditions(conditions); + + LinkedHashMap projection = new LinkedHashMap(); + // 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 projects = client.queryOnMongo(PROFILE_ID, totalDocs, 0, null, filter); + + List 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 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 readTableIDs(Connection con, String tableName, String columnNameProjectId) { + PreparedStatement p = null; + ResultSet resultSet = null; + + // Try block to catch exception/s + List listProjectIds = new ArrayList(); + 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; + + } + +} diff --git a/src/test/java/org/gcube/application/se/RuntimeResourceReader.java b/src/test/java/org/gcube/application/se/RuntimeResourceReader.java new file mode 100644 index 0000000..72677a0 --- /dev/null +++ b/src/test/java/org/gcube/application/se/RuntimeResourceReader.java @@ -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 listSE = new ArrayList(); + + /** + * 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 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 client = clientFor(ServiceEndpoint.class); + + List 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 listAp = new ArrayList(); + 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 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 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(); + } + + } + +} diff --git a/src/test/java/org/gcube/application/se/ServiceEndpointBean.java b/src/test/java/org/gcube/application/se/ServiceEndpointBean.java new file mode 100644 index 0000000..c0713d0 --- /dev/null +++ b/src/test/java/org/gcube/application/se/ServiceEndpointBean.java @@ -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 listAP; + + public ServiceEndpointBean() { + // TODO Auto-generated constructor stub + } + + public ServiceEndpointBean(org.gcube.common.resources.gcore.ServiceEndpoint.Runtime runtime, List listAP) { + super(); + this.runtime = runtime; + this.listAP = listAP; + } + + public org.gcube.common.resources.gcore.ServiceEndpoint.Runtime getRuntime() { + return runtime; + } + + public List getListAP() { + return listAP; + } + + public void setRuntime(org.gcube.common.resources.gcore.ServiceEndpoint.Runtime runtime2) { + this.runtime = runtime2; + } + + public void setListAP(List 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(); + } + +} diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore index b81672c..289e8e7 100644 --- a/src/test/resources/.gitignore +++ b/src/test/resources/.gitignore @@ -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 -- 2.17.1 From 288756e686166436e78d04010fbfb642958a8832 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 27 Mar 2023 16:41:51 +0200 Subject: [PATCH 02/14] Script completed for resynching Mongo -> PostGis -> Geoserver --- .../ReSyncCentroidsMetadataOnDB.java | 84 +++++++++++++++---- .../application/se/RuntimeResourceReader.java | 12 ++- 2 files changed, 76 insertions(+), 20 deletions(-) diff --git a/src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java b/src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java index e70eb19..cccea47 100644 --- a/src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java +++ b/src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java @@ -31,11 +31,18 @@ import org.gcube.common.scope.api.ScopeProvider; public class ReSyncCentroidsMetadataOnDB { - private static final String CONTEXT = "/gcube/devsec/devVRE"; - private static final String TOKEN = ""; // devVRE +// private static final String CONTEXT = "/gcube/devsec/devVRE"; +// private static final String TOKEN = ""; // devVRE + +// private static final String CONTEXT = "/pred4s/preprod/preVRE"; +// private static final String TOKEN = "5c3c7551-870d-4259-bd77-a7e603fe4949-980114272"; //preVRE + + private static final String CONTEXT = "/d4science.research-infrastructures.eu/D4OS/GNA"; + private static final String TOKEN = ""; //GNA // GEOPORTAL - private static final 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"; private static final String JSON_KEY_INTRODUZIONE = "introduzione"; @@ -43,13 +50,19 @@ public class ReSyncCentroidsMetadataOnDB { // 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 resourceName = "GNA-POSTGIS-DB"; //devVRE + //private static final String resourceName = "Geoserver-t postgis"; //preVRE + private static final String resourceName = "GNA-postgis"; //GNA + + 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; + public static final boolean READ_ONLY_MODE = true; + /** * Gets the client. * @@ -64,13 +77,31 @@ public class ReSyncCentroidsMetadataOnDB { } public static void main(String[] args) { + + System.out.println("READ_ONLY_MODE ENABLED: "+READ_ONLY_MODE); + System.out.println("CONTEXT IS: "+CONTEXT); + System.out.println("PROFILE_ID: "+PROFILE_ID); initGeoportalClient(); + + //preVRE +// String tableName = "profiledconcessioni_internal__prevre_centroids"; +// tableName = "profiledconcessioni_prevre_centroids"; +// tableName = "concessioni_estere_prevre_centroids"; +// tableName = "concessioni_estere_internal__prevre_centroids"; + + + //GNA +// PROFILE_ID = "concessioni-estere"; +// String tableName = "concessioni_estere_internal__gna_centroids"; +// tableName = "concessioni_estere_gna_centroids"; + PROFILE_ID = "profiledConcessioni"; + String tableName = "profiledconcessioni_internal__gna_centroids"; + tableName = "profiledconcessioni_gna_centroids"; - String tableName = "profiledconcessioni_internal__devvre_centroids"; - - tableName = "profiledconcessioni_devvre_centroids"; - + + long startTime = System.currentTimeMillis(); + try { RuntimeResourceReader rrr = new RuntimeResourceReader(CONTEXT, resourceName, platformName, category, null); ServiceEndpointBean se = rrr.getListSE().get(0); @@ -85,8 +116,8 @@ public class ReSyncCentroidsMetadataOnDB { // 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); + 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) { @@ -101,8 +132,10 @@ public class ReSyncCentroidsMetadataOnDB { // List listPublishedProjectsIntoService = // getListProjectsDVFiltered(); + int counter = 1; for (String projectId : listProjectIdsIntoDB) { Project project = null; + System.out.println("\n\n################ "+counter+" of "+listProjectIdsIntoDB.size()+ ") Reading the project id: " + projectId); try { project = client.getProjectByID(PROFILE_ID, projectId); } catch (Exception e) { @@ -113,7 +146,7 @@ public class ReSyncCentroidsMetadataOnDB { if (project == null) continue; - System.out.println("\n###Trying to update Centroid TABLE for project id: " + projectId); + 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); @@ -126,9 +159,14 @@ public class ReSyncCentroidsMetadataOnDB { hashMap.put(TABLE_COLUMN_DESCRIZIONE, projectMap.get(JSON_KEY_INTRODUZIONE) + ""); updateTable(dbConnection, tableName, "projectId='" + projectId + "'", hashMap); System.out.println("###UPDATED project id: " + projectId); + + Thread.sleep(500); + System.out.println("################ Update completed for: " + projectId); } catch (Exception e) { System.err.println("Error on updating the table for the project id: " + projectId); } + + counter++; } @@ -136,6 +174,12 @@ public class ReSyncCentroidsMetadataOnDB { // 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"); } @@ -215,8 +259,10 @@ public class ReSyncCentroidsMetadataOnDB { System.out.println("Executing: " + alterTableString); try { con.setAutoCommit(false); - con.createStatement().execute(alterTableString); - con.commit(); + if (!READ_ONLY_MODE) { + con.createStatement().execute(alterTableString); + con.commit(); + } } catch (SQLException e) { e.printStackTrace(); if (con != null) { @@ -242,8 +288,10 @@ public class ReSyncCentroidsMetadataOnDB { try { con.setAutoCommit(false); - con.createStatement().execute(alterTableString); - con.commit(); + if (!READ_ONLY_MODE) { + con.createStatement().execute(alterTableString); + con.commit(); + } } catch (SQLException e) { e.printStackTrace(); if (con != null) { @@ -291,8 +339,10 @@ public class ReSyncCentroidsMetadataOnDB { con.setAutoCommit(false); System.out.println("Executing: " + updatePS.toString()); - updatePS.executeUpdate(); - con.commit(); + if (!READ_ONLY_MODE) { + updatePS.executeUpdate(); + con.commit(); + } } catch (SQLException e) { e.printStackTrace(); if (con != null) { diff --git a/src/test/java/org/gcube/application/se/RuntimeResourceReader.java b/src/test/java/org/gcube/application/se/RuntimeResourceReader.java index 72677a0..4497c31 100644 --- a/src/test/java/org/gcube/application/se/RuntimeResourceReader.java +++ b/src/test/java/org/gcube/application/se/RuntimeResourceReader.java @@ -143,15 +143,21 @@ public class RuntimeResourceReader { */ public static void main(String[] args) { - String scope = "/gcube/devsec/devVRE"; + //String scope = "/gcube/devsec/devVRE"; + //String scope = "/pred4s/preprod/preVRE"; + String scope = "/d4science.research-infrastructures.eu/D4OS/GNA"; String platformName = "GeoServer"; + String category = "Gis"; + +// String platformName = "postgis"; +// String category = "Database"; // scope = "/pred4s/preprod/preVRE"; RuntimeResourceReader reader; try { ScopeProvider.instance.set(scope); - reader = new RuntimeResourceReader(scope, null, "postgis", "Database", null); + reader = new RuntimeResourceReader(scope, null, platformName, category, null); for (ServiceEndpointBean seb : reader.getListSE()) { System.out.println("Found: " + seb); @@ -175,7 +181,7 @@ public class RuntimeResourceReader { // TODO Auto-generated catch block e.printStackTrace(); } - + } } -- 2.17.1 From 9daa4bd3bc66470e2fedf2898d5f0d966beccd54 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 27 Mar 2023 16:47:33 +0200 Subject: [PATCH 03/14] updated --- .../java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java b/src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java index cccea47..f5f4ca2 100644 --- a/src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java +++ b/src/test/java/org/gcube/application/ReSyncCentroidsMetadataOnDB.java @@ -35,7 +35,7 @@ public class ReSyncCentroidsMetadataOnDB { // private static final String TOKEN = ""; // devVRE // private static final String CONTEXT = "/pred4s/preprod/preVRE"; -// private static final String TOKEN = "5c3c7551-870d-4259-bd77-a7e603fe4949-980114272"; //preVRE +// private static final String TOKEN = ""; //preVRE private static final String CONTEXT = "/d4science.research-infrastructures.eu/D4OS/GNA"; private static final String TOKEN = ""; //GNA -- 2.17.1 From b7f1e61b0482be8eb47b9cfa80def33f1049bb68 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 12 Apr 2023 17:49:43 +0200 Subject: [PATCH 04/14] Pushed script for #24793 --- pom.xml | 2 +- ...teGeoportalGisLinkInTheCentroidLayers.java | 418 ++++++++++++++++++ 2 files changed, 419 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java diff --git a/pom.xml b/pom.xml index fd8e482..c97e2d1 100644 --- a/pom.xml +++ b/pom.xml @@ -80,7 +80,7 @@ org.gcube.portlets.user - gcube-url-shortener + uri-resolver-manager [1.0.0,2.0.0-SNAPSHOT) compile diff --git a/src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java b/src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java new file mode 100644 index 0000000..d670e1d --- /dev/null +++ b/src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java @@ -0,0 +1,418 @@ +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; +import org.gcube.portlets.user.uriresolvermanager.UriResolverManager; +import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder; +import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.RESOLVE_AS; + +public class CreateGeoportalGisLinkInTheCentroidLayers { + +// private static String CONTEXT = "/gcube/devsec/devVRE"; +// private static String TOKEN = "c41a00c0-7897-48d2-a67a-05190d6ce5e6-98187548"; // 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 + + // GEOPORTAL + 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"; + 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"; // devVRE + private static final String resourceName = "Geoserver-t postgis"; //preVRE +// private static final String resourceName = "GNA-postgis"; // GNA + + private static final String TABLE_COLUMN_GEO_VIEWER_LINK = "geov_link"; + + private static ProjectsCaller client = null; + + public static final boolean READ_ONLY_MODE = false; + + /** + * 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) { + + System.out.println("READ_ONLY_MODE ENABLED: " + READ_ONLY_MODE); + System.out.println("CONTEXT IS: " + CONTEXT); + System.out.println("PROFILE_ID: " + PROFILE_ID); + + initGeoportalClient(); + + //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"; + //String tableName = "profiledconcessioni_internal__prevre_centroids"; + String tableName = "profiledconcessioni_prevre_centroids"; +// PROFILE_ID = "concessioni-estere"; +// String tableName = "concessioni_estere_internal__prevre_centroids"; +// tableName = "concessioni_estere_prevre_centroids"; + + // GNA +// PROFILE_ID = "concessioni-estere"; +// String tableName = "concessioni_estere_internal__gna_centroids"; +// tableName = "concessioni_estere_gna_centroids"; +// PROFILE_ID = "profiledConcessioni"; +// String tableName = "profiledconcessioni_internal__gna_centroids"; +// tableName = "profiledconcessioni_gna_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); + + // MUST BE DONE JUST ONCE + try { + boolean columnExists = checkColumnExists(dbConnection, tableName, TABLE_COLUMN_GEO_VIEWER_LINK); + + if(!columnExists) { + addColumnTable(dbConnection, tableName, TABLE_COLUMN_GEO_VIEWER_LINK, "TEXT"); + } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return; + } + + List listProjectIdsIntoDB = readTableIDs(dbConnection, tableName, "projectid"); + ScopeProvider.instance.set(CONTEXT); + SecurityTokenProvider.instance.set(TOKEN); + // List listPublishedProjectsIntoService = + // getListProjectsDVFiltered(); + + int counter = 1; + UriResolverManager uriResolverManager = new UriResolverManager("GEO"); + for (String projectId : listProjectIdsIntoDB) { + Project project = null; + System.out.println("\n\n################ " + counter + " of " + listProjectIdsIntoDB.size() + + ") Reading the project id: " + projectId); + 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 " + TABLE_COLUMN_GEO_VIEWER_LINK + + " column for project id: " + projectId); + + ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(false); + ProjectDV projectDV = ConvertToDataValueObjectModel.toProjectDV(project, projectBuilder); + //Geoportal Resolver QueryString Builder + GeoportalResolverQueryStringBuilder builder = new GeoportalResolverQueryStringBuilder(projectDV.getProfileID(),projectId); + builder.scope(CONTEXT); + + if(tableName.contains("internal")) { + builder.resolverAs(RESOLVE_AS.PRIVATE); + } + + Map params = builder.buildQueryParameters(); + String shortLink = uriResolverManager.getLink(params, true); + + LinkedHashMap hashMap = new LinkedHashMap(); + hashMap.put(TABLE_COLUMN_GEO_VIEWER_LINK, shortLink); + updateTable(dbConnection, tableName, "projectId='" + projectId + "'", hashMap); + System.out.println("###UPDATED project id: " + projectId); + + Thread.sleep(500); + System.out.println("################ Update completed for: " + projectId); + } catch (Exception e) { + System.err.println("Error on updating the table for the project id: " + projectId); + } + + counter++; + + } + + } 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 List getListProjectsDVFiltered() throws Exception { + // List listOfProjects = client.getListForProfileID(PROFILE_ID); + + SearchingFilter filter = new SearchingFilter(); + + // Where Clause + List conditions = new ArrayList(); + Map searchInto = new HashMap(); + searchInto.put("_lifecycleInformation._phase", "Published"); + WhereClause whereClause = new WhereClause(LOGICAL_OP.OR, searchInto); + + conditions.add(whereClause); + filter.setConditions(conditions); + + LinkedHashMap projection = new LinkedHashMap(); + // 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 projects = client.queryOnMongo(PROFILE_ID, totalDocs, 0, null, filter); + + List 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 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 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("Executing: " + 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("\n####Column exists: " + columnExists + "\n"); + return columnExists; + + } catch (SQLException e) { + System.err.println(e); + return false; + } + } + + public static void updateTable(Connection con, String tableName, String whereCondition, + LinkedHashMap 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 readTableIDs(Connection con, String tableName, String columnNameProjectId) { + PreparedStatement p = null; + ResultSet resultSet = null; + + // Try block to catch exception/s + List listProjectIds = new ArrayList(); + 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; + + } + +} -- 2.17.1 From 9cbd162c40ddb92e13d272b0cda6212cb680592d Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 12 Apr 2023 17:59:34 +0200 Subject: [PATCH 05/14] Updated pom --- CHANGELOG.md | 4 ++++ pom.xml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d83cc0..50efbf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ 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.0.4-SNAPSHOT] - 2023-04-12 + +- Integrated the deleteFileset method [#24977] + ## [v2.0.3-SNAPSHOT] - 2023-02-09 #### Enhancements diff --git a/pom.xml b/pom.xml index c97e2d1..ec6165c 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.application geoportal-data-common - 2.0.3-SNAPSHOT + 2.0.4-SNAPSHOT GeoPortal Data Common is common library used by GUI components developed for GeoNA -- 2.17.1 From d58d933816a414325d676de5fff4e7c15e739882 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 17 Apr 2023 16:02:18 +0200 Subject: [PATCH 06/14] in progress on #24166 --- CHANGELOG.md | 4 +- pom.xml | 2 +- .../geoportal/ProjectsCaller.java | 43 ++++++++++++++----- .../innerobject/FilesetDV.java | 29 +++++++++---- 4 files changed, 57 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50efbf0..cd46482 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,11 +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.0.4-SNAPSHOT] - 2023-04-12 +## [v2.1.0-SNAPSHOT] - 2023-04-12 - Integrated the deleteFileset method [#24977] -## [v2.0.3-SNAPSHOT] - 2023-02-09 +## [v2.0.3] - 2023-02-09 #### Enhancements diff --git a/pom.xml b/pom.xml index ec6165c..a9d7475 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.application geoportal-data-common - 2.0.4-SNAPSHOT + 2.1.0-SNAPSHOT GeoPortal Data Common is common library used by GUI components developed for GeoNA diff --git a/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java b/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java index 4ddd40c..11f01d4 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java +++ b/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java @@ -163,15 +163,15 @@ public class ProjectsCaller { /** * Gets the relationship chain. * - * @param profileID the profile ID - * @param projectID the project ID + * @param profileID the profile ID + * @param projectID the project ID * @param relationID the relation ID - * @param deep the deep + * @param deep the deep * @return the relationship chain * @throws Exception the exception */ - public Iterator getRelationshipChain(String profileID, String projectID, String relationID, Boolean deep) - throws Exception { + public Iterator getRelationshipChain(String profileID, String projectID, + String relationID, Boolean deep) throws Exception { LOG.info("getRelationshipChain called for projectID: {}, relationID: {}", projectID, projectID); Projects client = (Projects) getClient(profileID); return client.getRelationshipChain(projectID, relationID, deep); @@ -347,11 +347,11 @@ public class ProjectsCaller { /** * Creates the relationship. * - * @param fromProfileID the from profile ID - * @param fromProjectID the from project ID + * @param fromProfileID the from profile ID + * @param fromProjectID the from project ID * @param relationshipId the relationship id - * @param toProfileID the to profile ID - * @param toProjectID the to project ID + * @param toProfileID the to profile ID + * @param toProjectID the to project ID * @throws RemoteException the remote exception */ public void createRelationship(String fromProfileID, String fromProjectID, String relationshipId, @@ -391,11 +391,34 @@ public class ProjectsCaller { client.deleteRelation(request); } + /** + * Delete fileset. + * + * @param profileID the profile ID + * @param projectID the project ID + * @param jsonPathToFileset the json path to fileset + * @param force the force + * @return the project + * @throws RemoteException the remote exception + */ + public Project deleteFileset(String profileID, String projectID, String jsonPathToFileset, Boolean force) + throws RemoteException { + LOG.info("deleteFileset called for profileID {} and projectID {}, fileset path: {}", profileID, projectID, + jsonPathToFileset); + + Projects client = (Projects) getClient(profileID); + + Project project = client.deleteFileSet(projectID, jsonPathToFileset, force); + LOG.info("fileset {} deleted", jsonPathToFileset); + LOG.debug("returning new project: {} ", project.getTheDocument()); + return project; + } + /** * Simple query. * * @param profileID the profile ID - * @param filter the filter + * @param filter the filter * @return the iterator * @throws Exception the exception */ diff --git a/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/materialization/innerobject/FilesetDV.java b/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/materialization/innerobject/FilesetDV.java index 681263b..7e07577 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/materialization/innerobject/FilesetDV.java +++ b/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/materialization/innerobject/FilesetDV.java @@ -6,24 +6,32 @@ import java.util.List; public class FilesetDV implements Serializable { - /** * */ private static final long serialVersionUID = -3108729581669778828L; - private String name; + private String gcubeProfileFieldName; // It is the gcubeProfileFieldName in the UCD filePaths + private String filesetFieldName; // It is the fieldName in the UCD filePaths private List listPayloads = new ArrayList(); public FilesetDV() { } - public String getName() { - return name; + public String getFilesetFieldName() { + return filesetFieldName; } - public void setName(String name) { - this.name = name; + public void setFilesetFieldName(String filesetFieldName) { + this.filesetFieldName = filesetFieldName; + } + + public String getGcubeProfileFieldName() { + return gcubeProfileFieldName; + } + + public void setGcubeProfileFieldName(String name) { + this.gcubeProfileFieldName = name; } public List getListPayload() { @@ -41,12 +49,17 @@ public class FilesetDV implements Serializable { @Override public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("FilesetDV [name="); - builder.append(name); + builder.append("FilesetDV [gcubeProfileFieldName="); + builder.append(gcubeProfileFieldName); + builder.append(", filesetFieldName="); + builder.append(filesetFieldName); builder.append(", listPayloads="); builder.append(listPayloads); builder.append("]"); return builder.toString(); } + + + } -- 2.17.1 From f51896e28d2a3a109948ac19fe182d832fe50c79 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Tue, 18 Apr 2023 09:38:46 +0200 Subject: [PATCH 07/14] updated --- .../application/CreateGeoportalGisLinkInTheCentroidLayers.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java b/src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java index d670e1d..e81c4cd 100644 --- a/src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java +++ b/src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java @@ -35,7 +35,7 @@ import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResol public class CreateGeoportalGisLinkInTheCentroidLayers { // private static String CONTEXT = "/gcube/devsec/devVRE"; -// private static String TOKEN = "c41a00c0-7897-48d2-a67a-05190d6ce5e6-98187548"; // devVRE +// private static String TOKEN = ""; // devVRE private static final String CONTEXT = "/pred4s/preprod/preVRE"; private static final String TOKEN = ""; //preVRE -- 2.17.1 From 3db873b17e72c2965349f2d857bf50239264816f Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 26 Apr 2023 11:26:18 +0200 Subject: [PATCH 08/14] updated Test cases --- ...teGeoportalGisLinkInTheCentroidLayers.java | 28 ++++++++-------- .../org/gcube/application/Project_Tests.java | 33 ++++++++++++++----- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java b/src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java index e81c4cd..a5ee6f4 100644 --- a/src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java +++ b/src/test/java/org/gcube/application/CreateGeoportalGisLinkInTheCentroidLayers.java @@ -34,11 +34,11 @@ 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 = "/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 @@ -54,8 +54,8 @@ public class CreateGeoportalGisLinkInTheCentroidLayers { private static final String platformName = "postgis"; private static final String category = "Database"; // private static final String resourceName = "GNA-POSTGIS-DB"; // devVRE - private static final String resourceName = "Geoserver-t postgis"; //preVRE -// private static final String resourceName = "GNA-postgis"; // GNA + //private static final String resourceName = "Geoserver-t postgis"; //preVRE + private static final String resourceName = "GNA-postgis"; // GNA private static final String TABLE_COLUMN_GEO_VIEWER_LINK = "geov_link"; @@ -85,17 +85,17 @@ 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"; // preVRE - PROFILE_ID = "profiledConcessioni"; +// PROFILE_ID = "profiledConcessioni"; //String tableName = "profiledconcessioni_internal__prevre_centroids"; - String tableName = "profiledconcessioni_prevre_centroids"; +// String tableName = "profiledconcessioni_prevre_centroids"; // PROFILE_ID = "concessioni-estere"; // String tableName = "concessioni_estere_internal__prevre_centroids"; // tableName = "concessioni_estere_prevre_centroids"; @@ -103,10 +103,10 @@ public class CreateGeoportalGisLinkInTheCentroidLayers { // GNA // PROFILE_ID = "concessioni-estere"; // String tableName = "concessioni_estere_internal__gna_centroids"; -// tableName = "concessioni_estere_gna_centroids"; -// PROFILE_ID = "profiledConcessioni"; +// String tableName = "concessioni_estere_gna_centroids"; + PROFILE_ID = "profiledConcessioni"; // String tableName = "profiledconcessioni_internal__gna_centroids"; -// tableName = "profiledconcessioni_gna_centroids"; +// String tableName = "profiledconcessioni_gna_centroids"; long startTime = System.currentTimeMillis(); diff --git a/src/test/java/org/gcube/application/Project_Tests.java b/src/test/java/org/gcube/application/Project_Tests.java index 49dcf16..c52e21d 100644 --- a/src/test/java/org/gcube/application/Project_Tests.java +++ b/src/test/java/org/gcube/application/Project_Tests.java @@ -53,15 +53,15 @@ public class Project_Tests { // private static String CONTEXT = "/pred4s/preprod/preVRE"; // private static String TOKEN = ""; //preVRE -// 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 = "5c3c7551-870d-4259-bd77-a7e603fe4949-980114272"; // preVRE + private static String CONTEXT = "/gcube/devsec/devVRE"; + private static String TOKEN = ""; // devVRE private static String PROFILE_ID = "profiledConcessioni"; - private static String PROJECT_ID = "638488d4308f5c28c5ee074d"; + private static String PROJECT_ID = "642d3a1ac2133270c058ebe5"; + +// private static String CONTEXT = "/pred4s/preprod/preVRE"; +// private static String TOKEN = ""; // preVRE +// private static String PROFILE_ID = "profiledConcessioni"; +// private static String PROJECT_ID = "642d3a1ac2133270c058ebe5"; // private static String CONTEXT = "/pred4s/preprod/preVRE"; // private static String TOKEN = ""; // preVRE @@ -109,6 +109,21 @@ public class Project_Tests { System.out.println(projectDV.getSpatialReference()); } + + //@Test + public void checkDocumentSerDes() throws Exception { + Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID); + + String documentValueAsJson = project.getTheDocument().toJson(); + System.out.println("1 "+documentValueAsJson); + String updatedDocumentAsJson = new Document(Project.THE_DOCUMENT,documentValueAsJson).toJson(); + System.out.println("2 "+updatedDocumentAsJson); + Document setUpdatedDocument = new Document("$set", updatedDocumentAsJson); + System.out.println("3 "+setUpdatedDocument); + + } + + // @Test public void getListProjectsDV() throws Exception { @@ -371,7 +386,7 @@ public class Project_Tests { } } - @Test + //@Test public void testEvaluateRelationshipsChain() throws Exception { System.out.println("testEvaluateRelationshipsChain test"); -- 2.17.1 From f0508b4fd97369869124b582c4147198c50d5529 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 26 Apr 2023 14:14:54 +0200 Subject: [PATCH 09/14] Updated "deleteFileset" added "ignoreErrors" parameter --- .../geoportalcommon/geoportal/ProjectsCaller.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java b/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java index 11f01d4..86a7ed2 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java +++ b/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java @@ -398,17 +398,20 @@ public class ProjectsCaller { * @param projectID the project ID * @param jsonPathToFileset the json path to fileset * @param force the force + * @param ignoreErrors the ignore errors * @return the project * @throws RemoteException the remote exception */ - public Project deleteFileset(String profileID, String projectID, String jsonPathToFileset, Boolean force) + 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 client = (Projects) getClient(profileID); - Project project = client.deleteFileSet(projectID, jsonPathToFileset, force); + 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()); return project; -- 2.17.1 From 0fd55e8a589e655760eb0c7755a25a17a1e8187c Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Tue, 2 May 2023 17:56:30 +0200 Subject: [PATCH 10/14] Integrated with the Geoportal_Resolver service [#25031] --- CHANGELOG.md | 1 + .../geoportalcommon/GeoportalCommon.java | 58 ++++----- .../org/gcube/application/Project_Tests.java | 121 +++++++++++------- .../org/gcube/application/TestGNACommon.java | 69 ++++++++-- .../java/org/gcube/application/UCD_Tests.java | 2 - .../application/se/RuntimeResourceReader.java | 2 +- src/test/resources/.gitignore | 1 + 7 files changed, 156 insertions(+), 98 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd46482..edb6acc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [v2.1.0-SNAPSHOT] - 2023-04-12 - Integrated the deleteFileset method [#24977] +- Integrated with the Geoportal_Resolver service [#25031] ## [v2.0.3] - 2023-02-09 diff --git a/src/main/java/org/gcube/application/geoportalcommon/GeoportalCommon.java b/src/main/java/org/gcube/application/geoportalcommon/GeoportalCommon.java index 986739f..f3888d5 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/GeoportalCommon.java +++ b/src/main/java/org/gcube/application/geoportalcommon/GeoportalCommon.java @@ -3,11 +3,15 @@ package org.gcube.application.geoportalcommon; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Arrays; +import java.util.Map; import org.gcube.application.geoportalcommon.shared.GNADataEntryConfigProfile; import org.gcube.application.geoportalcommon.shared.GNADataViewerConfigProfile; import org.gcube.application.geoportalcommon.shared.GeoportalItemReferences; import org.gcube.application.geoportalcommon.shared.PublicLink; +import org.gcube.portlets.user.uriresolvermanager.UriResolverManager; +import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder; +import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.RESOLVE_AS; import org.gcube.portlets.user.urlshortener.UrlShortener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,39 +21,30 @@ import org.slf4j.LoggerFactory; * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * - * Aug 5, 2021 + * May 2, 2023 */ public class GeoportalCommon { /** The Constant LOG. */ private static final Logger LOG = LoggerFactory.getLogger(GeoportalCommon.class); - private GNADataViewerConfigProfile geonaDataProfile; - /** * Instantiates a new geoportal common. */ public GeoportalCommon() { } - /** - * Instantiates a new geoportal common. - * - * @param geonaDataProfile the geona data profile - */ - public GeoportalCommon(GNADataViewerConfigProfile geonaDataProfile) { - this.geonaDataProfile = geonaDataProfile; - } - /** * Gets the public links for. * + * @param gcubeScope the gcube scope * @param item the item * @param createShortURL creates and returns the short URL also. * @return the public links for * @throws Exception the exception */ - public GeoportalItemReferences getPublicLinksFor(GeoportalItemReferences item, boolean createShortURL) throws Exception { + public GeoportalItemReferences getPublicLinksFor(String gcubeScope, GeoportalItemReferences item, + boolean createShortURL) throws Exception { LOG.info("getPublicLinksFor called for: " + item); try { @@ -63,14 +58,16 @@ public class GeoportalCommon { if (item.getProfileID() == null) throw new Exception("Bad request, the ProfileID is null"); - if (this.geonaDataProfile == null) - this.geonaDataProfile = readGNADataViewerConfig(GeoportalCommonConstants.GEOPORTAL_DATA_VIEWER_APP); - - // Restricted Link - String link = String.format("%s?%s=%s&%s=%s", geonaDataProfile.getRestrictedPortletURL(), - GeoportalCommonConstants.GET_GEONA_ITEM_ID, item.getProjectID(), - GeoportalCommonConstants.GET_GEONA_ITEM_TYPE, item.getProfileID()); + UriResolverManager resolver = new UriResolverManager("GEO"); + // PRIVATE LINK + GeoportalResolverQueryStringBuilder builder = new GeoportalResolverQueryStringBuilder(item.getProfileID(), + item.getProjectID()); + builder.scope(gcubeScope); + builder.resolverAs(RESOLVE_AS.PRIVATE); + // builder.resolverAs(RESOLVE_AS.PRIVATE); + Map params = builder.buildQueryParameters(); + String link = resolver.getLink(params, false); String shortUrl = link; try { if (createShortURL) @@ -80,14 +77,14 @@ public class GeoportalCommon { } item.setRestrictedLink(new PublicLink(link, shortUrl)); - // Open Link - link = String.format("%s?%s=%s&%s=%s", geonaDataProfile.getOpenPortletURL(), - GeoportalCommonConstants.GET_GEONA_ITEM_ID, item.getProjectID(), - GeoportalCommonConstants.GET_GEONA_ITEM_TYPE, item.getProfileID()); - + // PUBLIC LINK + builder.resolverAs(RESOLVE_AS.PUBLIC); + params = builder.buildQueryParameters(); + link = resolver.getLink(params, false); shortUrl = link; try { - shortUrl = getShortUrl(link); + if (createShortURL) + shortUrl = getShortUrl(link); } catch (Exception e) { LOG.warn("Error on shortening the URL: ", e); } @@ -147,15 +144,6 @@ public class GeoportalCommon { } } - /** - * Gets the geona data profile. - * - * @return the geona data profile - */ - public GNADataViewerConfigProfile getGeonaDataProfile() { - return geonaDataProfile; - } - /** * Read GNA data viewer config. * diff --git a/src/test/java/org/gcube/application/Project_Tests.java b/src/test/java/org/gcube/application/Project_Tests.java index c52e21d..d85d080 100644 --- a/src/test/java/org/gcube/application/Project_Tests.java +++ b/src/test/java/org/gcube/application/Project_Tests.java @@ -1,6 +1,8 @@ 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; @@ -11,6 +13,7 @@ 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; @@ -25,6 +28,7 @@ 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.geoportal.UseCaseDescriptorCaller; import org.gcube.application.geoportalcommon.shared.SearchingFilter; import org.gcube.application.geoportalcommon.shared.SearchingFilter.LOGICAL_OP; import org.gcube.application.geoportalcommon.shared.WhereClause; @@ -34,8 +38,6 @@ 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 org.junit.Test; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; @@ -49,22 +51,21 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class Project_Tests { - private static ProjectsCaller client = null; - // private static String CONTEXT = "/pred4s/preprod/preVRE"; - // private static String TOKEN = ""; //preVRE + 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); + private static String CONTEXT; + private static String TOKEN; + + private static UseCaseDescriptorCaller clientUCD = null; + private static ProjectsCaller clientPrj = null; - private static String CONTEXT = "/gcube/devsec/devVRE"; - private static String TOKEN = ""; // devVRE private static String PROFILE_ID = "profiledConcessioni"; - private static String PROJECT_ID = "642d3a1ac2133270c058ebe5"; + private static String PROJECT_ID = "644a66e944aad51c80409a3b"; -// private static String CONTEXT = "/pred4s/preprod/preVRE"; -// private static String TOKEN = ""; // preVRE -// private static String PROFILE_ID = "profiledConcessioni"; -// private static String PROJECT_ID = "642d3a1ac2133270c058ebe5"; - - // private static String CONTEXT = "/pred4s/preprod/preVRE"; - // private static String TOKEN = ""; // preVRE + private static String MY_LOGIN = "francesco.mangiacrapa"; private static final String FOLLOWS = "follows"; private static final String PRECEDES = "precedes"; @@ -76,17 +77,41 @@ public class Project_Tests { } - @Before - public void getClient() { - // assumeTrue(GCubeTest.isTestInfrastructureEnabled()); + /** + * 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 + public void init() { + readContextSettings(); ScopeProvider.instance.set(CONTEXT); SecurityTokenProvider.instance.set(TOKEN); - client = GeoportalClientCaller.projects(); + clientPrj = GeoportalClientCaller.projects(); + clientUCD = GeoportalClientCaller.useCaseDescriptors(); } // @Test public void getList() throws Exception { - List listOfProjects = client.getListForProfileID(PROFILE_ID); + List listOfProjects = clientPrj.getListForProfileID(PROFILE_ID); int i = 0; for (Project project : listOfProjects) { @@ -96,7 +121,7 @@ public class Project_Tests { // @Test public void getByID() throws Exception { - Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID); + Project project = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID); ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true); ProjectDV projectDV = ConvertToDataValueObjectModel.toProjectDV(project, projectBuilder); System.out.println(projectDV); @@ -109,25 +134,23 @@ public class Project_Tests { System.out.println(projectDV.getSpatialReference()); } - - //@Test - public void checkDocumentSerDes() throws Exception { - Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID); - - String documentValueAsJson = project.getTheDocument().toJson(); - System.out.println("1 "+documentValueAsJson); - String updatedDocumentAsJson = new Document(Project.THE_DOCUMENT,documentValueAsJson).toJson(); - System.out.println("2 "+updatedDocumentAsJson); - Document setUpdatedDocument = new Document("$set", updatedDocumentAsJson); - System.out.println("3 "+setUpdatedDocument); - - } - + // @Test + public void checkDocumentSerDes() throws Exception { + Project project = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID); + + String documentValueAsJson = project.getTheDocument().toJson(); + System.out.println("1 " + documentValueAsJson); + String updatedDocumentAsJson = new Document(Project.THE_DOCUMENT, documentValueAsJson).toJson(); + System.out.println("2 " + updatedDocumentAsJson); + Document setUpdatedDocument = new Document("$set", updatedDocumentAsJson); + System.out.println("3 " + setUpdatedDocument); + + } // @Test public void getListProjectsDV() throws Exception { - List listOfProjects = client.getListForProfileID(PROFILE_ID); + List listOfProjects = clientPrj.getListForProfileID(PROFILE_ID); ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true); int i = 0; for (Project project : listOfProjects) { @@ -201,8 +224,8 @@ public class Project_Tests { filter.setProjection(projection); - Integer totalDocs = client.getTotalDocument(PROFILE_ID); - Iterator projects = client.queryOnMongo(PROFILE_ID, totalDocs, 0, null, filter); + Integer totalDocs = clientPrj.getTotalDocument(PROFILE_ID); + Iterator projects = clientPrj.queryOnMongo(PROFILE_ID, totalDocs, 0, null, filter); Iterable itP = () -> projects; Stream targetStream = StreamSupport.stream(itP.spliterator(), false); List listProjectIDs = targetStream.map(Project::getId).collect(Collectors.toList()); @@ -259,7 +282,7 @@ public class Project_Tests { // @Test public void getLifecycleForProjectId() throws Exception { - Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID); + Project project = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID); LifecycleInformation lci = project.getLifecycleInformation(); LifecycleInformationDV liDV = ConvertToDataValueObjectModel.toLifecycleInformationDV(lci); System.out.println(liDV); @@ -268,23 +291,23 @@ public class Project_Tests { // @Test public void getConfiguration() throws Exception { - Configuration config = client.getConfiguration(PROFILE_ID); + Configuration config = clientPrj.getConfiguration(PROFILE_ID); System.out.println(config); } // @Test public void getTotalDocument() throws Exception { - System.out.println(client.getTotalDocument(PROFILE_ID)); + System.out.println(clientPrj.getTotalDocument(PROFILE_ID)); } // @Test public void getListPhases() throws Exception { - List idsPhases = client.getIDsPhases(PROFILE_ID); + List idsPhases = clientPrj.getIDsPhases(PROFILE_ID); System.out.println(idsPhases); - PhaseDV[] phases = client.getPhasesIntoDocumentStoreCollection(PROFILE_ID); + PhaseDV[] phases = clientPrj.getPhasesIntoDocumentStoreCollection(PROFILE_ID); for (PhaseDV phaseDV : phases) { System.out.println(phaseDV); } @@ -294,13 +317,13 @@ public class Project_Tests { // @Test public void getRelationshipsChain() throws Exception { System.out.println("getRelationshipsChain test"); - Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID); + Project project = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID); List relations = project.getRelationships(); for (Relationship relationship : relations) { System.out.println("\n\ngetRelationshipsChain for " + relationship); - Iterator iterator = client.getRelationshipChain(PROFILE_ID, PROJECT_ID, + Iterator iterator = clientPrj.getRelationshipChain(PROFILE_ID, PROJECT_ID, relationship.getRelationshipName(), true); while (iterator.hasNext()) { @@ -386,7 +409,7 @@ public class Project_Tests { } } - //@Test + // @Test public void testEvaluateRelationshipsChain() throws Exception { System.out.println("testEvaluateRelationshipsChain test"); @@ -400,7 +423,7 @@ public class Project_Tests { PROJECT_ID = "63c80aebacb8c7657b858741"; - Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID); + Project project = clientPrj.getProjectByID(PROFILE_ID, PROJECT_ID); evaluateAdditionalIndexParameters(project); @@ -438,7 +461,7 @@ public class Project_Tests { List 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())); @@ -582,7 +605,7 @@ public class Project_Tests { // target.setProfileID(relationship.getTargetUCD()); // target.setId(relationship.getTargetID()); - Project target = client.getProjectByID(relationship.getTargetUCD(), relationship.getTargetID()); + Project target = clientPrj.getProjectByID(relationship.getTargetUCD(), relationship.getTargetID()); linkedProject.setTarget(target); if (recurse) { diff --git a/src/test/java/org/gcube/application/TestGNACommon.java b/src/test/java/org/gcube/application/TestGNACommon.java index aa4401c..99c35aa 100644 --- a/src/test/java/org/gcube/application/TestGNACommon.java +++ b/src/test/java/org/gcube/application/TestGNACommon.java @@ -1,45 +1,92 @@ package org.gcube.application; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + import org.gcube.application.geoportalcommon.GeoportalCommon; +import org.gcube.application.geoportalcommon.geoportal.GeoportalClientCaller; +import org.gcube.application.geoportalcommon.geoportal.ProjectsCaller; +import org.gcube.application.geoportalcommon.geoportal.UseCaseDescriptorCaller; import org.gcube.application.geoportalcommon.shared.GNADataEntryConfigProfile; import org.gcube.application.geoportalcommon.shared.GNADataViewerConfigProfile; import org.gcube.application.geoportalcommon.shared.GeoportalItemReferences; import org.gcube.application.geoportalcommon.shared.config.RoleRights; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; +import org.junit.Before; +import org.junit.Test; public class TestGNACommon { + + 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); + private static String CONTEXT; + private static String TOKEN; - private static String TOKEN = ""; - private static String CONTEXT = "/gcube/devsec/devVRE"; - private static String USERNAME = "francesco.mangiacrapa"; + private static UseCaseDescriptorCaller clientUCD = null; + private static ProjectsCaller clientPrj = null; + + private static String PROFILE_ID = "profiledConcessioni"; + private static String PROJECT_ID = "644a66e944aad51c80409a3b"; + + private static String MY_LOGIN = "francesco.mangiacrapa"; + + /** + * 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 public void init() { + readContextSettings(); ScopeProvider.instance.set(CONTEXT); SecurityTokenProvider.instance.set(TOKEN); + clientPrj = GeoportalClientCaller.projects(); + clientUCD = GeoportalClientCaller.useCaseDescriptors(); } // @Test - public GNADataViewerConfigProfile getGeoNaDataViewProfile() throws Exception { + public void getGeoNaDataViewProfile() throws Exception { System.out.println("getGeoNaDataViewProfile called"); ScopeProvider.instance.set(CONTEXT); GeoportalCommon gc = new GeoportalCommon(); GNADataViewerConfigProfile profile = gc.readGNADataViewerConfig(null); System.out.println("Returning profile: " + profile); - return profile; } - // @Test - public GeoportalItemReferences getLinks() throws Exception { - System.out.println("getGeoNaDataViewProfile called"); + //@Test + public void getLinks() throws Exception { + System.out.println("getLinks called"); ScopeProvider.instance.set(CONTEXT); GeoportalCommon gc = new GeoportalCommon(); - GeoportalItemReferences item = new GeoportalItemReferences("", "concessione"); - GeoportalItemReferences links = gc.getPublicLinksFor(item, true); - return links; + GeoportalItemReferences item = new GeoportalItemReferences(PROJECT_ID, PROFILE_ID); + GeoportalItemReferences links = gc.getPublicLinksFor(CONTEXT, item, true); + System.out.println(links); } // @Test diff --git a/src/test/java/org/gcube/application/UCD_Tests.java b/src/test/java/org/gcube/application/UCD_Tests.java index ab5d0d4..1e1a890 100644 --- a/src/test/java/org/gcube/application/UCD_Tests.java +++ b/src/test/java/org/gcube/application/UCD_Tests.java @@ -26,8 +26,6 @@ import org.gcube.common.authorization.library.provider.SecurityTokenProvider; 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; diff --git a/src/test/java/org/gcube/application/se/RuntimeResourceReader.java b/src/test/java/org/gcube/application/se/RuntimeResourceReader.java index 4497c31..4ce47d2 100644 --- a/src/test/java/org/gcube/application/se/RuntimeResourceReader.java +++ b/src/test/java/org/gcube/application/se/RuntimeResourceReader.java @@ -7,9 +7,9 @@ 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.common.scope.api.ScopeProvider; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.slf4j.Logger; diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore index 289e8e7..3c773eb 100644 --- a/src/test/resources/.gitignore +++ b/src/test/resources/.gitignore @@ -18,3 +18,4 @@ /FARM.gcubekey /gCubeApps.gcubekey /devVRE.gcubekey +/gcube_config.properties -- 2.17.1 From 39f084b5c5a19ad3756f854ccb345cd681ce3cdc Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 3 May 2023 10:53:21 +0200 Subject: [PATCH 11/14] Updated list of operations --- .../geoportalcommon/shared/config/OPERATION_ON_ITEM.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gcube/application/geoportalcommon/shared/config/OPERATION_ON_ITEM.java b/src/main/java/org/gcube/application/geoportalcommon/shared/config/OPERATION_ON_ITEM.java index 46c1167..14a61cf 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/shared/config/OPERATION_ON_ITEM.java +++ b/src/main/java/org/gcube/application/geoportalcommon/shared/config/OPERATION_ON_ITEM.java @@ -11,10 +11,11 @@ package org.gcube.application.geoportalcommon.shared.config; public enum OPERATION_ON_ITEM { CREATE_NEW_PROJECT("Create New Project"), - VIEW_ON_MAP("View on Map"), - SHOW_METADATA("Show Metadata"), + VIEW_PROJECT_AS_DOCUMENT("View Project as Document"), + VIEW_PROJECT_AS_JSON("View Projet as JSON"), + VIEW_ON_MAP("View on Map"), VIEW_REPORT("View the Report"), - EDIT_PROJECT("Edit the Project"), + EDIT_PROJECT("Edit the Project"), CLONE_PROJECT("Clone the Project"), PUBLISH_UNPUBLISH_PROJECT("Publish/UnPublish the Project"), DELETE_PROJECT("Delete the Project"), -- 2.17.1 From 6b63090ec40954eefb4ef766dc8bd78b7a0f4fbb Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 4 May 2023 16:10:24 +0200 Subject: [PATCH 12/14] improved toSpatialReference method --- .../ConvertToDataValueObjectModel.java | 28 +++++++++++-------- .../shared/geoportal/geojson/GeoJSON.java | 8 ++++-- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java b/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java index 7594b3f..ea236f2 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java +++ b/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java @@ -23,6 +23,7 @@ import org.gcube.application.geoportal.common.model.document.access.Access; import org.gcube.application.geoportal.common.model.document.accounting.AccountingInfo; import org.gcube.application.geoportal.common.model.document.accounting.PublicationInfo; import org.gcube.application.geoportal.common.model.document.identification.IdentificationReference; +import org.gcube.application.geoportal.common.model.document.identification.SpatialReference; import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation; import org.gcube.application.geoportal.common.model.document.relationships.Relationship; import org.gcube.application.geoportal.common.model.useCaseDescriptor.HandlerDeclaration; @@ -527,8 +528,8 @@ public class ConvertToDataValueObjectModel { project.getProfileVersion() != null ? project.getProfileVersion().getValue() : ""); theProject.setVersion(project.getVersion() != null ? project.getVersion().getValue() : ""); - - theProject.setTheDocument(toGenericDocumentDV(project.getId(), project.getTheDocument(), DocumentDV.class, projectReader.getListDocumentKeys(), projectReader.isIncludeFullDocumentMap())); + theProject.setTheDocument(toGenericDocumentDV(project.getId(), project.getTheDocument(), DocumentDV.class, + projectReader.getListDocumentKeys(), projectReader.isIncludeFullDocumentMap())); List relations = project.getRelationships(); @@ -608,18 +609,22 @@ public class ConvertToDataValueObjectModel { return null; GeoJSON geoJson = null; + try { geoJson = new GeoJSON(); - JSONObject jsonObject = new JSONObject(geoJSONObject).getJSONObject("geoJSON"); - geoJson.setType(jsonObject.getString("type")); - geoJson.setBbox(GisUtil.fromJSONArray(jsonObject.getJSONArray("bbox"))); + JSONObject jsonObject = new JSONObject(geoJSONObject).getJSONObject(SpatialReference.GEO_JSON); + geoJson.setType(jsonObject.getString(GeoJSON.TYPE)); + geoJson.setBbox(GisUtil.fromJSONArray(jsonObject.getJSONArray(GeoJSON.BBOX))); Crs crs = org.gcube.application.geoportal.client.utils.Serialization - .read(jsonObject.getJSONObject("crs").toString(), Crs.class); + .read(jsonObject.getJSONObject(GeoJSON.CRS).toString(), Crs.class); geoJson.setCrs(crs); geoJson.setGeoJSON(jsonObject.toString()); LOG.debug("toSpatialReference returning " + geoJson); + } catch (JSONException | IOException e) { - LOG.warn("Error on converting " + GeoJSON.class.getSimpleName() + " from : " + geoJSONObject, e); + LOG.warn("Error on converting " + GeoJSON.class.getSimpleName() + " from : " + geoJSONObject + + ". Is it empty?"); + LOG.debug("toSpatialReference error: ", e); } return geoJson; @@ -1003,10 +1008,11 @@ public class ConvertToDataValueObjectModel { } } } - - //Casting to String, in order to avoid GWT serialization issue, see #24537#note-9 and #24432 - String valueString = value!=null?value+"":null; - + + // Casting to String, in order to avoid GWT serialization issue, see + // #24537#note-9 and #24432 + String valueString = value != null ? value + "" : null; + documentAsMap.put(key, valueString); return documentAsMap; } diff --git a/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/geojson/GeoJSON.java b/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/geojson/GeoJSON.java index ed53996..2d4efaa 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/geojson/GeoJSON.java +++ b/src/main/java/org/gcube/application/geoportalcommon/shared/geoportal/geojson/GeoJSON.java @@ -6,11 +6,15 @@ import org.gcube.application.geoportalcommon.shared.geoportal.materialization.in public class GeoJSON implements Serializable { + public static final String TYPE = "type"; + public static final String BBOX = "bbox"; + public static final String CRS = "crs"; + public static final String COORDINATES = "coordinates"; /** * */ private static final long serialVersionUID = -7798331554142534921L; - + private String type; private Crs crs; private BBOXDV bbox; @@ -18,7 +22,7 @@ public class GeoJSON implements Serializable { public GeoJSON() { - } + } public String getType() { return type; -- 2.17.1 From 8d32c4e85b1877f0a0e8dc6c42846c3958b61229 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 11 May 2023 15:10:55 +0200 Subject: [PATCH 13/14] removed -SNAPSHOT to be released --- CHANGELOG.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edb6acc..a19c59e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ 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.1.0-SNAPSHOT] - 2023-04-12 +## [v2.1.0] - 2023-04-12 - Integrated the deleteFileset method [#24977] - Integrated with the Geoportal_Resolver service [#25031] diff --git a/pom.xml b/pom.xml index a9d7475..ee60da1 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.application geoportal-data-common - 2.1.0-SNAPSHOT + 2.1.0 GeoPortal Data Common is common library used by GUI components developed for GeoNA -- 2.17.1 From b1fcee1970b2d284e06711c173e5ed4afa6aa8c8 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 11 May 2023 15:46:36 +0200 Subject: [PATCH 14/14] Added -SNAPSHOT for merging on master --- CHANGELOG.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a19c59e..edb6acc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ 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.1.0] - 2023-04-12 +## [v2.1.0-SNAPSHOT] - 2023-04-12 - Integrated the deleteFileset method [#24977] - Integrated with the Geoportal_Resolver service [#25031] diff --git a/pom.xml b/pom.xml index ee60da1..a9d7475 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.application geoportal-data-common - 2.1.0 + 2.1.0-SNAPSHOT GeoPortal Data Common is common library used by GUI components developed for GeoNA -- 2.17.1