//----------------------------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.xpdl;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.MethodUtils;
import org.huihoo.workflow.WorkflowCloneable;
import org.huihoo.workflow.WorkflowComplexComponent;
import org.huihoo.workflow.WorkflowXPDLComponent;

import org.huihoo.commons.beanutils.ClassConverter;

public  class WorkflowParameter extends WorkflowComplexComponent implements WorkflowCloneable, WorkflowXPDLComponent
{
	// added serialver computed by the tool
	//private static final long serialVersionUID = 6445693740318585243L;
	
	protected Class type = String.class;
	
	protected Object value;
  
	public WorkflowParameter()
	{
		this(null,null,null);
	}
  
	public WorkflowParameter(String id, String name, Class type)
	{
		this(id, name, type,null);
	}
  
	public WorkflowParameter(
		String id,
		String name,
		Class type,
		Object initialValue)
	{
		super(id, name);
		
		this.type = type;
		this.value = initialValue;
	}
	
  public void setType(Class type_)
	{
		this.type = type_;
	}
	public Class getType()
	{
		return type;
	}
	public void setValue(Object value_)
	{				
		if (value_ == null)
		{
			return;
		}
		if (type!=null && value_.getClass().equals(type))
		{
			this.value = value_;
		}
		else
		{			
			if (value_ instanceof java.lang.String)
			{
				ConvertUtils.register(new ClassConverter(), Class.class);
				value = ConvertUtils.convert((String) value_, type);
			}
			else if(type.isPrimitive())
			{
				value=value_.toString();
			}
			else
			{
				throw new java.lang.IllegalArgumentException("WorkflowParameter#setValue");
			}
		}
	}
  
	public Object getValue()
	{
		return value;
	}
  
  public WorkflowParameter copy()
  throws CloneNotSupportedException
  {
    return (WorkflowParameter)clone();
  }
  
  public Object clone() throws CloneNotSupportedException
  {
		WorkflowParameter result=new WorkflowParameter(getUUID(), getName(), getType());
		result.setValue(copyValue()); 
		return result;  
  }
  
  public String getTagName()
  {
  	return "DataField";
  }
  
	public String toString()
	{
		return "[id="
			+ getUUID()
			+ "] "
			+ "[name="
			+ getName()
			+ "] "
			+ "[type="
			+ getType()
			+ "] "
			+ "[value="
			+ getValue()
			+ "]"
			+ "[description="
			+ getDescription()
			+ "] ";
	}
  
  private Object copyValue() 
  throws CloneNotSupportedException
  {
    if (value == null)
    {
      return null;
    }
    
		if( value instanceof java.lang.String)
		{
			return value;
		}
		else if(type.isPrimitive())
		{
			return value;
		}	
				
    try
    {
      return MethodUtils.invokeMethod(value, "clone", null);
    }
    catch (Throwable ex)
    {   	
      throw new CloneNotSupportedException("[class="+value.getClass()+",value="+value.toString()+"]");
    }
  } 
  
}
