/**
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 *
 * Copyright (C)
 * <a href="http://www.inab.org">Spanish National Institute of Bioinformatics (INB)</a>
 * <a href="http://www.bsc.es">Barcelona Supercomputing Center (BSC)</a>
 * <a href="http://inb.bsc.es">Computational Node 6</a>
 */

package org.inb.biomoby.shared.registry;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Dmitry Repchevsky
 */

@XmlRootElement(name="relationship")
public class Relationship<T extends AbstractTypeEntity> implements Serializable, Cloneable
{
    public static enum RELATIONSHIP_TYPE {ISA, HAS, HASA};
    public static enum DIRECTION_TYPE {root, leaves};
    
    private RELATIONSHIP_TYPE relationshipType;
    private ArrayList<T> entityTypeList;
    
    private String lsid;
    
    @XmlElement(name="relationship_type")
    public RELATIONSHIP_TYPE getRelationshipType()
    {
        return relationshipType;
    }
    
    public void setRelationshipType(RELATIONSHIP_TYPE relationshipType)
    {
        this.relationshipType = relationshipType;
    }
    
    public void addEntityType(T entityType)
    {
        getEntityTypeList().add(entityType);
    }

    @XmlElementRefs({
      @XmlElementRef(type=ObjectType.class),
      @XmlElementRef(type=ServiceType.class)})
    public List<T> getEntityTypeList()
    {
        if (entityTypeList == null)
        {
            entityTypeList = new ArrayList<T>();
        }
        
        return entityTypeList;
    }
    
    public String getLsid()
    {
        return lsid;
    }

    public void setLsid(String lsid)
    {
        this.lsid = lsid;
    }

    @Override
    public Relationship<T> clone()
    {
        Relationship<T> clone;

        try
        {
            clone = (Relationship<T>)super.clone();

            if (entityTypeList != null)
            {
                clone.entityTypeList = (ArrayList<T>)entityTypeList.clone();
                for (int i = 0, n = clone.entityTypeList.size(); i < n; i++)
                {
                    T entity = clone.entityTypeList.get(i);
                    clone.entityTypeList.set(i, (T)entity.clone());
                }
            }
        }
        catch(CloneNotSupportedException ex)
        {
            clone = null;
        }

        return clone;
    }
}
