//----------------------------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;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.huihoo.workflow.xpdl.ExtendedAttribute;

public abstract class WorkflowComplexComponent extends WorkflowSimpleComponent
{	
	protected Map extendedAttributes = new HashMap();

	/** 
	 * Construct a new WorkflowComponenet.
	 */
	protected WorkflowComplexComponent()
	{
		this(null,null);
	}
	
	/** 
	 * Construct a new WorkflowComponenet.
	 *
	 * @param uuid_ The element ID
	 * @param name The element name
	 */
	protected WorkflowComplexComponent(String uuid, String name)
	{
		this(uuid, name, null);
	}

	/** 
	 * Construct a new WorkflowComponenet.
	 *
	 * @param uuid_       The element ID
	 * @param name        The element name
	 * @param description The element description
	 */
	protected WorkflowComplexComponent(String uuid_, String name, String description)
	{
		super(uuid_, name, description);
		this.extendedAttributes = new HashMap();
	}

	/**  
	 *  Get a map of extended attributes.
	 *
	 *  @return A map of extended attributes
	 */
	public Map getExtendedAttributes()
	{
		return extendedAttributes;
	}

	/**  
	 *  add ExtendedAttribute to  a map of extended attributes.
	 *
	 */
	public void addExtendedAttribute(ExtendedAttribute attribute)
	{
		this.extendedAttributes.put(attribute.getName(), attribute);
	}
	
	public ExtendedAttribute removeExtendedAttribute(String nameOrID,boolean findByUUID)
	{
		if(findByUUID)
		{
			return removeExtendedAttribute_i(nameOrID);
		}
		
		return removeExtendedAttribute(nameOrID);
	}
	
	public ExtendedAttribute findExtendedAttribute(String nameOrID,boolean findByUUID)
	{
		if(findByUUID)
		{
			return findExtendedAttribute_i(nameOrID);
		}
		
		return findExtendedAttribute(nameOrID);		
	}
	
	public ExtendedAttribute removeExtendedAttribute(String name)
	{
		return (ExtendedAttribute) extendedAttributes.remove(name);
	}
	
	public ExtendedAttribute findExtendedAttribute(String name)
	{
		return (ExtendedAttribute) extendedAttributes.get(name);
	}

//------------------------------------------------------------------------
//
//------------------------------------------------------------------------
	
	protected ExtendedAttribute removeExtendedAttribute_i(String ID)
	{
		if(ID == null)
		{
			return null;
		}
		
		Iterator iterator=extendedAttributes.values().iterator();
		while(iterator.hasNext())
		{
			ExtendedAttribute extendAttr =(ExtendedAttribute)iterator.next();		
			if( ID.equals(extendAttr.getUUID()) )
			{
				return (ExtendedAttribute)extendedAttributes.remove(extendAttr.getName());
			}
		}
		
		return null;
	}
	
	protected ExtendedAttribute findExtendedAttribute_i(String ID)
	{
		if(ID == null)
		{
			return null;
		}
		
		Iterator iterator=extendedAttributes.values().iterator();
		
		while(iterator.hasNext())
		{
			ExtendedAttribute extendAttr =(ExtendedAttribute)iterator.next();		
			if( ID.equals(extendAttr.getUUID()) )
			{			
				return extendAttr;
			}
		}
		
		return null;
	}
}
