package com.sptci.echo2demo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Logger;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.sptci.jdo.PersistenceManagerFactory;

/**
 * A factory class for abstracting interactions between the
 * {@link InputFormModel} JDO JavaBean and the JDO data store.
 *
 * @author com.sptci.echo2.JDOBeanGenerator 2006-02-09 10:00:16-0600
 * @version $Id: InputFormModelFactory.java,v 1.2 2006/02/15 16:15:17 rakesh Exp $
 */
public class InputFormModelFactory
  implements Serializable
{
  /**
   * The singleton instance of the class.
   */
  private static final InputFormModelFactory singleton =
      new InputFormModelFactory();

  /**
   * The logger used to log errors/warnings to.
   */
  private static final Logger logger =
      Logger.getLogger( "com.sptci.echo2demo.InputFormModelFactory" );

  /**
   * Default constructor.  Cannot be instantiated.
   */
  protected InputFormModelFactory() {}

  /**
   * Return the singleton instance of the class.
   *
   * @return InputFormModelFactory The {@link #singleton} instance.
   */
  public static final InputFormModelFactory getInstance()
  {
    return singleton;
  }

  /**
   * Save the specified instance of {@link InputFormModel}
   * to the JDO data store.
   *
   * @param inputFormData The instance to persist to
   *   the data store.
   */
  public void save( InputFormModel inputFormData )
  {
    PersistenceManager persistenceManager = null;
    boolean active = false;

    try
    {
      persistenceManager =
        JDOHelper.getPersistenceManager( inputFormData );
      if ( persistenceManager == null )
      {
        persistenceManager =
          PersistenceManagerFactory.getPersistenceManager();
      }
      active = persistenceManager.currentTransaction().isActive();

      if ( ! active )
      {
        persistenceManager.currentTransaction().begin();
      }

      if ( ! JDOHelper.isPersistent( inputFormData ) )
      {
        persistenceManager.makePersistent( inputFormData );
      }

      if ( ! active )
      {
        persistenceManager.currentTransaction().commit();
      }
    }
    finally
    {
      if ( persistenceManager != null && ! active &&
          persistenceManager.currentTransaction().isActive() )
      {
        persistenceManager.currentTransaction().rollback();
        logger.warning( "Unknown problem.  Rollling back transaction." );
      }
    }
  }

  /**
   * Delete the specified instance of {@link InputFormModel}
   * from the JDO data store.
   *
   * @param inputFormData The instance to delete from
   *   the data store.
   */
  public void delete( InputFormModel inputFormData )
  {
    PersistenceManager persistenceManager = null;
    boolean active = false;

    try
    {
      persistenceManager =
        JDOHelper.getPersistenceManager( inputFormData );
      active = persistenceManager.currentTransaction().isActive();

      if ( ! active )
      {
        persistenceManager.currentTransaction().begin();
      }

      if ( JDOHelper.isPersistent( inputFormData ) )
      {
        persistenceManager.deletePersistent( inputFormData );
      }

      if ( ! active )
      {
        persistenceManager.currentTransaction().commit();
      }
    }
    finally
    {
      if ( persistenceManager != null && ! active &&
          persistenceManager.currentTransaction().isActive() )
      {
        persistenceManager.currentTransaction().rollback();
        logger.warning( "Unknown problem.  Rollling back transaction." );
      }
    }
  }
  /**
   * Return all the instance of {@link InputFormModel}
   * from the JDO data store.
   *
   * @return Collection An <code>ArrayList</code> of all the instances.
   */
  @SuppressWarnings(value={"unchecked"})
  public final Collection<InputFormModel> fetchAll()
  {
    Collection<InputFormModel> collection = new ArrayList<InputFormModel>();
    PersistenceManager persistenceManager =
      PersistenceManagerFactory.getPersistenceManager();
    Query query = null;
    try
    {
      query = persistenceManager.newQuery( InputFormModel.class );
      collection.addAll( (Collection<InputFormModel>) query.execute() );
    }
    finally
    {
      query.closeAll();
    }

    return collection;

  }

  /**
   * Return the list index number of the last record in a list
   * that contains all the objects from the data store.
   *
   * @return int The list index number for the last record.
   * @throws RuntimeException If JDO exceptions are encountered while
   *   fetching the objects.
   */
  public final int lastIndex() throws RuntimeException
  {
    return ( fetchAll().size() - 1 );
  }

  /**
   * Return the object that occupies the specified index number in a
   * list that contains all the object instances.
   *
   * @param index The index at which the object exists.
   * @return InputFormModel The appropriate object.  Returns <code>null</code>
   *   if no object is found.
   * @throws RuntimeException If JDO exceptions are encountered while
   *   fetching the object.
   */
  public InputFormModel fetchByIndex( int index ) throws RuntimeException
  {
    ArrayList<InputFormModel> list = (ArrayList<InputFormModel>) fetchAll();
    InputFormModel model = null;
    if ( index >= 0 && list.size() > index )
    {
      model = list.get( index );
    }

    return model;
  }
}
