## ---------------------------------------------------------------------------
## 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: JEntityLocal.vm 19144 2009-12-18 15:32:31Z durieuxp $
## ---------------------------------------------------------------------------

// This class is the bean specific part of JEntityLocal
// Generated by GenIC via Velocity. Do not edit directly this file.
// Start: $Id: JEntityLocal.vm 19144 2009-12-18 15:32:31Z durieuxp $

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

import java.lang.Error;
import java.lang.RuntimeException;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import org.ow2.jonas.lib.ejb21.JEntityContext;
import org.ow2.jonas.lib.ejb21.JEntityFactory;
import org.ow2.jonas.lib.ejb21.JEntityLocal;
import org.ow2.jonas.lib.ejb21.ha.JRepEntityLocal;
import org.ow2.jonas.lib.ejb21.RequestCtx;
import org.ow2.jonas.lib.ejb21.TraceEjb;
import org.ow2.jonas.lib.ejb21.EJBInvocation;
import org.ow2.jonas.lib.ejb21.JWrapper;
import org.objectweb.util.monolog.api.BasicLevel;
#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 JRepEntityLocal
#else
    extends JEntityLocal
#end
    implements PObject, $local
#*  *##if ($dd.needJormCoherenceHelper())##
        , ${dd.getJormCoherenceHelperFQItfName()}
#*  *##end##
    {
#else

public class $class extends JEntityLocal implements $local {
#end
    /**
     * Constructor
     */
    public $class(JEntityFactory bf) {
    super(bf);
    }

#foreach ($M in $methodList)

#if ($M.isRemoveThis())

    /**
     * remove this bean instance
     */
    public void remove() throws RemoveException {
    //TraceEjb.interp.log(BasicLevel.DEBUG, "");
       EJBInvocation ejbInv = new EJBInvocation();
    ejbInv.methodPermissionSignature = "$M.getSecuritySignature()";
    ejbInv.arguments = $M.getArrayObjectParameters();
    RequestCtx rctx = preInvoke($M.getTxAttribute());
        checkSecurity(ejbInv);
        JEntityContext bctx = null;
    try {
            // false -> avoids checking reentrant in cascade delete.
            // TODO find a better way to allow this.
        bctx = bs.getICtx(rctx.currTx, false);
            rctx.bmcalled = true;
        bctx.setRemoved();
    } 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 {
#if ($isClusterReplicated)
            postInvoke(rctx, bctx);
#else
        postInvoke(rctx);
#end
    }
    }

#else

    public $M.getReturnType() $M.getName()($M.getFormalParameters())
#if ( $M.getExceptionList().length() > 0 )
    throws $M.getExceptionList()
#end
    {
    //TraceEjb.interp.log(BasicLevel.DEBUG, "");
#if ($M.getReturnType() != "void")
    $M.getReturnType() result;
#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 (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 {
#if ($isClusterReplicated)
            postInvoke(rctx, bctx);
#else
        postInvoke(rctx);
#end
    }
#if ($M.getReturnType() != "void")
    return result;
#end
    }

#end ##else

#end ##foreach

    JWrapper jwrapper = null;
    public JWrapper getJWrapper() {
        if (jwrapper == null) {
            String jndiname = bf.getLocalHome().getJndiLocalName();
            Object pk = getPrimaryKey();
            jwrapper = new JWrapper(JWrapper.LOCAL_ENTITY, jndiname, pk);
        }

        return jwrapper;
    }

#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
     */

    Object pk = null;

    /**
     * The getPrimaryKey method is overrided in order to translate the PName
     * managed by the container, into a PK.
     */
    public Object getPrimaryKey() {
        try {
            if (pk == null) {
                pk = ((Coder) getEJBLocalHome()).encode((PName) super.getPrimaryKey());
            }
            return pk;
        } 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();
    }

###############################################################################
###############################################################################
##
## 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( beforeRecall )
        RequestCtx rctx = preInvoke(MethodDesc.TX_SUPPORTS);
        try {
            JEntityContext bctx = bs.getICtx(rctx.currTx, false);
            EJBInvocation ejbInv = new EJBInvocation();
            $bean_wrp b = ($bean_wrp) bctx.getInstance();
            ejbInv.bean = b;
            checkSecurity(ejbInv);
            rctx.bmcalled = true;
#end##

#macro( afterRecall )
        } 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 {
            postInvoke(rctx);
        }
#end

#*  *##foreach ($F in $cmrList)
#*  *##if (!$F.isMMu() && !$F.isMOu())##
    // IMPLEMENTATION OF THE ${F.getHelperFQClassName()} INTERFACE //
    //-------------------------------------------------------------//
################################################
#*      *##if ($F.getRsr().isTargetMultiple())
    public ${F.getTypeName()} jonasGet${F.getUFLName()}() {
        //TraceEjb.interp.log(BasicLevel.DEBUG, "");
        ${F.getTypeName()} result = null;
#beforeRecall()
            result = b.jonasGet${F.getUFLName()}();
#afterRecall()
        return result;
    }

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

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

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

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


#*  *##end##
#*  *##end## foreach CMR field
#end
}
// End: $Id: JEntityLocal.vm 19144 2009-12-18 15:32:31Z durieuxp $
