information-system-model/src/main/java/org/gcube/informationsystem/types/impl/TypeImpl.java

273 lines
7.8 KiB
Java
Raw Normal View History

2019-10-24 11:57:21 +02:00
package org.gcube.informationsystem.types.impl;
import java.lang.reflect.Method;
2019-11-04 18:06:46 +01:00
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable;
2020-12-11 17:28:56 +01:00
import java.util.HashMap;
import java.util.HashSet;
2020-12-11 17:28:56 +01:00
import java.util.Map;
import java.util.Set;
import org.gcube.com.fasterxml.jackson.annotation.JsonGetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
2020-07-07 17:04:25 +02:00
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.AccessType;
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-02-03 10:51:29 +01:00
import org.gcube.informationsystem.model.reference.properties.Header;
2020-02-04 09:30:19 +01:00
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;
2020-02-03 10:51:29 +01:00
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;
/**
* @author Luca Frosini (ISTI - CNR)
*/
2020-02-03 10:51:29 +01:00
public class TypeImpl implements Type {
2020-02-03 10:51:29 +01:00
private static Logger logger = LoggerFactory.getLogger(TypeImpl.class);
2019-10-24 11:26:49 +02:00
/**
2020-02-03 10:51:29 +01:00
* Generated Serial version UID
2019-10-24 11:26:49 +02:00
*/
2020-02-03 10:51:29 +01:00
private static final long serialVersionUID = -4333954207969059451L;
/*
2019-10-24 19:50:43 +02:00
public final static String DESCRIPTION = "DESCRIPTION";
2020-12-11 09:47:14 +01:00
public final static String VERSION = "VERSION";
2020-12-11 17:28:56 +01:00
public final static String CHANGELOG = "CHANGELOG";
*/
2020-12-11 17:28:56 +01:00
public static final Map<TypeVersion, String> DEFAULT_CHANGELOG_MAP;
private static final Map<String, String> DEFAULT_CHANGELOG_MAP_KEY_AS_STRING;
2020-12-11 17:28:56 +01:00
static {
DEFAULT_CHANGELOG_MAP = new HashMap<>();
DEFAULT_CHANGELOG_MAP.put(new TypeVersion(TypeVersion.MINIMAL_VERSION_STRING), TypeVersion.MINIMAL_VERSION_DESCRIPTION);
DEFAULT_CHANGELOG_MAP_KEY_AS_STRING = new HashMap<>();
DEFAULT_CHANGELOG_MAP_KEY_AS_STRING.put(TypeVersion.MINIMAL_VERSION_STRING, TypeVersion.MINIMAL_VERSION_DESCRIPTION);
2020-12-11 17:28:56 +01:00
}
2020-02-03 10:51:29 +01:00
protected Header header;
protected String name;
protected String description;
protected TypeVersion version;
@JsonProperty(value = CHANGELOG_PROPERTY, required = false)
@JsonInclude(Include.NON_NULL)
2020-12-11 17:28:56 +01:00
protected Map<TypeVersion, String> changelog;
@JsonProperty(value = "abstract")
protected boolean abstractType;
protected Set<String> superClasses;
2020-01-23 17:14:44 +01:00
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;
2020-02-04 09:30:19 +01:00
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());
}
}
if(properties.size()==0) {
properties = null;
}
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) {
2019-11-04 18:06:46 +01:00
ParameterizedType parameterizedType = (ParameterizedType) t;
return (Class<?>) parameterizedType.getRawType();
}
return (Class<?>) t;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
2020-02-03 10:51:29 +01:00
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)) {
2020-02-03 10:51:29 +01:00
typeDefinition = new TypeImpl(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
}
}
protected TypeImpl() {
}
2020-02-03 10:51:29 +01:00
protected TypeImpl(Class<? extends Element> clz) {
2020-02-04 09:30:19 +01:00
this.name = TypeMapper.getType(clz);
this.description = TypeMapper.getTypeDescription(clz);
this.version = TypeMapper.getTypeVersion(clz);
this.changelog = TypeMapper.getTypeChangelog(clz);
this.abstractType = false;
if (clz.isAnnotationPresent(Abstract.class)) {
this.abstractType = true;
}
}
2020-02-03 10:51:29 +01:00
@Override
public Header getHeader() {
return header;
}
2020-02-03 10:51:29 +01:00
@Override
public void setHeader(Header header) {
2020-02-03 10:51:29 +01:00
this.header = header;
}
@Override
public String getName() {
return name;
}
2019-10-24 11:26:49 +02:00
@Override
public String getDescription() {
return description;
}
2020-12-11 09:47:14 +01:00
@Override
public TypeVersion getVersion() {
2020-12-11 09:47:14 +01:00
return version;
}
@JsonGetter(value = VERSION_PROPERTY)
2021-01-07 11:16:40 +01:00
public String getVersionAsString() {
return version.toString();
}
@JsonSetter(value = VERSION_PROPERTY)
2021-01-07 11:16:40 +01:00
public void setVersion(String version) {
this.version = new TypeVersion(version);
}
2020-12-11 17:28:56 +01:00
@Override
public Map<TypeVersion, String> getChangelog() {
return changelog;
}
@JsonGetter(value = CHANGELOG_PROPERTY)
@JsonInclude(Include.NON_NULL)
2021-01-15 17:57:06 +01:00
public Map<String, String> getChangelogWithVersionAsString() {
if(this.changelog==null) {
return DEFAULT_CHANGELOG_MAP_KEY_AS_STRING;
}
Map<String, String> map = new HashMap<>();
for (TypeVersion typeVersion : changelog.keySet()) {
2020-12-11 17:28:56 +01:00
map.put(typeVersion.toString(), changelog.get(typeVersion));
}
return map;
}
@JsonSetter(value=CHANGELOG_PROPERTY)
2020-12-11 17:28:56 +01:00
public void setChangelog(Map<String, String> changelog) {
if(changelog==null) {
this.changelog = DEFAULT_CHANGELOG_MAP;
return;
}
2020-12-11 17:28:56 +01:00
this.changelog = new HashMap<>();
for (String version : changelog.keySet()) {
2020-12-11 17:28:56 +01:00
this.changelog.put(new TypeVersion(version), changelog.get(version));
}
}
2019-10-24 11:26:49 +02:00
@Override
public boolean isAbstract() {
return abstractType;
}
2019-10-24 11:26:49 +02:00
@Override
public Set<String> getSuperClasses() {
return superClasses;
}
2020-02-03 10:51:29 +01:00
@JsonInclude(Include.NON_EMPTY)
2020-01-23 17:14:44 +01:00
public Set<PropertyDefinition> getProperties() {
return properties;
}
@Override
@JsonIgnore
public AccessType getAccessType() {
return null;
}
2020-12-11 09:47:14 +01:00
}