/*
 * 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.sequence;


import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;


/**
 * The SequenceSessionBean is a wrapper for the Sequence and Sequenceline entity beans.
 * The session bean is what is accessed by the SequenceEntry application. This
 * bean also implements the getCustStatus method to retrieve all common
 * belonging to a particular customer.
 */
public class SequenceSesSL implements SessionBean {

    private SessionContext  sessionContext;
    private SequenceHomeLocal sequenceHome;

    public void ejbCreate() {
    }

    /**
     * nextKey: provides the next unique key from a sequence id.
     * keys are not guaranteed to be issued in order and without gaps.
     * The only guarantee is that the key is unique in this sequence id.
     * @param id  - Id of the sequence
     * @return  - an available integer key
     * @exception FinderException - the sequence id is invalid
     */
    public int nextKey(String id) throws FinderException {
        SequenceLocal sequence = null;
        int ret = 0;

        try {
            sequence = sequenceHome.findByPrimaryKey(id);
            ret = sequence.getUniqPk();
        } catch (EJBException e) {
            e.printStackTrace();
            throw e;
        }
        System.out.println(">>>> Next PK: " + ret);
        return ret;
    }

    public void ejbRemove() {
    }

    public void ejbActivate() {
    }

    public void ejbPassivate() {
    }

    public void setSessionContext(SessionContext sessionContext) {

        this.sessionContext = sessionContext;

        InitialContext initCtx = null;
        try {
            initCtx = new InitialContext();
        } catch( NamingException ne ) {
            ne.printStackTrace(System.err);
            throw new EJBException(ne);
        }

        try {
            // the homes are available via EJB links
            sequenceHome = (SequenceHomeLocal) initCtx.lookup("java:comp/env/ejb/SequenceHomeLocal");
        } catch( NamingException e ) {
            e.printStackTrace();
            throw new EJBException(e);
        }

    }
}

