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
This commit is contained in:
Luca Frosini 2016-06-30 15:15:12 +00:00
parent 9d37a4994b
commit 1b7d6aa21f
7 changed files with 100 additions and 18 deletions

View File

@ -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;

View File

@ -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<Facet, RelationProperty> addedFacets;

View File

@ -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<Out extends Resource, In extends Resource> extends
RelationImpl<Out, In> implements RelatedTo<Out, In> {

View File

@ -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<Out extends Entity, In extends Entity> implements Relation<Out, In> {
protected Out source;

View File

@ -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;
}

View File

@ -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<Entity> 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<Entity> type) throws Exception{
if(type.isAssignableFrom(Resource.class)){
return serializeResource((Class<? extends Resource>) type);
}else if(type.isAssignableFrom(Facet.class)){
return serializeFacet((Class<? extends Facet>) 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<? extends Resource> 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<? extends Facet> type) throws Exception{
public static String serializeFacet(Class<? extends Facet> 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 <T extends Entity> TypeDefinition createFacetDefinition(Class<T> type){
ISFacet isType = type.getAnnotation(ISFacet.class);
private static TypeDefinition createEntityDefinition(Class<? extends Entity> 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<? extends Resource> 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<? extends Facet> 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);

View File

@ -7,6 +7,6 @@ public class SerializationTest {
@Test
public void serialize() throws Exception{
TypeBinder.serialize(EntityTest.class);
TypeBinder.serializeFacet(EntityTest.class);
}
}