/*
 * 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
 *
 * Initial developer(s): Philippe Durieux
 * Contributor(s):
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas_ejb.container;

import java.lang.Object;
import javax.ejb.EJBException;
import javax.ejb.EJBLocalHome;
import javax.ejb.EJBLocalObject;
import javax.ejb.RemoveException;

/**
 * Generic part of the EJBLocalObject implementation
 */
public abstract class JEntityLocal extends JLocal {

    protected JEntityFactory bf;
    protected JEntitySwitch bs;

    /**
     * constructor
     */
    public JEntityLocal(JEntityFactory bf) {
	super(bf);
	TraceEjb.debugIc("JEntityLocal constructor");
	this.bf = bf;
    }

    /**
     * finish initialization
     */
    public void setEntitySwitch(JEntitySwitch bs) {
	TraceEjb.debugIc("JEntityLocal setEntitySwitch");
	this.bs = bs;
    }

    // --------------------------------------------------------------------------
    // EJBLocalObject implementation
    // remove() is implemented in the generated part.
    // --------------------------------------------------------------------------

    public abstract void remove() throws RemoveException;

    /**
     * Returns the enterprise Bean's local home interface.
     */
    public EJBLocalHome getEJBLocalHome() {
	TraceEjb.debugIc("JEntityLocal getEJBLocalHome");
	return bf.getLocalHome();
    }

    /**
     * returns the Primary Key for this EJBLocalObject
     */
    public Object getPrimaryKey() {
	TraceEjb.debugIc("JEntityLocal getPrimaryKey");
	if (bs == null) {
	    throw new EJBException("No Primary Key yet");
	}
	return bs.getPrimaryKey();
    }
 
    /**
     * Tests if a given EJB is identical to the invoked EJB object.
     *
     * @param     EJBLocalObject obj - An object to test for identity with the invoked object. 
     * @return    True if the given EJB object is identical to the invoked object. 
     * @throws EJBException: Thrown when the method failed due to a system-level failure.
     */
    public boolean isIdentical(EJBLocalObject obj) {
	TraceEjb.debugIc("JEntityLocal isIdentical");
	boolean ret = false;
	if (obj != null) {
	    ret = obj.equals(this);
	}
	return ret;
    }

    public RequestCtx preInvoke(int txa, String secu) {
	TraceEjb.debugIc("JEntityLocal preInvoke");
	return bf.preInvoke(txa, secu);
    }

    public void postInvoke(RequestCtx rctx) {
	TraceEjb.debugIc("JEntityLocal postInvoke");
	try {
	    bf.postInvoke(rctx);
	} finally {
	    if (rctx.sysExc != null) {
		bs.discardICtx(rctx.currTx);
	    } else {
		bs.releaseICtx(rctx.currTx);
	    }
	}
    }
}
