From 1b7d6aa21f530eb1b813dff1a0d68e6a31627240 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 30 Jun 2016 15:15:12 +0000 Subject: [PATCH] Implementing RR Service git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/information-system-model@129703 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../impl/entity/EntityImpl.java | 2 + .../impl/entity/ResourceImpl.java | 2 + .../impl/relation/RelatedToImpl.java | 2 +- .../impl/relation/RelationImpl.java | 2 +- .../model/annotations/ISEntity.java | 16 ++++ .../informationsystem/types/TypeBinder.java | 92 ++++++++++++++++--- .../type/SerializationTest.java | 2 +- 7 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 src/main/java/org/gcube/informationsystem/model/annotations/ISEntity.java diff --git a/src/main/java/org/gcube/informationsystem/impl/entity/EntityImpl.java b/src/main/java/org/gcube/informationsystem/impl/entity/EntityImpl.java index bcfe8e6..c418f74 100644 --- a/src/main/java/org/gcube/informationsystem/impl/entity/EntityImpl.java +++ b/src/main/java/org/gcube/informationsystem/impl/entity/EntityImpl.java @@ -3,6 +3,7 @@ */ package org.gcube.informationsystem.impl.entity; +import org.gcube.informationsystem.model.annotations.ISEntity; import org.gcube.informationsystem.model.annotations.ISRootEntity; import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.entity.Header; @@ -12,6 +13,7 @@ import org.gcube.informationsystem.model.entity.Header; * */ @ISRootEntity +@ISEntity(name="Entity", abstractType=true) public abstract class EntityImpl implements Entity { protected Header header; diff --git a/src/main/java/org/gcube/informationsystem/impl/entity/ResourceImpl.java b/src/main/java/org/gcube/informationsystem/impl/entity/ResourceImpl.java index effc2a6..22b213b 100644 --- a/src/main/java/org/gcube/informationsystem/impl/entity/ResourceImpl.java +++ b/src/main/java/org/gcube/informationsystem/impl/entity/ResourceImpl.java @@ -8,6 +8,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.gcube.informationsystem.model.annotations.ISResource; import org.gcube.informationsystem.model.entity.Facet; import org.gcube.informationsystem.model.entity.Resource; import org.gcube.informationsystem.model.relation.RelationProperty; @@ -16,6 +17,7 @@ import org.gcube.informationsystem.model.relation.RelationProperty; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ +@ISResource(name="Resource", abstractType=true) public abstract class ResourceImpl extends EntityImpl implements Resource { protected Map addedFacets; diff --git a/src/main/java/org/gcube/informationsystem/impl/relation/RelatedToImpl.java b/src/main/java/org/gcube/informationsystem/impl/relation/RelatedToImpl.java index 596f72c..e6eb3a2 100644 --- a/src/main/java/org/gcube/informationsystem/impl/relation/RelatedToImpl.java +++ b/src/main/java/org/gcube/informationsystem/impl/relation/RelatedToImpl.java @@ -13,7 +13,7 @@ import org.gcube.informationsystem.model.relation.RelationProperty; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -@ISRelation(name="RealatedTo", out=ResourceImpl.class, in=ResourceImpl.class) +@ISRelation(name="RelatedTo", out=ResourceImpl.class, in=ResourceImpl.class) public class RelatedToImpl extends RelationImpl implements RelatedTo { diff --git a/src/main/java/org/gcube/informationsystem/impl/relation/RelationImpl.java b/src/main/java/org/gcube/informationsystem/impl/relation/RelationImpl.java index eda6a2d..a9b3e4d 100644 --- a/src/main/java/org/gcube/informationsystem/impl/relation/RelationImpl.java +++ b/src/main/java/org/gcube/informationsystem/impl/relation/RelationImpl.java @@ -14,7 +14,7 @@ import org.gcube.informationsystem.model.relation.RelationProperty; * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * */ -@ISRelation(name="Realation", out=EntityImpl.class, in=EntityImpl.class, abstractClass= true) +@ISRelation(name="Relation", out=EntityImpl.class, in=EntityImpl.class, abstractClass= true) public abstract class RelationImpl implements Relation { protected Out source; diff --git a/src/main/java/org/gcube/informationsystem/model/annotations/ISEntity.java b/src/main/java/org/gcube/informationsystem/model/annotations/ISEntity.java new file mode 100644 index 0000000..2a28188 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/model/annotations/ISEntity.java @@ -0,0 +1,16 @@ +package org.gcube.informationsystem.model.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface ISEntity { + + String name() default ""; + String description() default ""; + boolean abstractType() default false; + +} diff --git a/src/main/java/org/gcube/informationsystem/types/TypeBinder.java b/src/main/java/org/gcube/informationsystem/types/TypeBinder.java index 56f0ee4..d1819dc 100644 --- a/src/main/java/org/gcube/informationsystem/types/TypeBinder.java +++ b/src/main/java/org/gcube/informationsystem/types/TypeBinder.java @@ -6,11 +6,14 @@ import java.util.Collections; import java.util.List; import org.gcube.informationsystem.model.annotations.ISEmbeddedType; +import org.gcube.informationsystem.model.annotations.ISEntity; import org.gcube.informationsystem.model.annotations.ISFacet; import org.gcube.informationsystem.model.annotations.ISProperty; import org.gcube.informationsystem.model.annotations.ISPropertyRef; +import org.gcube.informationsystem.model.annotations.ISResource; import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.entity.Facet; +import org.gcube.informationsystem.model.entity.Resource; import org.gcube.informationsystem.types.Type.OType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,22 +24,40 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class TypeBinder { private static final String DEFAULT_FACET_SUPERCLASS = "Facet"; + private static final String DEFAULT_RESOURCE_SUPERCLASS = "Entity"; private static final String DEFAULT_ENTITY_SUPERCLASS = "Entity"; + private static Logger logger = LoggerFactory.getLogger(TypeBinder.class); - public static String serializeResource(Class type) throws Exception{ - if (!type.isAnnotationPresent(ISFacet.class)) - throw new IllegalArgumentException("the class "+type.getCanonicalName()+" must be annotated with @ISType"); - TypeDefinition def = createFacetDefinition(type); + @SuppressWarnings("unchecked") + public static String serializeEntity(Class type) throws Exception{ + if(type.isAssignableFrom(Resource.class)){ + return serializeResource((Class) type); + }else if(type.isAssignableFrom(Facet.class)){ + return serializeFacet((Class) type); + } + + if (!type.isAnnotationPresent(ISEntity.class)) + throw new IllegalArgumentException("the class "+type.getCanonicalName()+" must be annotated with @ISEntity"); + TypeDefinition def = createEntityDefinition(type); + ObjectMapper mapper = new ObjectMapper(); + String json = mapper.writeValueAsString(def); + return json; + } + + public static String serializeResource(Class type) throws Exception{ + if (!type.isAnnotationPresent(ISResource.class)) + throw new IllegalArgumentException("the class "+type.getCanonicalName()+" must be annotated with @ISResource"); + TypeDefinition def = createResourceDefinition(type); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(def); return json; } - public static String serialize(Class type) throws Exception{ + public static String serializeFacet(Class type) throws Exception{ if (!type.isAnnotationPresent(ISFacet.class)) - throw new IllegalArgumentException("the class "+type.getCanonicalName()+" must be annotated with @ISType"); + throw new IllegalArgumentException("the class "+type.getCanonicalName()+" must be annotated with @ISFacet"); TypeDefinition def = createFacetDefinition(type); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(def); @@ -51,7 +72,7 @@ public class TypeBinder { String json = mapper.writeValueAsString(def); return json; } - + private static TypeDefinition createEmbeddedDefintion(Class type){ ISEmbeddedType isType = type.getAnnotation(ISEmbeddedType.class); TypeDefinition typeDefinition = new TypeDefinition(); @@ -63,20 +84,61 @@ public class TypeBinder { logger.trace("retrieved type definition {} ",typeDefinition); return typeDefinition; } - - - private static TypeDefinition createFacetDefinition(Class type){ - ISFacet isType = type.getAnnotation(ISFacet.class); + + private static TypeDefinition createEntityDefinition(Class type){ + ISEntity isType = type.getAnnotation(ISEntity.class); TypeDefinition typeDefinition = new TypeDefinition(); String name = isType.name().isEmpty()?type.getSimpleName():isType.name(); typeDefinition.name = name; - typeDefinition.properties = retrieveListOfProperties(type); typeDefinition.description = isType.description(); + typeDefinition.properties = retrieveListOfProperties(type); typeDefinition.abstractType = isType.abstractType(); - if (name.equals(DEFAULT_FACET_SUPERCLASS)) typeDefinition.superclasses= Collections.singletonList(DEFAULT_ENTITY_SUPERCLASS); - else { + + if (name.equals(DEFAULT_ENTITY_SUPERCLASS)) { + typeDefinition.superclasses = Collections.singletonList("V"); + } else { String superClassName = retrieveSuperClasses(type); - typeDefinition.superclasses= Collections.singletonList(superClassName.isEmpty()?DEFAULT_FACET_SUPERCLASS:superClassName); + typeDefinition.superclasses = Collections.singletonList(superClassName.isEmpty()?DEFAULT_ENTITY_SUPERCLASS:superClassName); + } + + logger.trace("retrieved type definition {} ",typeDefinition); + return typeDefinition; + } + + private static TypeDefinition createResourceDefinition(Class type){ + ISResource isResource = type.getAnnotation(ISResource.class); + TypeDefinition typeDefinition = new TypeDefinition(); + String name = isResource.name().isEmpty()?type.getSimpleName():isResource.name(); + typeDefinition.name = name; + typeDefinition.description = isResource.description(); + //typeDefinition.properties = retrieveListOfProperties(type); + typeDefinition.abstractType = isResource.abstractType(); + + if (name.equals(DEFAULT_RESOURCE_SUPERCLASS)) { + typeDefinition.superclasses = Collections.singletonList(DEFAULT_ENTITY_SUPERCLASS); + } else { + String superClassName = retrieveSuperClasses(type); + typeDefinition.superclasses = Collections.singletonList(superClassName.isEmpty()?DEFAULT_RESOURCE_SUPERCLASS:superClassName); + } + + logger.trace("retrieved type definition {} ",typeDefinition); + return typeDefinition; + } + + + private static TypeDefinition createFacetDefinition(Class type){ + ISFacet isFacet = type.getAnnotation(ISFacet.class); + TypeDefinition typeDefinition = new TypeDefinition(); + String name = isFacet.name().isEmpty()?type.getSimpleName():isFacet.name(); + typeDefinition.name = name; + typeDefinition.properties = retrieveListOfProperties(type); + typeDefinition.description = isFacet.description(); + typeDefinition.abstractType = isFacet.abstractType(); + if (name.equals(DEFAULT_FACET_SUPERCLASS)) { + typeDefinition.superclasses = Collections.singletonList(DEFAULT_ENTITY_SUPERCLASS); + } else { + String superClassName = retrieveSuperClasses(type); + typeDefinition.superclasses = Collections.singletonList(superClassName.isEmpty()?DEFAULT_FACET_SUPERCLASS:superClassName); } logger.trace("retrieved type definition {} ",typeDefinition); diff --git a/src/test/java/org/gcube/informationsystem/type/SerializationTest.java b/src/test/java/org/gcube/informationsystem/type/SerializationTest.java index de95227..1ca08e0 100644 --- a/src/test/java/org/gcube/informationsystem/type/SerializationTest.java +++ b/src/test/java/org/gcube/informationsystem/type/SerializationTest.java @@ -7,6 +7,6 @@ public class SerializationTest { @Test public void serialize() throws Exception{ - TypeBinder.serialize(EntityTest.class); + TypeBinder.serializeFacet(EntityTest.class); } }