//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE----------------------------
package org.huihoo.willow.core;

import org.huihoo.willow.LifecycleException;
import org.huihoo.workflow.event.EventAutoShutdown;
import org.huihoo.workflow.event.EventQueue;
import org.huihoo.workflow.selector.WorkflowRouteEvalutor;
import org.huihoo.workflow.server.WorkflowConfig;
import org.huihoo.workflow.server.WorkflowXPDL;
import org.huihoo.workflow.store.WorkflowTemplate;
import org.huihoo.workflow.xpdl.WorkflowPackage;

public class StandardXPDL extends ContainerBase implements WorkflowXPDL
{
	protected WorkflowPackage workflowPackage;
	
	protected EventQueue eventQueue;
	protected WorkflowRouteEvalutor workflowRouteEvalutor;

	protected WorkflowTemplate workflowTemplate;
	protected WorkflowConfig config;
	
	public StandardXPDL(WorkflowPackage workflowPackage, WorkflowTemplate workflowTemplate)
	{
		this.workflowPackage = workflowPackage;
		this.workflowTemplate = workflowTemplate;
		this.workflowTemplate.setOwnerWorkflowXPDL(this);

		this.eventQueue = new EventQueue();
	}

	public WorkflowTemplate getWorkflowTemplate()
	{
		return workflowTemplate;
	}

	public void setWorkflowConfig(WorkflowConfig config)
	{
		this.config = config;
	}
	public WorkflowConfig getWorkflowConfig()
	{
		return this.config;
	}

	public String getUUID()
	{
		return workflowPackage.getUUID();
	}

	public WorkflowPackage getWorkflowPackage()
	{
		return this.workflowPackage;
	}

	public EventQueue getEventQueue()
	{
		return this.eventQueue;
	}

	public void setRouteEvalutor(WorkflowRouteEvalutor workflowRouteEvalutor)
	{
		this.workflowRouteEvalutor = workflowRouteEvalutor;
	}

	public WorkflowRouteEvalutor getRouteEvalutor()
	{
		return this.workflowRouteEvalutor;
	}

	public synchronized void start() throws LifecycleException
	{
		if (started)
		{
			return;
		}

		super.start();
	}

	public synchronized void stop() throws LifecycleException
	{
		// Validate and update our current component state
		if (!started)
		{
			throw new LifecycleException(sm.getString("containerBase.notStarted", logName()));
		}
		
		eventQueue.postEvent(EventAutoShutdown.getShutdownEvent());
		
		super.stop();
	}
}
