2019-10-24 11:57:21 +02:00
|
|
|
package org.gcube.informationsystem.types.impl;
|
2019-10-23 18:19:55 +02:00
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
2019-11-04 18:06:46 +01:00
|
|
|
import java.lang.reflect.ParameterizedType;
|
2019-10-23 18:19:55 +02:00
|
|
|
import java.lang.reflect.TypeVariable;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2019-11-05 18:43:49 +01:00
|
|
|
import org.gcube.informationsystem.base.impl.ERImpl;
|
2020-01-30 10:26:43 +01:00
|
|
|
import org.gcube.informationsystem.base.reference.Element;
|
|
|
|
import org.gcube.informationsystem.base.reference.entities.EntityElement;
|
|
|
|
import org.gcube.informationsystem.base.reference.properties.PropertyElement;
|
|
|
|
import org.gcube.informationsystem.base.reference.relations.RelationElement;
|
2020-01-23 17:14:44 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.entities.Resource;
|
2019-10-24 11:57:21 +02:00
|
|
|
import org.gcube.informationsystem.types.TypeBinder;
|
2019-10-23 18:19:55 +02:00
|
|
|
import org.gcube.informationsystem.types.annotations.Abstract;
|
|
|
|
import org.gcube.informationsystem.types.annotations.ISProperty;
|
2020-01-30 10:26:43 +01:00
|
|
|
import org.gcube.informationsystem.types.impl.entities.EntityTypeImpl;
|
2019-10-23 18:19:55 +02:00
|
|
|
import org.gcube.informationsystem.types.impl.properties.PropertyDefinitionImpl;
|
2020-01-30 10:26:43 +01:00
|
|
|
import org.gcube.informationsystem.types.impl.properties.PropertyTypeImpl;
|
|
|
|
import org.gcube.informationsystem.types.impl.relations.RelationTypeImpl;
|
2019-10-23 18:19:55 +02:00
|
|
|
import org.gcube.informationsystem.types.reference.TypeDefinition;
|
|
|
|
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
2019-10-24 15:03:14 +02:00
|
|
|
import com.fasterxml.jackson.annotation.JsonTypeName;
|
2019-10-23 18:19:55 +02:00
|
|
|
|
2019-10-24 15:03:14 +02:00
|
|
|
// @JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY)
|
|
|
|
@JsonTypeName(value=TypeDefinition.NAME)
|
2019-11-05 18:43:49 +01:00
|
|
|
public class TypeDefinitionImpl extends ERImpl implements TypeDefinition {
|
2019-10-24 11:26:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generated Serial Version UID
|
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 2698204820689338513L;
|
2019-10-23 18:19:55 +02:00
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(TypeDefinitionImpl.class);
|
|
|
|
|
2019-10-24 19:50:43 +02:00
|
|
|
public final static String DESCRIPTION = "DESCRIPTION";
|
2019-10-23 18:19:55 +02:00
|
|
|
|
|
|
|
protected String name;
|
|
|
|
protected String description;
|
|
|
|
@JsonProperty(value="abstract")
|
|
|
|
protected boolean abstractType;
|
|
|
|
protected Set<String> superClasses;
|
2020-01-16 16:48:08 +01:00
|
|
|
|
2020-01-23 17:14:44 +01:00
|
|
|
protected Set<PropertyDefinition> properties;
|
|
|
|
|
2020-01-30 10:26:43 +01:00
|
|
|
protected <ISM extends Element> Set<String> retrieveSuperClasses(Class<? extends ISM> type, Class<ISM> baseClass, String topSuperClass){
|
2019-10-23 18:19:55 +02:00
|
|
|
Set<String> interfaceList = new HashSet<>();
|
|
|
|
|
|
|
|
if(type==baseClass){
|
2019-10-28 17:36:09 +01:00
|
|
|
if(topSuperClass!=null) {
|
|
|
|
interfaceList.add(topSuperClass);
|
|
|
|
}
|
2019-10-23 18:19:55 +02:00
|
|
|
return interfaceList;
|
|
|
|
}
|
|
|
|
|
|
|
|
Class<?>[] interfaces = type.getInterfaces();
|
|
|
|
|
|
|
|
for (Class<?> interfaceClass : interfaces) {
|
|
|
|
|
|
|
|
if(!baseClass.isAssignableFrom(interfaceClass)){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2020-01-30 10:26:43 +01:00
|
|
|
Class<? extends Element> clz = (Class<? extends Element>) interfaceClass;
|
2019-10-23 18:19:55 +02:00
|
|
|
interfaceList.add(TypeBinder.getType(clz));
|
|
|
|
}
|
|
|
|
|
|
|
|
return interfaceList;
|
|
|
|
}
|
|
|
|
|
2020-01-20 10:24:55 +01:00
|
|
|
protected Set<PropertyDefinition> retrieveListOfProperties(Class<?> type){
|
2019-10-23 18:19:55 +02:00
|
|
|
Set<PropertyDefinition> properties = new HashSet<>();
|
|
|
|
for (Method m : type.getDeclaredMethods()){
|
|
|
|
m.setAccessible(true);
|
|
|
|
if(m.isAnnotationPresent(ISProperty.class)){
|
|
|
|
if(m.isBridge()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ISProperty propAnnotation = m.getAnnotation(ISProperty.class);
|
|
|
|
PropertyDefinition prop = new PropertyDefinitionImpl(propAnnotation, m);
|
|
|
|
properties.add(prop);
|
|
|
|
logger.trace("Property {} retrieved in type {} ", prop, type.getSimpleName());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
2019-11-08 18:15:42 +01:00
|
|
|
protected Class<?> getGenericClass(java.lang.reflect.Type type){
|
2019-10-23 18:19:55 +02:00
|
|
|
TypeVariable<?> typeVariable = (TypeVariable<?>) type;
|
|
|
|
java.lang.reflect.Type[] bounds = typeVariable.getBounds();
|
|
|
|
java.lang.reflect.Type t = bounds[0];
|
2019-11-04 18:06:46 +01:00
|
|
|
if(t instanceof ParameterizedType) {
|
|
|
|
ParameterizedType parameterizedType = (ParameterizedType) t;
|
|
|
|
return (Class<?>) parameterizedType.getRawType();
|
|
|
|
}
|
2019-10-23 18:19:55 +02:00
|
|
|
return (Class<?>) t;
|
|
|
|
}
|
|
|
|
|
2019-10-24 11:26:49 +02:00
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
2020-01-30 10:26:43 +01:00
|
|
|
public static TypeDefinition getInstance(Class<? extends Element> clz) {
|
2020-01-15 11:55:39 +01:00
|
|
|
TypeDefinition typeDefinition = null;
|
|
|
|
try {
|
2020-01-30 10:26:43 +01:00
|
|
|
if(EntityElement.class.isAssignableFrom(clz)) {
|
|
|
|
typeDefinition = EntityTypeImpl.getEntityTypeDefinitionInstance((Class<? extends EntityElement>) clz);
|
2020-01-15 11:55:39 +01:00
|
|
|
return typeDefinition;
|
2020-01-30 10:26:43 +01:00
|
|
|
} else if(RelationElement.class.isAssignableFrom(clz)){
|
|
|
|
typeDefinition = RelationTypeImpl.getRelationTypeDefinitionInstance((Class<? extends RelationElement<?,?>>) clz);
|
2020-01-15 11:55:39 +01:00
|
|
|
return typeDefinition;
|
2020-01-30 10:26:43 +01:00
|
|
|
} else if(PropertyElement.class.isAssignableFrom(clz)){
|
|
|
|
typeDefinition = new PropertyTypeImpl(clz);
|
2020-01-15 11:55:39 +01:00
|
|
|
return typeDefinition;
|
|
|
|
} else if(TypeDefinition.class.isAssignableFrom(clz)) {
|
|
|
|
typeDefinition = new TypeDefinitionImpl(clz);
|
|
|
|
return typeDefinition;
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException("Serialization required");
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if(typeDefinition!=null) {
|
|
|
|
logger.debug("{} : {} ", clz, typeDefinition);
|
|
|
|
}
|
2019-10-24 11:26:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 17:36:09 +01:00
|
|
|
protected TypeDefinitionImpl() {}
|
|
|
|
|
2020-01-30 10:26:43 +01:00
|
|
|
protected TypeDefinitionImpl(Class<? extends Element> clz) {
|
2019-10-23 18:19:55 +02:00
|
|
|
this.name = TypeBinder.getType(clz);
|
|
|
|
this.description = TypeBinder.getStaticStringFieldByName(clz, DESCRIPTION, "");
|
|
|
|
this.abstractType = false;
|
|
|
|
|
|
|
|
if(clz.isAnnotationPresent(Abstract.class)){
|
|
|
|
this.abstractType = true;
|
|
|
|
}
|
|
|
|
|
2020-01-23 17:14:44 +01:00
|
|
|
if(!Resource.class.isAssignableFrom(clz)) {
|
|
|
|
this.properties = retrieveListOfProperties(clz);
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:55:39 +01:00
|
|
|
}
|
|
|
|
|
2019-10-23 18:19:55 +02:00
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2019-10-24 11:26:49 +02:00
|
|
|
@Override
|
2019-10-23 18:19:55 +02:00
|
|
|
public String getDescription() {
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
2019-10-24 11:26:49 +02:00
|
|
|
@Override
|
2019-10-23 18:19:55 +02:00
|
|
|
public boolean isAbstract() {
|
|
|
|
return abstractType;
|
|
|
|
}
|
|
|
|
|
2019-10-24 11:26:49 +02:00
|
|
|
@Override
|
2019-10-23 18:19:55 +02:00
|
|
|
public Set<String> getSuperClasses() {
|
|
|
|
return superClasses;
|
|
|
|
}
|
2020-01-16 14:00:34 +01:00
|
|
|
|
2020-01-23 17:14:44 +01:00
|
|
|
@Override
|
|
|
|
public Set<PropertyDefinition> getProperties() {
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:19:55 +02:00
|
|
|
}
|