/**
 * DefaultHandler.java
 * Created on May 20, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.job.handler;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.tandbergtv.watchpoint.pmm.entities.JobParameter;
import com.tandbergtv.watchpoint.pmm.job.callback.CallbackHelper;
import com.tandbergtv.watchpoint.pmm.job.referenceEvaluator.JobData;
import com.tandbergtv.watchpoint.pmm.job.referenceEvaluator.ReferenceEvaluatorChain;
import com.tandbergtv.watchpoint.pmm.job.util.JobScheduleInfoConstants;

/**
 * This is the default implementation of the handler for a job. This class calls the evaluator chain
 * to evaluate any parameter refs in the job parameters and creates a work order. No search is
 * performed.
 * 
 * @author spuranik
 */
public class DefaultHandler implements IJobHandler {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tandbergtv.watchpoint.pmm.job.handler.IJobHandler#executeJob(java.util.Map)
	 */
	@SuppressWarnings("unchecked")
	public void executeJob(Map<String, Object> callbackInfo, Date jobExecutionDate) {
		// template name
		String templateName = (String) callbackInfo
				.get(JobScheduleInfoConstants.JOB_SELECTED_TEMPLATE_NAME);

		// work order priority
		String priority = (String) callbackInfo.get(JobScheduleInfoConstants.JOB_PRIORITY);

		// resolve all parameter references
		ReferenceEvaluatorChain evaluatorChain = ReferenceEvaluatorChain.getInstance();
		// evaluate 'General', 'Partner' and 'Service' parameter reference types.
		JobData jobData = new JobData(jobExecutionDate);
		evaluatorChain.evaluate(jobData, callbackInfo);

		List<JobParameter> jobParameters = (List<JobParameter>) callbackInfo
				.get(JobScheduleInfoConstants.JOB_PARAMETERS);

		// kick off the work order
		CallbackHelper.createWorkOrder(templateName, priority, jobParameters);
	}
}
