## ---------------------------------------------------------------------------
## JOnAS: Java(TM) Open Application Server
## Copyright (C) 1999-2006 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: JEntityLocalHome.vm 18863 2009-10-16 08:18:26Z durieuxp $
## ---------------------------------------------------------------------------

// This class is the bean specific part of JEntityLocalHome
// Generated by GenIC via Velocity. Do not edit directly this file.
// Start: $Id: JEntityLocalHome.vm 18863 2009-10-16 08:18:26Z durieuxp $

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

import java.lang.Error;
import java.lang.Object;
import java.lang.RuntimeException;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import org.ow2.jonas.lib.ejb21.JEntityContext;
import org.ow2.jonas.lib.ejb21.JEntityLocalHome;
import org.ow2.jonas.lib.ejb21.JEntityFactory;
import org.ow2.jonas.lib.ejb21.JEntityLocal;
import org.ow2.jonas.lib.ejb21.JEntitySwitch;
import org.ow2.jonas.lib.ejb21.ha.JRepEntityLocalHome;
import org.ow2.jonas.lib.ejb21.RequestCtx;
import org.ow2.jonas.lib.ejb21.TraceEjb;
import org.ow2.jonas.deployment.ejb.EntityDesc;
import org.ow2.jonas.lib.ejb21.CollectionEnum;
import org.ow2.jonas.lib.ejb21.EJBInvocation;
import org.objectweb.util.monolog.api.BasicLevel;
#if ($EntityCMP2.booleanValue())
import org.objectweb.jorm.api.PClassMapping;
import org.objectweb.jorm.api.PException;
import org.objectweb.jorm.naming.api.PName;
import org.ow2.jonas.lib.ejb21.jorm.Coder;
import org.ow2.jonas.lib.ejb21.jorm.PObject;
import org.ow2.jonas.lib.ejb21.jorm.PObjectHome;

public class $class
#if ($isClusterReplicated)
    extends JRepEntityLocalHome
#else
    extends JEntityLocalHome
