/* Generated by Together */

package cobalt.deployment.descriptor.xml;

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;

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

public class EjbNode extends ElementNode {
	public EjbNode(Document doc, String tagName) {
        super((DocumentImpl)doc, tagName);
    }
    public static EjbDescriptor getEjbDescriptor(Node node) {
        //EjbDescriptor ejb = new EjbDescriptor();
        if (((Element)node).getTagName().equals("session"))
            return getSessionEjbDescriptor(node);
        else if (((Element)node).getTagName().equals("entity"))
            return getEntityEjbDescriptor(node);
        return null;
    }
    public static SessionEjbDescriptor getSessionEjbDescriptor(Node parent) {
        SessionEjbDescriptor ejb = new SessionEjbDescriptor();
        NodeList nodelist = parent.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") )
                ejb.setDescription(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("display-name") )
                ejb.setName(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("ejb-name") )
                ejb.setEjbName(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("small-icon") )
                ejb.setSmallIcon(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("large-icon") )
                ejb.setLargeIcon(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("home") )
                ejb.setHomeInterface(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("remote") )
                ejb.setRemoteInterface(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("ejb-class") )
                ejb.setBeanClass(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("session-type") )
                ejb.setSessionType(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("transaction-type") )
                ejb.setTransactionType(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("env-entry") )
                ejb.addEnv(XmlUtil.getEnvFromNode(node));
            else if ( tag.equals("ejb-ref") )
                ejb.addEjbReference(XmlUtil.getEjbReferenceFromNode(node));
            else if ( tag.equals("security-role-ref") )
                ejb.addSecurityRole(XmlUtil.getSecurityRoleFromNode(node));
            else if ( tag.equals("resource-ref") )
                ejb.addResource(XmlUtil.getResourceReferenceFromNode(node));
        }
        return ejb;
    }
    public static EntityEjbDescriptor getEntityEjbDescriptor(Node parent) {
        EntityEjbDescriptor ejb = new EntityEjbDescriptor();
        NodeList nodelist = parent.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") )
                ejb.setDescription(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("display-name") )
                ejb.setName(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("ejb-name") )
                ejb.setEjbName(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("small-icon") )
                ejb.setSmallIcon(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("large-icon") )
                ejb.setLargeIcon(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("home") )
                ejb.setHomeInterface(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("remote") )
                ejb.setRemoteInterface(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("ejb-class") )
                ejb.setBeanClass(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("persistence-type") )
                ejb.setTransactionType(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("prim-key-class") )
                ejb.setPrimaryKeyClassName(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("reentrant") )
                ejb.setReentrant(Boolean.getBoolean(XmlUtil.getNodeTextValue(node)));
            else if ( tag.equals("cmp-field") )
                ejb.putContainerManagedField(XmlUtil.getContainerManagedFieldFromNode(node));
            else if ( tag.equals("primkey-field") )
                ejb.setPrimaryKeyField(XmlUtil.getNodeTextValue(node));
            else if ( tag.equals("env-entry") )
                ejb.addEnv(XmlUtil.getEnvFromNode(node));
            else if ( tag.equals("ejb-ref") )
                ejb.addEjbReference(XmlUtil.getEjbReferenceFromNode(node));
            else if ( tag.equals("security-role-ref") )
                ejb.addSecurityRole(XmlUtil.getSecurityRoleFromNode(node));
            else if ( tag.equals("resource-ref") )
                ejb.addResource(XmlUtil.getResourceReferenceFromNode(node));
        }
        return ejb;
    }
    public void setEjbDescriptor(EjbDescriptor descriptor) {
        Document doc = getOwnerDocument();
        XmlUtil.appendDescriptorNode((DocumentImpl)doc, this, descriptor);
        XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "ejb-name", descriptor.getEjbName()));
        XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "home", descriptor.getHomeInterface()));
        XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "remote", descriptor.getRemoteInterface()));
        XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "ejb-class", descriptor.getBeanClass()));
        if (descriptor.getEjbType().equals("Session")) {
            // Setting Session Descriptor
        	XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "session-type", ((SessionEjbDescriptor)descriptor).getSessionType()));
        	XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "transaction-type", descriptor.getTransactionType()));
        }
        else {
            // Setting Entity Descriptor
        	XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "persistence-type", descriptor.getTransactionType()));
        	XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "prim-key-class", ((EntityEjbDescriptor)descriptor).getPrimaryKeyClassName()));
        	XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "reentrant", ((EntityEjbDescriptor)descriptor).reentrantString()));
            XmlUtil.appendContainerManagedFieldNode((DocumentImpl)doc, this,(EntityEjbDescriptor)descriptor/*((EntityEjbDescriptor)descriptor).getAllContainerManagedFields()*/);
        	XmlUtil.addNode(this, XmlUtil.getNewTextNode((DocumentImpl)doc, "primkey-field", ((EntityEjbDescriptor)descriptor).getPrimaryKeyField()));
        }
        XmlUtil.appendEnvDescriptorNode((DocumentImpl)doc, this, descriptor.getEnvs().iterator());
        XmlUtil.appendEjbReferenceNode((DocumentImpl)doc, this, descriptor.getEjbReferences().values().iterator());
        XmlUtil.appendSecurityRoleNode((DocumentImpl)doc, this, descriptor.getSecurityRoles().iterator());
        XmlUtil.appendResourceReferenceDescriptorNode((DocumentImpl)doc, this, descriptor.getResources().iterator());
    }
}
