You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2007/05/16 00:51:47 UTC

svn commit: r538353 - /db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/BrokerHelper.java

Author: arminw
Date: Tue May 15 15:51:45 2007
New Revision: 538353

URL: http://svn.apache.org/viewvc?view=rev&rev=538353
Log:
add methods to nullify objects, extend object link-support, optimize source

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/BrokerHelper.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/BrokerHelper.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/BrokerHelper.java?view=diff&rev=538353&r1=538352&r2=538353
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/BrokerHelper.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/BrokerHelper.java Tue May 15 15:51:45 2007
@@ -25,6 +25,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
+import java.util.Collections;
 
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.collections.iterators.ArrayIterator;
@@ -341,6 +342,10 @@
      */
     public boolean hasNullPKField(ClassDescriptor cld, Object obj)
     {
+        /*
+         arminw: similar check is used in IdentityFactoryImpl#createTransientOrRealIdentity
+         both methods have to be consistent!!
+        */
         FieldDescriptor[] fields = cld.getPkFields();
         boolean hasNull = false;
         // an unmaterialized proxy object can never have nullified PK's
@@ -352,8 +357,11 @@
             for(int i = 0; i < fields.length; i++)
             {
                 fld = fields[i];
-                hasNull = representsNull(fld, fld.getPersistentField().get(obj));
-                if(hasNull) break;
+                if(fld.representsNullAssociatedValue(obj))
+                {
+                    hasNull = true;
+                    break;
+                }
             }
         }
         return hasNull;
@@ -410,12 +418,14 @@
      */
     public ValueContainer[] getValuesForObject(FieldDescriptor[] fields, Object obj, boolean convertToSql, boolean assignAutoincrement) throws PersistenceBrokerException
     {
-        ValueContainer[] result = new ValueContainer[fields.length];
+        final ValueContainer[] result = new ValueContainer[fields.length];
 
+        FieldDescriptor fd;
+        Object cv;
         for(int i = 0; i < fields.length; i++)
         {
-            FieldDescriptor fd = fields[i];
-            Object cv = fd.getPersistentField().get(obj);
+            fd = fields[i];
+            cv = fd.getPersistentField().get(obj);
 
             /*
             handle autoincrement attributes if
@@ -423,7 +433,7 @@
             - field represents a 'null' value, is nullified
             and generate a new value
             */
-            if(assignAutoincrement && fd.isAutoIncrement() && representsNull(fd, cv))
+            if(assignAutoincrement && fd.isAutoIncrement() && fd.representsNull(cv))
             {
                 /*
                 setAutoIncrementValue returns a value that is
@@ -528,7 +538,7 @@
              */
             if(!(fld.isAutoIncrement()
                     || fld.isLocking()
-                    || !representsNull(fld, pkValues[i])))
+                    || !fld.representsNull(pkValues[i])))
             {
                 return false;
             }
@@ -551,9 +561,7 @@
             int fieldDescriptorSize = fieldDescriptors.length;
             for(int i = 0; i < fieldDescriptorSize; i++)
             {
-                FieldDescriptor fd = fieldDescriptors[i];
-                Object pkValue = fd.getPersistentField().get(obj);
-                if (representsNull(fd, pkValue))
+                if (fieldDescriptors[i].representsNullAssociatedValue(obj))
                 {
                     return false;
                 }
@@ -803,7 +811,8 @@
 
     private void linkOrUnlink(boolean doLink, Object obj, boolean insert)
     {
-        ClassDescriptor cld = m_broker.getDescriptorRepository().getDescriptorFor(obj.getClass());
+        Object objReal = proxyFactory.getRealObject(obj);
+        ClassDescriptor cld = m_broker.getClassDescriptor(objReal.getClass());
 
         if (cld.getObjectReferenceDescriptors().size() > 0)
         {
@@ -812,7 +821,7 @@
             while (descriptors.hasNext())
             {
                 ObjectReferenceDescriptor ord = (ObjectReferenceDescriptor) descriptors.next();
-                linkOrUnlinkOneToOne(doLink, obj, ord, insert);
+                linkOrUnlinkOneToOne(doLink, objReal, ord, insert);
             }
         }
         if (cld.getCollectionDescriptors().size() > 0)
@@ -822,7 +831,7 @@
             while (descriptors.hasNext())
             {
                 CollectionDescriptor cod = (CollectionDescriptor) descriptors.next();
-                linkOrUnlinkXToMany(doLink, obj, cod, insert);
+                linkOrUnlinkXToMany(doLink, objReal, cod, insert);
             }
         }
     }
@@ -875,15 +884,14 @@
      */
     public boolean link(Object obj, String attributeName, Object reference, boolean insert)
     {
-        ClassDescriptor cld = m_broker.getDescriptorRepository().getDescriptorFor(
-                proxyFactory.getRealClass(obj));
-        ObjectReferenceDescriptor ord;
+        Object objReal = proxyFactory.getRealObject(obj);
+        ClassDescriptor cld = m_broker.getClassDescriptor(objReal.getClass());
         boolean match = false;
         // first look for reference then for collection
-        ord = cld.getObjectReferenceDescriptorByName(attributeName);
+        ObjectReferenceDescriptor ord = cld.getObjectReferenceDescriptorByName(attributeName);
         if (ord != null)
         {
-            linkOrUnlinkOneToOne(true, obj, ord, insert);
+            linkOrUnlinkOneToOne(true, objReal, ord, insert);
             match = true;
         }
         else
@@ -891,7 +899,14 @@
             CollectionDescriptor cod = cld.getCollectionDescriptorByName(attributeName);
             if (cod != null)
             {
-                linkOrUnlinkXToMany(true, obj, cod, insert);
+                if(reference == null)
+                {
+                    linkOrUnlinkXToMany(true, objReal, cod, insert);
+                }
+                else
+                {
+                    link(objReal, cod, reference);
+                }
                 match = true;
             }
         }
@@ -937,15 +952,15 @@
     private boolean linkOrUnlink(boolean doLink, Object obj, String attributeName, boolean insert)
     {
         boolean match = false;
-        ClassDescriptor cld = m_broker.getDescriptorRepository().getDescriptorFor(
-                proxyFactory.getRealClass(obj));
+        Object objReal = proxyFactory.getRealObject(obj);
+        ClassDescriptor cld = m_broker.getClassDescriptor(objReal.getClass());
         ObjectReferenceDescriptor ord;
 
         // first look for reference then for collection
         ord = cld.getObjectReferenceDescriptorByName(attributeName);
         if (ord != null)
         {
-            linkOrUnlinkOneToOne(doLink, obj, ord, insert);
+            linkOrUnlinkOneToOne(doLink, objReal, ord, insert);
             match = true;
         }
         else
@@ -953,7 +968,7 @@
             CollectionDescriptor cod = cld.getCollectionDescriptorByName(attributeName);
             if (cod != null)
             {
-                linkOrUnlinkXToMany(doLink, obj, cod, insert);
+                linkOrUnlinkXToMany(doLink, objReal, cod, insert);
                 match = true;
             }
         }
@@ -973,26 +988,55 @@
         }
     }
 
