/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.framework.deploy;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jfox.framework.util.XmlUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class ComponentDescriptor implements Descriptor {
    private String desc;
    private String implementation;
    private String factor = "";
    private boolean singleton = true;
    private boolean typeSingleton = true;
    private ConstructorDescriptor constructor;
    private List properties = new ArrayList();

    private ModuleDescriptor moduleDescriptor;

    public ComponentDescriptor(ModuleDescriptor moduleDescriptor) {
        this.moduleDescriptor = moduleDescriptor;
    }

    public void processXML(Node node) throws ComponentDescriptionException {
        setDescription(XmlUtils.getChildNodeValueOf(node, "description"));
        setFactor(XmlUtils.getChildNodeValueOf(node, "factor"));
        setImplementation(XmlUtils.getChildNodeValueOf(node, "implementation"));
        setSingleton(XmlUtils.getChildNodeValueOf(node, "singleton"));
        setTypeSingleton(XmlUtils.getChildNodeValueOf(node, "type-singleton"));
        Node constructorNode = XmlUtils.getChildNodeOf(node, "constructor");
        if(constructorNode != null) {
            ConstructorDescriptor cd = new ConstructorDescriptor();
            cd.processXML(constructorNode);
            setConstructorDescriptor(cd);
        }

        Iterator it = XmlUtils.getElementsByTagName((Element) node, "property");
        while(it.hasNext()) {
            Node propertyNode = (Node) it.next();
            PropertyDescriptor pd = new PropertyDescriptor();
            pd.processXML(propertyNode);
            addPropertyDescriptor(pd);
        }

    }

    public String getDescription() {
        return desc;
    }

    public void setDescription(String desc) {
        this.desc = desc;
    }

    public String getImplementation() {
        return implementation;
    }

    public void setImplementation(String componentImplementation) {
        this.implementation = componentImplementation;
    }

    public String getFactor() {
        return factor;
    }

    public void setFactor(String factor) {
        if(factor != null) this.factor = factor;
    }

    public boolean isSingleton() {
        return singleton;
    }

    public void setSingleton(String singleton) {
        if(singleton != null && !singleton.equals("")) {
            if(singleton.equals("true") || singleton.equals("false")) {
                this.singleton = Boolean.valueOf(singleton).booleanValue();
            }
        }
    }

    public boolean isTypeSingleton() {
        return typeSingleton;
    }

    public void setTypeSingleton(String typeSingleton) {
        if(typeSingleton != null && !typeSingleton.equals("")) {
            if(typeSingleton.equals("true") || typeSingleton.equals("false")) {
                this.typeSingleton = Boolean.valueOf(typeSingleton).booleanValue();
            }
        }
    }

    public int getPriority() {
        return moduleDescriptor.getPriority();
    }

    public ConstructorDescriptor getConstructorDescriptor() {
        return constructor;
    }

    public void setConstructorDescriptor(ConstructorDescriptor constructor) {
        this.constructor = constructor;
    }

    public void addPropertyDescriptor(PropertyDescriptor pd) {
        properties.add(pd);
    }

    public PropertyDescriptor[] getPropertyDescriptors() {
        return (PropertyDescriptor[]) properties.toArray(new PropertyDescriptor[properties.size()]);
    }

    public String getModule() {
        return moduleDescriptor.getDescription();
    }

    public static void main(String[] args) {

    }

}

