From f18743308bd5441597f61f9bf80730c36459ed03 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 18 Feb 2021 15:14:24 +0100 Subject: [PATCH] Improved Resource definition --- .../model/reference/entities/Resource.java | 4 +- ...dEntry.java => RelatedResourcesEntry.java} | 8 +--- .../types/annotations/ResourceSchema.java | 2 +- .../types/impl/entities/ResourceTypeImpl.java | 40 ++++++++++--------- .../impl/properties/LinkedEntityImpl.java | 24 +++++++++++ 5 files changed, 50 insertions(+), 28 deletions(-) rename src/main/java/org/gcube/informationsystem/types/annotations/{ResourceSchemaRelatedEntry.java => RelatedResourcesEntry.java} (91%) diff --git a/src/main/java/org/gcube/informationsystem/model/reference/entities/Resource.java b/src/main/java/org/gcube/informationsystem/model/reference/entities/Resource.java index 9f54103..0a2e596 100644 --- a/src/main/java/org/gcube/informationsystem/model/reference/entities/Resource.java +++ b/src/main/java/org/gcube/informationsystem/model/reference/entities/Resource.java @@ -13,7 +13,7 @@ import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.types.annotations.Abstract; import org.gcube.informationsystem.types.annotations.ResourceSchema; import org.gcube.informationsystem.types.annotations.ResourceSchemaEntry; -import org.gcube.informationsystem.types.annotations.ResourceSchemaRelatedEntry; +import org.gcube.informationsystem.types.annotations.RelatedResourcesEntry; import org.gcube.informationsystem.types.reference.Change; import org.gcube.informationsystem.types.reference.TypeMetadata; import org.gcube.informationsystem.utils.TypeVersion; @@ -31,7 +31,7 @@ import org.gcube.informationsystem.utils.TypeVersion; @ResourceSchemaEntry(relation=ConsistsOf.class, facet=Facet.class, min=1, description="Any Resource consists of one or more Facets which describes the different aspects of the resource."), }, resources= { - @ResourceSchemaRelatedEntry(source=Resource.class, relation=IsRelatedTo.class, target=Resource.class, description="Any Resource can be related to any other resource.") + @RelatedResourcesEntry(source=Resource.class, relation=IsRelatedTo.class, target=Resource.class, description="Any Resource can be related to any other resource.") } ) @TypeMetadata(name = Resource.NAME, description = "This is the base class for Resources", version = TypeVersion.MINIMAL_VERSION_STRING) diff --git a/src/main/java/org/gcube/informationsystem/types/annotations/ResourceSchemaRelatedEntry.java b/src/main/java/org/gcube/informationsystem/types/annotations/RelatedResourcesEntry.java similarity index 91% rename from src/main/java/org/gcube/informationsystem/types/annotations/ResourceSchemaRelatedEntry.java rename to src/main/java/org/gcube/informationsystem/types/annotations/RelatedResourcesEntry.java index a987fbe..5744ed6 100644 --- a/src/main/java/org/gcube/informationsystem/types/annotations/ResourceSchemaRelatedEntry.java +++ b/src/main/java/org/gcube/informationsystem/types/annotations/RelatedResourcesEntry.java @@ -22,7 +22,7 @@ import org.gcube.informationsystem.types.reference.properties.PropertyDefinition */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) -public @interface ResourceSchemaRelatedEntry { +public @interface RelatedResourcesEntry { @JsonProperty Class source() default Resource.class; @@ -37,10 +37,4 @@ public @interface ResourceSchemaRelatedEntry { @JsonProperty String description() default ""; - @JsonProperty - int min() default 0; - - @JsonProperty - int max() default -1; - } \ No newline at end of file diff --git a/src/main/java/org/gcube/informationsystem/types/annotations/ResourceSchema.java b/src/main/java/org/gcube/informationsystem/types/annotations/ResourceSchema.java index 1beebf0..818ec6e 100644 --- a/src/main/java/org/gcube/informationsystem/types/annotations/ResourceSchema.java +++ b/src/main/java/org/gcube/informationsystem/types/annotations/ResourceSchema.java @@ -16,5 +16,5 @@ public @interface ResourceSchema { ResourceSchemaEntry[] facets() default {}; - ResourceSchemaRelatedEntry[] resources() default {}; + RelatedResourcesEntry[] resources() default {}; } diff --git a/src/main/java/org/gcube/informationsystem/types/impl/entities/ResourceTypeImpl.java b/src/main/java/org/gcube/informationsystem/types/impl/entities/ResourceTypeImpl.java index e92e661..3dd48db 100644 --- a/src/main/java/org/gcube/informationsystem/types/impl/entities/ResourceTypeImpl.java +++ b/src/main/java/org/gcube/informationsystem/types/impl/entities/ResourceTypeImpl.java @@ -10,7 +10,7 @@ import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.types.TypeMapper; import org.gcube.informationsystem.types.annotations.ResourceSchema; import org.gcube.informationsystem.types.annotations.ResourceSchemaEntry; -import org.gcube.informationsystem.types.annotations.ResourceSchemaRelatedEntry; +import org.gcube.informationsystem.types.annotations.RelatedResourcesEntry; import org.gcube.informationsystem.types.impl.properties.LinkedEntityImpl; import org.gcube.informationsystem.types.reference.entities.ResourceType; import org.gcube.informationsystem.types.reference.properties.LinkedEntity; @@ -47,24 +47,28 @@ public class ResourceTypeImpl extends EntityTypeImpl implements ResourceType { this.resources = new ArrayList<>(); ResourceSchema[] resourceSchemaArray = clz.getAnnotationsByType(ResourceSchema.class); for(ResourceSchemaEntry resourceSchemaEntry : resourceSchemaArray[0].facets()) { - LinkedEntityImpl resourceSchemaEntryDefinition = new LinkedEntityImpl(); - resourceSchemaEntryDefinition.setSource(TypeMapper.getType(clz)); - resourceSchemaEntryDefinition.setRelation(TypeMapper.getType(resourceSchemaEntry.relation())); - resourceSchemaEntryDefinition.setTarget(TypeMapper.getType(resourceSchemaEntry.facet())); - resourceSchemaEntryDefinition.setDescription(resourceSchemaEntry.description()); - resourceSchemaEntryDefinition.setMin(resourceSchemaEntry.min()); - resourceSchemaEntryDefinition.setMax(resourceSchemaEntry.max()); - this.facets.add(resourceSchemaEntryDefinition); + LinkedEntity linkedEntity = new LinkedEntityImpl(); + linkedEntity.setSource(TypeMapper.getType(clz)); + linkedEntity.setRelation(TypeMapper.getType(resourceSchemaEntry.relation())); + linkedEntity.setTarget(TypeMapper.getType(resourceSchemaEntry.facet())); + linkedEntity.setDescription(resourceSchemaEntry.description()); + linkedEntity.setMin(resourceSchemaEntry.min()); + linkedEntity.setMax(resourceSchemaEntry.max()); + this.facets.add(linkedEntity); } - for(ResourceSchemaRelatedEntry resourceSchemaRelatedEntry : resourceSchemaArray[0].resources()) { - LinkedEntity resourceSchemaEntryDefinition = new LinkedEntityImpl(); - resourceSchemaEntryDefinition.setSource(TypeMapper.getType(resourceSchemaRelatedEntry.source())); - resourceSchemaEntryDefinition.setRelation(TypeMapper.getType(resourceSchemaRelatedEntry.relation())); - resourceSchemaEntryDefinition.setTarget(TypeMapper.getType(resourceSchemaRelatedEntry.target())); - resourceSchemaEntryDefinition.setDescription(resourceSchemaRelatedEntry.description()); - resourceSchemaEntryDefinition.setMin(resourceSchemaRelatedEntry.min()); - resourceSchemaEntryDefinition.setMax(resourceSchemaRelatedEntry.max()); - this.resources.add(resourceSchemaEntryDefinition); + for(RelatedResourcesEntry resourceSchemaRelatedEntry : resourceSchemaArray[0].resources()) { + LinkedEntity linkedEntity = new LinkedEntityImpl(); + linkedEntity.setSource(TypeMapper.getType(resourceSchemaRelatedEntry.source())); + linkedEntity.setRelation(TypeMapper.getType(resourceSchemaRelatedEntry.relation())); + linkedEntity.setTarget(TypeMapper.getType(resourceSchemaRelatedEntry.target())); + linkedEntity.setDescription(resourceSchemaRelatedEntry.description()); + /* + * We cannot impose any constraint on related resources. + * The annotation are only for documentation purpose. + */ + linkedEntity.setMin(0); + linkedEntity.setMax(-1); + this.resources.add(linkedEntity); } } } diff --git a/src/main/java/org/gcube/informationsystem/types/impl/properties/LinkedEntityImpl.java b/src/main/java/org/gcube/informationsystem/types/impl/properties/LinkedEntityImpl.java index 7357846..2b67e5f 100644 --- a/src/main/java/org/gcube/informationsystem/types/impl/properties/LinkedEntityImpl.java +++ b/src/main/java/org/gcube/informationsystem/types/impl/properties/LinkedEntityImpl.java @@ -3,6 +3,8 @@ */ package org.gcube.informationsystem.types.impl.properties; +import java.util.Objects; + import org.gcube.com.fasterxml.jackson.annotation.JsonTypeName; import org.gcube.informationsystem.base.impl.properties.PropertyElementImpl; import org.gcube.informationsystem.types.reference.properties.LinkedEntity; @@ -93,4 +95,26 @@ public final class LinkedEntityImpl extends PropertyElementImpl implements Linke } } + @Override + public int hashCode() { + return Objects.hash(source, relation, target, min, max, description); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + LinkedEntityImpl other = (LinkedEntityImpl) obj; + return Objects.equals(source, other.source) && + Objects.equals(relation, other.relation) && + Objects.equals(target, other.target) && + Objects.equals(min, other.min) && + Objects.equals(max, other.max) && + Objects.equals(description, other.description); + } + }