//----------------------------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.workflow.coreImpl.xpdl;
import java.util.ArrayList;
import java.util.List;

import org.huihoo.workflow.xpdl.ExtendedAttribute;
import org.huihoo.workflow.xpdl.WorkflowApplication;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowPackageHeader;
import org.huihoo.workflow.xpdl.WorkflowParameter;
import org.huihoo.workflow.xpdl.WorkflowProcess;
import org.huihoo.workflow.xpdl.WorkflowScript;
import org.huihoo.workflow.xpdl.XPDLGlobals;

public class WorkflowPackageImpl extends WorkflowPackage
{
	protected WorkflowScript script;
	protected WorkflowPackageHeader packageHeader = null;

	protected List applications = new ArrayList();
	protected List parameters = new ArrayList();
	protected List processes = new ArrayList();
  
	public WorkflowPackageImpl()
	{
		super();
	}
	
	public WorkflowPackageImpl(String uuid,String name)
	{
		super(uuid,name);
	}
	
	public WorkflowPackageImpl(String uuid,String name,String description)
	{
		super(uuid,name,description);
	}
	
	public WorkflowPackageHeader getPackageHeader()
	{
		return (this.packageHeader);
	}
	public void setPackageHeader(WorkflowPackageHeader packageHeader)
	{
		this.packageHeader = packageHeader;
	}
	public List getApplications()
	{
		return (this.applications);
	}
	public WorkflowApplication findApplication(String appId)
	{
		if(appId == null)
		{
			return null;
		}
		
		int sizeApp = applications.size();
		for (int i = 0; i < sizeApp; ++i)
		{
			WorkflowApplication app= (WorkflowApplication) applications.get(i);
			if (appId.equals(app.getUUID()))
			{
				return app;
			}
		}
		return null;
	}

	public WorkflowApplication findApplication(String appNameOrID,boolean findByUUID)
	{
		if(appNameOrID == null)
		{
			return null;
		}
		
		if(findByUUID)
		{
			return findApplication(appNameOrID);
		}
		
		int sizeApp = applications.size();
		for (int i = 0; i < sizeApp; ++i)
		{
			WorkflowApplication app= (WorkflowApplication) applications.get(i);
			if (appNameOrID.equals(app.getName()))
			{
				return app;
			}
		}
		return null;
	}
		
	public List getDataFields()
	{
		return (this.parameters);
	}
	public WorkflowParameter findDataField(String paramId)
	{
		if(paramId == null)
		{
			return null;
		}
		
		int sizeParam = parameters.size();
		for (int i = 0; i < sizeParam; ++i)
		{
			WorkflowParameter  parameter = (WorkflowParameter) parameters.get(i);
			if (paramId.equals(parameter.getUUID()))
			{
				return parameter;
			}
		}
		return null;
	}
	
	public  WorkflowParameter        findDataField(String paramNameOrID,boolean findByUUID)
	{
		if(paramNameOrID == null)
		{
			return null;
		}
		
		if(findByUUID)
		{
			return findDataField(paramNameOrID);
		}
		
		int sizeParam = parameters.size();
		for (int i = 0; i < sizeParam; ++i)
		{
			WorkflowParameter  parameter = (WorkflowParameter) parameters.get(i);
			if (paramNameOrID.equals(parameter.getName()))
			{
				return parameter;
			}
		}
		return null;
	}
	
	public List getWorkflowProcesses()
	{
		return (this.processes);
	}
	public WorkflowProcess findWorkflowProcess(String processId)
	{
		if(processId == null)
		{
			return null;
		}
		int sizeProcess = processes.size();
		for (int i = 0; i < sizeProcess; ++i)
		{
			WorkflowProcess workflowProcess = (WorkflowProcess) processes.get(i);
			if (processId.equals(workflowProcess.getUUID()))
			{
				return workflowProcess;
			}
		}
		return null;
	}
	
	public WorkflowProcess findWorkflowProcess(String processNameOrID,boolean findByUUID)
	{
		if(processNameOrID == null)
		{
			return null;
		}
		
		if(findByUUID)
		{
			return findWorkflowProcess(processNameOrID);
		}
		
		int sizeProcess = processes.size();
		WorkflowProcess workflowProcess = null;
		for (int i = 0; i < sizeProcess; ++i)
		{
			workflowProcess = (WorkflowProcess) processes.get(i);
			if (processNameOrID.equals(workflowProcess.getName()))
			{
				return workflowProcess;
			}
		}
		return null;
	}
	
	public WorkflowScript getScript()
	{
		return (this.script);
	}
	public void setScript(WorkflowScript script_)
	{
		this.script = script_;
	}
	
	//--------------------------------------------------------------------
	public void addWorkflowProcess(WorkflowProcess workflowProcess)
	{
		if(processes.contains(workflowProcess))
		{
			return;
		}
		
		workflowProcess.setWorkflowPackage(this);
		this.processes.add(workflowProcess);
	}
	
	public WorkflowProcess removeWorkflowProcess(WorkflowProcess workflowProcess)
	{
		if(!processes.contains(workflowProcess))
		{
			return null;
		}

		this.processes.remove(workflowProcess);
		workflowProcess.setWorkflowPackage(null);
		return workflowProcess;
	}
		
	public void addDataField(WorkflowParameter initParameter)
	{
		if(parameters.contains(initParameter))
		{
			return;
		}
		this.parameters.add(initParameter);
	}
	public WorkflowParameter removeDataField(WorkflowParameter initParameter)
	{
		if(!parameters.contains(initParameter))
		{
			return null;
		}

		this.parameters.remove(initParameter);
		
		return initParameter;
	}
		
	public void addApplication(WorkflowApplication application)
	{
		if(applications.contains(application))
		{
			return;
		}
		
		this.applications.add(application);
	}

	public WorkflowApplication removeApplication(WorkflowApplication application)
	{
		if(!applications.contains(application))
		{
			return null;
		}

		this.applications.remove(application);
		return application;
	}
		
	public WorkflowProcess getMainWorkflowProcess()
	{	
		ExtendedAttribute attribute =findExtendedAttribute(XPDLGlobals.ID_PROCESS_MAIN);
		if(attribute==null)
		{
			return null;
		}
    
		String mainProcessId= attribute.getValue();    
		return findWorkflowProcess(mainProcessId);
	}
	
	public String getTagName()
	{
		return "Package";
	}
	
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		
		sb.append("[");
		sb.append(getTagName()+":");
		sb.append("[uuid=");
		sb.append(uuid==null ? "?" : uuid);
		sb.append("],");
		sb.append("[name=");
		sb.append(name==null ? "?" : name);
		sb.append("]");
		sb.append("]");
		
		return sb.toString();
	}
}

