package com.tandbergtv.watchpoint.pmm.action.asset.arrival;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

import com.tandbergtv.watchpoint.pmm.util.PathConverter;

/**
 * @author Raj Prakash
 */
public class InitializeLocalVariables implements ActionHandler {

	private static final long serialVersionUID = -4845781453389373424L;
	
	// The metadata file extension
	private static final String METADATA_FILE_EXTENSION = ".xml";

	/** 
	 * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
	 */
	public void execute(ExecutionContext context) throws Exception {
		String filePath = getStringValue(context, Variables.MAPPING_LOOP_PATH);
		
		//if metadata file
		if(filePath.toLowerCase().endsWith(METADATA_FILE_EXTENSION)) {
			String metadataFileUNCPath = PathConverter.getInstance().convertUnixToUNC(filePath);

			context.setVariable(Variables.IS_METADATA_FILE, true);
			context.setVariable(Variables.METADATA_FILE_PATH, filePath);
			context.setVariable(Variables.METADATA_FILE_UNC_PATH, metadataFileUNCPath);
		} else {
			context.setVariable(Variables.IS_METADATA_FILE, false);
		}
	}

	/*
	 * Gets the String value of the variable (in case the data type is not string). Uses toString()
	 * for all data types.
	 */
	private String getStringValue(ExecutionContext context, String variableName) {
		Object value = context.getVariable(variableName);
		return (value != null) ? value.toString() : null;
	}
	
}
