/**
 * SetStatus.java
 * Created on Jun 25, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.action.asset.distribution;

import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

/**
 * This class sets the 'status' variable in the context with the value specified in the config file   
 * 
 * @author spuranik
 *
 */
public class SetStatusAction implements ActionHandler {

	private static final long serialVersionUID = 1L;

	// these strings should match the ones in the template.
	public static String STATUS_NAME = "statusName";
	public static String STATUS_VALUE = "statusValue";
	public static String SUCCESS_VALUE = "successValue";

	public static String ASSET_DISTRIBUTION_PROPERTIES = "/template-actions/assetDistribution.properties";
	public static String DISTRIBUTION_STATUS_NAME = "asset.distribution.status.name";
	public static String DISTRIBUTION_STATUS_VALUE = "asset.distribution.status.value";
	public static String STATUS_SUCCESS_VALUE = "asset.distribution.success.value";

	private static final Logger logger = Logger.getLogger(SetStatusAction.class);

	/* (non-Javadoc)
	 * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
	 */
	public void execute(ExecutionContext context) throws Exception {
		InputStream stream = this.getClass().getResourceAsStream(ASSET_DISTRIBUTION_PROPERTIES);
		Properties config = new Properties();
		config.load(stream);
		String statusName = config.getProperty(DISTRIBUTION_STATUS_NAME);
		String statusValue = config.getProperty(DISTRIBUTION_STATUS_VALUE);
		String statusSuccessValue = config.getProperty(STATUS_SUCCESS_VALUE);

		context.setVariable(STATUS_NAME, statusName);
		context.setVariable(STATUS_VALUE, statusValue);
		context.setVariable(SUCCESS_VALUE, statusSuccessValue);

		logger.debug("Set " + STATUS_NAME + " to: " + statusName + " , " + STATUS_VALUE + " to: "
				+ statusValue + " and " + SUCCESS_VALUE + " to: " + statusSuccessValue);
	}

}
