001    package com.sptci.echo2;
002    
003    import java.beans.PropertyChangeEvent;
004    import java.beans.IntrospectionException;
005    import java.io.Serializable;
006    import java.util.logging.Logger;
007    
008    import javax.jdo.PersistenceManager;
009    import javax.jdo.JDOHelper;
010    import com.sptci.jdo.PersistenceManagerFactory;
011    
012    /**
013     * A sub-class of {@link PropertyChangeListener} used to synchronise 
014     * changes made to properties in UI components to the {@link #bean} 
015     * JDO object.
016     *
017     * <p>Copyright 2006 Sans Pareil Technologies, Inc.</p>
018     * @author Rakesh Vidyadharan 2006-05-05
019     * @version $Id: JDOModelPropertyChangeListener.java,v 1.3 2006/02/14 22:40:32 rakesh Exp $
020     */
021    public class JDOModelPropertyChangeListener extends ModelPropertyChangeListener 
022      implements Serializable
023    {
024      /**
025       * The logger used to log errors to
026       */
027      private static transient final Logger logger =
028        Logger.getLogger( "com.sptci.echo2.JDOModelPropertyChangeListener" );
029    
030      /**
031       * Create a new instance of the class using the specified java bean.
032       *
033       * @param bean The data object which is to be managed.
034       * @throws IntrospectionException If errors are encountered while
035       *   introspecting the {@link #bean} class.
036       */
037      public JDOModelPropertyChangeListener( Object bean )
038        throws IntrospectionException
039      {
040        super( bean );
041      }
042    
043      /**
044       * Over-riddent implementation of the method defined in <code>
045       * PropertyChangeListener</code>.  Start a new JDO <code>transaction
046       * </code>, apply the changes using {@link 
047       * ModelPropertyChangeListener#propertyChange}, and then 
048       * <code>commit</code> the <code>transaction</code>.
049       *
050       * @param event A <code>PropertyChangeEvent</code> object describing 
051       *   the event source and the property that has changed.
052       * @throws BindingException If errors are encountered while
053       *   attempting to modify the {@link #bean}.
054       */
055      public void propertyChange( PropertyChangeEvent event )
056      {
057        PersistenceManager persistenceManager = null;
058        boolean active = false;
059    
060        try
061        {
062          persistenceManager = JDOHelper.getPersistenceManager( bean );
063          if ( persistenceManager == null )
064          {
065            persistenceManager = 
066              PersistenceManagerFactory.getPersistenceManager();
067          }
068    
069          active = persistenceManager.currentTransaction().isActive();
070    
071          if ( ! active )
072          {
073            persistenceManager.currentTransaction().begin();
074          }
075    
076          super.propertyChange( event );
077    
078          if ( ! active )
079          {
080            persistenceManager.currentTransaction().commit();
081          }
082        }
083        catch ( Throwable t )
084        {
085          throw new BindingException( t );
086        }
087        finally
088        {
089          if ( persistenceManager != null && ! active &&
090              persistenceManager.currentTransaction().isActive() )
091          {
092            persistenceManager.currentTransaction().rollback();
093            logger.warning( "Unknown problem.  Rollling back transaction." );
094          }
095        }
096      }
097    }