/* Generated by Together */

package cobalt.deployment.descriptor.xml;

import java.util.Iterator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.Vector;

import org.w3c.dom.Node;
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.descriptor.dist.*;
import cobalt.deployment.util.J2eeXmlUtil;

/**
 * Title:        J2EE enterprise application xml
 * Description:  J2EE-specific enterprise application xml description
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author YeonJun Choi
 * @version 1.0
 */

public class J2eeApplicationNode extends ElementNode {
	public J2eeApplicationNode(Document doc) {
        super((DocumentImpl)doc, "j2ee-ri-specific-information");
    }
    public J2eeApplicationDescriptor getApplicationDescriptor() {
        return null;
    }
    public void setApplicationDescriptor(J2eeApplicationDescriptor descriptor) {
		Node node;
        Vector ejbs = new Vector();
        J2eeEjbDescriptor ejb;
        Document doc = getOwnerDocument();
        J2eeXmlUtil.addNode(this, new ElementImpl((DocumentImpl)doc,"server-name"));
        J2eeXmlUtil.appendRoleMapping((DocumentImpl)doc, this, "rolemapping", descriptor.getRoles().values().iterator());
        // Setting Enterprise Beans
        ejbs = descriptor.getEjbBundles().getEjbs();
        /*
        Vector ejbBundle = descriptor.getEjbBundles().getEjbs();
        for ( int i=0 ; i<ejbBundle.size() ; i++) {
            J2eeEjbBundleDescriptor tempEjb = (J2eeEjbBundleDescriptor)ejbBundle.elementAt(i);
            ejbs.addAll(tempEjb.getEjbs());
        }
        */
        node = J2eeXmlUtil.getNewElementNode((DocumentImpl)doc,"enterprise-beans");
       	appendChild(node);
        for ( int i=0 ; i<ejbs.size() ; i++) {
            Node child = J2eeXmlUtil.getNewElementNode((DocumentImpl)doc,"ejb");
            node.appendChild(child);
            ejb = (J2eeEjbDescriptor)ejbs.elementAt(i);
            J2eeXmlUtil.addNode(child, J2eeXmlUtil.getNewTextNode((DocumentImpl)doc, "ejb-name", ejb.getEjbName()));
            J2eeXmlUtil.addNode(child, J2eeXmlUtil.getNewTextNode((DocumentImpl)doc, "jndi-name", ejb.getJndiName()));
            J2eeXmlUtil.appendCmp((DocumentImpl)doc, child, ejb.getCmp());
	        J2eeXmlUtil.appendResourceReferenceDescriptor((DocumentImpl)doc, child, descriptor.getEjbBundles().getResRefs(ejb.getDescriptor()));
	        J2eeXmlUtil.appendEjbReferenceDescriptor((DocumentImpl)doc, child, descriptor.getEjbBundles().getEjbRefs(ejb.getDescriptor()));
        }

        //J2eeXmlUtil.appendResourceReferenceDescriptor((DocumentImpl)doc, this, descriptor.getEjbBundles().getResRefs());
        //J2eeXmlUtil.appendEjbReferenceDescriptor((DocumentImpl)doc, this, descriptor.getEjbBundles().getEjbRefs());
        //XmlUtil.appendSecurityRealmNode((DocumentImpl)doc, this, descriptor.getSecurityRoles());
    }

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