//----------------------------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/god4j
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : god4j@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.god4j.editor.elements.set;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.huihoo.god4j.editor.XPDLCollection;
import org.huihoo.god4j.editor.XPDLCollectionComponent;
import org.huihoo.god4j.editor.XPDLComplexComponent;
import org.huihoo.god4j.editor.XPDLComponent;
import org.huihoo.god4j.editor.elements.XPDLApplication;
import org.huihoo.god4j.editor.elements.XPDLFormalParameter;
import org.huihoo.workflow.WorkflowComplexComponent;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.xpdl.WorkflowApplication;
import org.huihoo.workflow.xpdl.parameter.FormalParameter;
/**
* Represents a WfMC DTD element that has the similar name.
* This class is a collection of class <b>FormalParameter</b> instances.
*
* @see XPDL
*/
public class XPDLFormalParameters extends XPDLCollection
{
	/**
	* Creates a new instance of the class.
	*
	* @param myPackage The package which elements holds instance
	*                  of this class.
	*/
	public XPDLFormalParameters(XPDLComplexComponent parent)
	{
		super(parent,"FormalParameters");
	}
	/**
	* Generates a new element of the class which instances
	* are members of collection of this class.
	*
	* return The generated instance of class that makes collection.
	*/
	public XPDLCollectionComponent demoComponent()
	{
		XPDLFormalParameter a = new XPDLFormalParameter(this, null);
		a.initComponent();
		a.structComponent();
		a.loadComponent();
		
		return a;
	}
	/**
	* It is overriden to set the read-only property of Id attribute
	* to the proper value in the case of formal parameters of workflow process:
	* if FormalParameter instance is used in transition conditions,
	* it is set to be read only, otherwise, it is set to be editable.
	*/
	public Collection getTableComponents()
	{
		return super.getTableComponents();
	}
	/**
	* Returns a formal parameter with specified ID.
	*
	* @param  ID The 'Id' attribute of wanted instance of
	*         FormalParameter element defined within collection.
	* @return The instance of FormalParameter element that
	*         have the specified ID as an 'Id' attribute
	*         if it exist among all defined formal parameters within
	*         collection, if it doesn't exist, <tt>null</tt> is
	*         returned.
	*/
	public XPDLFormalParameter getXPDLFormalParameter(String ID)
	{
		return (XPDLFormalParameter) super.findComponent(ID);
	}
	/**
	* Checks if specified formal parameter can be removed. FormalParameter
	* can be removed only if it is not used anywhere.
	*/
	public boolean canRemoveElement(XPDLComponent el)
	{
		boolean remove = true;
		XPDLFormalParameter toRemove = (XPDLFormalParameter) el;
		return remove;
	}

	public int[] getInvisibleTableIndexes()
	{
		int[] itfo = new int[2];
		itfo[0] = 0;
		itfo[1] = 4;
		return itfo;
	}
	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------	
	}

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------
	}
	
	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------
		XPDLApplication xpApp = (XPDLApplication) parent_component;
		WorkflowApplication wfApp = (WorkflowApplication) xpApp.getWorkflowComponent();

		if (wfApp == null)
		{
			return;
		}

		List acts = wfApp.getFormalParameters();

		//1. remove all missing FormalParameter
		List element_clones = (List) component_elements.clone();
		Iterator it = element_clones.iterator();
		while (it.hasNext())
		{
			XPDLCollectionComponent el = (XPDLCollectionComponent) it.next();
			if (el instanceof XPDLFormalParameter)
			{
				if (wfApp.findFormalParameter(el.getUUID()) == null)
				{
					removeComponent(el);
				}
			}
		}

		//2. add all newly FormalParameter

		for (int i = 0; i < acts.size(); ++i)
		{
			FormalParameter act = (FormalParameter) acts.get(i);
			XPDLCollectionComponent el = findComponent(act.getUUID());
			if (el == null)
			{
				XPDLFormalParameter xpFOP = new XPDLFormalParameter(this, act);
				xpFOP.initComponent();
				xpFOP.structComponent();
				xpFOP.loadComponent();

				addComponent(xpFOP);
			}
		}
	}

	public void saveComponent()
	{
    if(isReadOnly())
    {
      return;
    }
    
		//	-----------------------------------------------
		super.saveComponent();
		//	-----------------------------------------------
		WorkflowComponent workflow_component = getOwnerComponent().getWorkflowComponent();
		if (workflow_component == null)
		{
			return;
		}
		
		saveComponent(workflow_component);

	}

	public void saveComponent(WorkflowComponent workflow_component)
	{
		//	-----------------------------------------------
		super.saveComponent(workflow_component);
		//	-----------------------------------------------

		WorkflowComplexComponent xpdlElement=(WorkflowComplexComponent)workflow_component;
		
		if ((xpdlElement == null) || (!(xpdlElement instanceof WorkflowApplication)))
		{
			return;
		}

		WorkflowApplication wfApp = (WorkflowApplication) xpdlElement;
		wfApp.getFormalParameters().clear();

		Iterator it = component_elements.iterator();
		while (it.hasNext())
		{
			XPDLCollectionComponent el = (XPDLCollectionComponent) it.next();
			if (el instanceof XPDLFormalParameter)
			{
				FormalParameter formal_P = new FormalParameter();
				XPDLFormalParameter xformal_P = (XPDLFormalParameter) el;
				xformal_P.saveComponent(formal_P);
				xformal_P.setWorkflowComponent(formal_P);
				wfApp.getFormalParameters().add(formal_P);
			}
		}
	}
	//==================================================================
}
