/* 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.*;
import cobalt.deployment.util.XmlUtil;
import cobalt.deployment.util.CommonUtil;

/**
 * Title:        ejb components xml
 * Description:  J2EE standard module of ejb components xml description
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author YeonJun Choi
 * @version 1.0
 */

public class EjbBundleNode extends ElementNode {
    public EjbBundleNode(Document doc) {
        super((DocumentImpl)doc, "ejb-jar");
    }
    // getting EjbBundleDescriptor from this element
    static public EjbBundleDescriptor 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 ) {
                        EjbDescriptor ejb = EjbNode.getEjbDescriptor(childlist.item(j));
                        bundle.addEjbDescriptor(ejb);
                        try {
                            bundle.setEjbMethods(ejb);
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                    }
                }
            }
            else if ( tag.equals("assembly-descriptor") ) {
                setAssemblyDescriptor(bundle, node);
            }
            else if ( tag.equals("ejb-client-jar") ) {
                bundle.setClientJar(XmlUtil.getNodeTextValue(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);
            tag = node.getNodeName();
System.out.println("EjbBundleNode : ========================== tag : " + tag);
            if ( tag.equals("security-role") ) {
                Node child = node.getFirstChild();
                bundle.addRole(XmlUtil.getNodeTextValue(child), XmlUtil.getNodeTextValue(child.getNextSibling()));
            }
            else if ( tag.equals("container-transaction") ) {
                XmlUtil.setContainerTransactionFromNode(node, bundle);
            }
            else if ( tag.equals("method-permission") ) {
                XmlUtil.setMethodPermissionFromNode(node, bundle);
            }
        }
    }
    // setting this element using ejb bundle descriptor
    public void setEjbBundleDescriptor(EjbBundleDescriptor descriptor) {
		EjbNode ejbNode;
        EjbDescriptor ejb;
        String beanType;
        Object[] entry;
        MethodNode mtdNode;
        MethodDescriptor md;
        MethodPermission mp;
        ContainerTransaction ct;

        Document doc = getOwnerDocument();
        XmlUtil.appendDescriptorNode((DocumentImpl)doc, this, descriptor);
    	Vector ejbs = descriptor.getEjbDescriptorValues();
        // Setting ejb-descriptor
        Node node = new ElementImpl((DocumentImpl)doc,"enterprise-beans");
        appendChild(node);
        for ( int i=0 ; i<ejbs.size(); i++) {
            ejb = (EjbDescriptor)ejbs.elementAt(i);
            beanType = ejb.getEjbType();
            ejbNode = new EjbNode(doc, beanType.toLowerCase());
            ejbNode.setEjbDescriptor(ejb);
            node.appendChild(ejbNode);
        }
        // Setting assembly-descriptor
        node = new ElementImpl((DocumentImpl)doc,"assembly-descriptor");
        appendChild(node);
        Node child;
        Iterator iterator;

        // add Security Roles
        XmlUtil.appendSecurityRealmNode((DocumentImpl)doc, node,descriptor.getSecurityRoles());
        /*
        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 = (EjbDescriptor)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()));
                XmlUtil.addNode(child, XmlUtil.getNewTextNode((DocumentImpl)doc, "ejb-client-jar", CommonUtil.getFileName(descriptor.getClientJar())));
            }
        }
    }

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