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:
parent
9d37a4994b
commit
1b7d6aa21f
|
@ -3,6 +3,7 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.informationsystem.impl.entity;
|
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.annotations.ISRootEntity;
|
||||||
import org.gcube.informationsystem.model.entity.Entity;
|
import org.gcube.informationsystem.model.entity.Entity;
|
||||||
import org.gcube.informationsystem.model.entity.Header;
|
import org.gcube.informationsystem.model.entity.Header;
|
||||||
|
@ -12,6 +13,7 @@ import org.gcube.informationsystem.model.entity.Header;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@ISRootEntity
|
@ISRootEntity
|
||||||
|
@ISEntity(name="Entity", abstractType=true)
|
||||||
public abstract class EntityImpl implements Entity {
|
public abstract class EntityImpl implements Entity {
|
||||||
|
|
||||||
protected Header header;
|
protected Header header;
|
||||||
|
|
|
@ -8,6 +8,7 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.gcube.informationsystem.model.annotations.ISResource;
|
||||||
import org.gcube.informationsystem.model.entity.Facet;
|
import org.gcube.informationsystem.model.entity.Facet;
|
||||||
import org.gcube.informationsystem.model.entity.Resource;
|
import org.gcube.informationsystem.model.entity.Resource;
|
||||||
import org.gcube.informationsystem.model.relation.RelationProperty;
|
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/
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ISResource(name="Resource", abstractType=true)
|
||||||
public abstract class ResourceImpl extends EntityImpl implements Resource {
|
public abstract class ResourceImpl extends EntityImpl implements Resource {
|
||||||
|
|
||||||
protected Map<Facet, RelationProperty> addedFacets;
|
protected Map<Facet, RelationProperty> addedFacets;
|
||||||
|
|
|
@ -13,7 +13,7 @@ import org.gcube.informationsystem.model.relation.RelationProperty;
|
||||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
* @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
|
public class RelatedToImpl<Out extends Resource, In extends Resource> extends
|
||||||
RelationImpl<Out, In> implements RelatedTo<Out, In> {
|
RelationImpl<Out, In> implements RelatedTo<Out, In> {
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ import org.gcube.informationsystem.model.relation.RelationProperty;
|
||||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
* @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> {
|
public abstract class RelationImpl<Out extends Entity, In extends Entity> implements Relation<Out, In> {
|
||||||
|
|
||||||
protected Out source;
|
protected Out source;
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
}
|
|
@ -6,11 +6,14 @@ import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.gcube.informationsystem.model.annotations.ISEmbeddedType;
|
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.ISFacet;
|
||||||
import org.gcube.informationsystem.model.annotations.ISProperty;
|
import org.gcube.informationsystem.model.annotations.ISProperty;
|
||||||
import org.gcube.informationsystem.model.annotations.ISPropertyRef;
|
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.Entity;
|
||||||
import org.gcube.informationsystem.model.entity.Facet;
|
import org.gcube.informationsystem.model.entity.Facet;
|
||||||
|
import org.gcube.informationsystem.model.entity.Resource;
|
||||||
import org.gcube.informationsystem.types.Type.OType;
|
import org.gcube.informationsystem.types.Type.OType;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -21,22 +24,40 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
public class TypeBinder {
|
public class TypeBinder {
|
||||||
|
|
||||||
private static final String DEFAULT_FACET_SUPERCLASS = "Facet";
|
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 final String DEFAULT_ENTITY_SUPERCLASS = "Entity";
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(TypeBinder.class);
|
private static Logger logger = LoggerFactory.getLogger(TypeBinder.class);
|
||||||
|
|
||||||
|
|
||||||
public static String serializeResource(Class<Entity> type) throws Exception{
|
@SuppressWarnings("unchecked")
|
||||||
if (!type.isAnnotationPresent(ISFacet.class))
|
public static String serializeEntity(Class<Entity> type) throws Exception{
|
||||||
throw new IllegalArgumentException("the class "+type.getCanonicalName()+" must be annotated with @ISType");
|
if(type.isAssignableFrom(Resource.class)){
|
||||||
TypeDefinition def = createFacetDefinition(type);
|
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();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
String json = mapper.writeValueAsString(def);
|
String json = mapper.writeValueAsString(def);
|
||||||
return json;
|
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))
|
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);
|
TypeDefinition def = createFacetDefinition(type);
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
String json = mapper.writeValueAsString(def);
|
String json = mapper.writeValueAsString(def);
|
||||||
|
@ -51,7 +72,7 @@ public class TypeBinder {
|
||||||
String json = mapper.writeValueAsString(def);
|
String json = mapper.writeValueAsString(def);
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TypeDefinition createEmbeddedDefintion(Class<?> type){
|
private static TypeDefinition createEmbeddedDefintion(Class<?> type){
|
||||||
ISEmbeddedType isType = type.getAnnotation(ISEmbeddedType.class);
|
ISEmbeddedType isType = type.getAnnotation(ISEmbeddedType.class);
|
||||||
TypeDefinition typeDefinition = new TypeDefinition();
|
TypeDefinition typeDefinition = new TypeDefinition();
|
||||||
|
@ -63,20 +84,61 @@ public class TypeBinder {
|
||||||
logger.trace("retrieved type definition {} ",typeDefinition);
|
logger.trace("retrieved type definition {} ",typeDefinition);
|
||||||
return typeDefinition;
|
return typeDefinition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static TypeDefinition createEntityDefinition(Class<? extends Entity> type){
|
||||||
private static <T extends Entity> TypeDefinition createFacetDefinition(Class<T> type){
|
ISEntity isType = type.getAnnotation(ISEntity.class);
|
||||||
ISFacet isType = type.getAnnotation(ISFacet.class);
|
|
||||||
TypeDefinition typeDefinition = new TypeDefinition();
|
TypeDefinition typeDefinition = new TypeDefinition();
|
||||||
String name = isType.name().isEmpty()?type.getSimpleName():isType.name();
|
String name = isType.name().isEmpty()?type.getSimpleName():isType.name();
|
||||||
typeDefinition.name = name;
|
typeDefinition.name = name;
|
||||||
typeDefinition.properties = retrieveListOfProperties(type);
|
|
||||||
typeDefinition.description = isType.description();
|
typeDefinition.description = isType.description();
|
||||||
|
typeDefinition.properties = retrieveListOfProperties(type);
|
||||||
typeDefinition.abstractType = isType.abstractType();
|
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);
|
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);
|
logger.trace("retrieved type definition {} ",typeDefinition);
|
||||||
|
|
|
@ -7,6 +7,6 @@ public class SerializationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serialize() throws Exception{
|
public void serialize() throws Exception{
|
||||||
TypeBinder.serialize(EntityTest.class);
|
TypeBinder.serializeFacet(EntityTest.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue