db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegn...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/otm/core ConcreteEditingContext.java
Date Mon, 25 Aug 2003 19:26:07 GMT
olegnitz    2003/08/25 12:26:07

  Modified:    src/java/org/apache/ojb/otm/core ConcreteEditingContext.java
  Log:
  Solved swizzling problem for unidirectional relationships and not only them :)
  
  Revision  Changes    Path
  1.28      +81 -49    db-ojb/src/java/org/apache/ojb/otm/core/ConcreteEditingContext.java
  
  Index: ConcreteEditingContext.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/core/ConcreteEditingContext.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- ConcreteEditingContext.java	8 Aug 2003 20:27:38 -0000	1.27
  +++ ConcreteEditingContext.java	25 Aug 2003 19:26:07 -0000	1.28
  @@ -743,7 +743,9 @@
           // N:1 relations
           Iterator iter = mif.getObjectReferenceDescriptors().iterator();
           ObjectReferenceDescriptor rds = null;
  +        PersistentField f;
           Object relObj = null;
  +        Object cntxRelObj;
           Identity relOid;
           boolean isDependent;
   
  @@ -755,14 +757,20 @@
               {
                   continue;
               }
  -            relObj = rds.getPersistentField().get(object);
  +            f = rds.getPersistentField();
  +            relObj = f.get(object);
               if (relObj != null)
               {
                   relOid = new Identity(relObj, _pb);
  -                if (_objects.get(relOid) == null)
  +                cntxRelObj = lookup(relOid);
  +                if (cntxRelObj == null)
                   {
                       insertInternal(relOid, relObj, lock, false, isDependent);
                   }
  +                else if (cntxRelObj != relObj)
  +                {
  +                    f.set(object, cntxRelObj);
  +                }
               }
           }
   
  @@ -771,8 +779,9 @@
           CollectionDescriptor cds;
           Object col;
           Iterator colIterator;
  -        PersistentField f;
           Class type;
  +        ArrayList newCol;
  +        boolean needToReplaceCollection;
   
           while (collections.hasNext())
           {
  @@ -793,6 +802,9 @@
                       continue;
                   }
   
  +                newCol = new ArrayList();
  +                needToReplaceCollection = false;
  +
                   if (Collection.class.isAssignableFrom(type))
                   {
                       colIterator = ((Collection) col).iterator();
  @@ -812,10 +824,21 @@
                   {
                       relObj = colIterator.next();
                       relOid = new Identity(relObj, _pb);
  -                    if (_objects.get(relOid) == null)
  +                    cntxRelObj = lookup(relOid);
  +                    if (cntxRelObj == null)
                       {
                           insertInternal(relOid, relObj, lock, false, isDependent);
  +                        cntxRelObj = relObj;
                       }
  +                    else if (cntxRelObj != relObj)
  +                    {
  +                        needToReplaceCollection = true;
  +                    }
  +                    newCol.add(cntxRelObj);
  +                }
  +                if (needToReplaceCollection)
  +                {
  +                    setCollectionField(object, f, newCol);
                   }
               }
           }
  @@ -1053,8 +1076,8 @@
           {
               CollectionDescriptor cds = (CollectionDescriptor) it.next();
               PersistentField f = cds.getPersistentField();
  -            Class type = f.getType();
               ArrayList list = collections[count];
  +            ArrayList newCol;
   
               if (list == null)
               {
  @@ -1062,61 +1085,70 @@
               }
               else
               {
  -                if (Collection.class.isAssignableFrom(type))
  +                newCol = new ArrayList();
  +                for (Iterator it2 = list.iterator(); it2.hasNext(); )
                   {
  -                    Collection col = (Collection) f.get(obj);
  +                    Identity relOid = (Identity) it2.next();
  +                    Object relObj = _pb.getObjectByIdentity(relOid);
   
  -                    if (col == null)
  -                    {
  -                        if (type == List.class || type == Collection.class)
  -                        {
  -                            col = new ArrayList();
  -                        }
  -                        else if (type == Set.class)
  -                        {
  -                            col = new HashSet();
  -                        }
  -                        else
  -                        {
  -                            try
  -                            {
  -                                col = (Collection) type.newInstance();
  -                            }
  -                            catch (Exception ex)
  -                            {
  -                                System.err.println("Cannot instantiate collection field:
" + f);
  -                                ex.printStackTrace();
  -                                continue;
  -                            }
  -                        }
  -                    }
  -                    else
  +                    if (relObj != null)
                       {
  -                        col.clear();
  +                        newCol.add(relObj);
                       }
  -                    for (Iterator it2 = list.iterator(); it2.hasNext(); )
  -                    {
  -                        Identity relOid = (Identity) it2.next();
  -                        Object relObj = _pb.getObjectByIdentity(relOid);
  +                }
  +                setCollectionField(obj, f, newCol);
  +            }
  +        }
  +    }
   
  -                        if (relObj != null)
  -                        {
  -                            col.add(relObj);
  -                        }
  -                    }
  +    private void setCollectionField(Object obj, PersistentField f, List newCol)
  +    {
  +        Class type = f.getType();
  +
  +        if (Collection.class.isAssignableFrom(type))
  +        {
  +            Collection col = (Collection) f.get(obj);
  +
  +            if (col == null)
  +            {
  +                if (type == List.class || type == Collection.class)
  +                {
  +                    col = new ArrayList();
                   }
  -                else if (type.isArray())
  +                else if (type == Set.class)
                   {
  -                    int length = list.size();
  -                    Object array = Array.newInstance(type.getComponentType(), length);
  -
  -                    for (int i = 0; i < length; i++)
  +                    col = new HashSet();
  +                }
  +                else
  +                {
  +                    try
                       {
  -                        Array.set(array, i, list.get(i));
  +                        col = (Collection) type.newInstance();
  +                    }
  +                    catch (Exception ex)
  +                    {
  +                        System.err.println("Cannot instantiate collection field: " + f);
  +                        ex.printStackTrace();
  +                        return;
                       }
  -                    f.set(obj, array);
                   }
               }
  +            else
  +            {
  +                col.clear();
  +            }
  +            col.addAll(newCol);
  +        }
  +        else if (type.isArray())
  +        {
  +            int length = newCol.size();
  +            Object array = Array.newInstance(type.getComponentType(), length);
  +
  +            for (int i = 0; i < length; i++)
  +            {
  +                Array.set(array, i, newCol.get(i));
  +            }
  +            f.set(obj, array);
           }
       }
   
  
  
  

---------------------------------------------------------------------
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