db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/core PersistenceBrokerImpl.java
Date Sun, 17 Apr 2005 13:29:37 GMT
brj         2005/04/17 06:29:37

  Modified:    src/java/org/apache/ojb/broker/util Tag: OJB_1_0_RELEASE
                        BrokerHelper.java
               src/test/org/apache/ojb/broker Tag: OJB_1_0_RELEASE
                        CollectionTest.java
               src/java/org/apache/ojb/broker/util/collections Tag:
                        OJB_1_0_RELEASE RemovalAwareCollection.java
               src/java/org/apache/ojb/broker/core Tag: OJB_1_0_RELEASE
                        PersistenceBrokerImpl.java
  Log:
  - do not delete non persistent object in afterStore()
  - throw PBException when deleting non persistent objct
  see http://issues.apache.org/jira/browse/OJB-23
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.57.2.12 +27 -3     db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java
  
  Index: BrokerHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java,v
  retrieving revision 1.57.2.11
  retrieving revision 1.57.2.12
  diff -u -r1.57.2.11 -r1.57.2.12
  --- BrokerHelper.java	3 Apr 2005 02:14:45 -0000	1.57.2.11
  +++ BrokerHelper.java	17 Apr 2005 13:29:37 -0000	1.57.2.12
  @@ -343,6 +343,7 @@
   
       /**
        * Get the values of the fields for an obj
  +     * Autoincrement values are automatically set.
        * @param fields
        * @param obj
        * @throws PersistenceBrokerException
  @@ -420,7 +421,7 @@
       }
   
       /**
  -     * returns true if the primary key fields are valid, else false.
  +     * returns true if the primary key fields are valid for store, else false.
        * PK fields are valid if each of them is either an OJB managed
        * attribute (autoincrement or locking) or if it contains
        * a valid non-null value
  @@ -428,7 +429,7 @@
        * @param pkValues the array of PK values
        * @return boolean
        */
  -    public boolean assertValidPkFields(FieldDescriptor[] fieldDescriptors, Object[] pkValues)
  +    public boolean assertValidPksForStore(FieldDescriptor[] fieldDescriptors, Object[]
pkValues)
       {
           int fieldDescriptorSize = fieldDescriptors.length;
           for(int i = 0; i < fieldDescriptorSize; i++)
  @@ -449,6 +450,29 @@
       }
   
       /**
  +     * returns true if the primary key fields are valid for delete, else false.
  +     * PK fields are valid if each of them contains a valid non-null value
  +     * @param cld the ClassDescriptor
  +     * @param obj the object 
  +     * @return boolean
  +     */
  +    public boolean assertValidPkForDelete(ClassDescriptor cld, Object obj)
  +    {
  +        FieldDescriptor fieldDescriptors[] = cld.getPkFields();
  +        int fieldDescriptorSize = fieldDescriptors.length;
  +        for(int i = 0; i < fieldDescriptorSize; i++)
  +        {
  +            FieldDescriptor fd = fieldDescriptors[i];
  +            Object pkValue = fd.getPersistentField().get(obj);
  +            if (!assertValidPkValue(pkValue))
  +            {
  +                return false;
  +            }
  +        }
  +        return true;
  +    }
  +
  +    /**
        * returns true if a value is non-null, STring instances are also checked,
        * if they are non-empty.
        * @param pkValue the value to check
  
  
  
  No                   revision
  No                   revision
  1.11.2.4  +39 -1     db-ojb/src/test/org/apache/ojb/broker/CollectionTest.java
  
  Index: CollectionTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/CollectionTest.java,v
  retrieving revision 1.11.2.3
  retrieving revision 1.11.2.4
  diff -u -r1.11.2.3 -r1.11.2.4
  --- CollectionTest.java	9 Mar 2005 20:58:39 -0000	1.11.2.3
  +++ CollectionTest.java	17 Apr 2005 13:29:37 -0000	1.11.2.4
  @@ -833,6 +833,44 @@
       }
   
       /**
  +     * Test RemovalAwareCollection remove() of non persistent obj
  +     */
  +    public void testRemovalAwareCollection2()
  +    {
  +        String prefix = "testRemovalAwareCollection2_" + System.currentTimeMillis();
  +
  +        Identity gathererId;
  +        Gatherer loadedCopy;
  +        Gatherer gatherer = new Gatherer(null, "Gatherer_" + prefix);
  +        List coll = new ArrayList();
  +        coll.add(new CollectibleBase("Base_1_" + prefix));
  +        coll.add(new CollectibleBase("Base_2_" + prefix));
  +        gatherer.setCollectiblesBase(coll);
  +
  +        broker.beginTransaction();
  +        broker.store(gatherer);
  +        broker.commitTransaction();
  +        assertTrue(gatherer.getGatId() != null);
  +        gathererId = broker.serviceIdentity().buildIdentity(gatherer);
  +
  +        broker.clearCache();
  +        loadedCopy = (Gatherer) broker.getObjectByIdentity(gathererId);
  +        assertNotNull(loadedCopy);
  +        assertNotNull(loadedCopy.getCollectiblesBase());
  +        assertTrue(loadedCopy.getCollectiblesBase() instanceof RemovalAwareCollection);
  +        assertEquals(2, loadedCopy.getCollectiblesBase().size());
  +        
  +        // add and remove non persistent obj
  +        loadedCopy.getCollectiblesBase().add(new CollectibleBase("Base_3_" + prefix));
  +        assertEquals(3, loadedCopy.getCollectiblesBase().size());
  +        loadedCopy.getCollectiblesBase().remove(2);
  +
  +        broker.beginTransaction();
  +        broker.store(loadedCopy);
  +        broker.commitTransaction();
  +    }
  +
  +    /**
        * Test RemovalAwareCollection remove() and clear()
        */
       public void testRemovalAwareCollectionProxy()
  
  
  
  No                   revision
  No                   revision
  1.7.2.2   +8 -2      db-ojb/src/java/org/apache/ojb/broker/util/collections/RemovalAwareCollection.java
  
  Index: RemovalAwareCollection.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/collections/RemovalAwareCollection.java,v
  retrieving revision 1.7.2.1
  retrieving revision 1.7.2.2
  diff -u -r1.7.2.1 -r1.7.2.2
  --- RemovalAwareCollection.java	11 Mar 2005 11:31:05 -0000	1.7.2.1
  +++ RemovalAwareCollection.java	17 Apr 2005 13:29:37 -0000	1.7.2.2
  @@ -17,6 +17,7 @@
   
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerException;
  +import org.apache.ojb.broker.metadata.ClassDescriptor;
   
   import java.util.Iterator;
   import java.util.Vector;
  @@ -47,7 +48,12 @@
           Iterator iter = allObjectsToBeRemoved.iterator();
           while (iter.hasNext())
           {
  -            broker.delete(iter.next());
  +            Object obj = iter.next();
  +            ClassDescriptor cld = broker.getClassDescriptor(obj.getClass());
  +            if (broker.serviceBrokerHelper().assertValidPkForDelete(cld, obj))
  +            {    
  +                broker.delete(obj);
  +            }    
           }
           allObjectsToBeRemoved.clear();
       }
  
  
  
  No                   revision
  No                   revision
  1.83.2.15 +13 -4     db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java
  
  Index: PersistenceBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java,v
  retrieving revision 1.83.2.14
  retrieving revision 1.83.2.15
  diff -u -r1.83.2.14 -r1.83.2.15
  --- PersistenceBrokerImpl.java	18 Mar 2005 19:25:05 -0000	1.83.2.14
  +++ PersistenceBrokerImpl.java	17 Apr 2005 13:29:37 -0000	1.83.2.15
  @@ -504,12 +504,21 @@
               {
                   return;
               }
  +            
  +            ClassDescriptor cld = getClassDescriptor(obj.getClass());
  +            //BRJ: check for valid pk
  +            if (!serviceBrokerHelper().assertValidPkForDelete(cld, obj))
  +            {
  +                String msg = "Cannot delete object without valid PKs. " + obj;
  +                logger.error(msg);
  +                throw new PersistenceBrokerException(msg);
  +            }
  +            
               /**
                * MBAIRD
                * 2. register object in markedForDelete map.
                */
               markedForDelete.add(obj);
  -            ClassDescriptor cld = getClassDescriptor(obj.getClass());
               Identity oid = new Identity(obj, this, cld);
   
               // Invoke events on PersistenceBrokerAware instances and listeners
  @@ -1716,12 +1725,12 @@
           storeReferences(obj, cld, insert);
   
           Object[] pkValues = oid.getPrimaryKeyValues();
  -        if (!serviceBrokerHelper().assertValidPkFields(cld.getPkFields(), pkValues))
  +        if (!serviceBrokerHelper().assertValidPksForStore(cld.getPkFields(), pkValues))
           {
               // BRJ: fk values may be part of pk, but the are not known during
               // creation of Identity. so we have to get them here
               pkValues = serviceBrokerHelper().getKeyValues(cld, obj);
  -            if (!serviceBrokerHelper().assertValidPkFields(cld.getPkFields(), pkValues))
  +            if (!serviceBrokerHelper().assertValidPksForStore(cld.getPkFields(), pkValues))
               {
                   String append = insert ? " on insert" : " on update" ;
                   throw new PersistenceBrokerException("assertValidPkFields failed for Object
of type: " + cld.getClassNameOfObject() + append);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message