001    package com.sptci.echo2demo;
002    
003    import java.io.Serializable;
004    import java.util.ArrayList;
005    import java.util.Collection;
006    import java.util.logging.Logger;
007    
008    import javax.jdo.JDOHelper;
009    import javax.jdo.PersistenceManager;
010    import javax.jdo.Query;
011    
012    import com.sptci.jdo.PersistenceManagerFactory;
013    
014    /**
015     * A factory class for abstracting interactions between the
016     * {@link InputFormModel} JDO JavaBean and the JDO data store.
017     *
018     * @author com.sptci.echo2.JDOBeanGenerator 2006-02-09 10:00:16-0600
019     * @version $Id: InputFormModelFactory.java,v 1.2 2006/02/15 16:15:17 rakesh Exp $
020     */
021    public class InputFormModelFactory
022      implements Serializable
023    {
024      /**
025       * The singleton instance of the class.
026       */
027      private static final InputFormModelFactory singleton =
028          new InputFormModelFactory();
029    
030      /**
031       * The logger used to log errors/warnings to.
032       */
033      private static final Logger logger =
034          Logger.getLogger( "com.sptci.echo2demo.InputFormModelFactory" );
035    
036      /**
037       * Default constructor.  Cannot be instantiated.
038       */
039      protected InputFormModelFactory() {}
040    
041      /**
042       * Return the singleton instance of the class.
043       *
044       * @return InputFormModelFactory The {@link #singleton} instance.
045       */
046      public static final InputFormModelFactory getInstance()
047      {
048        return singleton;
049      }
050    
051      /**
052       * Save the specified instance of {@link InputFormModel}
053       * to the JDO data store.
054       *
055       * @param inputFormData The instance to persist to
056       *   the data store.
057       */
058      public void save( InputFormModel inputFormData )
059      {
060        PersistenceManager persistenceManager = null;
061        boolean active = false;
062    
063        try
064        {
065          persistenceManager =
066            JDOHelper.getPersistenceManager( inputFormData );
067          if ( persistenceManager == null )
068          {
069            persistenceManager =
070              PersistenceManagerFactory.getPersistenceManager();
071          }
072          active = persistenceManager.currentTransaction().isActive();
073    
074          if ( ! active )
075          {
076            persistenceManager.currentTransaction().begin();
077          }
078    
079          if ( ! JDOHelper.isPersistent( inputFormData ) )
080          {
081            persistenceManager.makePersistent( inputFormData );
082          }
083    
084          if ( ! active )
085          {
086            persistenceManager.currentTransaction().commit();
087          }
088        }
089        finally
090        {
091          if ( persistenceManager != null && ! active &&
092              persistenceManager.currentTransaction().isActive() )
093          {
094            persistenceManager.currentTransaction().rollback();
095            logger.warning( "Unknown problem.  Rollling back transaction." );
096          }
097        }
098      }
099    
100      /**
101       * Delete the specified instance of {@link InputFormModel}
102       * from the JDO data store.
103       *
104       * @param inputFormData The instance to delete from
105       *   the data store.
106       */
107      public void delete( InputFormModel inputFormData )
108      {
109        PersistenceManager persistenceManager = null;
110        boolean active = false;
111    
112        try
113        {
114          persistenceManager =
115            JDOHelper.getPersistenceManager( inputFormData );
116          active = persistenceManager.currentTransaction().isActive();
117    
118          if ( ! active )
119          {
120            persistenceManager.currentTransaction().begin();
121          }
122    
123          if ( JDOHelper.isPersistent( inputFormData ) )
124          {
125            persistenceManager.deletePersistent( inputFormData );
126          }
127    
128          if ( ! active )
129          {
130            persistenceManager.currentTransaction().commit();
131          }
132        }
133        finally
134        {
135          if ( persistenceManager != null && ! active &&
136              persistenceManager.currentTransaction().isActive() )
137          {
138            persistenceManager.currentTransaction().rollback();
139            logger.warning( "Unknown problem.  Rollling back transaction." );
140          }
141        }
142      }
143      /**
144       * Return all the instance of {@link InputFormModel}
145       * from the JDO data store.
146       *
147       * @return Collection An <code>ArrayList</code> of all the instances.
148       */
149      @SuppressWarnings(value={"unchecked"})
150      public final Collection<InputFormModel> fetchAll()
151      {
152        Collection<InputFormModel> collection = new ArrayList<InputFormModel>();
153        PersistenceManager persistenceManager =
154          PersistenceManagerFactory.getPersistenceManager();
155        Query query = null;
156        try
157        {
158          query = persistenceManager.newQuery( InputFormModel.class );
159          collection.addAll( (Collection<InputFormModel>) query.execute() );
160        }
161        finally
162        {
163          query.closeAll();
164        }
165    
166        return collection;
167    
168      }
169    
170      /**
171       * Return the list index number of the last record in a list
172       * that contains all the objects from the data store.
173       *
174       * @return int The list index number for the last record.
175       * @throws RuntimeException If JDO exceptions are encountered while
176       *   fetching the objects.
177       */
178      public final int lastIndex() throws RuntimeException
179      {
180        return ( fetchAll().size() - 1 );
181      }
182    
183      /**
184       * Return the object that occupies the specified index number in a
185       * list that contains all the object instances.
186       *
187       * @param index The index at which the object exists.
188       * @return InputFormModel The appropriate object.  Returns <code>null</code>
189       *   if no object is found.
190       * @throws RuntimeException If JDO exceptions are encountered while
191       *   fetching the object.
192       */
193      public InputFormModel fetchByIndex( int index ) throws RuntimeException
194      {
195        ArrayList<InputFormModel> list = (ArrayList<InputFormModel>) fetchAll();
196        InputFormModel model = null;
197        if ( index >= 0 && list.size() > index )
198        {
199          model = list.get( index );
200        }
201    
202        return model;
203      }
204    }