package com.tandbergtv.metadatamanager.model;

public abstract class Relation {

	private long id;
	private Asset ownerAsset;
	private Asset targetAsset;
	private int direction;
	private int addRevision = NextRevision.STARTING_REVISION_NUMBER;
	private int deleteRevision = NextRevision.NULL_REVISION_NUMBER;

	/**
	 * 
	 */
	public Relation() {
	}

	/**
	 * @param ownerAsset
	 * @param targetAsset
	 */
	public Relation(Asset ownerAsset, Asset targetAsset) {
		this.ownerAsset = ownerAsset;
		this.targetAsset = targetAsset;
	}
	
	public Relation(Asset ownerAsset, Asset targetAsset, NextRevision nextRevision) {
		this.ownerAsset = ownerAsset;
		this.targetAsset = targetAsset;
		this.addRevision = nextRevision.getRevisionNumber();
	}


	/**
	 * Checks if the current relationship being set is a valid relationship or
	 * not.
	 * 
	 * @return true If the realtionship is valid else false
	 */
	public abstract boolean isValid();

	public void setId(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public void setOwnerAsset(Asset ownerAsset) {
		this.ownerAsset = ownerAsset;
	}

	public Asset getOwnerAsset() {
		return ownerAsset;
	}

	/**
	 * @return the targetAsset
	 */
	public Asset getTargetAsset() {
		return targetAsset;
	}

	/**
	 * @param targetAsset
	 *            the targetAsset to set
	 */
	public void setTargetAsset(Asset targetAsset) {
		this.targetAsset = targetAsset;
	}

	/**
	 * 
	 * @return
	 */
	public int getDirection() {
		return direction;
	}

	/**
	 * 
	 * @param direction
	 */
	public void setDirection(int direction) {
		this.direction = direction;
	}

	public int getAddRevision() {
		return addRevision;
	}

	public void setAddRevision(int addRevision) {
		this.addRevision = addRevision;
	}

	public int getDeleteRevision() {
		return deleteRevision;
	}

	public void setDeleteRevision(int deleteRevision) {
		this.deleteRevision = deleteRevision;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((ownerAsset == null) ? 0 : ownerAsset.hashCode());
		result = prime * result
				+ ((targetAsset == null) ? 0 : targetAsset.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Relation))
			return false;
		Relation other = (Relation) obj;
		if (ownerAsset == null) {
			if (other.ownerAsset != null)
				return false;
		} else if (!ownerAsset.equals(other.ownerAsset))
			return false;
		if (targetAsset == null) {
			if (other.targetAsset != null)
				return false;
		} else if (!targetAsset.equals(other.targetAsset))
			return false;
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String tmp = "[" + id + "] (";
		
		if (this.ownerAsset != null)
			tmp = tmp + this.ownerAsset.getId();
		
		if (this.targetAsset != null)
			tmp = tmp + " " + this.targetAsset.getId();
		
		tmp = tmp + ")";
		
		return tmp;
	}
}
