db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/singlevm PersistenceBrokerImpl.java
Date Sun, 06 Apr 2003 14:54:59 GMT
thma        2003/04/06 07:54:59

  Modified:    src/test/org/apache/ojb/broker OneToManyTest.java
               src/test/org/apache/ojb repository_junit.xml
               src/java/org/apache/ojb/broker/singlevm
                        PersistenceBrokerImpl.java
  Added:       src/java/org/apache/ojb/broker/util/collections
                        RemovalAwareCollection.java
               src/test/org/apache/ojb/broker
                        ProductGroupWithRemovalAwareCollection.java
  Log:
  wrote a new collection to be used with PB apps. It will track removed objects and perform
the respective pb.delete() oparations in the afterStore() event.
  
  Revision  Changes    Path
  1.1                  db-ojb/src/java/org/apache/ojb/broker/util/collections/RemovalAwareCollection.java
  
  Index: RemovalAwareCollection.java
  ===================================================================
  package org.apache.ojb.broker.util.collections;
  
  import java.util.Iterator;
  import java.util.Vector;
  
  import org.apache.ojb.broker.PersistenceBrokerAware;
  import org.apache.ojb.broker.PersistenceBrokerException;
  import org.apache.ojb.broker.PersistenceBrokerFactory;
  
  /**
   * This is a collection that tracks removal and addition of elements.
   * This tracking allow the PersistenceBroker to delete elements from
   * the database that have been removed from the collection before a
   * PB.store() orperation occurs.
   * This will allow to use the PB api in way pretty close to ODMG persistent
   * collections!
   * @author Thomas Mahler
   */
  public class RemovalAwareCollection extends ManageableVector implements PersistenceBrokerAware
  {
  	private Vector allObjectsToBeRemoved = new Vector();
  
      /**
       * @see org.apache.ojb.broker.PersistenceBrokerAware#afterDelete()
       */
      public void afterDelete() throws PersistenceBrokerException
      {
      }
  
      /**
       * @see org.apache.ojb.broker.PersistenceBrokerAware#afterLookup()
       */
      public void afterLookup() throws PersistenceBrokerException
      {
      }
  
      /**
       * @see org.apache.ojb.broker.PersistenceBrokerAware#afterStore()
       * @todo: once a PB instance is available as a callback parameter we should use it!
       */
      public void afterStore() throws PersistenceBrokerException
      {
      	// make sure allObjectsToBeRemoved does not contain
      	// any instances that got re-added to the list
      	allObjectsToBeRemoved.removeAll(this);
      		
      	Iterator iter = allObjectsToBeRemoved.iterator();
      	while (iter.hasNext())
      	{
      		// @todo: once a PB instance is available as a callback parameter we should use it!
      		PersistenceBrokerFactory.defaultPersistenceBroker().delete(iter.next());	
      	}
      	allObjectsToBeRemoved.clear();    	
      }
  
      /**
       * @see org.apache.ojb.broker.PersistenceBrokerAware#beforeDelete()
       */
      public void beforeDelete() throws PersistenceBrokerException
      {
      }
  
      /**
       * @see org.apache.ojb.broker.PersistenceBrokerAware#beforeStore()
       */
      public void beforeStore() throws PersistenceBrokerException
      {
      }
  
      /**
       * @see java.util.List#remove(int)
       */
      public Object remove(int index)
      {
      	Object toBeRemoved = super.remove(index);
      	registerForDeletion(toBeRemoved);    	
          return toBeRemoved;
      }
      
      
      
  
  	protected void registerForDeletion(Object toBeRemoved)
  	{
  		//only add objects once to avoid double deletions
  		if (! allObjectsToBeRemoved.contains(toBeRemoved))
  		{
  			this.allObjectsToBeRemoved.add(toBeRemoved);	
  		}
  	}
  
      /**
       * @see java.util.Collection#remove(Object)
       */
      public boolean remove(Object o)
      {
          boolean result = super.remove(o);
      	registerForDeletion(o);    	
          return result;        
      }
  
      /**
       * @see java.util.Vector#removeAllElements()
       */
      public synchronized void removeAllElements()
      {
  		for (int i = 0; i < this.size(); i++)
  		{
  			registerForDeletion(this.get(i));	
  		}
          super.removeAllElements();
      }
  
  
      /**
       * @see java.util.Vector#removeElementAt(int)
       */
      public synchronized void removeElementAt(int index)
      {
      	Object toBeDeleted = this.get(index);
      	registerForDeletion(toBeDeleted);
          super.removeElementAt(index);
      }
  
      /**
       * @see java.util.AbstractList#removeRange(int, int)
       */
      protected void removeRange(int fromIndex, int toIndex)
      {
  		for (int i = fromIndex; i < toIndex; i++)
  		{
  			registerForDeletion(this.get(i));	
  		}
          super.removeRange(fromIndex, toIndex);
      }
  
  }
  
  
  
  1.6       +37 -0     db-ojb/src/test/org/apache/ojb/broker/OneToManyTest.java
  
  Index: OneToManyTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/OneToManyTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- OneToManyTest.java	5 Feb 2003 20:03:42 -0000	1.5
  +++ OneToManyTest.java	6 Apr 2003 14:54:59 -0000	1.6
  @@ -38,6 +38,43 @@
       public void testDelete()
       {
       }
  +	
  +	/**
  +	 * test the removal aware functionality.
  +	 */
  +    public void testDeleteWithRemovalAwareCollection()
  +    {
  +    	ProductGroupWithRemovalAwareCollection pg = new ProductGroupWithRemovalAwareCollection();
  +    	pg.setGroupName("nice group");
  +    	
  +    	Identity pgId = new Identity(pg, broker);
  +    	
  +    	Article a = new Article();
  +    	a.setArticleName("a");
  +    	Article b = new Article();
  +    	b.setArticleName("b");
  +    	Article c = new Article();
  +    	c.setArticleName("c");
  +    	
  +    	pg.add(a);
  +    	pg.add(b);
  +    	pg.add(c);
  +    	   	
  +    	broker.store(pg);
  +    	broker.clearCache();
  +    	pg = (ProductGroupWithRemovalAwareCollection) broker.getObjectByIdentity(pgId);
  +    	assertEquals(3,pg.getAllArticles().size());    	   	
  +    	   	
  +    	pg.getAllArticles().remove(c);
  +    	pg.getAllArticles().remove(0);
  +    	broker.store(pg);
  +    	
  +    	broker.clearCache();
  +    	pg = (ProductGroupWithRemovalAwareCollection) broker.getObjectByIdentity(pgId);
  +    	assertEquals(1,pg.getAllArticles().size());    	   	
  +    	
  +    	
  +    }
   
       public void testRead()
       {
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/ProductGroupWithRemovalAwareCollection.java
  
  Index: ProductGroupWithRemovalAwareCollection.java
  ===================================================================
  package org.apache.ojb.broker;
  
  import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
  
  
  /** represents a product group containing a set of Articles.
   * @see Article
   */
  public class ProductGroupWithRemovalAwareCollection implements java.io.Serializable
  {
  
      /** collection containing all articles of a given product group*/
      private RemovalAwareCollection allArticlesInGroup;
  
      /** the unique id of a product group*/
      private int groupId;
  
      /** the name of a group*/
      private String groupName;
  
  
      /** return group id*/
      public int getId()
      {
          return groupId;
      }
  
  
      /** return groupname*/
      public String getName()
      {
          return groupName;
      }
  
      public ProductGroupWithRemovalAwareCollection()
      {
      }
  
      /** return List of all Articles in productgroup*/
      public RemovalAwareCollection getAllArticles()
      {
          return allArticlesInGroup;
      }
      
      public synchronized void add(Article art)
      {
      	if (allArticlesInGroup == null)
      	{	
      		allArticlesInGroup = new RemovalAwareCollection();	
      	}
      	this.allArticlesInGroup.add(art);	
      }
  
      /** set group id*/
      public void setId(int newValue)
      {
          groupId = newValue;
      }
  
      /**
       * Sets the allArticlesInGroup.
       * @param allArticlesInGroup The allArticlesInGroup to set
       */
      public void setAllArticlesInGroup(RemovalAwareCollection allArticlesInGroup)
      {
          this.allArticlesInGroup = allArticlesInGroup;
      }
  
  
      /**
       * Gets the groupId.
       * @return Returns a int
       */
      public int getGroupId()
      {
          return groupId;
      }
  
      /**
       * Sets the groupId.
       * @param groupId The groupId to set
       */
      public void setGroupId(int groupId)
      {
          this.groupId = groupId;
      }
  
      /**
       * Gets the groupName.
       * @return Returns a String
       */
      public String getGroupName()
      {
          return groupName;
      }
  
      /**
       * Sets the groupName.
       * @param groupName The groupName to set
       */
      public void setGroupName(String groupName)
      {
          this.groupName = groupName;
      }
  
  }
  
  
  
  1.55      +28 -0     db-ojb/src/test/org/apache/ojb/repository_junit.xml
  
  Index: repository_junit.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit.xml,v
  retrieving revision 1.54
  retrieving revision 1.55
  diff -u -r1.54 -r1.55
  --- repository_junit.xml	14 Mar 2003 21:07:44 -0000	1.54
  +++ repository_junit.xml	6 Apr 2003 14:54:59 -0000	1.55
  @@ -161,6 +161,34 @@
         </collection-descriptor>
      </class-descriptor>
   
  +<!-- Definitions for org.apache.ojb.broker.ProductGroupWithRemovalAwareCollection -->
  +   <class-descriptor
  +   	  class="org.apache.ojb.broker.ProductGroupWithRemovalAwareCollection"
  +   	  table="Kategorien"
  +   >
  +      <field-descriptor
  +         name="groupId"
  +         column="Kategorie_Nr"
  +         jdbc-type="INTEGER"
  +         primarykey="true"
  +      />
  +      <field-descriptor
  +         name="groupName"
  +         column="KategorieName"
  +         jdbc-type="VARCHAR"
  +      />
  +      <collection-descriptor
  +         name="allArticlesInGroup"
  +         element-class-ref="org.apache.ojb.broker.Article"
  +         collection-class="org.apache.ojb.broker.util.collections.RemovalAwareCollection"
  +         auto-retrieve="true"
  +         auto-update="true"
  +      >
  +         <inverse-foreignkey field-ref="productGroupId"/>
  +      </collection-descriptor>
  +   </class-descriptor>
  +
  +
      <!-- Definitions for org.apache.ojb.broker.ProductGroupWithArray -->
      <class-descriptor
      	  class="org.apache.ojb.broker.ProductGroupWithArray"
  
  
  
  1.151     +7 -1      db-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerImpl.java
  
  Index: PersistenceBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerImpl.java,v
  retrieving revision 1.150
  retrieving revision 1.151
  diff -u -r1.150 -r1.151
  --- PersistenceBrokerImpl.java	2 Apr 2003 19:49:45 -0000	1.150
  +++ PersistenceBrokerImpl.java	6 Apr 2003 14:54:59 -0000	1.151
  @@ -67,6 +67,7 @@
   import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PBState;
  +import org.apache.ojb.broker.PersistenceBrokerAware;
   import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.TransactionAbortedException;
   import org.apache.ojb.broker.TransactionInProgressException;
  @@ -790,6 +791,11 @@
                           assertFkAssignment(otherObj, obj, cds);
                           storeCollectionObject(cds, otherObj);
                       }
  +                }
  +                // invoke event on collection
  +                if (col instanceof PersistenceBrokerAware)
  +                {
  +                	((PersistenceBrokerAware) col).afterStore();	
                   }
               }
           }
  
  
  

Mime
View raw message