/* Generated by Together */

package cobalt.deployment.descriptor.xml;

import java.util.Vector;
import java.util.Set;
import java.util.Iterator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;

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.MethodDescriptor;
import cobalt.deployment.descriptor.dist.*;
import cobalt.deployment.util.Weblogic51XmlUtil;

/**
 * Title:        WebLogic 5.1 container managed xml
 * Description:  WebLogic 5.1-specific container managed xml description
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author YeonJun Choi
 * @version 1.0
 */

public class Weblogic51ContainerManagedNode extends ElementNode {
    public Weblogic51ContainerManagedNode(Document doc) {
        super((DocumentImpl)doc, "weblogic-rdbms-jar");
    }
/*
    // getting EjbBundleDescriptor from this element
    static public Weblogic51EjbBundleDescriptor getEjbBundleDescriptor(EjbBundleDescriptor bundle, Node parent) {
        //EjbBundleDescriptor bundle = new EjbBundleDescriptor();
        NodeList nodelist = parent.getChildNodes().item(0).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") )
                bundle.setDescription(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("display-name") )
                bundle.setName(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("small-icon") )
                bundle.setSmallIcon(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("large-icon") )
                bundle.setLargeIcon(XmlUtil.getNodeTextValue(node));
            if ( tag.equals("enterprise-beans") ) {
            	NodeList childlist = node.getChildNodes();
                for ( int j=0 ; j<childlist.getLength(); j++) {
                	if ( childlist.item(j) instanceof Element )
                        bundle.addEjbDescriptor(EjbNode.getEjbDescriptor(childlist.item(j)));
                }
            }
            else if ( tag.equals("assembly-descriptor") ) {
                setAssemblyDescriptor(bundle, node);
            }
        }
        return bundle;
    }
    static void setAssemblyDescriptor(EjbBundleDescriptor bundle, Node parent) {
        NodeList nodelist = parent.getChildNodes();
        Node node = null;
        String tag = null;
        for ( int i=0 ; i<nodelist.getLength(); i++) {
            node = nodelist.item(i);
            if ( tag.equals("security-role") ) {
                Node child = node.getFirstChild();
                bundle.addRole(XmlUtil.getNodeTextValue(child), XmlUtil.getNodeTextValue(child.getNextSibling()));
            }
        }
    }
*/
    // setting this element using ejb bundle descriptor
    public void setDescriptor(Weblogic51ContainerManaged descriptor) {
        Weblogic51DbmsBeanDescriptor ejb;
        Weblogic51DbmsNode ejbNode;

        Document doc = getOwnerDocument();
    	//Vector ejbs = descriptor.getEjbs();
        Iterator iterator = descriptor.getEjbsIterator();
        //for ( int i=0 ; i<ejbs.size(); i++) {
        while ( iterator.hasNext() ) {
            //ejb = (Weblogic51DbmsBeanDescriptor)ejbs.elementAt(i);
            ejb = (Weblogic51DbmsBeanDescriptor)iterator.next();
            ejbNode = new Weblogic51DbmsNode(doc);
            ejbNode.setDescriptor(ejb);
            appendChild(ejbNode);
            //if (!(ejb instanceof Weblogic51EntityEjbDescriptor))	continue;
            //Weblogic51XmlUtil.appendContainerManagedBeanNode((DocumentImpl)doc, this, (Weblogic51EntityEjbDescriptor)ejb);
        }
        Weblogic51XmlUtil.addNode(this, Weblogic51XmlUtil.getNewTextNode((DocumentImpl)doc, "create-default-dbms-tables", descriptor.isCreateTableString()));
        // Setting assembly-descriptor
        //node = new ElementImpl((DocumentImpl)doc,"assembly-descriptor");
        //appendChild(node);
        /*
        Node node = null, child = null;
        Iterator iterator;

        // add Security Roles
        Weblogic51XmlUtil.appendRoleMapping((DocumentImpl)doc, this,descriptor.getRoles().values().iterator());

        // add Transaction Isolation
        for ( int i=0 ; i<ejbs.size(); i++) {
            ejb = (Weblogic51EjbDescriptor)ejbs.elementAt(i);
            Weblogic51TransactionIsolation ti = ejb.getTransactionIsolation();
            for ( int j=0 ; j<Weblogic51TransactionIsolation.ISOLATION_LEVEL.length ; j++) {
                iterator = ti.getMethodsIterator(Weblogic51TransactionIsolation.ISOLATION_LEVEL[j]);
                if (!iterator.hasNext())	continue;
                node = new ElementImpl((DocumentImpl)doc,"transaction-isolation");
                appendChild(node);
                child = Weblogic51XmlUtil.getNewTextNode((DocumentImpl)doc, "isolation-level", Weblogic51TransactionIsolation.ISOLATION_LEVEL[j]);
                node.appendChild(child);
                while (iterator.hasNext()) {
	            	mtdNode = new MethodNode(doc);
	                mtdNode.setMethodDescriptor((MethodDescriptor)iterator.next());
	                node.appendChild(mtdNode);
                }
            }
        }
        */
    }

    public void writeNodeToFile(File file) throws IOException {
		BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(file));
		fos.write(getXmlString().getBytes());
        fos.close();
    }
}
