Added Specific class to define a Resource

This commit is contained in:
Luca Frosini 2020-01-16 16:48:08 +01:00
parent f5f69bc6a6
commit 006cfb03e4
5 changed files with 119 additions and 59 deletions

View File

@ -3,9 +3,7 @@ package org.gcube.informationsystem.types.impl;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.gcube.informationsystem.base.impl.ERImpl;
@ -17,17 +15,12 @@ import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.types.TypeBinder;
import org.gcube.informationsystem.types.annotations.Abstract;
import org.gcube.informationsystem.types.annotations.ISProperty;
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.impl.entities.EntityTypeDefinitionImpl;
import org.gcube.informationsystem.types.impl.properties.PropertyDefinitionImpl;
import org.gcube.informationsystem.types.impl.properties.PropertyTypeDefinitionImpl;
import org.gcube.informationsystem.types.impl.properties.ResourceEntryDefinitionImpl;
import org.gcube.informationsystem.types.impl.relations.RelationTypeDefinitionImpl;
import org.gcube.informationsystem.types.reference.TypeDefinition;
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
import org.gcube.informationsystem.types.reference.properties.ResourceEntryDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -53,8 +46,7 @@ public class TypeDefinitionImpl extends ERImpl implements TypeDefinition {
protected boolean abstractType;
protected Set<String> superClasses;
protected Set<PropertyDefinition> properties;
protected List<ResourceEntryDefinition> facets;
protected List<ResourceEntryDefinition> resources;
protected static <ISM extends ISManageable> Set<String> retrieveSuperClasses(Class<? extends ISM> type, Class<ISM> baseClass, String topSuperClass){
Set<String> interfaceList = new HashSet<>();
@ -116,7 +108,7 @@ public class TypeDefinitionImpl extends ERImpl implements TypeDefinition {
TypeDefinition typeDefinition = null;
try {
if(BaseEntity.class.isAssignableFrom(clz)) {
typeDefinition = new EntityTypeDefinitionImpl((Class<? extends BaseEntity>) clz);
typeDefinition = EntityTypeDefinitionImpl.getEntityTypeDefinitionInstance((Class<? extends BaseEntity>) clz);
return typeDefinition;
} else if(BaseRelation.class.isAssignableFrom(clz)){
typeDefinition = new RelationTypeDefinitionImpl((Class<? extends BaseRelation<?,?>>) clz);
@ -150,42 +142,10 @@ public class TypeDefinitionImpl extends ERImpl implements TypeDefinition {
if(!Resource.class.isAssignableFrom(clz)){
this.properties = retrieveListOfProperties(clz);
}else {
@SuppressWarnings("unchecked")
Class<? extends Resource> r = (Class<? extends Resource>) clz;
setResourceSchemaEntries(r);
}
}
private void setResourceSchemaEntries(Class<? extends Resource> clz){
if(clz.isAnnotationPresent(ResourceSchema.class)) {
this.facets = new ArrayList<>();
this.resources = new ArrayList<>();
ResourceSchema[] resourceSchemaArray = clz.getAnnotationsByType(ResourceSchema.class);
for(ResourceSchemaEntry resourceSchemaEntry : resourceSchemaArray[0].facets()) {
ResourceEntryDefinitionImpl resourceSchemaEntryDefinition = new ResourceEntryDefinitionImpl();
resourceSchemaEntryDefinition.setSource(TypeBinder.getType(clz));
resourceSchemaEntryDefinition.setRelation(TypeBinder.getType(resourceSchemaEntry.relation()));
resourceSchemaEntryDefinition.setTarget(TypeBinder.getType(resourceSchemaEntry.facet()));
resourceSchemaEntryDefinition.setDescription(resourceSchemaEntry.description());
resourceSchemaEntryDefinition.setMin(resourceSchemaEntry.min());
resourceSchemaEntryDefinition.setMax(resourceSchemaEntry.max());
this.facets.add(resourceSchemaEntryDefinition);
}
for(ResourceSchemaRelatedEntry resourceSchemaRelatedEntry : resourceSchemaArray[0].resources()) {
ResourceEntryDefinition resourceSchemaEntryDefinition = new ResourceEntryDefinitionImpl();
resourceSchemaEntryDefinition.setSource(TypeBinder.getType(resourceSchemaRelatedEntry.source()));
resourceSchemaEntryDefinition.setRelation(TypeBinder.getType(resourceSchemaRelatedEntry.relation()));
resourceSchemaEntryDefinition.setTarget(TypeBinder.getType(resourceSchemaRelatedEntry.target()));
resourceSchemaEntryDefinition.setDescription(resourceSchemaRelatedEntry.description());
resourceSchemaEntryDefinition.setMin(resourceSchemaRelatedEntry.min());
resourceSchemaEntryDefinition.setMax(resourceSchemaRelatedEntry.max());
this.resources.add(resourceSchemaEntryDefinition);
}
}
}
@Override
public String getName() {
return name;
@ -210,13 +170,5 @@ public class TypeDefinitionImpl extends ERImpl implements TypeDefinition {
public Set<PropertyDefinition> getProperties() {
return properties;
}
public List<ResourceEntryDefinition> getFacets() {
return facets;
}
public List<ResourceEntryDefinition> getResources() {
return resources;
}
}

View File

@ -15,7 +15,7 @@ import org.gcube.informationsystem.utils.ISMapper;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName(value=EntityTypeDefinition.NAME)
public final class EntityTypeDefinitionImpl extends TypeDefinitionImpl implements EntityTypeDefinition {
public class EntityTypeDefinitionImpl extends TypeDefinitionImpl implements EntityTypeDefinition {
/**
* Generated Serial Version UID
@ -24,6 +24,20 @@ public final class EntityTypeDefinitionImpl extends TypeDefinitionImpl implement
// private static final String VERTEX_CLASS_NAME = "V";
public static EntityTypeDefinition getEntityTypeDefinitionInstance(Class<? extends BaseEntity> clz) {
EntityTypeDefinition entityTypeDefinition;
if(Resource.class.isAssignableFrom(clz)) {
@SuppressWarnings("unchecked")
Class<? extends Resource> c = (Class<? extends Resource>) clz;
entityTypeDefinition = new ResourceTypeDefinitionImpl(c);
return entityTypeDefinition;
} else {
entityTypeDefinition = new EntityTypeDefinitionImpl(clz);
}
return entityTypeDefinition;
}
protected EntityTypeDefinitionImpl() {
super();
}
@ -31,11 +45,7 @@ public final class EntityTypeDefinitionImpl extends TypeDefinitionImpl implement
public EntityTypeDefinitionImpl(Class<? extends BaseEntity> clz) {
super(clz);
if(Resource.class.isAssignableFrom(clz)){
@SuppressWarnings("unchecked")
Class<? extends Resource> c = (Class<? extends Resource>) clz;
this.superClasses = retrieveSuperClasses(c, Resource.class, Entity.NAME);
} else if(Facet.class.isAssignableFrom(clz)){
if(Facet.class.isAssignableFrom(clz)){
@SuppressWarnings("unchecked")
Class<? extends Facet> c = (Class<? extends Facet>) clz;
this.superClasses = retrieveSuperClasses(c, Facet.class, Entity.NAME);
@ -53,7 +63,6 @@ public final class EntityTypeDefinitionImpl extends TypeDefinitionImpl implement
throw new RuntimeException("Type Hierachy Error for class " + clz.getSimpleName());
}
}

View File

@ -0,0 +1,80 @@
package org.gcube.informationsystem.types.impl.entities;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.types.TypeBinder;
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.impl.properties.ResourceEntryDefinitionImpl;
import org.gcube.informationsystem.types.reference.entities.ResourceTypeDefinition;
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
import org.gcube.informationsystem.types.reference.properties.ResourceEntryDefinition;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName(value=ResourceTypeDefinition.NAME)
public class ResourceTypeDefinitionImpl extends EntityTypeDefinitionImpl implements ResourceTypeDefinition {
/**
* Generated Serial Version UID
*/
private static final long serialVersionUID = -5964819173421808432L;
protected List<ResourceEntryDefinition> facets;
protected List<ResourceEntryDefinition> resources;
public ResourceTypeDefinitionImpl(Class<? extends Resource> clz) {
super(clz);
setResourceSchemaEntries(clz);
}
private void setResourceSchemaEntries(Class<? extends Resource> clz){
if(clz.isAnnotationPresent(ResourceSchema.class)) {
this.facets = new ArrayList<>();
this.resources = new ArrayList<>();
ResourceSchema[] resourceSchemaArray = clz.getAnnotationsByType(ResourceSchema.class);
for(ResourceSchemaEntry resourceSchemaEntry : resourceSchemaArray[0].facets()) {
ResourceEntryDefinitionImpl resourceSchemaEntryDefinition = new ResourceEntryDefinitionImpl();
resourceSchemaEntryDefinition.setSource(TypeBinder.getType(clz));
resourceSchemaEntryDefinition.setRelation(TypeBinder.getType(resourceSchemaEntry.relation()));
resourceSchemaEntryDefinition.setTarget(TypeBinder.getType(resourceSchemaEntry.facet()));
resourceSchemaEntryDefinition.setDescription(resourceSchemaEntry.description());
resourceSchemaEntryDefinition.setMin(resourceSchemaEntry.min());
resourceSchemaEntryDefinition.setMax(resourceSchemaEntry.max());
this.facets.add(resourceSchemaEntryDefinition);
}
for(ResourceSchemaRelatedEntry resourceSchemaRelatedEntry : resourceSchemaArray[0].resources()) {
ResourceEntryDefinition resourceSchemaEntryDefinition = new ResourceEntryDefinitionImpl();
resourceSchemaEntryDefinition.setSource(TypeBinder.getType(resourceSchemaRelatedEntry.source()));
resourceSchemaEntryDefinition.setRelation(TypeBinder.getType(resourceSchemaRelatedEntry.relation()));
resourceSchemaEntryDefinition.setTarget(TypeBinder.getType(resourceSchemaRelatedEntry.target()));
resourceSchemaEntryDefinition.setDescription(resourceSchemaRelatedEntry.description());
resourceSchemaEntryDefinition.setMin(resourceSchemaRelatedEntry.min());
resourceSchemaEntryDefinition.setMax(resourceSchemaRelatedEntry.max());
this.resources.add(resourceSchemaEntryDefinition);
}
}
}
@Override
public List<ResourceEntryDefinition> getFacets() {
return facets;
}
@Override
public List<ResourceEntryDefinition> getResources() {
return resources;
}
@Override
@JsonIgnore
public Set<PropertyDefinition> getProperties() {
return properties;
}
}

View File

@ -19,8 +19,7 @@ import org.gcube.informationsystem.types.reference.relations.RelationTypeDefinit
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName(value=RelationTypeDefinition.NAME)
public final class RelationTypeDefinitionImpl
extends TypeDefinitionImpl implements RelationTypeDefinition {
public final class RelationTypeDefinitionImpl extends TypeDefinitionImpl implements RelationTypeDefinition {
/**
* Generated Serial Version UID

View File

@ -0,0 +1,20 @@
package org.gcube.informationsystem.types.reference.entities;
import java.util.List;
import org.gcube.informationsystem.types.impl.entities.ResourceTypeDefinitionImpl;
import org.gcube.informationsystem.types.reference.properties.ResourceEntryDefinition;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize(as=ResourceTypeDefinitionImpl.class)
public interface ResourceTypeDefinition extends EntityTypeDefinition {
public static final String NAME = "ResourceTypeDefinition"; //ResourceTypeDefinition.class.getSimpleName();
public static final String DESCRIPTION = "Used to define a Resource Type";
public static final String VERSION = "1.0.0";
public List<ResourceEntryDefinition> getFacets();
public List<ResourceEntryDefinition> getResources();
}