#end
    implements Coder, PObjectHome, $localhome {
#else
    public class $class
#if ($isClusterReplicated)
    extends JRepEntityLocalHome
#else
    extends JEntityLocalHome
#end
    implements $localhome {
#end

        // JOnAS version used to deploy
        public static final String JONAS_VERSION = "$jVersion";

        public $class(EntityDesc dd, JEntityFactory bf) {
            super(dd, bf);
        }

#foreach ($M in $methodList)

#if ($M.isCreate())

        /**
         * create method
         */
        public $M.getReturnType() $M.getName()($M.getFormalParameters())
#if ( $M.getExceptionList().length() > 0 )
            throws $M.getExceptionList()
#end
            {
                //TraceEjb.interp.log(BasicLevel.DEBUG, "");
                EJBInvocation ejbInv = new EJBInvocation();
                ejbInv.methodPermissionSignature = "$M.getSecuritySignature()";
                ejbInv.arguments = $M.getArrayObjectParameters();
                RequestCtx rctx = preInvoke($M.getTxAttribute());
                JEntitySwitch bs = null;
                JEntityFactory ef = (JEntityFactory) bf;
                JEntityContext bctx = null;
                boolean mapped = false;
                try {
                    bs = ef.getJEntitySwitch();
                    bctx = ef.getJContext(bs);
                    bctx.setEntitySwitch(bs);
                    $bean_wrp b = ($bean_wrp) bctx.getInstance();
                    ejbInv.bean = b;
                    checkSecurity(ejbInv);
                    rctx.bmcalled = true;
#if ($EntityCMP2.booleanValue())
                    Object pk = b._ejb$M.getCapName()($M.getActualParameters());
#else
                    Object pk = b.ejb$M.getCapName()($M.getActualParameters());
#end
                    bctx.setActive();
                    boolean pkadded = ef.rebindEJB(rctx.currTx, bctx, pk);
                    bs.bindICtx(rctx.currTx, bctx);
                    mapped = true;
                    bctx.setNewInstance();
                    try {
                        b.ejbPost$M.getCapName()($M.getActualParameters());
                    } catch (EJBException e) {
                        if (pkadded) {
                            bs.forceDiscardICtx(rctx.currTx);
                            bctx = null;
                        }
                        throw e;
                    }
                } catch (javax.ejb.AccessLocalException e) {
                    throw new EJBException("Security Exception thrown by an enterprise Bean", e);
                } 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 (! mapped && bctx != null) {
                        ef.releaseJContext(bctx);
                    }
                    postInvoke(rctx);
                }
#if ($isClusterReplicated)
                if (mapped && bctx != null) {
                    replicateCreate(bctx);
                }
#end

                return ($local) bs.getLocal();
            }

#elseif ($M.isFinderCol())

        public $M.getReturnType() $M.getName()($M.getFormalParameters())
#if ( $M.getExceptionList().length() > 0 )
            throws $M.getExceptionList()
#end
            {
                //TraceEjb.interp.log(BasicLevel.DEBUG, "");

                Vector v = new Vector();
                boolean mapped = false;
                Collection pkl;
                EJBInvocation ejbInv = new EJBInvocation();
                ejbInv.methodPermissionSignature = "$M.getSecuritySignature()";
                ejbInv.arguments = $M.getArrayObjectParameters();
                RequestCtx rctx = preInvoke($M.getTxAttribute());
                JEntitySwitch bs = null;
                JEntityFactory ef = (JEntityFactory) bf;
                JEntityContext bctx = null;
                try {
                    ef.syncForFind(rctx.currTx);
                    bctx = ef.getJContext(null);
                    $bean_wrp b = ($bean_wrp) bctx.getInstance();
                    ejbInv.bean = b;
                    checkSecurity(ejbInv);
                    bctx.setFinding();
                    rctx.bmcalled = true;
                    pkl = b.ejb$M.getCapName()($M.getActualParameters());
                    bctx.setActive();
                    Iterator pki = pkl.iterator();
                    while (pki.hasNext()) {
                        Object _pk = pki.next();
#if ($EntityCMP2.booleanValue())
                        // Fix bug #300526
                        PName pn = (PName) _pk;
                        if ((pn == null) || (pn.isNull())) {
                            v.addElement(null);
                        } else
#end
                        {
                            bs = ef.getEJB(_pk);
                            if (!mapped) {
                                mapped = bs.tryBindICtx(rctx.currTx, bctx, false);
                            }
                            v.addElement(bs.getLocal());
                        }
                    }
                } catch (javax.ejb.AccessLocalException e) {
                    throw new EJBException("Security Exception thrown by an enterprise Bean", e);
                } 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 (! mapped && bctx != null) {
                        ef.releaseJContext(bctx);
                    }
                    postInvoke(rctx);
                }
                return v;
            }

#elseif ($M.isFinderEnum())

        public $M.getReturnType() $M.getName()($M.getFormalParameters())
#if ( $M.getExceptionList().length() > 0 )
            throws $M.getExceptionList()
