From 2fb4767205b478bf5e7b622ded5ec2136b0b1f90 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Fri, 3 Feb 2023 11:40:56 +0100 Subject: [PATCH] Added method get Access from the Document --- CHANGELOG.md | 3 +- .../ConvertToDataServiceModel.java | 71 +++++++++++++++++++ .../ConvertToDataValueObjectModel.java | 17 +++-- 3 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/gcube/application/geoportalcommon/ConvertToDataServiceModel.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 7abbb55..caa7834 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,11 +4,12 @@ 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.2-SNAPSHOT] - 2022-02-02 +## [v2.0.2-SNAPSHOT] - 2022-02-03 #### Enhancements - [#24432] Reverted serialization from Sting to Object values returning the Document as Map +- [#24475] Propagated the Access Policy in the fileset ## [v2.0.1] - 2022-01-19 diff --git a/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataServiceModel.java b/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataServiceModel.java new file mode 100644 index 0000000..779a39d --- /dev/null +++ b/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataServiceModel.java @@ -0,0 +1,71 @@ +package org.gcube.application.geoportalcommon; + +import org.gcube.application.geoportal.common.model.document.access.Access; +import org.gcube.application.geoportal.common.model.document.access.AccessPolicy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider; + +/** + * The Class ConvertToDataServiceModel. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Feb 3, 2023 + */ +public class ConvertToDataServiceModel { + + private static Logger LOG = LoggerFactory.getLogger(ConvertToDataServiceModel.class); + + /** + * Gets the access from document section. + * + * @param theJSONDocument the the JSON document + * @param sectionJSONPath the section JSON path + * @return the access from document section + */ + public static Access getAccessFromDocumentSection(String theJSONDocument, String sectionJSONPath) { + String accessPolicyPath = String.format("%s.%s", sectionJSONPath, ConvertToDataValueObjectModel.POLICY); + + AccessPolicy accessPolicy = null; + com.jayway.jsonpath.Configuration config = com.jayway.jsonpath.Configuration.builder() + .jsonProvider(new JsonOrgJsonProvider()).build(); + + // Reading policy fields + try { + JsonPath theSectionPolycJsonPath = JsonPath.compile(accessPolicyPath); + String policy = theSectionPolycJsonPath.read(theJSONDocument, config).toString(); + LOG.debug("Read " + ConvertToDataValueObjectModel.POLICY + ": " + policy + ", from section: " + + accessPolicyPath); + if (policy != null) { + accessPolicy = AccessPolicy.valueOf(policy.toUpperCase()); + } + } catch (Exception e) { + LOG.info("No " + ConvertToDataValueObjectModel.POLICY + " found in the path: " + accessPolicyPath); + } + + // Reading policy fields + String licenseIDPath = String.format("%s.%s", sectionJSONPath, ConvertToDataValueObjectModel.LICENSE_ID); + String licenseID = null; + try { + JsonPath theSectionLicenseJsonPath = JsonPath.compile(licenseIDPath); + licenseID = theSectionLicenseJsonPath.read(theJSONDocument, config).toString(); + LOG.debug("Read " + ConvertToDataValueObjectModel.LICENSE_ID + ": " + licenseID + ", from section: " + + licenseIDPath); + } catch (Exception e) { + LOG.info("No " + ConvertToDataValueObjectModel.LICENSE_ID + " found in the path: " + licenseIDPath); + } + + Access access = new Access(); + if (accessPolicy != null) + access.setPolicy(accessPolicy); + if (licenseID != null) + access.setLicense(licenseID); + + LOG.info("Access is: " + access.getPolicy() + " / " + access.getLicense()); + return access; + } + +} diff --git a/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java b/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java index d5bf957..9610900 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java +++ b/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java @@ -101,6 +101,10 @@ public class ConvertToDataValueObjectModel { public static List KEYSET_POSSIBLE_DATE = Arrays.asList("start", "end", "created", "updated", "inizio", "fine", "creato", "aggiornato"); + public static String LICENSE_ID = "licenseID"; + + public static String POLICY = "policy"; + /** * To use case descriptor DV. * @@ -1242,7 +1246,7 @@ public class ConvertToDataValueObjectModel { * @param targetProjectID the target project ID * @param relationName the relation name * @return the JSON object - * @throws JSONException + * @throws JSONException */ public static JSONObject toTimelineJSONModel(Project theProject, JSONObject sourceJsonTemplate, String targetUCD, String targetProjectID, String relationName) throws JSONException { @@ -1259,7 +1263,7 @@ public class ConvertToDataValueObjectModel { targetJsonObject.put("relationship_name", relationName); Iterator itKeys = sourceJsonTemplate.keys(); - if(itKeys!=null) { + if (itKeys != null) { while (itKeys.hasNext()) { Object key = itKeys.next(); String jsonPath = null; @@ -1279,10 +1283,10 @@ public class ConvertToDataValueObjectModel { } catch (Exception e) { LOG.trace("Error on setting key: {}, path: {}", theKey, jsonPath); } - + } } - + return targetJsonObject; } @@ -1293,9 +1297,10 @@ public class ConvertToDataValueObjectModel { * @param theProject the the project * @param timelineJSONObject the timeline JSON object * @return the temporal reference DV - * @throws JSONException + * @throws JSONException */ - public static TemporalReferenceDV toTemporalReferenceDV(Project theProject, JSONObject timelineJSONObject) throws JSONException { + public static TemporalReferenceDV toTemporalReferenceDV(Project theProject, JSONObject timelineJSONObject) + throws JSONException { TemporalReferenceDV tr = null;