-    private void linkOrUnlinkXToMany(boolean doLink, Object obj, CollectionDescriptor cod, boolean insert)
+    private void linkOrUnlinkXToMany(boolean doLink, Object realObject, CollectionDescriptor cod, boolean insert)
     {
-        if (doLink)
+        if(doLink)
         {
+            Object referencedObjects = cod.getPersistentField().get(realObject);
             if (cod.isMtoNRelation())
-            {
-                m_broker.linkMtoN(obj, cod, insert);
+                {
+                    if(referencedObjects == null)
+                {
+                referencedObjects = Collections.EMPTY_LIST;
+            }
+                m_broker.getMtoNBroker().storeMtoN(realObject, cod, referencedObjects, insert);
             }
             else
             {
-                m_broker.linkOneToMany(obj, cod, insert);
+                if(referencedObjects != null) m_broker.storeAndLinkOneToMany(true, realObject, cod, referencedObjects, insert);
             }
         }
         else
         {
-            m_broker.unlinkXtoN(obj, cod);
+            if(cod.isMtoNRelation())
+            {
+                // if this is a m:n mapped table, remove entries from indirection table
+                m_broker.getMtoNBroker().deleteMtoNImplementor(cod, realObject);
+            }
+            else
+            {
+                Object collectionObject = cod.getPersistentField().get(realObject);
+                if (collectionObject != null)
+                {
+                    Iterator colIterator = BrokerHelper.getCollectionIterator(collectionObject);
+                    ClassDescriptor cld = null;
+                    while (colIterator.hasNext())
+                    {
+                        Object target = colIterator.next();
+                        // materialize the real object instance
+                        target = proxyFactory.getRealObject(target);
+                        if(cld == null)
+                        {
+                            cld = m_broker.getClassDescriptor(proxyFactory.getRealClass(target));
+                        }
+                        m_broker.unlinkFK(target, cld, cod);
+                    }
+                }
+            }
         }
     }
 
-    private void linkOrUnlinkOneToOne(boolean doLink, Object obj, ObjectReferenceDescriptor ord, boolean insert)
+    private void linkOrUnlinkOneToOne(boolean doLink, Object realObject, ObjectReferenceDescriptor ord, boolean insert)
     {
         /*
         arminw: we need the class-descriptor where the reference is declared, thus we ask the
@@ -1003,18 +1047,18 @@
         ClassDescriptor cld = ord.getClassDescriptor();
         if(cld.isInterface())
         {
-            cld = m_broker.getDescriptorRepository().getDescriptorFor(proxyFactory.getRealClass(obj));
+            cld = m_broker.getClassDescriptor(realObject.getClass());
         }
 
         if (doLink)
         {
-            m_broker.linkOneToOne(obj, cld, ord, insert);
+            m_broker.storeAndLinkOneToOne(true, realObject, cld, ord, insert);
         }
         else
         {
-            m_broker.unlinkFK(obj, cld, ord);
+            m_broker.unlinkFK(realObject, cld, ord);
             // in 1:1 relation we have to set relation to null
-            ord.getPersistentField().set(obj, null);
+            ord.getPersistentField().set(realObject, null);
         }
     }
 
@@ -1083,8 +1127,9 @@
         }
         else
         {
-            ClassDescriptor cld = m_broker.getClassDescriptor(referenceToLink.getClass());
-            m_broker.link(referenceToLink, cld, cds, source, false);
+            Object realReference = proxyFactory.getRealObject(referenceToLink);
+            ClassDescriptor cld = m_broker.getClassDescriptor(realReference.getClass());
+            m_broker.link(realReference, cld, cds, source, false);
         }
     }
 
@@ -1107,7 +1152,7 @@
         {
             colIterator = ((Collection) collectionOrArray).iterator();
         }
-        else if (collectionOrArray.getClass().isArray())
+        else if (collectionOrArray != null && collectionOrArray.getClass().isArray())
         {
             colIterator = new ArrayIterator(collectionOrArray);
         }
@@ -1141,7 +1186,7 @@
             CollectionUtils.addAll(newCol, ((ManageableCollection) collectionOrArray).ojbIterator());
             result = newCol.toArray();
         }
-        else if (collectionOrArray.getClass().isArray())
+        else if (collectionOrArray != null && collectionOrArray.getClass().isArray())
         {
             result = (Object[]) collectionOrArray;
         }
@@ -1177,6 +1222,46 @@
     }
 
 //    /**
+//     * Nullify the specified object by setting all primary key fields to <tt>null</tt>.
+//     *
+//     * @param objContext The object context to nullify.
+//     */
+//    public void nullifyObject(ObjectContext objContext)
+//    {
+//        nullifyObject(objContext.getObject(), objContext.getCld());
+//    }
+
+    /**
+     * Nullify the specified object by setting all primary key fields to <tt>null</tt>.
+     * After this the object can safely be re-stored.
+     *
+     * @param obj The object to nullify.
+     */
+    public void nullifyObject(Object obj)
+    {
+        ClassDescriptor cld = m_broker.getClassDescriptor(m_broker.getProxyFactory().getRealClass(obj));
+        nullifyObject(obj, cld);
+    }
+
+    /**
+     * Nullify the specified object by setting all primary key fields to <tt>null</tt>.
+     * After this the object can safely be re-stored.
+     *
+     * @param obj The object to nullify.
+     * @param cld The {@link org.apache.ojb.broker.metadata.ClassDescriptor} of the object.
+     */
+    public void nullifyObject(Object obj, ClassDescriptor cld)
+    {
+        Object target = m_broker.getProxyFactory().getRealObject(obj);
+        FieldDescriptor[] pks = cld.getPkFields();
+        for(int i = 0; i < pks.length; i++)
+        {
+            FieldDescriptor pk = pks[i];
+            pk.getPersistentField().set(target, null);
+        }
+    }
+
+//    /**
 //     * Use this method to extract the {@link org.apache.ojb.broker.metadata.ClassDescriptor} where
 //     * the {@link org.apache.ojb.broker.metadata.ObjectReferenceDescriptor reference} is declared.
 //     * It's possible that the reference is declared in a super-class.
@@ -1196,7 +1281,7 @@
 //        ClassDescriptor cld = reference.getClassDescriptor();
 //        if(cld.isInterface())
 //        {
-//            cld = broker.getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(source));
+//            cld = broker.getClassDescriptor(proxyHelper.getRealClass(source));
 //        }
 //        return cld;
 //    }



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