From a424844dce4a1ee0ee4329e8e2d26a52f7d42ab7 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 10 May 2023 17:25:54 +0200 Subject: [PATCH] Improved Metadata management --- .../contexts/ServerContextCache.java | 8 ++++---- .../instances/base/ElementManagement.java | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ServerContextCache.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ServerContextCache.java index 9158707..1569a8c 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ServerContextCache.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/ServerContextCache.java @@ -21,6 +21,7 @@ import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement; +import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.requests.RequestUtility; import org.gcube.informationsystem.resourceregistry.requests.ServerRequestInfo; import org.gcube.informationsystem.serialization.ElementMapper; @@ -98,8 +99,7 @@ public class ServerContextCache extends ContextCache { } protected boolean isUserAllowedToGetPrivacyMeta() { - // TODO - return true; + return ElementManagement.isUserAllowedToGetPrivacyMeta(); } public synchronized List getContexts() throws ResourceRegistryException { @@ -117,8 +117,8 @@ public class ServerContextCache extends ContextCache { protected Metadata getMetadataForPrivacy(ObjectMapper objectMapper, Metadata metadata) { ObjectNode objectNode = objectMapper.valueToTree(metadata); - objectNode.set(Metadata.CREATED_BY_PROPERTY, new TextNode(Metadata.HIDDEN_FOR_PRIVACY_USER)); - objectNode.set(Metadata.LAST_UPDATE_BY_PROPERTY, new TextNode(Metadata.HIDDEN_FOR_PRIVACY_USER)); + objectNode.replace(Metadata.CREATED_BY_PROPERTY, new TextNode(Metadata.HIDDEN_FOR_PRIVACY_USER)); + objectNode.replace(Metadata.LAST_UPDATE_BY_PROPERTY, new TextNode(Metadata.HIDDEN_FOR_PRIVACY_USER)); try { Metadata metadataWithPrivacy = objectMapper.treeToValue(objectNode, Metadata.class); return metadataWithPrivacy; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java index 6d5eba3..97ece1e 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java @@ -1215,6 +1215,11 @@ public abstract class ElementManagement { return element; } + public static boolean isUserAllowedToGetPrivacyMeta() { + // TODO + return true; + } + protected JsonNode getPropertyForJson(String key, Object object) throws ResourceRegistryException { try { if(object == null) { @@ -1228,7 +1233,12 @@ public abstract class ElementManagement { if(key.compareTo(IdentifiableElement.METADATA_PROPERTY) == 0) { // Keeping the metadata MetadataOrient metadataOrient = MetadataUtility.getMetadataOrient((ODocument) object); - JsonNode metadataJson = OrientDBUtility.toJsonNode(metadataOrient); + ObjectNode metadataJson = (ObjectNode) OrientDBUtility.toJsonNode(metadataOrient); + + if(!isUserAllowedToGetPrivacyMeta()) { + metadataJson.replace(Metadata.CREATED_BY_PROPERTY, new TextNode(Metadata.HIDDEN_FOR_PRIVACY_USER)); + metadataJson.replace(Metadata.LAST_UPDATE_BY_PROPERTY, new TextNode(Metadata.HIDDEN_FOR_PRIVACY_USER)); + } // TODO check a solution for supertypes TypesCache typesCache = TypesCache.getInstance(); @@ -1237,7 +1247,7 @@ public abstract class ElementManagement { ObjectMapper objectMapper = new ObjectMapper(); Collection superClasses = metadataType.getSuperTypes(); ArrayNode arrayNode = objectMapper.valueToTree(superClasses); - ((ObjectNode) metadataJson).replace(Element.SUPERTYPES_PROPERTY, arrayNode); + metadataJson.replace(Element.SUPERTYPES_PROPERTY, arrayNode); return metadataJson; }