package org.gcube.informationsystem.types.impl.entities; import java.io.StringWriter; import org.gcube.informationsystem.base.reference.entities.BaseEntity; import org.gcube.informationsystem.base.reference.properties.Header; import org.gcube.informationsystem.context.reference.entities.Context; import org.gcube.informationsystem.model.reference.entities.Entity; import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; 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.utils.ISMapper; import com.fasterxml.jackson.annotation.JsonTypeName; @JsonTypeName(value=EntityTypeDefinition.NAME) public final class EntityTypeDefinitionImpl extends TypeDefinitionImpl implements EntityTypeDefinition { /** * Generated Serial Version UID */ private static final long serialVersionUID = 2614315845213942880L; private static final String VERTEX_CLASS_NAME = "V"; protected EntityTypeDefinitionImpl() { super(); } public EntityTypeDefinitionImpl(Class clz) { super(clz); if(Resource.class.isAssignableFrom(clz)){ @SuppressWarnings({"unchecked"}) Class c = (Class) clz; this.superClasses = retrieveSuperClasses(c, Resource.class, Entity.NAME); } else if(Facet.class.isAssignableFrom(clz)){ @SuppressWarnings({"unchecked"}) Class c = (Class) clz; this.superClasses = retrieveSuperClasses(c, Facet.class, Entity.NAME); } else if(EntityTypeDefinition.class.isAssignableFrom(clz)){ this.superClasses = retrieveSuperClasses(clz, BaseEntity.class, VERTEX_CLASS_NAME); this.superClasses.add(TypeDefinition.class.getSimpleName()); } else if(Context.class.isAssignableFrom(clz)){ this.superClasses = retrieveSuperClasses(clz, BaseEntity.class, VERTEX_CLASS_NAME); } else { throw new RuntimeException("Type Hierachy Error"); } } /* * 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(); } } } }