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

143 lines
4.3 KiB
Java

package org.gcube.informationsystem.types.impl.relations;
import java.io.StringWriter;
import org.gcube.informationsystem.base.reference.entities.BaseEntity;
import org.gcube.informationsystem.base.reference.properties.Header;
import org.gcube.informationsystem.base.reference.relations.BaseRelation;
import org.gcube.informationsystem.context.reference.relations.IsParentOf;
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;
import org.gcube.informationsystem.types.impl.TypeDefinitionImpl;
import org.gcube.informationsystem.types.reference.TypeDefinition;
import org.gcube.informationsystem.types.reference.entities.EntityTypeDefinition;
import org.gcube.informationsystem.types.reference.relations.RelationTypeDefinition;
import org.gcube.informationsystem.utils.ISMapper;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName(value=RelationTypeDefinition.NAME)
public final class RelationTypeDefinitionImpl<Out extends BaseEntity, In extends BaseEntity, Rel extends BaseRelation<Out,In>>
extends TypeDefinitionImpl<Rel> implements RelationTypeDefinition<Out,In,Rel> {
/**
* Generated Serial Version UID
*/
private static final long serialVersionUID = 2221831081869571296L;
private static final String EDGE_CLASS_NAME = "E";
@JsonInclude(JsonInclude.Include.NON_NULL)
protected String sourceType;
@JsonInclude(JsonInclude.Include.NON_NULL)
protected String targetType;
protected RelationTypeDefinitionImpl() {
super();
}
public RelationTypeDefinitionImpl(Class<Rel> clz) {
super(clz);
if(IsRelatedTo.class.isAssignableFrom(clz)){
@SuppressWarnings({"unchecked", "rawtypes"})
Class<? extends IsRelatedTo> c = (Class<? extends IsRelatedTo>) clz;
this.superClasses = retrieveSuperClasses(c, IsRelatedTo.class, Relation.NAME);
} else if(ConsistsOf.class.isAssignableFrom(clz)) {
@SuppressWarnings({"unchecked", "rawtypes"})
Class<? extends ConsistsOf> c = (Class<? extends ConsistsOf>) clz;
this.superClasses = retrieveSuperClasses(c, ConsistsOf.class, Relation.NAME);
} else if(RelationTypeDefinition.class.isAssignableFrom(clz)){
this.superClasses = retrieveSuperClasses(clz, BaseRelation.class, EDGE_CLASS_NAME);
this.superClasses.add(TypeDefinition.class.getSimpleName());
} else if(IsParentOf.class.isAssignableFrom(clz)){
this.superClasses = retrieveSuperClasses(clz, BaseRelation.class, EDGE_CLASS_NAME);
} else {
throw new RuntimeException("Type Hierachy Error");
}
java.lang.reflect.Type[] typeParameters = clz.getTypeParameters();
@SuppressWarnings("unchecked")
Class<Out> sourceClass = (Class<Out>) getGenericClass(typeParameters[0]);
@SuppressWarnings("unchecked")
Class<In> targetClass = (Class<In>) getGenericClass(typeParameters[1]);
this.sourceType = TypeBinder.getType(sourceClass);
this.targetType = TypeBinder.getType(targetClass);
}
public String getSourceType() {
return sourceType;
}
public String getTargetType() {
return targetType;
}
/*
* 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();
}
}
}
protected EntityTypeDefinition<Out> source;
protected EntityTypeDefinition<In> target;
@Override
public EntityTypeDefinition<Out> getSource() {
return source;
}
@Override
public void setSource(EntityTypeDefinition<Out> source) {
this.source = source;
}
@Override
public EntityTypeDefinition<In> getTarget() {
return target;
}
@Override
public void setTarget(EntityTypeDefinition<In> target) {
this.target = target;
}
}