//----------------------------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.coreImpl.xpdl.parser.converters;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
import org.huihoo.workflow.xpdl.activity.JoinType;

public final class JoinTypeConverter  implements Converter
{
  // ----------------------------------------------------------- Constructors
  /**
   * Create a {@link Converter} that will throw a {@link ConversionException}
   * if a conversion error occurs.
   */
  public JoinTypeConverter()
  {
    this.defaultValue = null;
    this.useDefault = false;
  }
  /**
   * Create a {@link Converter} that will return the specified default value
   * if a conversion error occurs.
   *
   * @param defaultValue The default value to be returned
   */
  public JoinTypeConverter(Object defaultValue)
  {
    this.defaultValue = defaultValue;
    this.useDefault = true;
  }
  // ----------------------------------------------------- Instance Variables
  /**
   * The default value specified to our Constructor, if any.
   */
  private Object defaultValue = null;
  /**
   * Should we return the default value on conversion errors?
   */
  private boolean useDefault = true;
  // --------------------------------------------------------- Public Methods
  /**
   * Convert the specified input object into an output object of the
   * specified type.
   *
   * @param type Data type to which this value should be converted
   * @param value The input value to be converted
   *
   * @exception ConversionException if conversion cannot be performed
   *  successfully
   */
  public Object convert(Class type, Object value)
  {
    if (value == null)
    {
      if (useDefault)
      {
        return (defaultValue);
      }
      else
      {
        throw new ConversionException("No value specified");
      }
    }
    if (value instanceof JoinType)
    {
      return (value);
    }
        
    return (JoinType.parse(value.toString())); 
  }
}
