/* 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.ArrayList;
import java.util.Iterator;
import java.util.List;

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

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

public class ContainerTransactionDescriptor extends Descriptor {
    private EJBBundle ejbBundle;
    private byte transactionAttr;
    private List methods = new ArrayList();

    public ContainerTransactionDescriptor(EJBBundle ejbBundle) {
        this.ejbBundle = ejbBundle;
    }

    public void processXML(Node node) throws EJBDescriptionException {
        super.processXML(node);
        setTransactionAttr(XmlUtil.getChildNodeValueOf(node, "trans-attribute"));
        Iterator methodElements = XmlUtil.getElementsByTagName((Element) node, "method");
        while(methodElements.hasNext()) {
            Element methodElement = (Element) methodElements.next();
            MethodDescriptor md = new MethodDescriptor();
            md.processXML(methodElement);
            methods.add(md);
        }

        // add Method Transaction to EJBDescriptor
        for(int i = 0; i < methods.size(); i++) {
            MethodDescriptor md = (MethodDescriptor) methods.get(i);
            EJBDescriptor ejbDesc = ejbBundle.getEjb(md.getEjbName());
            if(ejbDesc == null)
                throw new EJBDescriptionException("Undefined ejb-name \"" + md.getEjbName() + "\"");
            MethodTransaction mt = new MethodTransaction();
            mt.setMethod(md);
            mt.setTransactionAttributeString(getTransactionAttrString());
            ejbDesc.addMethodTransaction(mt);
        }
    }

    public EJBBundle getEjbBundle() {
        return ejbBundle;
    }

    public void setEjbBundle(EJBBundle ejbBundle) {
        this.ejbBundle = ejbBundle;
    }

    public String getTransactionAttrString() {
        return TransactionAttribute.getTransactionAttrString(transactionAttr);
    }

    public void setTransactionAttr(String transactionAttrString) {
        this.transactionAttr = TransactionAttribute.getTransactionAttribute(transactionAttrString);
    }

    public List getMethods() {
        return methods;
    }

    public void setMethods(List methods) {
        this.methods = methods;
    }

    public static void main(String[] args) {

    }
}

