/**
 * OnSpecificDate.java
 * Created on Jul 3, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.job.timers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.tandbergtv.watchpoint.pmm.entities.RuleParameter;
import com.tandbergtv.watchpoint.pmm.entities.RuleParameterDataType;
import com.tandbergtv.watchpoint.pmm.entities.RuleTypeParameter;
import com.tandbergtv.watchpoint.pmm.job.ui.JobUIConstants;

/**
 * This class reads the job rule parameters to get the specific date for which the time will be
 * represented. The time is optional; if provided it will be used else its set to midnight.
 * It result object indicates that this is a one-time job to be executed at this
 * startdate time.
 *  
 * @author spuranik
 * 
 */
public class OnDate implements ITimeRepresentation {

	private static final Logger logger = Logger.getLogger(OnDate.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tandbergtv.watchpoint.pmm.job.timers.ITimeRepresentation#getTime(java.util.List,
	 *      java.util.List)
	 */
	public Object getTime(List<RuleTypeParameter> ruleTypeParams, List<RuleParameter> params,
			Date startDate) {

		// get the job date and time from the job rule params
		String jobDate = TimerHelper.getParameter(ruleTypeParams, params,
				RuleParameterDataType.DATE);
		String jobTime = TimerHelper.getParameter(ruleTypeParams, params,
				RuleParameterDataType.TIME);

		// if no job date is specified return null.
		if (jobDate == null) {
			logger.error("Job date not specified.");
			return null;
		}

		Date jobStartDate;
		try {
			SimpleDateFormat sf = new SimpleDateFormat(JobUIConstants.JOB_RULE_DATE_FORMAT);
			sf.setLenient(false);
			Date jDate = sf.parse(jobDate);
			jobStartDate = TimerHelper.getStartDateTime(jDate, jobTime);
			// this is not a periodic timer, so set the interval to 0.
			TimerResult result = new TimerResult(false, 0, jobStartDate);
			return result;
		} catch (ParseException e) {
			logger.error("Error while getting time: " + e.toString());
			return null;
		}
	}
}
