package com.tandbergtv.watchpoint.pmm.entities;

import java.util.ArrayList;
import java.util.List;

import com.tandbergtv.workflow.core.ProcessPriority;

/**
 * This class represents the job instance created for a partner/service
 *  
 * @author spuranik
 */
public class Job {
	// id for all jobs not yet in db
	public static final int DEFAULT_ID = 0;
	private static final int HASHCODE_MULTIPLICATION_FACTOR = 5;
	private static final int HASHCODE_ADDITION_FACTOR = 101;

	// job id in db
	private long id;

	// job name provided by user
	private String name;

	// partner/service context, this job is associated with
	private Context context;

	// will be used as the work order priority
	private ProcessPriority priority;

	// template name (- version) associated with this job
	private String templateName;

	// all params part of the template
	private List<JobParameter> jobParams;

	// Rule instance associated with this job.
	private Rule rule;
	
	/**
	 * Default ctor
	 */
	public Job() {
		jobParams = new ArrayList<JobParameter>();
	}

	/**
	 * @param id
	 *            to be set as jobId
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @return jobId
	 */
	public long getId() {
		return id;
	}

	/**
	 * @return job name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            to be set as job name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return context id for this job
	 */
	public Context getContext() {
		return context;
	}

	/**
	 * @param contextId
	 *            to be set as this job's contextid
	 */
	public void setContext(Context context) {
		this.context = context;
	}

	/**
	 * @return priority for this job
	 */
	public ProcessPriority getPriority() {
		return priority;
	}

	/**
	 * @param priority
	 *            to be set for job
	 */
	public void setPriority(ProcessPriority priority) {
		this.priority = priority;
	}

	/**
	 * @return template name for this job
	 */
	public String getTemplateName() {
		return templateName;
	}

	/**
	 * @param templateName
	 *            to be set as job's template
	 */
	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}

	/**
	 * @return list of job params for this job
	 */
	public List<JobParameter> getJobParams() {
		return jobParams;
	}

	/**
	 * @param jobParams
	 *            for this job
	 */
	public void setJobParams(List<JobParameter> jobParams) {
		this.jobParams = jobParams;
	}

	/**
	 * @return rule for this job
	 */
	public Rule getRule() {
		return rule;
	}

	/**
	 * @param rule
	 *            to be set for this job
	 */
	public void setRule(Rule rule) {
		this.rule = rule;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Job) {
			Job jobObj = (Job) obj;

			if (jobObj.getId() != DEFAULT_ID && this.getId() != DEFAULT_ID) {
				return (jobObj.getId() == this.getId());
			} else {
				return super.equals(obj);
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		long hash = this.id * HASHCODE_MULTIPLICATION_FACTOR + HASHCODE_ADDITION_FACTOR;
		int hashCode = new Long(hash).hashCode();
		if (this.id == DEFAULT_ID) {
			hashCode = super.hashCode();
		}
		return hashCode;
	}

	/**
	 * @return
	 * 	true/false based on whether the rule for this job is associated
	 *  with titles or not.
	 */
	public boolean isTitleAssociated() {
		return this.getRule().getType().getTitlesAssociated();
	}
	
	/**
	 * Adds the given parameter to the list of job parameters.
	 * 
	 * @param parameter
	 */
	public void addJobParameter(JobParameter parameter) {
		jobParams.add(parameter);
	}

	/**
	 * Gets the value associated with a job parameter matching the provided name
	 * 
	 * @param name
	 * @return
	 */
	public JobParameter getJobParameter(String name) {
		for (JobParameter p : jobParams) {
			if (p.getName().equals(name)) {
				return p;
			}
		}
		return null;
	}
}
