package org.jfox.ejb.meta;

import org.w3c.dom.Node;

/**
 * @author <a href="mailto:yangyong@ufsoft.com.cn">Young Yang</a>
 */

public class EnvEntryDescriptor extends Descriptor {
  private String name;
  private String type;
  private String value;

  public EnvEntryDescriptor() {
  }

  public void processXML(Node node) throws EJBDescriptionException {
    super.processXML(node);
    setName(XmlUtil.getChildNodeValueOf(node, "env-entry-name"));
    setType(XmlUtil.getChildNodeValueOf(node, "env-entry-type"));
    setValue(XmlUtil.getChildNodeValueOf(node, "env-entry-value"));
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
  }

  public Object getValueObject() {
    String type = getType();
    if (value == null || "".equals(value) && !type.equals("java.lang.String"))
      return null;
    if (type.equals("java.lang.Boolean"))
      return new Boolean(value);
    if (type.equals("java.lang.String"))
      return value;
    if (type.equals("java.lang.Integer"))
      return new Integer(value);
    if (type.equals("java.lang.Double"))
      return new Double(value);
    if (type.equals("java.lang.Byte"))
      return new Byte(value);
    if (type.equals("java.lang.Short"))
      return new Short(value);
    if (type.equals("java.lang.Long"))
      return new Long(value);
    if (type.equals("java.lang.Float"))
      return new Float(value);
    else
      throw new IllegalArgumentException("Illegal type for environment entry: " + type);
  }

  public boolean equals(Object obj) {
    if (obj instanceof EnvEntryDescriptor)
      return name.equals(((EnvEntryDescriptor) obj).name);
    else
      return false;
  }

  public int hashCode() {
    return name.hashCode();
  }

  public static void main(String[] args) {

  }
}