/* Generated by Together */

package cobalt.deployment.descriptor.xml;

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

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.apache.xerces.dom.NodeImpl;
import org.w3c.dom.Element;
import org.apache.xerces.dom.ElementImpl;
import org.w3c.dom.Document;
import org.apache.xerces.dom.DocumentImpl;

import cobalt.deployment.descriptor.*;
import cobalt.deployment.descriptor.dist.*;
import cobalt.deployment.util.CommonUtil;
import cobalt.deployment.util.XmlUtil;

/**
 * Title:        for deployment information descriptor
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author YeonJun Choi
 * @version 1.0
 */

public class Jeus30EjbNode extends ElementNode {
	public Jeus30EjbNode(Document doc, String type) {
        super((DocumentImpl)doc, type);
    }
/*
    public static EjbDescriptor getEjbDescriptor(Node node) {
        //EjbDescriptor ejb = new EjbDescriptor();
        if (((Element)node).getTagName().equals("session"))
            return getSessionEjbDescriptor(node);
        return getEntityEjbDescriptor(node);
    }
    public static SessionEjbDescriptor getSessionEjbDescriptor(Node parent) {
        SessionEjbDescriptor ejb = new SessionEjbDescriptor();
        NodeList nodelist = parent.getChildNodes();
        Node node = null;
        String tag;
        for ( int i=0 ; i<nodelist.getLength() ; i++) {
            node = nodelist.item(i);
            if ( !(node instanceof Element) ) continue;
            tag = ((Element)node).getTagName();
            if ( tag.equals("description") )
                ejb.setDescription(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("display-name") )
                ejb.setName(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("small-icon") )
                ejb.setSmallIcon(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("large-icon") )
                ejb.setLargeIcon(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("home") )
                ejb.setHomeInterface(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("remote") )
                ejb.setRemoteInterface(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("ejb-class") )
                ejb.setBeanClass(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("session-type") )
                ejb.setSessionType(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("transaction-type") )
                ejb.setTransactionType(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("env-entry") )
                ejb.addEnv(XmlUtil.getEnvFromNode(node));
            else if ( tag.equals("ejb-ref") )
                ejb.addEjbReference(XmlUtil.getEjbReferenceFromNode(node));
            else if ( tag.equals("security-role-ref") )
                ejb.addSecurityRole(XmlUtil.getSecurityRoleFromNode(node));
            else if ( tag.equals("resource-ref") )
                ejb.addResource(XmlUtil.getResourceReferenceFromNode(node));
        }
        return ejb;
    }
    public static EntityEjbDescriptor getEntityEjbDescriptor(Node parent) {
        EntityEjbDescriptor ejb = new EntityEjbDescriptor();
        NodeList nodelist = parent.getChildNodes();
        Node node = null;
        String tag;
        for ( int i=0 ; i<nodelist.getLength() ; i++) {
            node = nodelist.item(i);
            if ( !(node instanceof Element) ) continue;
            tag = ((Element)node).getTagName();
            if ( tag.equals("description") )
                ejb.setDescription(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("display-name") )
                ejb.setName(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("small-icon") )
                ejb.setSmallIcon(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("large-icon") )
                ejb.setLargeIcon(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("home") )
                ejb.setHomeInterface(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("remote") )
                ejb.setRemoteInterface(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("ejb-class") )
                ejb.setBeanClass(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("persistence-type") )
                ejb.setTransactionType(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("prim-key-class") )
                ejb.setPrimaryKeyClassName(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("reentrant") )
                ejb.setReentrant(Boolean.getBoolean(XmlUtil.getNodeTextValue(node)));
            else if ( tag.equals("cmp-field") )
                ejb.putContainerManagedField(XmlUtil.getContainerManagedFieldFromNode(node));
            else if ( tag.equals("prim-key-field") )
                ejb.setPrimaryKeyField(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("env-entry") )
                ejb.addEnv(XmlUtil.getEnvFromNode(node));
            else if ( tag.equals("ejb-ref") )
                ejb.addEjbReference(XmlUtil.getEjbReferenceFromNode(node));
            else if ( tag.equals("security-role-ref") )
                ejb.addSecurityRole(XmlUtil.getSecurityRoleFromNode(node));
            else if ( tag.equals("resource-ref") )
                ejb.addResource(XmlUtil.getResourceReferenceFromNode(node));
        }
        return ejb;
    }
*/
    public void setEjbDescriptor(Jeus30EjbDescriptor descriptor) {
        Document doc = getOwnerDocument();
        //Element root = doc.getDocumentElement();
        appendStateless1((DocumentImpl)doc, this, descriptor);
        if ( descriptor instanceof Jeus30StatefulEjbDescriptor)
            appendStatefulOnly((DocumentImpl)doc, this, (Jeus30StatefulEjbDescriptor)descriptor);
        else if ( descriptor instanceof Jeus30BMPEjbDescriptor)
            appendBMPOnly((DocumentImpl)doc, this, (Jeus30BMPEjbDescriptor)descriptor);
        else if ( descriptor instanceof Jeus30CMPEjbDescriptor)
            appendCMPOnly((DocumentImpl)doc, this, (Jeus30CMPEjbDescriptor)descriptor);
        appendStateless2((DocumentImpl)doc, this, descriptor);
	}
	void appendStateless1(DocumentImpl doc, ElementNode parent, Jeus30EjbDescriptor descriptor)
    {
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "ejb-name", descriptor.getEjbName()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "export-port", descriptor.getExportPortValue()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "export-name", descriptor.getExportName()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "export-iiop", descriptor.isExportIIOPValue()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "isLocalInvocationOptimized", descriptor.isLocalInvocOptimizedValue()));
        appendPool(doc, this, descriptor.getBeanPool(), "bean-pool");
    }
	void appendStateless2(DocumentImpl doc, ElementNode parent, Jeus30EjbDescriptor descriptor)
    {
        appendEnvDescriptorNode(doc, this, descriptor.getEnvs().iterator());
        appendReferenceDescriptorNode(doc, this, descriptor.getEjbRef().values().iterator(), "ejb-ref");
        appendReferenceDescriptorNode(doc, this, descriptor.getResRef().values().iterator(), "res-ref");
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "capacity", descriptor.getCapacityValue()));
        appendClusteringNode(doc, this, descriptor.getCluster());
        if ( descriptor.isRunAsId() ) {
            Node node = XmlUtil.getNewElementNode(doc, "run-as-identity");
            parent.appendChild(node);
            XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "role-name", descriptor.getRunAsRoleName()));
            XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "principal-name", descriptor.getRunAsPrincipalName()));
        }
    }
	void appendStatefulOnly(DocumentImpl doc, ElementNode parent, Jeus30StatefulEjbDescriptor descriptor)
    {
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "passivation-timeout", descriptor.getPassivationTimeoutValue()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "disconnect-timeout", descriptor.getDisconnectTimeoutValue()));
        appendPool(doc, parent, descriptor.getConPool(), "connect-pool");
        appendPool(doc, this, descriptor.getThreadPool(), "thread-pool");

        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "file-db-path", descriptor.getFileDbPath()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "file-db-name", descriptor.getFileDbName()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "min-hole", descriptor.getMinHoleValue()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "packing-rate", descriptor.getPackingRateValue()));
    }
	void appendBMPOnly(DocumentImpl doc, ElementNode parent, Jeus30BMPEjbDescriptor descriptor)
    {
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "passivation-timeout", descriptor.getPassivationTimeoutValue()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "disconnect-timeout", descriptor.getDisconnectTimeoutValue()));
        appendPool(doc, parent, descriptor.getConPool(), "connect-pool");
        appendPool(doc, parent, descriptor.getThreadPool(), "thread-pool");

        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "engine-type", descriptor.getEngineType()));
        appendNonModifyingMethods(doc, parent, descriptor.getMethodDescriptorIterator());
    }
	void appendCMPOnly(DocumentImpl doc, ElementNode parent, Jeus30CMPEjbDescriptor descriptor)
    {
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "passivation-timeout", descriptor.getPassivationTimeoutValue()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "disconnect-timeout", descriptor.getDisconnectTimeoutValue()));
        appendPool(doc, parent, descriptor.getConPool(), "connect-pool");
        appendPool(doc, parent, descriptor.getThreadPool(), "thread-pool");

        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "engine-type", descriptor.getEngineType()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "fetch-size", descriptor.getFetchSizeValue()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "init-caching", descriptor.isInitCachingValue()));
        appendNonModifyingMethods(doc, parent, descriptor.getMethodDescriptorIterator());
        appendCmpFields(doc, parent, descriptor);
        appendPrimKeys(doc, parent, descriptor.getPrimKeyIterator());
        appendFindMethods(doc, parent, descriptor);
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "db-vendor", descriptor.getDbVendor()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "data-source-name", descriptor.getDsName()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "is-creating-table", descriptor.isCreateTableValue()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "is-deleting-table", descriptor.isDeleteTableValue()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "table-name", descriptor.getTableName()));
        XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "isolation-level", descriptor.getIsolationLevel()));
    }
    void appendPool(DocumentImpl doc, ElementNode parent, Jeus30PoolConfig descriptor, String poolName)
    {
        Node current = XmlUtil.getNewElementNode(doc, poolName);
        parent.appendChild(current);
        XmlUtil.addNode(current, XmlUtil.getNewTextNode(doc, "pool-min", descriptor.getPoolMinValue()));
        XmlUtil.addNode(current, XmlUtil.getNewTextNode(doc, "pool-max", descriptor.getPoolMaxValue()));
        XmlUtil.addNode(current, XmlUtil.getNewTextNode(doc, "pool-step", descriptor.getPoolStepValue()));
        XmlUtil.addNode(current, XmlUtil.getNewTextNode(doc, "resizing-period", descriptor.getPoolResizePeriodValue()));
    }
    void appendEnvDescriptorNode(DocumentImpl doc, ElementNode parent, Iterator iterator)
    {
		if (!iterator.hasNext())	return;
        EnvEntry entry = null;
		Node node = new ElementImpl(doc, "env");
        parent.appendChild(node);
		while ( iterator.hasNext() ) {
			entry = (EnvEntry)iterator.next();
            XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "name", entry.getName()));
            XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "type", entry.getType()));
            XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "value", (String)entry.getValue()));
        }
    }
    /*
     * refName : ejb-ref or res-ref
    */
    void appendReferenceDescriptorNode(DocumentImpl doc, ElementNode parent, Iterator iterator, String refName)
    {
		if (!iterator.hasNext())	return;
        Jeus30ReferenceDescriptor entry = null;
		Node node = new ElementImpl(doc, refName);
        parent.appendChild(node);
        Node child = null;
		while ( iterator.hasNext() ) {
			entry = (Jeus30ReferenceDescriptor)iterator.next();
            child = new ElementImpl(doc, "jndi-info");
            node.appendChild(child);
            XmlUtil.addNode(child, XmlUtil.getNewTextNode(doc, "ref-name", entry.getReferenceName()));
            XmlUtil.addNode(child, XmlUtil.getNewTextNode(doc, "export-name", entry.getExportName()));
        }
    }
    void appendReferenceDescriptorNode(DocumentImpl doc, ElementNode parent, Jeus30EjbCluster descriptor)
	{
		if ( !descriptor.isClustering() )	return;
		Node node = new ElementImpl(doc, "clustering");
		parent.appendChild(node);
		XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "cluster-name", descriptor.getClusterName()));
        for ( int i=0 ; i< descriptor.getClusterExportNameList().size() ; i++ )
			XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "cluster-export-name", (String)descriptor.getClusterExportNameList().get(i)));
        XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "home-clustered", descriptor.isHomeClusteredValue()));
		XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "ejb-home-component-failover", descriptor.isEjbHomeFailoverValue()));
		Iterator iterator = descriptor.getEjbHomeIdempotentMtds().iterator();
		while ( iterator.hasNext() )
			XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "ejb-home-idempotent-methods", ((MethodDescriptor)iterator.next()).methodString()));
        XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "ejb-remote-component-failover", descriptor.isEjbRemoteFailoverValue()));
		iterator = descriptor.getEjbRemoteIdempotentMtds().iterator();
		while ( iterator.hasNext() )
		 XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "ejb-remote-idempotent-methods", ((MethodDescriptor)iterator.next()).methodString()));
    }
    void appendNonModifyingMethods(DocumentImpl doc, ElementNode parent, Iterator iterator)
    {
        if ( !iterator.hasNext() )	return;
        while ( iterator.hasNext() ) {
            MethodDescriptor method = (MethodDescriptor)iterator.next();
            XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "non-modifying-methods", method.methodString()));
        }
    }
    void appendCmpFields(DocumentImpl doc, ElementNode parent, Jeus30CMPEjbDescriptor descriptor)
    {
        Iterator iterator = descriptor.getFieldIterator();
        if ( !iterator.hasNext() )	return;
        Node node = null;
        Field field = null;
        while ( iterator.hasNext() ) {
			node = XmlUtil.getNewElementNode(doc, "cmpfields");
            parent.appendChild(node);
            field = (Field)iterator.next();
            XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "field", field.getName()));
            XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "column-name", descriptor.getField(field)));
            XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "type", field.getType()));
        }
    }
    void appendPrimKeys(DocumentImpl doc, ElementNode parent, Iterator iterator)
    {
        if ( !iterator.hasNext() )	return;
        while ( iterator.hasNext() ) {
            XmlUtil.addNode(parent, XmlUtil.getNewTextNode(doc, "prim-key-fields", (String)iterator.next()));
        }
    }
    void appendFindMethods(DocumentImpl doc, ElementNode parent, Jeus30CMPEjbDescriptor descriptor)
    {
        Iterator iterator = descriptor.getFindMtdIterator();
        if ( !iterator.hasNext() )	return;
        Node node = null;
        MethodDescriptor method = null;
        while ( iterator.hasNext() ) {
			node = XmlUtil.getNewElementNode(doc, "find-methods");
            parent.appendChild(node);
            method = (MethodDescriptor)iterator.next();
            XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "method", method.methodString()));
            XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "sql", descriptor.getFindSql(method)));
        }
    }
	void appendClusteringNode(DocumentImpl doc, ElementNode parent, Jeus30EjbCluster descriptor)
	{
		if ( !descriptor.isClustering() )
			return;
		Node node = XmlUtil.getNewElementNode(doc, "clustering");
		parent.appendChild(node);
		XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "cluster-name", descriptor.getClusterName()));
		int i = 0;
		for ( i = 0; i<descriptor.getClusterExportNameList().size(); i++ )
			XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "cluster-name", (String)descriptor.getClusterExportNameList().get(i)));
		XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "home-clustered", descriptor.isHomeClusteredValue()));
		XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "ejb-home-component-failover", descriptor.isEjbHomeFailoverValue()));
		Iterator iterator = descriptor.getEjbHomeIdempotentMtds().iterator();
		while ( iterator.hasNext() )
			XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "ejb-home-idempotent-methods", ((MethodDescriptor)iterator.next()).methodString()));
		XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "ejb-remote-component-failover", descriptor.isEjbRemoteFailoverValue()));
		iterator = descriptor.getEjbRemoteIdempotentMtds().iterator();
		while ( iterator.hasNext() )
			XmlUtil.addNode(node, XmlUtil.getNewTextNode(doc, "ejb-remote-idempotent-methods", ((MethodDescriptor)iterator.next()).methodString()));
	}
}