#end
            {
                //TraceEjb.interp.log(BasicLevel.DEBUG, "");

                CollectionEnum v = new CollectionEnum();
                boolean mapped = false;
                Enumeration pkl;
                EJBInvocation ejbInv = new EJBInvocation();
                ejbInv.methodPermissionSignature = "$M.getSecuritySignature()";
                ejbInv.arguments = $M.getArrayObjectParameters();
                RequestCtx rctx = preInvoke($M.getTxAttribute());
                JEntitySwitch bs = null;
                JEntityFactory ef = (JEntityFactory) bf;
                JEntityContext bctx = null;
                try {
                    ef.syncForFind(rctx.currTx);
                    bctx = ef.getJContext(null);
                    $bean_wrp b = ($bean_wrp) bctx.getInstance();
                    ejbInv.bean = b;
                    checkSecurity(ejbInv);
                    bctx.setFinding();
                    rctx.bmcalled = true;
                    pkl = b.ejb$M.getCapName()($M.getActualParameters());
                    bctx.setActive();
                    while (pkl.hasMoreElements()) {
                        Object _pk = pkl.nextElement();
#if ($EntityCMP2.booleanValue())
                        // Fix bug #300526
                        PName pn = (PName) _pk;
                        if ((pn == null) || (pn.isNull())) {
                            v.addElement(null);
                        } else
#end
                        {
                            bs = ef.getEJB(_pk);
                            if (!mapped) {
                                mapped = bs.tryBindICtx(rctx.currTx, bctx, false);
                            }
                            v.addElement(bs.getLocal());
                        }
                    }
                } catch (javax.ejb.AccessLocalException e) {
                    throw new EJBException("Security Exception thrown by an enterprise Bean", e);
                } 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 (! mapped && bctx != null) {
                        ef.releaseJContext(bctx);
                    }
                    postInvoke(rctx);
                }
                return v;
            }

#elseif ($M.isFinderSimple())
        /**
         * finder method returning only 1 PK
         */
        public $M.getReturnType() $M.getName()($M.getFormalParameters())
#if ( $M.getExceptionList().length() > 0 )
            throws $M.getExceptionList()
#end
            {
                EJBInvocation ejbInv = new EJBInvocation();
                ejbInv.methodPermissionSignature = "$M.getSecuritySignature()";
                ejbInv.arguments = $M.getArrayObjectParameters();
                RequestCtx rctx = preInvoke($M.getTxAttribute());
                JEntitySwitch bs = null;
                JEntityFactory ef = (JEntityFactory) bf;
                JEntityContext bctx = null;
                boolean mapped = false;
                try {
                    ef.syncForFind(rctx.currTx);
                    bctx = ef.getJContext(null);
                    $bean_wrp b = ($bean_wrp) bctx.getInstance();
                    ejbInv.bean = b;
                    checkSecurity(ejbInv);
                    bs = ef.getJEntitySwitch();
                    bctx.setEntitySwitch(bs);
                    bctx.setFinding();
                    rctx.bmcalled = true;
                    Object pk = b.ejb$M.getCapName()($M.getActualParameters());
#if ($EntityCMP2.booleanValue())
                    // Fix bug #300526
                    PName pn = (PName) pk;
                    if ((pn == null) || (pn.isNull())) {
                        return null;
                    }
#end
                    bctx.setActive();
                    JEntitySwitch old = ef.existEJB(pk, bs, false);
                    if (old != null) {
                        bs = old;
                        bctx.setEntitySwitch(bs);
                    }
                    mapped = bs.tryBindICtx(rctx.currTx, bctx, true);
                } 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 (! mapped && bctx != null) {
                        ef.releaseJContext(bctx);
                    }
                    postInvoke(rctx);
                }
                return ($local) bs.getLocal();
            }

#elseif ($M.isFinderByPk())
        /**
         * findByPrimaryKey
         */
        public $M.getReturnType() $M.getName()($M.getFormalParameters())
#if ( $M.getExceptionList().length() > 0 )
            throws $M.getExceptionList()
#end
            {
                EJBInvocation ejbInv = new EJBInvocation();
                ejbInv.methodPermissionSignature = "$M.getSecuritySignature()";
                ejbInv.arguments = $M.getArrayObjectParameters();
                RequestCtx rctx = preInvoke($M.getTxAttribute());
                JEntitySwitch bs = null;
                JEntityFactory ef = (JEntityFactory) bf;
                JEntityContext bctx = null;
                boolean mapped = false;
                try {
                    bctx = ef.getJContext(null);
                    $bean_wrp b = ($bean_wrp) bctx.getInstance();
                    ejbInv.bean = b;
                    checkSecurity(ejbInv);
                    bs = ef.getJEntitySwitch();
                    bctx.setEntitySwitch(bs);
                    bctx.setFinding();
                    rctx.bmcalled = true;
                    Object pk =
#if ($EntityCMP2.booleanValue())
                        decode($M.getActualParameters());
#else
                        $M.getActualParameters();
#end
                    JEntitySwitch old = ef.existEJB(pk, bs, true);
                    if (old != null) {
                        bs = old;
                        bctx.setEntitySwitch(bs);
                    } else {
                        try {
                            pk = b.ejb$M.getCapName()(($pk) $M.getActualParameters());
                        } catch (FinderException e) {
                            bs.forceDiscardICtx(rctx.currTx);
                            throw e;
                        }
                        ef.startEJB(pk);
                    }
                    bctx.setActive();
                    mapped = bs.tryBindICtx(rctx.currTx, bctx, true);
                } 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);
