/**
 * RenameMetadataFile.java
 * Created on Aug 8, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.action.asset.arrival;

import java.io.File;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

import com.tandbergtv.watchpoint.pmm.util.PathConverter;

/**
 * This class renames the file (which should be a metadata file) to ADI.XML This ensures that the
 * metadata file is always called ADI.XML
 * 
 * @author spuranik
 * 
 */
public class RenameMetadataFile implements ActionHandler {

	private static final long serialVersionUID = 2736376997971892728L;
	private static String ADI_FILENAME = "ADI.XML";

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
	 */
	public void execute(ExecutionContext context) throws Exception {
		// get source filepath.
		String filePath = (String) context.getVariable(Variables.METADATA_FILE_PATH);

		// rename the metadata file to ADI.xml
		File source = new File(filePath);
		File dest = new File(source.getParent(), ADI_FILENAME);
		boolean renamed = source.renameTo(dest);

		if (!renamed) {
			throw new RuntimeException("Could not rename metadata file: " + filePath
					+ " to ADI.XML");
		}

		// set related context vars to point to the new filename
		context.setVariable(Variables.METADATA_FILE_PATH, dest.getAbsolutePath());
		context.setVariable(Variables.METADATA_FILE_UNC_PATH, PathConverter.getInstance()
				.convertUnixToUNC(dest.getAbsolutePath()));
		context.setVariable(Variables.MAPPING_LOOP_PATH, dest.getAbsolutePath());
	}
}
