/* Copyright Dassault Systemes, 1999, 2009 */


package examples.development.components.mycommand;

import com.engineous.sdk.component.DefaultComponentExecutor;
import com.engineous.sdk.component.PluginRuntime;
import com.engineous.sdk.runtime.RtException;
import com.engineous.sdk.runtime.RuntimeEnv;
import com.engineous.sdk.vars.ScalarVariable;
import com.engineous.sdk.vars.Value;
import com.engineous.sdk.vars.VariableException;

/**
 * Runtime class for the MyCommand example component.
 * Just calls the OSCommand plugin.
 * There are comments explaining where additional processing could be added.
 */
public class MyCommandExecutor
		extends DefaultComponentExecutor {

	/**
	 * Execution method for MyCommand.  Just execute the embedded oscommand plugin.
	 * @param runEnv
	 * @throws RtException
	 * @see com.engineous.sdk.component.DefaultComponentExecutor#execute(com.engineous.sdk.runtime.RuntimeEnv)
	 */
	public void execute(RuntimeEnv runEnv)
			throws RtException {

		super.execute(runEnv);

		// Get the runtime plugin object to execute.
		PluginRuntime oscmd = (PluginRuntime)runEnv.getPlugin(MyCommandHandler.COMMAND_PLUGIN_NAME);

		// Put any pre-execute actions here.  The configuration of the OSCommand can be changed by
		// modifying the value of the Parameters that are substituted into the Command line.
		// Eventually there may be a runtime API for the plugin to allow more dynamic usage.

		// Check the platform by looking at the system property os.name
		// And select the Property that has the correct command for that platform.
		String os = System.getProperty("os.name", "unknown").toLowerCase();
		String commandPropertyName;
		if (os.startsWith("win")) {
			// Microsoft Windows, use notepad
			commandPropertyName = "wincommand";
		}
		else {
			// All unix platforms attempt to use 'gedit' off the path. 
			commandPropertyName = "unixcommand";
		}

		try {
			// Set up local Parameter 'command' to be the correct command for the current platform.
			ScalarVariable commandParam = runEnv.getContext().getScalar("command");
			Value commandValue = commandParam.getValueObj();

			// Get the correct command Property
			ScalarVariable commandProperty = (ScalarVariable)runEnv.getProperty(commandPropertyName);
			String commandName = commandProperty.getValueObj().getAsString();

			// Set the parameter that is substituted into the command line based on the value of the proper property
			commandValue.setValue(commandName);
		}
		catch (VariableException ex1) {
			// A VariableException means that a required parameter is missing or unusable.  Fail.
			throw new RtException(ex1, "Unable to access required Parameter or Property of MyComponent.");
		}


		// Now execute the OSCommand by calling the plugin.
		try {
			oscmd.execute(MyCommandHandler.COMMAND_PLUGIN_NAME, runEnv.getContext(), null);
		}
		catch (RtException ex) {
			// Perform any operations related to the command FAILING here.
			// In this case, just give up. 
			throw ex;
		}

		// Do any post-execution actions here.  The above 'catch' will have returned from this routine
		// if the command failed outright.

	}

}