#if ($EntityCMP2.booleanValue())
                } catch (org.objectweb.jorm.api.PException e) {
                    rctx.sysExc = e;
                    throw new EJBException("decoding PK failed", e);
#end
                } finally {
                    if (! mapped && bctx != null) {
                        ef.releaseJContext(bctx);
                    }
                    postInvoke(rctx);
                }
                return ($local) bs.getLocal();
            }

#elseif ($M.isRemovePk())

        /**
         * Remove by PK
         * @throws RemoveException
         */
        public void remove(Object p1) throws $M.getExceptionList() {
            //TraceEjb.interp.log(BasicLevel.DEBUG, "");

            EJBInvocation ejbInv = new EJBInvocation();
            ejbInv.methodPermissionSignature = "$M.getSecuritySignature()";
            ejbInv.arguments = $M.getArrayObjectParameters();
            RequestCtx rctx = preInvoke($M.getTxAttribute());
            JEntitySwitch bs = null;
            JEntityFactory ef = (JEntityFactory) bf;
            JEntityContext bctx = null;
            boolean mapped = false;
            try {
                ef.dummyFinderException(true);
                bctx = ef.getJContext(null);
                $bean_wrp b = ($bean_wrp) bctx.getInstance();
                ejbInv.bean = b;
                checkSecurity(ejbInv);
#if ($EntityCMP2.booleanValue())
                Object pk = decode(($pk) p1);
                bs = ef.getEJB(pk);
                bctx.setEntitySwitch(bs);
                b.ejbFindByPrimaryKey((PName)pk);
#else
                bs = ef.getEJB(p1);
                bctx.setEntitySwitch(bs);
                b.ejbFindByPrimaryKey(($pk) p1);
#end
                bctx = bs.getICtx(rctx.currTx, bctx);
                // Must set this before setRemove() because exception may
                // occur in ejbRemove, and in that case, we must keep the context.
                mapped = true;
                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);
#if ($EntityCMP2.booleanValue())
            } catch (PException e) {
                rctx.sysExc = e;
                throw new EJBException("JORM PException thrown", e);
#end
            } catch (FinderException e) {
                rctx.sysExc = e;
                throw new EJBException(e.toString());
            } finally {
                if (!mapped && bctx != null) {
                    // release context in case of exception.
                    ef.releaseJContext(bctx);
                }
                postInvoke(rctx);
            }
        }

#else

        /**
         * Home Method
         */
        public $M.getReturnType() $M.getName()($M.getFormalParameters())
#if ( $M.getExceptionList().length() > 0 )
            throws $M.getExceptionList()
