/**
 * SetFTPDestinationAction.java
 * Created on Jun 25, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.action.asset.distribution.ftp;

import java.io.File;

import org.apache.log4j.Logger;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

/**
 * This class concatenates the ftp location source file name to prepare the destination 
 * path for the file being ftp'd. 
 * 
 * @author spuranik
 */
public class SetFTPDestinationAction implements ActionHandler {

	private static final long serialVersionUID = 1L;

	// these strings should match the start variables in the template
	private static String CONTENT_FILE_PATH = "contentPath";
	private static String FTP_LOCATION = "ftpLocation";
	private static String DESTINATION_LOCATION = "destinationFilepath";
	private static String FTP_PATH_SEPERATOR = "/";
	private static final Logger logger = Logger.getLogger(SetFTPDestinationAction.class);

	/* (non-Javadoc)
	 * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
	 */
	public void execute(ExecutionContext context) throws Exception {
		String ftpLocation = (String) context.getVariable(FTP_LOCATION);

		// end the ftp link with "/". The file name will be suffixed after this char.
		if (!ftpLocation.trim().endsWith(FTP_PATH_SEPERATOR)) {
			ftpLocation += FTP_PATH_SEPERATOR;
		}

		String sourceFilepath = (String) context.getVariable(CONTENT_FILE_PATH);
		File sourceFile = new File(sourceFilepath);

		// this assumes that the ftp location has the filepath separator at the end.
		String destination = ftpLocation + sourceFile.getName();

		context.setVariable(DESTINATION_LOCATION, destination);
		logger.debug("Set destination filepath to: " + destination);
	}

}
