/**
 * Context.java
 * Created on May 13, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.entities;

import java.util.ArrayList;
import java.util.List;

/**
 * @author spuranik
 * 
 * This class provides a level of indirection for representing a partner/service This class is used
 * via its sub classes.
 */
public class Context {

	// id in db
	private long id;

	// the container type for this context
	private ContainerType containerType;

	// list of jobs associated with the container of this context
	List<Job> jobs;

	private List<ContainerProperty> properties = new ArrayList<ContainerProperty>(); 
	
	/**
	 * Default ctor
	 */
	public Context() {
		super();
	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @return the containerType
	 */
	public ContainerType getContainerType() {
		return containerType;
	}

	/**
	 * @param containerType
	 *            the containerType to set
	 */
	public void setContainerType(ContainerType containerType) {
		this.containerType = containerType;
	}

	/**
	 * @return the jobs
	 */
	public List<Job> getJobs() {
		return jobs;
	}

	/**
	 * @param jobs
	 *            the jobs to set
	 */
	public void setJobs(List<Job> jobs) {
		this.jobs = jobs;
	}

	/**
	 * @return the container of this context. Derived classes will return appropriate type casted
	 *         containers.
	 */
	public IContainer getContainer() {
		return null;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	// @Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		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 (getClass() != obj.getClass())
			return false;
		final Context other = (Context) obj;
		if (id != other.id)
			return false;
		return true;
	}

	/**
	 * @return the properties
	 */
	public List<ContainerProperty> getProperties() {
		return properties;
	}

	/**
	 * @param properties the properties to set
	 */
	public void setProperties(List<ContainerProperty> properties) {
		this.properties = properties;
	}
	
	public void addProperty(String name, String value) {
		if (name == null || name.isEmpty()) {
			return;
		}
		
		ContainerProperty property = propertyExists(name);
		if (property != null) {
			property.setValue(value);
		} else {
			this.properties.add(new ContainerProperty(name, value, this));
		}
	}

	public void removeProperty(String name) {
		ContainerProperty property = propertyExists(name);
		if (property != null) {
			properties.remove(property);
		}
	}
	
	public ContainerProperty propertyExists(String name) {
		for (ContainerProperty prop : this.properties) {
			if (prop.getName().equalsIgnoreCase(name))
				return prop;
		}
		return null;
	}
}
