/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jtests.beans.transacted;

import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import javax.ejb.CreateException;

/**
 * Entity bean with bean managed persistence.
 */
public class SimpleEC extends SimpleCommon implements EntityBean {

    protected EntityContext entityContext;

    // Bean state
    public String accno;	// PK
    public String customer;
    public long balance;

    /**
     * Create without arg
     * transaction attribute = supports (by default in xml file) 
     */
    public String ejbCreate() throws CreateException {

	// Must init fields in any case.
	accno = "000";
	customer = "Initial";
	balance = 0;
	// In BMP, return PK.
	return accno;
    }

    /**
     * transaction attribute = notsupported
     */
    public String ejbCreate(int i) throws CreateException  {

	if (isAssociated()) {
	    throw new EJBException("ejbCreate(int i): should not be in a transaction");
	}
	Integer v = new Integer((int)i);
	accno = v.toString();
	customer = "by int";
	balance = 1000 + i;
	
	// In BMP, return PK.
	return accno;
    }

    /**
     * transaction attribute = required
     */
    public String ejbCreateForRequired(long i)  throws CreateException {

	if (!isAssociated()) {
	    throw new EJBException("ejbCreate(long i): should be in a transaction");
	}
	Integer v = new Integer((int)i);
	accno = v.toString();
	customer = "Required";
	balance = 1000 + i;
	
	// In BMP, return PK.
	return accno;
    }

    /**
     * transaction attribute = never
     */
    public String ejbCreateForNever(short i)  throws CreateException {

	if (isAssociated()) {
	    throw new EJBException("ejbCreateForNever(short i): should not be in a transaction");
	}
	Integer v = new Integer((int)i);
	accno = v.toString();
	customer = "by short";
	balance = 1000 + i;
	
	// In BMP, return PK.
	return accno;
    }

    /**
     * transaction attribute = requiresnew
     */
    public String ejbCreateForRequiresNew(String i)  throws CreateException {

	if (!isAssociated()) {
	    throw new EJBException("ejbCreate(String i): should be in a transaction");
	}
	accno = i;
	customer = "by string";
	balance = 100;

	// In BMP, return PK.
	return accno;
    }

    /**
     * transaction attribute = mandatory
     */
    public String ejbCreateForMandatory(char i)  throws CreateException {

	if (!isAssociated()) {
	    throw new EJBException("ejbCreateForMandatory(char i): should be in a transaction");
	}
	Integer v = new Integer((int)i);
	accno = v.toString();
	customer = "by char";
	balance = 200;
	
	// In BMP, return PK.
	return accno;
    }

    /**
     * transaction attribute = supports
     */
    public String ejbCreateForSupports(boolean intx)  throws CreateException {

	if (intx) {
	    accno = "TRUE";
	    balance = 101;
	    if (!isAssociated()) {
		throw new EJBException("ejbCreateForSupports(true): should be in a transaction");
	    }
	} else {
	    accno = "FALSE";
	    balance = 102;
	    if (isAssociated()) {
		throw new EJBException("ejbCreate(false): should not be in a transaction");
	    }
	}
	customer = "by boolean";
	
	// In BMP, return PK.
	return accno;
    }


    /**
     * transaction attribute = default (= supports)
     */
    public void ejbPostCreate() {

    }

    /**
     * transaction attribute = notsupported
     */
    public void ejbPostCreate(int i) {

	if (isAssociated()) {
	    throw new EJBException("ejbPostCreate(int i): should not be in a transaction");
	}
    }

    /**
     * transaction attribute = required
     */
    public void ejbPostCreateForRequired(long i) {

	if (!isAssociated()) {
	    throw new EJBException("ejbPostCreateForRequired(long i): should be in a transaction");
	}
    }

    /**
     * transaction attribute = never
     */
    public void ejbPostCreateForNever(short i) {

	if (isAssociated()) {
	    throw new EJBException("ejbPostCreateForNever(short i): should not be in a transaction");
	}
    }

    /**
     * transaction attribute = requiresnew
     */
    public void ejbPostCreateForRequiresNew(String i) {

	if (!isAssociated()) {
	    throw new EJBException("ejbPostCreateForRequiresNew(String i): should be in a transaction");
	}
    }

    /**
     * transaction attribute = mandatory
     */
    public void ejbPostCreateForMandatory(char i) {

	if (!isAssociated()) {
	    throw new EJBException("ejbPostCreateForMandatory(char i): should be in a transaction");
	}
    }

    /**
     * transaction attribute = supports
     */
    public void ejbPostCreateForSupports(boolean intx) {

	if (intx) {
	    if (!isAssociated()) {
		throw new EJBException("ejbPostCreateForSupports: should be in a transaction");
	    }
	} else {
	    if (isAssociated()) {
		throw new EJBException("ejbPostCreateForSupports: should not be in a transaction");
	    }
	}
    }


    /**
     * This method return true if there is an association of a transaction with this thread
     */
    public boolean  ejbHomeOpwith_notsupported() {
	return isAssociated();
    }

    /**
     * This method return true if there is an association of a transaction with this thread
     */
    public boolean ejbHomeOpwith_supports() {
	return isAssociated();
    }
   
    /**
     * This method return true if there is an association of a transaction with this thread
     */
    public boolean  ejbHomeOpwith_required() {
	return isAssociated();
    }

    /**
     * This method return true if there is an association of a transaction with this thread
     */
    public boolean ejbHomeOpwith_requires_new() {
	return isAssociated();
    }

    /**
     * This method return true if there is an association of a transaction with this thread
     */
    public boolean ejbHomeOpwith_mandatory() {
	return isAssociated();
    }
  
    /**
     * This method return true if there is an association of a transaction with this thread
     */
    public boolean ejbHomeOpwith_never() {
	return isAssociated();
    }

    public void ejbRemove() throws RemoveException {

    }

    public void ejbPassivate() {

    }

    public void ejbActivate() {

    }
    
    public void ejbLoad() {

    }

    public void ejbStore() {

    }

    public void setEntityContext(EntityContext ctx) {
	this.entityContext = ctx;
    }

    public void unsetEntityContext() {
	this.entityContext = null;
    }
	
 	
}
