/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This 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 (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.varia.autonumber;

import javax.naming.InitialContext;

import org.jboss.logging.Logger;

/**
 * AutoNumberFactory can persistently auto number items. 
 *
 * @author <a href="mailto:michel.anke@wolmail.nl">Michel de Groot</a>
 * @version $Revision: 81038 $
 */
public class AutoNumberFactory
{
   private static final Logger log = Logger.getLogger(AutoNumberFactory.class);

   private static AutoNumberHome autoNumberHome;

   /**
    * Gets the next key for the given collection.
    * Note 1: you must deploy EJB AutoNumber
    * Note 2: the keys are persistent in your database, independent of 
    * the actual table
    * Note 3: you can only add instances to the collection which have a 
    * key generated by this method, otherwise the keys are not guaranteed
    * to be unique
    * Note 4: key values are >= 0
    * @param collectionName the name of the collection for which you want an autonumber
    * @throws ArrayIndexOutOfBoundsException if no more numbers are available
    */
   public static synchronized Integer getNextInteger(String collectionName)
      throws ArrayIndexOutOfBoundsException
   {
      Integer value = null;
      AutoNumber autoNumber = null;
      if (autoNumberHome == null)
      {
         try
         {
            autoNumberHome = (AutoNumberHome) new InitialContext().lookup("JBossUtilAutoNumber");
         }
         catch (javax.naming.NamingException e)
         {
            log.error("operation failed", e);
         }
      }

      try
      {
         autoNumber = autoNumberHome.findByPrimaryKey(collectionName);
      }
      catch (javax.ejb.FinderException e)
      {
         // autonumber does not exist yet, create one at value 0
         try
         {
            autoNumber = autoNumberHome.create(collectionName);
         }
         catch (javax.ejb.CreateException x)
         {
            log.error("operation failed", x);
         }
         catch (java.rmi.RemoteException x)
         {
            log.error("operation failed", x);
         }

         try
         {
            autoNumber.setValue(new Integer(0));
         }
         catch (java.rmi.RemoteException x)
         {
            log.error("operation failed", x);
         }
      }
      catch (java.rmi.RemoteException e)
      {
         log.error("operation failed", e);
      }

      try
      {
         value = autoNumber.getValue();
         autoNumber.setValue(new Integer(value.intValue() + 1));
      }
      catch (java.rmi.RemoteException e)
      {
         log.error("operation failed", e);
      }

      return value;
   }

   /**
    * Resets the given autonumber to zero.
    * Use with extreme care!
    */
   public static synchronized void resetAutoNumber(String collectionName)
   {
      setAutoNumber(collectionName, new Integer(0));
   }

   /**
    * Sets the given autonumber to the given value so that it starts
    * counting at the given value.
    * Use with extreme care!
    */
   public static synchronized void setAutoNumber(String collectionName, Integer value)
   {
      AutoNumber autoNumber = null;
      if (autoNumberHome == null)
      {
         try
         {
            autoNumberHome = (AutoNumberHome)
               new InitialContext().lookup("JBossUtilAutoNumber");
         }
         catch (javax.naming.NamingException e)
         {
            log.error("operation failed", e);
         }
      }

      try
      {
         autoNumber = autoNumberHome.findByPrimaryKey(collectionName);
      }
      catch (javax.ejb.FinderException e)
      {
         // autonumber does not exist yet, create one

         try
         {
            autoNumber = autoNumberHome.create(collectionName);
         }
         catch (javax.ejb.CreateException x)
         {
            log.error("operation failed", x);
         }
         catch (java.rmi.RemoteException x)
         {
            log.error("operation failed", x);
         }
      }
      catch (java.rmi.RemoteException e)
      {
         log.error("operation failed", e);
      }

      try
      {
         autoNumber.setValue(value);
      }
      catch (java.rmi.RemoteException e)
      {
         log.error("operation failed", e);
      }
   }
}

