## ---------------------------------------------------------------------------
## JOnAS: Java(TM) Open Application Server
## Copyright (C) 1999-2004 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, Helene Joanin.
## Contributor(s): ______________________________________.
##
## ---------------------------------------------------------------------------
## $Id: JEntityRemote.vm 11886 2007-10-31 14:51:39Z loris $
## ---------------------------------------------------------------------------

// This class is the bean specific part of JEntityRemote
// Generated by GenIC via Velocity. Do not edit directly this file.
// Start: $Id: JEntityRemote.vm 11886 2007-10-31 14:51:39Z loris $

#if ($package.length() != 0)
package $package;
#end

import java.lang.Error;
import java.lang.RuntimeException;
import java.rmi.RemoteException;
import java.rmi.AccessException;

import javax.ejb.Handle;
import javax.ejb.RemoveException;
import javax.ejb.EJBException;

import org.ow2.carol.rmi.exception.RmiUtility;
import org.ow2.carol.util.configuration.ConfigurationRepository;

import org.ow2.jonas.lib.ejb21.JEntityContext;
import org.ow2.jonas.lib.ejb21.JEntityFactory;
import org.ow2.jonas.lib.ejb21.JEntityRemote;
import org.ow2.jonas.lib.ejb21.ha.JRepEntityRemote;
import org.ow2.jonas.lib.ejb21.RequestCtx;
import org.ow2.jonas.lib.ejb21.TraceEjb;
import org.ow2.jonas.lib.svc.JHandleIIOP;
import org.objectweb.util.monolog.api.BasicLevel;
import org.ow2.jonas.lib.ejb21.EJBInvocation;

#if ($EntityCMP2.booleanValue())
import org.ow2.jonas.deployment.ejb.MethodDesc;
import org.ow2.jonas.lib.ejb21.jorm.PObject;
import org.ow2.jonas.lib.ejb21.jorm.Coder;
import org.objectweb.jorm.naming.api.PName;



public class $class
#if ($isClusterReplicated)
    extends JRepEntityRemote
#else
    extends JEntityRemote
#end
    implements PObject, $remote
#*  *##if ($dd.needJormCoherenceHelper())##
        , ${dd.getJormCoherenceHelperFQItfName()}
