package com.tandbergtv.watchpoint.pmm.action.asset.creation;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

public class InitializeVariablesAction implements ActionHandler{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1103703335509984122L;
	
	private static final String PROGRESS_STATUS_NAME = "Package Created";
	
	private static final String PROGRESS_STATUS_VALUE = "OK";
	
	private static final String PROGRESS_SUCCESS_STATUS = "true";
	
	private static final String PACKAGE_PATH_VARIABLE = "tarFilepath";
	
	
	

	public void execute(ExecutionContext context) throws Exception {
		
		long processId = context.getProcessInstance().getId();
		long tokenId = context.getProcessInstance().getRootToken().getId();
		
		/* set token id and process id */
		context.setVariable(AssetCreationVariables.PROCESS_ID.toString(), new Long(processId).toString());
		context.setVariable(AssetCreationVariables.TOKEN_ID.toString(), new Long(tokenId).toString());
		
		/* set variable for allocating specific resource */
		String name = "_resource_host_"+ new Long(tokenId).toString();
		String resourceIP = getStringValue(context, AssetCreationVariables.RESOURCE_IP.toString());
		context.setVariable(name, resourceIP);
		
		/* set PMM progress update message constants */
		context.setVariable(AssetCreationVariables.PROGRESS_NAME.toString(), PROGRESS_STATUS_NAME);
		context.setVariable(AssetCreationVariables.PROGRESS_VALUE.toString(), PROGRESS_STATUS_VALUE);
		context.setVariable(AssetCreationVariables.PROGRESS_STATUS.toString(), PROGRESS_SUCCESS_STATUS);
		
		/* set PMM store info constants */
		context.setVariable(AssetCreationVariables.PACKAGE_PATH_VARIABLE.toString(), PACKAGE_PATH_VARIABLE);
		
		/* set Constants for Export package request */
		context.setVariable(AssetCreationVariables.PUBLISH_SITES.toString(), "false");
		context.setVariable(AssetCreationVariables.SEND_ALL_ASSETS.toString(), "true");
	}
	/* Return the value of a process variable as a string, irrespective of the variable type */
	private String getStringValue(ExecutionContext context, String variableName) {
		Object value = context.getVariable(variableName);
		return (value == null) ? null : value.toString();
	}
	
}
