/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.ejb.meta;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class EJBBundle extends Descriptor {
    private String displayName;
    private String smallIcon;
    private String largeIcon;
    // ejb-name => ejbDescriptor
    private Map ejbs = new HashMap();

    public EJBBundle(Document root) throws EJBDescriptionException {
        processXML(root.getDocumentElement());
    }


    /**
     * node is the Document element
     *
     * @param node
     * @throws EJBDescriptionException
     */
    public void processXML(Node node) throws EJBDescriptionException {
        super.processXML(node);
        setDisplayName(XmlUtil.getChildNodeValueOf(node, "display-name"));
        setSmallIcon(XmlUtil.getChildNodeValueOf(node, "small-icon"));
        setLargeIcon(XmlUtil.getChildNodeValueOf(node, "large-icon"));

        // session bean
        Iterator sessionBeans = XmlUtil.getElementsByTagName((Element) node, "session");
        while(sessionBeans.hasNext()) {
            Element session = (Element) sessionBeans.next();
            SessionBeanDescriptor sbd = new SessionBeanDescriptor();
            sbd.processXML(session);
            addEjb(sbd);
        }

        // entity bean not supported now
        // message driven bean not supported now
        // security-role not supported now
        // method-permission not supported now

        Iterator cmts = XmlUtil.getElementsByTagName((Element) node, "container-transaction");
        while(cmts.hasNext()) {
            Element cmt = (Element) cmts.next();
            ContainerTransactionDescriptor ctd = new ContainerTransactionDescriptor(this);
            // will register cmt to EJBDescriptor
            ctd.processXML(cmt);
        }

        // set the ejb reference ejb-name
        initEjbReference();

    }

    private void initEjbReference() throws EJBDescriptionException {
        for(Iterator it = ejbs.values().iterator(); it.hasNext();) {
            EJBDescriptor ejb = (EJBDescriptor) it.next();
            List ejbRefs = ejb.getEjbReferences();
            for(int i = 0; i < ejbRefs.size(); i++) {
                EJBReferenceDescriptor ejbRef = (EJBReferenceDescriptor) ejbRefs.get(i);
                String linkedEjbName = ejbRef.getEjbName();
                if(linkedEjbName == null || linkedEjbName.trim().equals("")) {
                    // if not specify ejb-link, just use ejb-ref
                    ejbRef.setJndiName(ejbRef.getRefName());
                }
                else {
                    if(!ejbs.containsKey(linkedEjbName)) {
                        throw new EJBDescriptionException("not find the ejb name for ejb-link: " + linkedEjbName + " in ejb: " + ejb.getEjbName());
                    }
                    ejbRef.setJndiName(((EJBDescriptor) ejbs.get(linkedEjbName)).getJndiName());

                }
            }
        }
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getSmallIcon() {
        return smallIcon;
    }

    public void setSmallIcon(String smallIcon) {
        this.smallIcon = smallIcon;
    }

    public String getLargeIcon() {
        return largeIcon;
    }

    public void setLargeIcon(String largeIcon) {
        this.largeIcon = largeIcon;
    }

    private void addEjb(EJBDescriptor ejbDesc) {
        ejbDesc.setEJBBundle(this);
        ejbs.put(ejbDesc.getEjbName(), ejbDesc);
    }

    public EJBDescriptor getEjb(String ejbName) {
        return (EJBDescriptor) ejbs.get(ejbName);
    }

    public EJBDescriptor[] getEjbDescriptors() {
        return (EJBDescriptor[]) ejbs.values().toArray(new EJBDescriptor[ejbs.size()]);
    }

    public static void main(String[] args) {

    }

}

