//----------------------------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.atoms;
import java.util.List;

import org.huihoo.god4j.editor.XPDLAttribute;
import org.huihoo.god4j.editor.XPDLComplexComponent;
import org.huihoo.god4j.editor.XPDLComponent;
import org.huihoo.god4j.editor.panels.JavaGroupPanel;
import org.huihoo.god4j.editor.panels.JavaPanel;
import org.huihoo.workflow.WorkflowComponent;
import org.huihoo.workflow.xpdl.WorkflowActivity;
import org.huihoo.workflow.xpdl.XPDLGlobals;
import org.huihoo.workflow.xpdl.activity.PerformerType;
/**
* Represents a WfMC DTD element that has the similar name.
* 
* @see XPDL
*/
public class XPDLPerformer extends XPDLComplexComponent //XPDLSimpleComponent
{
	private XPDLAttribute attrType = null;
	private XPDLAttribute attrList = null;

	public XPDLPerformer(XPDLComplexComponent parent)
	{
		super( parent,"Performer");
	}


	public JavaPanel getJavaPanel()
	{
		return new JavaGroupPanel(this, new XPDLComponent[] {  attrType,attrList}, getLabel());
	}

	public Object clone()
	{
		XPDLPerformer p = (XPDLPerformer) super.clone();
		p.attrType = (XPDLAttribute) this.attrType.clone();
		p.attrList = (XPDLAttribute) this.attrList.clone();
		return p;
	}

	//==================================================================
	public void initComponent()
	{
		//	-----------------------------------------------
		super.initComponent();
		//	-----------------------------------------------	
		attrType =
			new XPDLAttribute(
				this,
		"PerformerType",
				new String[] { "AUTOMATION", "PARTICIPANT", "DEPARTMENT", "RANK", "CATEGORY", "GROUP", "ROLE", "SELECTOR" },
				1);
		attrType.initComponent();

		attrList = new XPDLAttribute(this,"PerformerList");
		attrList.initComponent();
	}

	public void structComponent()
	{
		//	-----------------------------------------------
		super.structComponent();
		//	-----------------------------------------------
		attrType.structComponent();
		attrList.structComponent();
		
		complexChildren.add(attrType);
		complexChildren.add(attrList);

		attributes.add(attrType);
		attributes.add(attrList);
	}
	
	public void loadComponent()
	{
		//	-----------------------------------------------
		super.loadComponent();
		//	-----------------------------------------------
		attrType.loadComponent();
		attrList.loadComponent();
		
		XPDLComplexComponent parent_component = getParentComponent();
		if (parent_component !=null )
		{
			WorkflowComponent workflow_component =  parent_component.getWorkflowComponent();

			if (workflow_component == null)
			{
				return;
			}

			if (workflow_component instanceof WorkflowActivity)
			{
				List pfs = ((WorkflowActivity) workflow_component).getPerformers();
				int size = pfs.size();
				StringBuffer buf = new StringBuffer();
				for (int i = 0; i < size; ++i)
				{
					if (i == (size - 1))
					{
						buf.append(pfs.get(i));
					}
					else
					{
						buf.append(pfs.get(i) + XPDLGlobals.PERFORMER_SEPARATOR);
					}
				}
				attrList.setValue(buf.toString());

				PerformerType pt = ((WorkflowActivity) workflow_component).getPerformerType();
				if (pt != null)
				{
					String pfType = pt.getType();
					if (pfType.endsWith("AUTOMATION"))
					{
						attrType.setValue("AUTOMATION");
					}
					else
					{
						int st = "PERFORMER_".length();
						int sp = pfType.lastIndexOf("_BASED");
						attrType.setValue(pfType.substring(st, sp));
					}
				}
			}
		}
	}

	/**
	* 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();
		attrList.saveComponent();
		
		XPDLComplexComponent parent_component = getParentComponent();
		if (parent_component !=null )
		{
			WorkflowComponent workflow_component =  parent_component.getWorkflowComponent();

			if (workflow_component == null)
			{
				return;
			}

			if (workflow_component instanceof WorkflowActivity)
			{
				((WorkflowActivity) workflow_component).parsePerformers(attrList.toString());

				String pt_value = attrType.toString();
				if (pt_value.equals("AUTOMATION"))
				{
					((WorkflowActivity) workflow_component).setPerformerType(PerformerType.parse("PERFORMER_" + pt_value));
				}
				else
				{
					((WorkflowActivity) workflow_component).setPerformerType(PerformerType.parse("PERFORMER_" + pt_value + "_BASED"));
				}
			}
		}
	}

	//==================================================================
}
