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

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

public class Weblogic60EjbBundleNode extends ElementNode {
    public Weblogic60EjbBundleNode(Document doc) {
        super((DocumentImpl)doc, "weblogic-ejb-jar");
    }
/*
    // 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 setEjbBundleDescriptor(Weblogic60EjbBundleDescriptor descriptor) {
		Weblogic60EjbNode ejbNode;
        Weblogic60EjbDescriptor ejb;
        String beanType;
        Object[] entry;
        MethodNode mtdNode;
        MethodDescriptor md;

        Document doc = getOwnerDocument();
        //XmlUtil.appendDescriptorNode((DocumentImpl)doc, this, descriptor);
        Weblogic60XmlUtil.addNode(this, Weblogic60XmlUtil.getNewTextNode((DocumentImpl)doc, "description", descriptor.getDescription()));
    	Vector ejbs = descriptor.getEjbs();

        // Setting ejb-descriptor
        //Node node = new ElementImpl((DocumentImpl)doc,"enterprise-beans");
        //appendChild(node);
        for ( int i=0 ; i<ejbs.size(); i++) {
            ejb = (Weblogic60EjbDescriptor)ejbs.elementAt(i);
            ejbNode = new Weblogic60EjbNode(doc);
            ejbNode.setEjbDescriptor(ejb);
            appendChild(ejbNode);
        }
        // Setting assembly-descriptor
        //node = new ElementImpl((DocumentImpl)doc,"assembly-descriptor");
        //appendChild(node);
        Node node = null, child = null;
        Iterator iterator;

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

        // add Transaction Isolation
        for ( int i=0 ; i<ejbs.size(); i++) {
            ejb = (Weblogic60EjbDescriptor)ejbs.elementAt(i);
            Weblogic60TransactionIsolation ti = ejb.getTransactionIsolation();
            for ( int j=0 ; j<Weblogic60TransactionIsolation.ISOLATION_LEVEL.length ; j++) {
                iterator = ti.getMethodsIterator(Weblogic60TransactionIsolation.ISOLATION_LEVEL[j]);
                if (!iterator.hasNext())	continue;
                node = new ElementImpl((DocumentImpl)doc,"transaction-isolation");
                appendChild(node);
                child = Weblogic60XmlUtil.getNewTextNode((DocumentImpl)doc, "isolation-level", Weblogic60TransactionIsolation.ISOLATION_LEVEL[j]);
                node.appendChild(child);
                while (iterator.hasNext()) {
	            	mtdNode = new MethodNode(doc);
	                mtdNode.setMethodDescriptor((MethodDescriptor)iterator.next());
	                node.appendChild(mtdNode);
                }
            }
        }
        /*
        iterator = descriptor.getSecurityRoles().keySet().iterator();
        while ( iterator.hasNext() ) {
            child = new ElementImpl((DocumentImpl)doc,"security-role");
            node.appendChild(child);
            String key = (String)iterator.next();
            XmlUtil.addNode(child, XmlUtil.getNewTextNode((DocumentImpl)doc, "role-name", key ));
            XmlUtil.addNode(child, XmlUtil.getNewTextNode((DocumentImpl)doc, "description", descriptor.getRoleDescription(key)));
        }
        */
        // add MethodPermission
        /*
        for ( int i=0 ; i<ejbs.size(); i++) {
            ejb = (Weblogic60EjbDescriptor)ejbs.elementAt(i);
            iterator = ejb.getMethodPermIterator();
            while ( iterator.hasNext() ) {
                child = new ElementImpl((DocumentImpl)doc,"method-permission");
                node.appendChild(child);
                mp = (MethodPermission)iterator.next();
                entry = mp.getRoles().toArray();
                for (int j=0 ; j<entry.length ; j++)
                	XmlUtil.addNode(child, XmlUtil.getNewTextNode((DocumentImpl)doc, "role-name", (String)entry[j]));
                entry = mp.getMethods().toArray();
                for (int j=0 ; j<entry.length ; j++) {
	            	mtdNode = new MethodNode(doc);
	                mtdNode.setMethodDescriptor((MethodDescriptor)entry[j]);
	                child.appendChild(mtdNode);
                }
            }
        }
        // add Container Transacion
        for ( int i=0 ; i<ejbs.size(); i++) {
            ejb = (EjbDescriptor)ejbs.elementAt(i);
            iterator = ejb.getContainerTransactionIterator();
            while ( iterator.hasNext() ) {
                child = new ElementImpl((DocumentImpl)doc,"container-transaction");
                node.appendChild(child);
                ct = (ContainerTransaction)iterator.next();
                XmlUtil.addNode(child, XmlUtil.getNewTextNode((DocumentImpl)doc, "description", ct.getDescription()));
                entry = ct.getMethods().toArray();
                for (int j=0 ; j<entry.length ; j++) {
	            	mtdNode = new MethodNode(doc);
	                mtdNode.setMethodDescriptor((MethodDescriptor)entry[j]);
	                child.appendChild(mtdNode);
                }
                XmlUtil.addNode(child, XmlUtil.getNewTextNode((DocumentImpl)doc, "trans-attribute", ct.getAttribute()));
            }
        }
        */
    }

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