From 85c773fb1bca7fe0f2c810f002de23d4e736cabf Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 14 Sep 2021 15:58:00 +0200 Subject: [PATCH] Relation instances request returns relations and not Resources --- CHANGELOG.md | 4 ++ .../relations/IsRelatedToManagement.java | 41 ++++++++++++++++++ .../model/relations/RelationManagement.java | 42 ++++--------------- 3 files changed, 52 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9894f7..fac54e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Prepared Query has been fixed to properly consider polymorphic parameter and provided constraint [#20298] - Add tests to check the proper behaviour of operation involving Propagation Constraints [#20923] - The contexts property is not included in header is not requested [#21953] +- When serialising a resource the ConsistsOf does not include source [#22001] +- Getting all the relations of a type it is returned a list of relations and not the list of Resources sources of such relation [#22003] + ## [v4.0.0] @@ -24,6 +27,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Added support to add contexts in instances header for instances safe methods only [#20012] - Switched JSON management to gcube-jackson [#19116] + ## [v3.0.0] [r4.21.0] - 2020-03-30 - Refactored code to support IS Model reorganization (e.g naming, packages) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/IsRelatedToManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/IsRelatedToManagement.java index d0469e1..a1cae3d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/IsRelatedToManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/IsRelatedToManagement.java @@ -1,18 +1,25 @@ package org.gcube.informationsystem.resourceregistry.instances.model.relations; +import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.informationsystem.base.reference.AccessType; +import org.gcube.informationsystem.base.reference.entities.EntityElement; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint; +import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException; +import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; +import org.gcube.informationsystem.resourceregistry.utils.Utility; +import org.gcube.informationsystem.types.reference.entities.EntityType; import org.gcube.informationsystem.types.reference.entities.ResourceType; /** @@ -56,6 +63,40 @@ public class IsRelatedToManagement extends RelationManagement sourceEntityManagement = getSourceEntityManagement(); + ((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfAsJsonNode()); + } + + if(includeTarget) { + EntityElementManagement targetEntityManagement = getTargetEntityManagement(); + + /* + * This function differ from the super implementation in RelationElementManagement from the following line of code + * An IsRelatedTo must not include the whole target Resource (with all the facets) instead only the Resource entity. + * + * ((ObjectNode) relation).replace(Relation.TARGET_PROPERTY, targetEntityManagement.serializeAsJsonNode()); + */ + ((ObjectNode) relation).replace(Relation.TARGET_PROPERTY, targetEntityManagement.serializeSelfAsJsonNode()); + } + + } catch(ResourceRegistryException e) { + logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e); + throw e; + } catch(Exception e) { + logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e); + throw new ResourceRegistryException(e); + } + + return relation; + } + + @Override public void sanityCheck() throws SchemaViolationException, ResourceRegistryException { // TODO Auto-generated method stub diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java index 418c3d7..20de325 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java @@ -1,8 +1,9 @@ package org.gcube.informationsystem.resourceregistry.instances.model.relations; +import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.UUID; @@ -187,34 +188,6 @@ public abstract class RelationManagement fullSerialize(Map visitedSourceResources) throws ResourceRegistryException { @@ -765,20 +738,21 @@ public abstract class RelationManagement serializeEdges(Iterable edges, boolean postFilterPolymorphic) throws ResourceRegistryException { - Map visitedSourceResources = new HashMap<>(); + // Map visitedSourceResources = new HashMap<>(); + List serilizedEdges = new ArrayList<>(); for(ODocument d : edges) { OEdge edge = (OEdge) d; - // TODO check because it was using compare if(postFilterPolymorphic && getOClass().isSubClassOf(typeName)) { continue; } RelationManagement relationManagement = ElementManagementUtility.getRelationManagement(getWorkingContext(), oDatabaseDocument, edge); - visitedSourceResources = relationManagement.fullSerialize(visitedSourceResources); + // visitedSourceResources = relationManagement.fullSerialize(visitedSourceResources); + serilizedEdges.add(relationManagement.serializeAsJsonNode()); } - return visitedSourceResources.values(); + return serilizedEdges; } protected String serializeJsonNodeCollectionAsString(Collection collection) throws ResourceRegistryException { @@ -797,7 +771,5 @@ public abstract class RelationManagement collection = serializeEdges(edges, false); return serializeJsonNodeCollectionAsString(collection); } - - }