/**
 * PitchDateCalculator.java
 * Created on Aug 19, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.action.schedule.creation;

import java.util.Calendar;
import java.util.Date;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

import com.tandbergtv.watchpoint.pmm.util.DateUtility;

/**
 * Adds the offset (number of days) to the job execution date and sets that as the pitch schedule
 * date.
 * 
 * @author spuranik
 * 
 */
public class PitchDateCalculator implements ActionHandler {

	private static final long serialVersionUID = -6056861255547498742L;
	private static final String PMM_DATE_FORMAT = "PMM.date.format";
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
	 */
	public void execute(ExecutionContext context) throws Exception {
		int offset = (Integer) context.getVariable(Variables.OFFSET_IN_DAYS);
		Date jobExecutionDate = (Date) context.getVariable(Variables.JOB_EXECUTION_DATE);

		// add the offset and get the pitch date. Set that back in the context.
		Calendar cal = Calendar.getInstance();
		cal.setTime(jobExecutionDate);
		cal.add(Calendar.DATE, offset);		
		Date pitchDate = cal.getTime();		

		context.setVariable(Variables.PITCH_DATE, DateUtility.formatDate(pitchDate, PMM_DATE_FORMAT));		
		context.setVariable(Variables.FORMATTED_PITCH_DATE, DateUtility.formatDate(pitchDate, PMM_DATE_FORMAT));
	}
	
}