#end
            {
                //TraceEjb.interp.log(BasicLevel.DEBUG, "");

                EJBInvocation ejbInv = new EJBInvocation();
                ejbInv.methodPermissionSignature = "$M.getSecuritySignature()";
                ejbInv.arguments = $M.getArrayObjectParameters();
                RequestCtx rctx = preInvoke($M.getTxAttribute());
                JEntityFactory ef = (JEntityFactory) bf;
#if ($M.getReturnType() != "void")
                $M.getReturnType() result;
#end
                JEntityContext bctx = null;
                try {
                    bctx = ef.getJContext(null);
                    $bean_wrp b = ($bean_wrp) bctx.getInstance();
                    ejbInv.bean = b;
                    checkSecurity(ejbInv);
                    rctx.bmcalled = true;
#if ($M.getReturnType() != "void")
                    result =
#end
                        b.ejbHome$M.getCapName()($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 (bctx != null) {
                        ef.releaseJContext(bctx);
                    }
                    postInvoke(rctx);
                }
#if ($M.getReturnType() != "void")
                return result;
#end
            }

#end

#end

        public JEntityLocal createLocalObject() {
            return new $local_wrp((JEntityFactory)bf);
        }

#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
         * between PName and PK
         */

        // IMPLEMENTATION OF THE Coder INTERFACE //
        /**
         * This method build a PName with the primary key specifed in parameter.
         * To build the pname the PNamingContext associated to the bean is used.
         * Futhermore the PKFieldExtractor class is used as a specific PNameGetter
         * in case of the PName is composite (several fields).
         */
        public PName decode(Object pk)
            throws PException, UnsupportedOperationException {
            if (pk == null) {
                return ((PClassMapping) bf).getPBinder().getNull();
            }
#**##if($pkIsUserClass.booleanValue())
            return ((PClassMapping) bf).getPBinder().decodeAbstract(
                                                                    new PkFieldExtractor(($pk) pk), null);
#**##else ## Pk is a java.lang class (String, Long, ...)
            return ((PClassMapping) bf).getPBinder().$pkDecodeMethod;
#**##end
        }

        /**
         * This method build a primary key with the PName specifed in parameter.
         * To build the primary key the PNamingContext associated to the bean is
         * used. In case of the PName is composite (several fields), the
         * encodeAbstract must return a specific PNameGetter.
         */
        public Object encode(PName pn)
            throws PException, UnsupportedOperationException {
            if (pn==null || pn.isNull()) {
                return null;
            }
#**##if($pkIsUserClass.booleanValue())
            $pk pk = new $pk();
            ${dd.getJormPNameGetterClassName()} png = (${dd.getJormPNameGetterClassName()})
                                                    ((PClassMapping) bf).getPBinder().encodeAbstract(pn);
#*      *##foreach ($pkf in $fieldPkList)
#*        *##if (!$pkf.isMustBeConvert())
            pk.${pkf.getName()} = png.pnGet${pkf.getNameUpperFirst()}(null);
#*        *##else
            pk.${pkf.getName()} = (${pkf.getTypeName()})${pkf.getConvertClassName()}.toMemory((png.pnGet${pkf.getNameUpperFirst()}(null)));
#*        *##end
#*      *##end
            return pk;
#**##else ## Pk is a java.lang class (String, Long, ...)
            return $pkMappingToMemoryMethod(((PClassMapping) bf).getPBinder().$pkEncodeMethod(pn));
#**##end
        }

#**##if($pkIsUserClass.booleanValue())
        /**
         * This class is used to decode a primary key into a PName. It is
         * specilized to the name structure
         */
        private class PkFieldExtractor implements ${dd.getJormPNameGetterClassName()} {
    $pk pk;
    public PkFieldExtractor($pk pk) {
        this.pk = pk;
    }
#*      *##foreach ($pkf in $fieldPkList)
    public ${pkf.getJormTypeName()} pnGet${pkf.getNameUpperFirst()}(Object ctx) {
#*          *##if (!$pkf.isMustBeConvert())
        return pk.${pkf.getName()};
#*          *##else
        return (${pkf.getJormTypeName()})${pkf.getConvertClassName()}.toStorage((pk.${pkf.getName()}));
#*          *##end
    }
#*      *##end
}
#**##end

        // IMPLEMENTATION OF THE PObjectHome INTERFACE //

        public PObject getPObject(PName pn) {
            return (PObject) ((JEntityFactory) bf).getEJB(pn).getLocal();
        }
#end ##EntityCMP2
    }
// End: $Id: JEntityLocalHome.vm 18863 2009-10-16 08:18:26Z durieuxp $
