/* 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.Weblogic60XmlUtil;

/**
 * Title:        WebLogic 6.x dbms configuration xml
 * Description:  WebLogic 6.x-specific dbms configuration xml description
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author YeonJun Choi
 * @version 1.0
 */

public class Weblogic60DbmsNode extends ElementNode {
    public Weblogic60DbmsNode(Document doc) {
        super((DocumentImpl)doc, "weblogic-rdbms-bean");
    }
/*
    // getting EjbBundleDescriptor from this element
    static public Weblogic60EjbBundleDescriptor 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(Weblogic60DbmsDescriptor descriptor) {
    public void setDescriptor(Weblogic60DbmsBeanDescriptor descriptor) {
        Document doc = getOwnerDocument();
        Weblogic60XmlUtil.addNode(this, Weblogic60XmlUtil.getNewTextNode((DocumentImpl)doc, "ejb-name", descriptor.getEjbName()));
        Weblogic60XmlUtil.addNode(this, Weblogic60XmlUtil.getNewTextNode((DocumentImpl)doc, "pool-name", descriptor.getPoolName()));
        Weblogic60XmlUtil.addNode(this, Weblogic60XmlUtil.getNewTextNode((DocumentImpl)doc, "data-source-name", descriptor.getDsName()));
        Weblogic60XmlUtil.addNode(this, Weblogic60XmlUtil.getNewTextNode((DocumentImpl)doc, "table-name", descriptor.getTableName()));
        Weblogic60XmlUtil.appendFieldMap((DocumentImpl)doc, this, descriptor.getFieldmap());
        Weblogic60XmlUtil.appendFinder((DocumentImpl)doc, this, descriptor.getFinderIterator());
        Weblogic60XmlUtil.addNode(this, Weblogic60XmlUtil.getNewTextNode((DocumentImpl)doc, "enable-tuned-updates", descriptor.isTunedUpdateString()));
    }

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