/* Generated by Together */

package cobalt.deployment.descriptor.xml;

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 dbms configuration xml
 * Description:  WebLogic 5.1-specific dbms configuration xml description
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author YeonJun Choi
 * @version 1.0
 */

public class Weblogic51DbmsNode extends ElementNode {
    public Weblogic51DbmsNode(Document doc) {
        super((DocumentImpl)doc, "weblogic-rdbms-bean");
    }
/*
    // 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(Weblogic51DbmsDescriptor descriptor) {
    public void setDescriptor(Weblogic51DbmsBeanDescriptor descriptor) {
        Document doc = getOwnerDocument();
        Weblogic51XmlUtil.addNode(this, Weblogic51XmlUtil.getNewTextNode((DocumentImpl)doc, "pool-name", descriptor.getPoolName()));
        Weblogic51XmlUtil.addNode(this, Weblogic51XmlUtil.getNewTextNode((DocumentImpl)doc, "schema-name", descriptor.getSchemaName()));
        Weblogic51XmlUtil.addNode(this, Weblogic51XmlUtil.getNewTextNode((DocumentImpl)doc, "table-name", descriptor.getTableName()));
        Weblogic51XmlUtil.appendFieldMap((DocumentImpl)doc, this, descriptor.getFieldmap());
        if (descriptor.getFinders().size() > 1)
            Weblogic51XmlUtil.appendFinder((DocumentImpl)doc, this, descriptor.getFinderIterator());
        Node newNode = Weblogic51XmlUtil.getNewElementNode((DocumentImpl)doc, "options");
        Weblogic51XmlUtil.addNode(this, newNode);
        Weblogic51XmlUtil.addNode(newNode, Weblogic51XmlUtil.getNewTextNode((DocumentImpl)doc,"use-quoted-names", "false"));
    }

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