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> extends TypeDefinitionImpl implements RelationTypeDefinition { /** * 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 clz) { super(clz); if(IsRelatedTo.class.isAssignableFrom(clz)){ @SuppressWarnings({"unchecked", "rawtypes"}) Class c = (Class) clz; this.superClasses = retrieveSuperClasses(c, IsRelatedTo.class, Relation.NAME); } else if(ConsistsOf.class.isAssignableFrom(clz)) { @SuppressWarnings({"unchecked", "rawtypes"}) Class c = (Class) 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 sourceClass = (Class) getGenericClass(typeParameters[0]); @SuppressWarnings("unchecked") Class targetClass = (Class) 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 source; protected EntityTypeDefinition target; @Override public EntityTypeDefinition getSource() { return source; } @Override public void setSource(EntityTypeDefinition source) { this.source = source; } @Override public EntityTypeDefinition getTarget() { return target; } @Override public void setTarget(EntityTypeDefinition target) { this.target = target; } }