/* 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 org.w3c.dom.Node;

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

public class SessionBeanDescriptor extends EJBDescriptor {
    public static final String STATEFUL_SESSION = "Stateful";
    public static final String STATELESS_SESSION = "Stateless";
    public static final String BEAN_MANAGED_TRANSACTION = "Bean";
    public static final String CONTAINER_MANAGED_TRANSACTION = "Container";
    private String sessionType;
    private String transactionType;

    public SessionBeanDescriptor() {
    }

    public void processXML(Node node) throws EJBDescriptionException {
        super.processXML(node);
        setSessionType(XmlUtil.getChildNodeValueOf(node, "session-type"));
        setTransactionType(XmlUtil.getChildNodeValueOf(node, "transaction-type"));
    }

    public String getSessionType() {
        return sessionType;
    }

    public String getTransactionType() {
        return transactionType;
    }

    public String getType() {
        return "Session";
    }

    public boolean isBeanManagedTransaction() {
        return BEAN_MANAGED_TRANSACTION.equals(transactionType);
    }

    public boolean isStateless() {
        return STATELESS_SESSION.equals(sessionType);
    }

    public void setSessionType(String type) {
        if(!STATEFUL_SESSION.equals(type) && !STATELESS_SESSION.equals(type)) {
            throw new IllegalArgumentException("session-type must be one of \"Stateful\" and \"Stateless\"");
        }
        sessionType = type;
    }

    public void setTransactionType(String txType) {
        if(!BEAN_MANAGED_TRANSACTION.equals(txType) && !CONTAINER_MANAGED_TRANSACTION.equals(txType)) {
            throw new IllegalArgumentException("transaction-type must be one of \"Bean\" and \"Container\"");
        }
        transactionType = txType;
    }

    public static void main(String[] args) {

    }
}

