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

160 lines
5.3 KiB
Java
Raw Normal View History

2019-10-24 11:26:49 +02:00
package org.gcube.informationsystem.types.impl.relations;
2019-10-24 19:50:43 +02:00
import java.io.StringWriter;
2019-11-04 18:06:46 +01:00
import java.lang.reflect.ParameterizedType;
2019-10-24 19:50:43 +02:00
2019-10-24 11:26:49 +02:00
import org.gcube.informationsystem.base.reference.entities.BaseEntity;
2019-10-24 19:50:43 +02:00
import org.gcube.informationsystem.base.reference.properties.Header;
2019-10-24 11:26:49 +02:00
import org.gcube.informationsystem.base.reference.relations.BaseRelation;
import org.gcube.informationsystem.context.reference.relations.IsParentOf;
2019-11-04 18:06:46 +01:00
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
2019-10-24 11:26:49 +02:00
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.types.TypeBinder;
2019-10-24 11:57:21 +02:00
import org.gcube.informationsystem.types.impl.TypeDefinitionImpl;
2019-10-24 15:03:14 +02:00
import org.gcube.informationsystem.types.reference.entities.EntityTypeDefinition;
import org.gcube.informationsystem.types.reference.relations.RelationTypeDefinition;
2019-10-24 19:50:43 +02:00
import org.gcube.informationsystem.utils.ISMapper;
2019-10-24 11:26:49 +02:00
import com.fasterxml.jackson.annotation.JsonInclude;
2019-10-24 15:03:14 +02:00
import com.fasterxml.jackson.annotation.JsonTypeName;
2019-10-24 11:26:49 +02:00
2019-10-24 15:03:14 +02:00
@JsonTypeName(value=RelationTypeDefinition.NAME)
2019-11-04 18:06:46 +01:00
public final class RelationTypeDefinitionImpl<ES extends EntityTypeDefinition<S>, ET extends EntityTypeDefinition<T>, S extends BaseEntity, T extends BaseEntity>
extends TypeDefinitionImpl implements RelationTypeDefinition<ES, ET, S,T> {
2019-10-24 11:26:49 +02:00
/**
2019-10-24 15:03:14 +02:00
* Generated Serial Version UID
2019-10-24 11:26:49 +02:00
*/
private static final long serialVersionUID = 2221831081869571296L;
2019-10-30 09:57:14 +01:00
// private static final String EDGE_CLASS_NAME = "E";
2019-10-24 11:26:49 +02:00
@JsonInclude(JsonInclude.Include.NON_NULL)
protected String sourceType;
@JsonInclude(JsonInclude.Include.NON_NULL)
protected String targetType;
protected RelationTypeDefinitionImpl() {
super();
}
2019-11-04 18:06:46 +01:00
public RelationTypeDefinitionImpl(Class<? extends BaseRelation<S,T>> clz) {
2019-10-24 11:26:49 +02:00
super(clz);
if(IsRelatedTo.class.isAssignableFrom(clz)){
2019-11-04 18:06:46 +01:00
@SuppressWarnings({"unchecked"})
Class<? extends IsRelatedTo<? extends Resource, ? extends Resource>> c = (Class<? extends IsRelatedTo<? extends Resource, ? extends Resource>>) clz;
2019-10-24 11:26:49 +02:00
this.superClasses = retrieveSuperClasses(c, IsRelatedTo.class, Relation.NAME);
} else if(ConsistsOf.class.isAssignableFrom(clz)) {
2019-11-04 18:06:46 +01:00
@SuppressWarnings({"unchecked"})
Class<? extends ConsistsOf<? extends Resource, ? extends Facet>> c = (Class<? extends ConsistsOf<? extends Resource, ? extends Facet>>) clz;
2019-10-24 11:26:49 +02:00
this.superClasses = retrieveSuperClasses(c, ConsistsOf.class, Relation.NAME);
} else if(RelationTypeDefinition.class.isAssignableFrom(clz)){
2019-10-30 09:57:14 +01:00
@SuppressWarnings({"unchecked", "rawtypes"})
Class<? extends RelationTypeDefinition> c = (Class<? extends RelationTypeDefinition>) clz;
this.superClasses = retrieveSuperClasses(c, RelationTypeDefinition.class, BaseRelation.NAME);
} else if(IsParentOf.class.isAssignableFrom(clz)){
2019-11-04 18:06:46 +01:00
@SuppressWarnings({"unchecked"})
2019-10-30 09:57:14 +01:00
Class<? extends IsParentOf> c = (Class<? extends IsParentOf>) clz;
this.superClasses = retrieveSuperClasses(c, IsParentOf.class, BaseRelation.NAME);
} else if(BaseRelation.class.isAssignableFrom(clz)){
this.superClasses = retrieveSuperClasses(clz, BaseRelation.class, null);
} else {
throw new RuntimeException("Type Hierachy Error");
2019-10-24 11:26:49 +02:00
}
java.lang.reflect.Type[] typeParameters = clz.getTypeParameters();
2019-11-04 18:06:46 +01:00
if(typeParameters.length==0) {
typeParameters = ((ParameterizedType) clz.getGenericInterfaces()[0]).getActualTypeArguments();
@SuppressWarnings("unchecked")
Class<S> sourceClass = (Class<S>) typeParameters[0];
@SuppressWarnings("unchecked")
Class<T> targetClass = (Class<T>) typeParameters[1];
this.sourceType = TypeBinder.getType(sourceClass);
this.targetType = TypeBinder.getType(targetClass);
}else {
@SuppressWarnings("unchecked")
Class<S> sourceClass = (Class<S>) getGenericClass(typeParameters[0]);
@SuppressWarnings("unchecked")
Class<T> targetClass = (Class<T>) getGenericClass(typeParameters[1]);
this.sourceType = TypeBinder.getType(sourceClass);
this.targetType = TypeBinder.getType(targetClass);
}
2019-10-24 11:26:49 +02:00
}
public String getSourceType() {
return sourceType;
}
public String getTargetType() {
return targetType;
}
2019-10-24 19:50:43 +02:00
/*
* Java does not support multiple inheritance.
* TypeDefinitionImpl is the superclass so that this class does not inherits the methods and field of BaseRelationImpl
* We need to copy them.
*/
protected Header header;
@Override
public Header getHeader() {
return header;
}
@Override
public void setHeader(Header header){
this.header = header;
}
@Override
public String toString(){
StringWriter stringWriter = new StringWriter();
try {
ISMapper.marshal(this, stringWriter);
return stringWriter.toString();
}catch(Exception e){
try {
ISMapper.marshal(this.header, stringWriter);
return stringWriter.toString();
} catch(Exception e1){
return super.toString();
}
}
}
2019-11-04 18:06:46 +01:00
protected ES source;
protected ET target;
2019-10-24 19:50:43 +02:00
2019-10-24 11:26:49 +02:00
@Override
2019-11-04 18:06:46 +01:00
public ES getSource() {
2019-10-24 19:50:43 +02:00
return source;
2019-10-24 11:26:49 +02:00
}
2019-10-24 19:50:43 +02:00
2019-10-24 11:26:49 +02:00
@Override
2019-11-04 18:06:46 +01:00
public void setSource(ES source) {
2019-10-24 19:50:43 +02:00
this.source = source;
2019-10-24 11:26:49 +02:00
}
2019-10-24 19:50:43 +02:00
2019-10-24 11:26:49 +02:00
@Override
2019-11-04 18:06:46 +01:00
public ET getTarget() {
2019-10-24 19:50:43 +02:00
return target;
2019-10-24 11:26:49 +02:00
}
2019-10-24 19:50:43 +02:00
2019-10-24 11:26:49 +02:00
@Override
2019-11-04 18:06:46 +01:00
public void setTarget(ET target) {
2019-10-24 19:50:43 +02:00
this.target = target;
2019-10-24 11:26:49 +02:00
}
}