/*
 * Created on 2004-8-7
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package workflow.live.xpdl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import workflow.wfmc.xpdl.WorkflowActivity;
import workflow.wfmc.xpdl.WorkflowProcess;
import workflow.wfmc.xpdl.WorkflowProcessHeader;
import workflow.wfmc.xpdl.WorkflowTransition;
import workflow.live.LiveElement;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class LiveProcess extends LiveElement
{
	private String packageID;
	private boolean expired;

	private WorkflowProcessHeader processHeader;
	private Map attributes = new HashMap();
	
	private List transitions = new ArrayList();
	private List activities = new ArrayList();

	public LiveProcess()
	{
	}

	public LiveProcess(WorkflowProcess workflowProcess)
	{
		super(workflowProcess);
		
		this.packageID=workflowProcess.getWorkflowPackage().getUUID();
		this.attributes=workflowProcess.getExtendedAttributes();
		
		this.processHeader=workflowProcess.getProcessHeader();
		
		WorkflowTransition target = null;
		for (int i = 0; i < workflowProcess.getWorkflowTransitions().size(); ++i)
		{
			target = (WorkflowTransition) workflowProcess.getWorkflowTransitions().get(i);
			transitions.add(new LiveTransition(target));
		}
		
		WorkflowActivity target2 = null;
		for (int i = 0; i < workflowProcess.getWorkflowActivities().size(); ++i)
		{
			target2 = (WorkflowActivity) workflowProcess.getWorkflowActivities().get(i);
			activities.add(new LiveActivity(target2));
		}
	}

	public String getPackageID()
	{
		return (this.packageID);
	}

	public WorkflowProcessHeader getProcessHeader()
	{
		return (this.processHeader);
	}

	public boolean isExpired()
	{
		return (this.expired);
	}

	public LiveActivity findActivity(String activityId)
	{
		LiveActivity target = null;
		for (int i = 0; i < activities.size(); ++i)
		{
			target = (LiveActivity) activities.get(i);
			if (target.getUUID().equals(activityId))
			{
				return target;
			}
		}
		return null;
	}

	public LiveTransition findTransition(String transitionId)
	{
		LiveTransition target = null;
		for (int i = 0; i < transitions.size(); ++i)
		{
			target = (LiveTransition) transitions.get(i);
			if (target.getUUID().equals(transitionId))
			{
				return target;
			}
		}
		return null;
	}

	public LiveTransition findTransition(
		LiveActivity fromActivity,
		LiveActivity toActivity)
	{
		LiveTransition target = null;
		String fromID = null;
		String toID  = null;

		for (int i = 0; i < transitions.size(); ++i)
		{
			target = (LiveTransition) transitions.get(i);
			fromID = target.getFromActivityID();
			toID = target.getToActivityID();

			if (fromID.equals(fromActivity.getUUID()) && toID.equals(toActivity.getUUID()))
			{
				return target;
			}
		}
		return null;
	}

	public Map getExtendedAttributes()
	{
		return (this.attributes);
	}
}
