246 lines
7.3 KiB
Java
246 lines
7.3 KiB
Java
package org.gcube.informationsystem.types.impl;
|
|
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.ParameterizedType;
|
|
import java.lang.reflect.TypeVariable;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
import org.gcube.com.fasterxml.jackson.annotation.JsonGetter;
|
|
import org.gcube.com.fasterxml.jackson.annotation.JsonInclude;
|
|
import org.gcube.com.fasterxml.jackson.annotation.JsonInclude.Include;
|
|
import org.gcube.com.fasterxml.jackson.annotation.JsonProperty;
|
|
import org.gcube.com.fasterxml.jackson.annotation.JsonSetter;
|
|
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;
|
|
import org.gcube.informationsystem.model.reference.properties.Header;
|
|
import org.gcube.informationsystem.types.TypeMapper;
|
|
import org.gcube.informationsystem.types.annotations.Abstract;
|
|
import org.gcube.informationsystem.types.annotations.ISProperty;
|
|
import org.gcube.informationsystem.types.impl.entities.EntityTypeImpl;
|
|
import org.gcube.informationsystem.types.impl.properties.PropertyDefinitionImpl;
|
|
import org.gcube.informationsystem.types.impl.properties.PropertyTypeImpl;
|
|
import org.gcube.informationsystem.types.impl.relations.RelationTypeImpl;
|
|
import org.gcube.informationsystem.types.reference.Type;
|
|
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
|
|
import org.gcube.informationsystem.utils.TypeVersion;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class TypeImpl implements Type {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(TypeImpl.class);
|
|
|
|
/**
|
|
* Generated Serial version UID
|
|
*/
|
|
private static final long serialVersionUID = -4333954207969059451L;
|
|
|
|
public final static String DESCRIPTION = "DESCRIPTION";
|
|
public final static String VERSION = "VERSION";
|
|
public final static String CHANGELOG = "CHANGELOG";
|
|
|
|
public static final Map<TypeVersion, String> DEFAULT_CHANGELOG_MAP;
|
|
|
|
static {
|
|
DEFAULT_CHANGELOG_MAP = new HashMap<>();
|
|
DEFAULT_CHANGELOG_MAP.put(new TypeVersion("1.0.0"), "First Version");
|
|
}
|
|
|
|
protected Header header;
|
|
|
|
protected String name;
|
|
protected String description;
|
|
protected TypeVersion version;
|
|
protected Map<TypeVersion, String> changelog;
|
|
|
|
@JsonProperty(value="abstract")
|
|
protected boolean abstractType;
|
|
protected Set<String> superClasses;
|
|
|
|
protected Set<PropertyDefinition> properties;
|
|
|
|
protected <E extends Element> Set<String> retrieveSuperClasses(Class<? extends E> type, Class<E> baseClass, String topSuperClass){
|
|
Set<String> interfaceList = new HashSet<>();
|
|
|
|
if(type==baseClass){
|
|
if(topSuperClass!=null) {
|
|
interfaceList.add(topSuperClass);
|
|
}
|
|
return interfaceList;
|
|
}
|
|
|
|
Class<?>[] interfaces = type.getInterfaces();
|
|
|
|
for (Class<?> interfaceClass : interfaces) {
|
|
|
|
if(!baseClass.isAssignableFrom(interfaceClass)){
|
|
continue;
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
Class<? extends Element> clz = (Class<? extends Element>) interfaceClass;
|
|
interfaceList.add(TypeMapper.getType(clz));
|
|
}
|
|
|
|
return interfaceList;
|
|
}
|
|
|
|
protected Set<PropertyDefinition> retrieveListOfProperties(Class<?> type){
|
|
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;
|
|
}
|
|
|
|
protected Class<?> getGenericClass(java.lang.reflect.Type type){
|
|
TypeVariable<?> typeVariable = (TypeVariable<?>) type;
|
|
java.lang.reflect.Type[] bounds = typeVariable.getBounds();
|
|
java.lang.reflect.Type t = bounds[0];
|
|
if(t instanceof ParameterizedType) {
|
|
ParameterizedType parameterizedType = (ParameterizedType) t;
|
|
return (Class<?>) parameterizedType.getRawType();
|
|
}
|
|
return (Class<?>) t;
|
|
}
|
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
public static Type getInstance(Class<? extends Element> clz) {
|
|
Type typeDefinition = null;
|
|
try {
|
|
if(EntityElement.class.isAssignableFrom(clz)) {
|
|
typeDefinition = EntityTypeImpl.getEntityTypeDefinitionInstance((Class<? extends EntityElement>) clz);
|
|
return typeDefinition;
|
|
} else if(RelationElement.class.isAssignableFrom(clz)){
|
|
typeDefinition = RelationTypeImpl.getRelationTypeDefinitionInstance((Class<? extends RelationElement<?,?>>) clz);
|
|
return typeDefinition;
|
|
} else if(PropertyElement.class.isAssignableFrom(clz)){
|
|
typeDefinition = new PropertyTypeImpl(clz);
|
|
return typeDefinition;
|
|
} else if(Type.class.isAssignableFrom(clz)) {
|
|
typeDefinition = new TypeImpl(clz);
|
|
return typeDefinition;
|
|
} else {
|
|
throw new RuntimeException("Serialization required");
|
|
}
|
|
} finally {
|
|
if(typeDefinition!=null) {
|
|
logger.debug("{} : {} ", clz, typeDefinition);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected TypeImpl() {}
|
|
|
|
protected TypeImpl(Class<? extends Element> clz) {
|
|
this.name = TypeMapper.getType(clz);
|
|
this.description = TypeMapper.getStaticStringFieldByName(clz, DESCRIPTION, "");
|
|
String versionString = TypeMapper.getStaticStringFieldByName(clz, VERSION, TypeVersion.MINIMAL_VERSION_STRING);
|
|
this.version = new TypeVersion(versionString);
|
|
|
|
this.changelog = TypeMapper.getStaticFieldByName(clz, CHANGELOG, DEFAULT_CHANGELOG_MAP);
|
|
|
|
if(!changelog.containsKey(version)) {
|
|
throw new RuntimeException("The Type " + name + " does not provided the appropriated changelog Map");
|
|
}
|
|
|
|
this.abstractType = false;
|
|
|
|
if(clz.isAnnotationPresent(Abstract.class)){
|
|
this.abstractType = true;
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public Header getHeader() {
|
|
return header;
|
|
}
|
|
|
|
@Override
|
|
public void setHeader(Header header){
|
|
this.header = header;
|
|
}
|
|
|
|
|
|
@Override
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
@Override
|
|
public String getDescription() {
|
|
return description;
|
|
}
|
|
|
|
@Override
|
|
public TypeVersion getVersion() {
|
|
return version;
|
|
}
|
|
|
|
@JsonGetter(value=VERSION_PROPERTY)
|
|
protected String getVersionAsString() {
|
|
return version.toString();
|
|
}
|
|
|
|
@JsonSetter(value = VERSION_PROPERTY)
|
|
protected void setVersion(String version) {
|
|
this.version = new TypeVersion(version);
|
|
}
|
|
|
|
@Override
|
|
public Map<TypeVersion, String> getChangelog() {
|
|
return changelog;
|
|
}
|
|
|
|
@JsonGetter(value=CHANGELOG_PROPERTY)
|
|
public Map<String, String> getChangelogwithVersionAsString() {
|
|
Map<String,String> map = new HashMap<>();
|
|
for(TypeVersion typeVersion : changelog.keySet()) {
|
|
map.put(typeVersion.toString(), changelog.get(typeVersion));
|
|
}
|
|
return map;
|
|
}
|
|
|
|
@JsonSetter(value = CHANGELOG_PROPERTY)
|
|
public void setChangelog(Map<String, String> changelog) {
|
|
this.changelog = new HashMap<>();
|
|
for(String version : changelog.keySet()) {
|
|
this.changelog.put(new TypeVersion(version), changelog.get(version));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean isAbstract() {
|
|
return abstractType;
|
|
}
|
|
|
|
@Override
|
|
public Set<String> getSuperClasses() {
|
|
return superClasses;
|
|
}
|
|
|
|
@JsonInclude(Include.NON_EMPTY)
|
|
public Set<PropertyDefinition> getProperties() {
|
|
return properties;
|
|
}
|
|
|
|
|
|
|
|
}
|