/*
 * Created on Jul 9, 2008 (C) Copyright TANDBERG Television Ltd.
 */

package com.tandbergtv.watchpoint.pmm.action;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

import com.tandbergtv.watchpoint.pmm.util.TemplateProperties;

/**
 * Persists the value of the variable containing the Allocated Resource for the current Token to a
 * different variable for the Token.
 * 
 * @author Vijay Silva
 */
public class StoreAllocatedResource implements ActionHandler {

	/* Serialization UID */
	private static final long serialVersionUID = 7471577048158647411L;

	/* Property containing the allocated resource variable name */
	private static final String ALLOCATED_RESOURCE_VARIABLE = "General.AllocatedResourceVariable";

	/* The Variable Name to store the last Allocated Resource for the current Token */
	private String variableName;

	/**
	 * Get the value of the '_allocated_resource' variable persisted by the framework and store in a
	 * specified process variable
	 * 
	 * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
	 */
	public void execute(ExecutionContext context) throws ActionException {
		if (this.variableName == null || this.variableName.trim().length() == 0) {
			String msg = "The variable name for which to store the last allocated resource "
					+ "is no specified (blank / null).";
			throw new ActionException(msg);
		}

		String resourceVariableName = TemplateProperties.getString(ALLOCATED_RESOURCE_VARIABLE);
		resourceVariableName += context.getToken().getId();
		Object value = context.getVariable(resourceVariableName);
		context.setVariable(this.variableName, value);
	}
}
