/* Generated by Together */

package cobalt.deployment.descriptor.xml;

import java.util.Vector;
import java.util.Set;
import java.util.Iterator;
import java.util.ArrayList;
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.XmlUtil;

/**
 * Title:        for deployment information descriptor
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author YeonJun Choi
 * @version 1.0
 */
public class Jeus30EjbBundleNode extends ElementNode {
    public Jeus30EjbBundleNode(Document doc) {
        super((DocumentImpl)doc, "jeus-ejb-dd");
    }
/*
    // getting EjbBundleDescriptor from this element
    static public Jeus30EjbBundleDescriptor 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(Jeus30EjbBundleDescriptor descriptor) {
		Jeus30EjbNode ejbNode;
        Jeus30EjbDescriptor ejb;
        String type;

        Document doc = getOwnerDocument();
        appendModuleInfo((DocumentImpl)doc, this, descriptor);

    	Vector ejbs = descriptor.getEjbs();

        // Setting ejb-descriptor
        Node node = new ElementImpl((DocumentImpl)doc,"beanlist");
        appendChild(node);
        for ( int i=0 ; i<ejbs.size(); i++) {
            ejb = (Jeus30EjbDescriptor)ejbs.elementAt(i);
            if (ejb instanceof Jeus30CMPEjbDescriptor )
                type = "container-managed";
            else if (ejb instanceof Jeus30BMPEjbDescriptor )
                type = "bean-managed";
            else if (ejb instanceof Jeus30BMPEjbDescriptor )
                type = "stateful";
            else
                type = "stateless";
            ejbNode = new Jeus30EjbNode(doc, type);
            ejbNode.setEjbDescriptor(ejb);
            node.appendChild(ejbNode);
        }
    }

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

    void appendModuleInfo(DocumentImpl doc, ElementNode parent, Jeus30EjbBundleDescriptor descriptor)
    {
        Node current = XmlUtil.getNewElementNode(doc, "module-info");
        parent.appendChild(current);
        XmlUtil.addNode(current, XmlUtil.getNewTextNode((DocumentImpl)doc, "module-name", descriptor.getModuleName()));

        Iterator iterator = descriptor.getSecurityRoleIterator();
        Node node = null;
        Jeus30SecurityRole role = null;
        int i;
        while ( iterator.hasNext() ) {
	    node = XmlUtil.getNewElementNode(doc, "role-assign");
	    current.appendChild(node);
            role = (Jeus30SecurityRole)iterator.next();
	        XmlUtil.addNode(node, XmlUtil.getNewTextNode((DocumentImpl)doc, "role-name", role.getRoleName()));
            for ( i=0 ; i< role.getUserSize() ; i++ )
	        XmlUtil.addNode(node, XmlUtil.getNewTextNode((DocumentImpl)doc, "user", role.getUserAt(i)));
        }
    }
}