#*  *##end##
    {
#else

public class $class
#if ($isClusterReplicated)
    extends JRepEntityRemote
#else
    extends JEntityRemote
#end
    implements $remote {
#end

    /**
     * Constructor
     */
    public $class(JEntityFactory bf) throws RemoteException {

        super(bf);
    }

    /**
     * EJBObject interface : getHandle.
     * If Handle not build yet, create a ne one before.
     */
    public Handle getHandle() {
       String protocol = ConfigurationRepository.getCurrentConfiguration().getProtocol().getName();
       //TraceEjb.interp.log(BasicLevel.DEBUG, "Current protocol = " + protocol);

        if (protocol.equals("iiop")) {
            return new JHandleIIOP(this);
        } else {
            return new $handle(this);
        }
    }

#foreach ($M in $methodList)

#if ($M.isRemoveThis())

    /**
     * remove this bean instance
     */
    public void remove() throws RemoteException, RemoveException {

        //TraceEjb.interp.log(BasicLevel.DEBUG, "");

        #beforeThrowRemoteException()

#if ($isClusterReplicated)
/* ******************REPLICATION CODE STARTS HERE*********************** */
        if (hasResponse()) {
            cmilogger.log(BasicLevel.DEBUG, "Response found");
            // Obtain the response associated with the request
            // and delete the request because the method is going to return
            Object response = getResponse();
            return;
        } else {
            cmilogger.log(BasicLevel.DEBUG, "Response not found");
        }
/* **********************END OF REPLICATION CODE************************ */
#end

        EJBInvocation ejbInv = new EJBInvocation();
        ejbInv.methodPermissionSignature = "$M.getSecuritySignature()";
        ejbInv.arguments = $M.getArrayObjectParameters();

        RequestCtx rctx = preInvoke($M.getTxAttribute());
        checkSecurity(ejbInv);
        JEntityContext bctx = null;
        try {
            bctx = bs.getICtx(rctx.currTx, true);
            rctx.bmcalled = true;
            bctx.setRemoved();
        } catch (javax.ejb.AccessLocalException e) {
            throw new AccessException("Security Exception thrown by an enterprise Bean", e);
        } catch (RuntimeException e) {
            rctx.sysExc = e;
            throw new RemoteException("RuntimeException thrown by an enterprise Bean", e);
        } catch (Error e) {
            rctx.sysExc = e;
            throw new RemoteException("Error thrown by an enterprise Bean", e);
        } catch (RemoteException e) {
            rctx.sysExc = e;
            throw e;
        } finally {
#if ($isClusterReplicated)
/* ******************REPLICATION CODE STARTS HERE*********************** */
            // There is no response to the client (void parameter needed by HA)
            postInvoke(rctx, bctx, null);
/* **********************END OF REPLICATION CODE************************ */
#else
            postInvoke(rctx);
#end
        }

        #afterThrowRemoteException()

    }

#else

    public $M.getReturnType() $M.getName()($M.getFormalParameters()) throws $M.getExceptionList() {

        //TraceEjb.interp.log(BasicLevel.DEBUG, "");

        #beforeThrowRemoteException()

        #if ($M.getReturnType() != "void")
        $M.getReturnType() result = $M.getDefaultValue();
        #end

#if ($isClusterReplicated)
/* ******************REPLICATION CODE STARTS HERE*********************** */
        if (hasResponse()) {
            cmilogger.log(BasicLevel.DEBUG, "Response found");
            // Obtain the response associated with the request
            // and delete the request because the method is going to return
            Object response = getResponse();
    #if ($M.getReturnType() == "void")
            cmilogger.log(BasicLevel.DEBUG, "\tResponse void");
            return;
    #else
            cmilogger.log(BasicLevel.DEBUG, "\tResponse not void");
        #if ($M.getWrapperType() != "")
            return ($M.getReturnType()) (($M.getWrapperType())response).$M.getReturnType()Value();
        #else
            return ($M.getReturnType()) response;
        #end
    #end
        } else {
            cmilogger.log(BasicLevel.DEBUG, "Response not found");
        }
/* **********************END OF REPLICATION CODE************************ */
#end

        EJBInvocation ejbInv = new EJBInvocation();
        ejbInv.methodPermissionSignature = "$M.getSecuritySignature()";
        ejbInv.arguments = $M.getArrayObjectParameters();

        RequestCtx rctx = preInvoke($M.getTxAttribute());
        JEntityContext bctx = null;
        try {
            bctx = bs.getICtx(rctx.currTx, true);
            $bean_wrp b = ($bean_wrp) bctx.getInstance();
            ejbInv.bean = b;
            checkSecurity(ejbInv);
            rctx.bmcalled = true;
            #if ($M.getReturnType() != "void")
            result =
            #end
            b.$M.getName()($M.getActualParameters());
        } catch (javax.ejb.AccessLocalException e) {
            throw new AccessException("Security Exception thrown by an enterprise Bean", e);
        } catch (RuntimeException e) {
            rctx.sysExc = e;
            throw new RemoteException("RuntimeException thrown by an enterprise Bean", e);
        } catch (Error e) {
            rctx.sysExc = e;
            throw new RemoteException("Error thrown by an enterprise Bean", e);
        } catch (RemoteException e) {
            rctx.sysExc = e;
            throw e;
        } finally {
#if ($isClusterReplicated)
/* ******************REPLICATION CODE STARTS HERE*********************** */
            // If exists, pass the return type to postInvoke (Needed by HA)
        #if ($M.getReturnType() != "void")
            #if ($M.getWrapperType() != "")
            postInvoke(rctx, bctx, new $M.getWrapperType()(result));
            #else
            postInvoke(rctx, bctx, result);
            #end
        #else
            postInvoke(rctx, bctx, null);
        #end
/* **********************END OF REPLICATION CODE************************ */
#else
            postInvoke(rctx);
#end
        }
        #if ($M.getReturnType() != "void")
        return result;
        #end

        #afterThrowRemoteException()
    }

#end

#end

#if ($EntityCMP2.booleanValue())
    /**
     * CMP2 methods :
     * ==============
     * In our case it has been choosen that the container (the generic part)
     * manages PName. This is the generated objects which do the translation
     */

    /**
     * The getPrimaryKey method is overrided in order to translate the PName
     * managed by the container, into a PK.
     */
    public Object getPrimaryKey() {
        try {
            return ((Coder) getEJBHome())
                .encode((PName) super.getPrimaryKey());
        } catch (Exception e) {
            TraceEjb.interp.log(BasicLevel.ERROR,
                "Impossible to translate the PName in Primary key", e);
            throw new EJBException(
                "Impossible to translate the PName in Primary key", e);
        }
    }

    // IMPLEMENTATION OF THE PObject INTERFACE //

    public PName getPName() {
        return (PName) super.getPrimaryKey();
    }
    // Management of the CMR coherence //
    //---------------------------------//
###############################################################################
###############################################################################
##
## Iterate over the CMR field in order to add the methods which are used to
## maintain the coherence of relations. There are two macro which avoid to
## duplicate the source code.
##
#macro( beforeRecallr )
        RequestCtx rctx = null;
        try {
            rctx = preInvoke(MethodDesc.TX_SUPPORTS);
        } catch (RemoteException e) {
            throw new EJBException("Remote Exception raised:", e);
        }
        try {
            JEntityContext bctx = bs.getICtx(rctx.currTx, false);
            $bean_wrp b = ($bean_wrp) bctx.getInstance();
            rctx.bmcalled = true;
#end##
#macro( afterRecallr )
        } catch (EJBException e) {
            rctx.sysExc = e;
            throw e;
        } catch (RuntimeException e) {
            rctx.sysExc = e;
            throw new EJBException("RuntimeException thrown by an enterprise Bean", e);
        } catch (Error e) {
            rctx.sysExc = e;
            throw new EJBException("Error thrown by an enterprise Bean"+e);
        } catch (RemoteException e) {
            rctx.sysExc = e;
            throw new EJBException("Remote Exception raised:", e);
        } finally {
            try {
                postInvoke(rctx);
            } catch (RemoteException e) {
                throw new EJBException("Remote Exception raised:", e);
            }
        }
#end
#*  *##foreach ($F in $cmrList)
#*  *##if (!$F.isMMu() && !$F.isMOu())##
    // Methods to manage the coherence of the ${F.getName()} CMR field //
    //------------------------------------------------------------//
################################################
#*      *##if ($F.getRsr().isTargetMultiple())
    public ${F.getTypeName()} jonasGet${F.getUFLName()}() {
        //TraceEjb.interp.log(BasicLevel.DEBUG, "");
        ${F.getTypeName()} result = null;
#beforeRecallr()
            result = b.jonasGet${F.getUFLName()}();
#afterRecallr()
        return result;
    }

    public void jonasSet${F.getUFLName()}(${F.getTypeName()} val) {
        //TraceEjb.interp.log(BasicLevel.DEBUG, "");
#beforeRecallr()
            b.jonasSet${F.getUFLName()}(val);
#afterRecallr()
    }

    public void jonasAdd${F.getUFLName()}(${F.getOppositeHelperFQClassName()} bean) {
        //TraceEjb.interp.log(BasicLevel.DEBUG, "");
#beforeRecallr()
            b.jonasAdd${F.getUFLName()}(bean);
#afterRecallr()
    }

    public void jonasRemove${F.getUFLName()}(${F.getOppositeHelperFQClassName()} bean) {
        //TraceEjb.interp.log(BasicLevel.DEBUG, "");
#beforeRecallr()
            b.jonasRemove${F.getUFLName()}(bean);
#afterRecallr()
    }

##
#*      *##else## simple CMR field
##
    public ${F.getOppositeHelperFQClassName()} jonasGet${F.getUFLName()}() {
        //TraceEjb.interp.log(BasicLevel.DEBUG, "");
        ${F.getOppositeHelperFQClassName()} result = null;
#beforeRecallr()
            result = b.jonasGet${F.getUFLName()}();
#afterRecallr()
        return result;
    }

    public void jonasSet${F.getUFLName()}(${F.getOppositeHelperFQClassName()} val) {
        //TraceEjb.interp.log(BasicLevel.DEBUG, "");
#beforeRecallr()
            b.jonasSet${F.getUFLName()}(val);
#afterRecallr()
    }
#*      *##end## multivalued CMR field


#*  *##end##
#*  *##end## foreach CMR field
#end

}
// End: $Id: JEntityRemote.vm 11886 2007-10-31 14:51:39Z loris $
