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