//----------------------------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;
import org.huihoo.god4j.editor.XPDLAttribute;
import org.huihoo.god4j.editor.XPDLComplexComponent;
import org.huihoo.god4j.editor.elements.atoms.XPDLXpression;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.xpdl.WorkflowCondition;
import org.huihoo.workflow.xpdl.WorkflowTransition;
import org.huihoo.workflow.xpdl.condition.ConditionType;
import org.huihoo.workflow.xpdl.condition.ScriptCondition;
import org.huihoo.workflow.xpdl.condition.XCDATACondition;
/**
* Represents a WfMC DTD element that has the similar name.
*
* @see XPDL
*/
public class XPDLCondition extends XPDLComplexComponent
{
	private XPDLXpression refXpression = null; // min=0, max=unbounded
	private XPDLAttribute attrType = null;
	
	/**
	* Creates a new instance of the class.
	*/
	public XPDLCondition(XPDLComplexComponent parent)
	{
		super(parent,"Condition");
	}
	/**
	* Overrides super-class method to retrieve the defined
	* condition.
	*
	* @return The condition of transition.
	*/
	public String toString()
	{
		return refXpression.toString();
	}
	public Object getValue()
	{
		return refXpression.getValue();
	}

	public Object getContentType()
	{
		return attrType.getValue();
	}
	
	/**
	* Used to create exact copy of instance of this class.
	* The newly created instance will have all the properties
	* same as the copied one.
	*
	* @return The newly created instance of this class.
	*/
	public Object clone()
	{
		XPDLCondition c = (XPDLCondition) super.clone();
		c.attrType = (XPDLAttribute) this.attrType.clone();
		c.refXpression = (XPDLXpression) this.refXpression.clone();
		
		c.structComponent();
		
		return c;
	}
	
//==================================================================
	/**
	* The classes that are derived from this class has to give its
	* definition for this method. It is used to insert all members
	* of those classes that are derived from XPDLComp
	* class (or classes that are derived from that class) into
	* one list, and the members that are derived from
	* XPDLAttribute class also to insert into other list.
	* Many methods of this class uses those lists:
	* <code>toXPDL</code> method uses them to properly write an element
	* tag to the XPDL file,
	* <code>fromXPDL</code> method uses them to properly read an element
	* tag from the XPDL file,
	* <code>getPanel</code> method uses them to properly display members
	* of this class,
	* <code>get</code> and <code>set</code> uses them to properly
	* retrieve/set the elements from list, ... and so on
	* <p>NOTE: The order of inserted elements is relevant for XPDL to be
	*          valid (members of classes derived from this class must be
	*          inserted into first mentioned list in the same order that
	*          they are within a corresponding tag for those classes
	*          within WfMC XPDL).
	*/
	public void initComponent()
	{
//	-----------------------------------------------
		super.initComponent();
//	-----------------------------------------------
		refXpression = new XPDLXpression(this); // min=0, max=unbounded
		refXpression.initComponent();
		
		//attrType = new XPDLAttribute("Type", this,new String[] { "", "CONDITION", "OTHERWISE", "EXCEPTION", "DEFAULTEXCEPTION" }, 1);		
		attrType = new XPDLAttribute( this,"Type",new String[] { "","CONDITION_XCDATA","CONDITION_SCRIPT" }, 0);
		attrType.initComponent();
	}

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------
		attrType.structComponent();		
		refXpression.structComponent();
		
		complexChildren.add(attrType);		
		complexChildren.add(refXpression);
		
		attributes.add(attrType);
	}
	
	/**
	* Supposed to be overriden by derived classes to implement
	* element specific reading from an XPDL Model. This implementation
	* satisfies only "simple" elements.
	*/
	public void loadComponent() 
	{
//	-----------------------------------------------
		super.loadComponent();
//	-----------------------------------------------
		attrType.loadComponent();
		refXpression.loadComponent();
		
		if(parent_component!=null && (parent_component instanceof XPDLComplexComponent))
		{
			WorkflowComponent workflow_component=((XPDLComplexComponent)parent_component).getWorkflowComponent();
			if(workflow_component!=null && (workflow_component instanceof WorkflowTransition))
			{
				WorkflowTransition trans=(WorkflowTransition)workflow_component;
				WorkflowCondition condtion=trans.getCondition();
				if(condtion!=null)
				{
					attrType.setValue(condtion.getType().getType());
					refXpression.setValue(condtion.getContent());
				}
			}
		}
	}

	/**
	* Supposed to be overriden by derived classes to implement
	* element specific writting to an XPDL Model.
	*
	*/
	public void saveComponent() 
	{
    if(isReadOnly())
    {
      return;
    }
    
//	-----------------------------------------------
		super.saveComponent();
//	-----------------------------------------------
		attrType.saveComponent();
		refXpression.saveComponent();
		
		String condition_type=attrType.toString();
		String condition_content=refXpression.toString();

		if(condition_type.equals("") || condition_content.equals(""))
		{
			return;
		}
		
		if(parent_component!=null && (parent_component instanceof XPDLTransition))
		{
			
			WorkflowComponent workflow_component=((XPDLTransition)parent_component).getWorkflowComponent();
			if(workflow_component!=null && (workflow_component instanceof WorkflowTransition))
			{		
				WorkflowTransition trans=(WorkflowTransition)workflow_component;

				if(ConditionType.CONDITION_SCRIPT.equals(ConditionType.parse(condition_type)))
				{
					trans.setCondition(new ScriptCondition(condition_content));
				}
				else
				{
					trans.setCondition(new XCDATACondition(condition_content));
				}
			}
		}
	}
	
	public void saveComponent(WorkflowComponent workflow_component)
	{
		attrType.saveComponent(workflow_component);
		refXpression.saveComponent(workflow_component);
		
		String condition_type=attrType.toString();
		String condition_content=refXpression.toString();

		if(condition_type.equals("") || condition_content.equals(""))
		{
			return;
		}
		
		if(parent_component!=null && (parent_component instanceof XPDLTransition))
		{
			
			if(workflow_component!=null && (workflow_component instanceof WorkflowTransition))
			{		
				WorkflowTransition trans=(WorkflowTransition)workflow_component;

				if(ConditionType.CONDITION_SCRIPT.equals(ConditionType.parse(condition_type)))
				{
					trans.setCondition(new ScriptCondition(condition_content));
				}
				else
				{
					trans.setCondition(new XCDATACondition(condition_content));
				}
			}
		}	
	}
//==================================================================
}
