/*
 * Created on Jul 16, 2008 (C) Copyright TANDBERG Television Ltd.
 */

package com.tandbergtv.watchpoint.pmm.action.schedule.distribution;

import java.io.File;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

/**
 * @author Vijay Silva
 */
public class SetFTPDistributionTarget implements ActionHandler {

	/* Serialization UID */
	private static final long serialVersionUID = -8756091053261074006L;

	/**
	 * Sets the complete FTP URL to use when distributing the Schedule File.
	 * 
	 * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
	 */
	public void execute(ExecutionContext context) throws Exception {
		String path = this.getStringValue(context, Variables.SCHEDULE_FILE_PATH_DISTRIBUTION);
		String targetURL = this.getStringValue(context, Variables.DISTRIBUTION_TARGET); 
		
		File distributionFile = new File(path);
		if (!targetURL.endsWith("/")) 
			targetURL += "/";
		targetURL += distributionFile.getName();
		
		context.setVariable(Variables.DISTRIBUTION_FTP_URL, targetURL);
	}

	/* Get the string value for a variable */
	private String getStringValue(ExecutionContext context, String name) {
		Object value = context.getVariable(name);
		return (value != null) ? value.toString() : null;
	}
}
