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/util BrokerHelper.java
Date Sun, 21 Nov 2004 09:35:11 GMT
brj         2004/11/21 01:35:11

  Modified:    src/java/org/apache/ojb/broker/util BrokerHelper.java
  Log:
  refactoring link / unlink
  
  Revision  Changes    Path
  1.70      +105 -70   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.69
  retrieving revision 1.70
  diff -u -r1.69 -r1.70
  --- BrokerHelper.java	11 Nov 2004 23:48:26 -0000	1.69
  +++ BrokerHelper.java	21 Nov 2004 09:35:10 -0000	1.70
  @@ -653,7 +653,7 @@
       /**
        * Unlink all references from this object.
        * More info see OJB doc.
  -     * @param obj
  +     * @param obj Object with reference
        */
       public void unlink(Object obj)
       {
  @@ -663,49 +663,25 @@
       private void linkOrUnlink(boolean doLink, Object obj, boolean insert)
       {
           ClassDescriptor cld = m_broker.getDescriptorRepository().getDescriptorFor(obj.getClass());
  -        // never returns null, thus we can direct call iterator
  -        Iterator descriptors = null;
  -        if(cld.getObjectReferenceDescriptors().size() > 0)
  -        {
  -            descriptors = cld.getObjectReferenceDescriptors().iterator();
  -            ObjectReferenceDescriptor ord;
  -            while(descriptors.hasNext())
  +
  +        if (cld.getObjectReferenceDescriptors().size() > 0)
  +        {
  +            // never returns null, thus we can direct call iterator
  +            Iterator descriptors = cld.getObjectReferenceDescriptors().iterator();
  +            while (descriptors.hasNext())
               {
  -                ord = (ObjectReferenceDescriptor) descriptors.next();
  -                if(doLink)
  -                {
  -                    m_broker.linkOneToOne(obj, cld, ord, insert);
  -                }
  -                else
  -                {
  -                    m_broker.unlinkFK(obj, cld, ord);
  -                    // in 1:1 relation we have to set relation to null
  -                    ord.getPersistentField().set(obj, null);
  -                }
  +                ObjectReferenceDescriptor ord = (ObjectReferenceDescriptor) descriptors.next();
  +                linkOrUnlinkOneToOne(doLink, obj, ord, insert);
               }
           }
  -        if(cld.getCollectionDescriptors().size() > 0)
  +        if (cld.getCollectionDescriptors().size() > 0)
           {
  -            descriptors = cld.getCollectionDescriptors().iterator();
  -            CollectionDescriptor cod;
  -            while(descriptors.hasNext())
  +            // never returns null, thus we can direct call iterator
  +            Iterator descriptors = cld.getCollectionDescriptors().iterator();
  +            while (descriptors.hasNext())
               {
  -                cod = (CollectionDescriptor) descriptors.next();
  -                if(doLink)
  -                {
  -                    if(cod.isMtoNRelation())
  -                    {
  -                        m_broker.linkMtoN(obj, cod, insert);
  -                    }
  -                    else
  -                    {
  -                        m_broker.linkOneToMany(obj, cod, insert);
  -                    }
  -                }
  -                else
  -                {
  -                    m_broker.unlinkXtoN(obj, cod);
  -                }
  +                CollectionDescriptor cod = (CollectionDescriptor) descriptors.next();
  +                linkOrUnlinkOneToMany(doLink, obj, cod, insert);
               }
           }
       }
  @@ -718,6 +694,22 @@
        * More info see OJB doc.
        *
        * @param obj Object with reference
  +     * @param ord the ObjectReferenceDescriptor of the reference
  +     * @param insert flag signals insert operation
  +     */
  +    public void link(Object obj, ObjectReferenceDescriptor ord, boolean insert)
  +    {
  +       linkOrUnlink(true, obj, ord, insert);
  +    }
  +    
  +    /**
  +     * This method concatenate the main object and the specified reference
  +     * object (1:1 reference a referenced object, 1:n and m:n reference a
  +     * collection of referenced objects) by hand. This method is needed when
  +     * in the reference metadata definitions the auto-xxx setting was disabled.
  +     * More info see OJB doc.
  +     *
  +     * @param obj Object with reference
        * @param attributeName field name of the reference
        * @param insert flag signals insert operation
        * @return true if the specified reference was found and linking was successful
  @@ -730,58 +722,101 @@
       /**
        * Unlink the specified reference from this object.
        * More info see OJB doc.
  -     * @param obj
  +     * @param obj Object with reference
  +     * @param attributeName field name of the reference
        */
       public boolean unlink(Object obj, String attributeName)
       {
           return linkOrUnlink(false, obj, attributeName, false);
       }
   
  +    /**
  +     * Unlink the specified reference from this object.
  +     * More info see OJB doc.
  +     *
  +     * @param obj Object with reference
  +     * @param ord the ObjectReferenceDescriptor of the reference
  +     * @param insert flag signals insert operation
  +     */
  +    public void unlink(Object obj, ObjectReferenceDescriptor ord, boolean insert)
  +    {
  +       linkOrUnlink(true, obj, ord, insert);
  +    }
  +    
       private boolean linkOrUnlink(boolean doLink, Object obj, String attributeName, boolean
insert)
       {
           boolean match = false;
           ClassDescriptor cld = m_broker.getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(obj));
  -        ObjectReferenceDescriptor ord = cld.getObjectReferenceDescriptorByName(attributeName);
  -        if(ord != null)
  +        ObjectReferenceDescriptor ord;
  +        
  +        // first look for reference then for collection
  +        ord = cld.getObjectReferenceDescriptorByName(attributeName);
  +        if (ord != null)
           {
  -            if(doLink)
  -            {
  -                m_broker.linkOneToOne(obj, cld, ord, insert);
  -            }
  -            else
  -            {
  -                m_broker.unlinkFK(obj, cld, ord);
  -                // in 1:1 relation we have to set relation to null
  -                ord.getPersistentField().set(obj, null);
  -            }
  +            linkOrUnlinkOneToOne(doLink, obj, ord, insert);
               match = true;
           }
           else
           {
               CollectionDescriptor cod = cld.getCollectionDescriptorByName(attributeName);
  -            if(cod != null)
  +            if (cod != null)
               {
  -                if(doLink)
  -                {
  -                    if(cod.isMtoNRelation())
  -                    {
  -                        m_broker.linkMtoN(obj, cod, insert);
  -                    }
  -                    else
  -                    {
  -                        m_broker.linkOneToMany(obj, cod, insert);
  -                    }
  -                }
  -                else
  -                {
  -                    m_broker.unlinkXtoN(obj, cod);
  -                }
  +                linkOrUnlinkOneToMany(doLink, obj, cod, insert);
                   match = true;
  -            }
  +            }           
           }
  +        
           return match;
       }
   
  +    private void linkOrUnlink(boolean doLink, Object obj, ObjectReferenceDescriptor ord,
boolean insert)
  +    {
  +        if (ord instanceof CollectionDescriptor)
  +        {
  +            linkOrUnlinkOneToMany(doLink, obj, (CollectionDescriptor) ord, insert);
  +        }
  +        else
  +        {
  +            linkOrUnlinkOneToOne(doLink, obj, ord, insert);
  +        }
  +    }
  +
  +    private void linkOrUnlinkOneToMany(boolean doLink, Object obj, CollectionDescriptor
cod, boolean insert)
  +    {
  +        if (doLink)
  +        {
  +            if (cod.isMtoNRelation())
  +            {
  +                m_broker.linkMtoN(obj, cod, insert);
  +            }
  +            else
  +            {
  +                m_broker.linkOneToMany(obj, cod, insert);
  +            }
  +        }
  +        else
  +        {
  +            m_broker.unlinkXtoN(obj, cod);
  +        }      
  +    }
  +
  +    private void linkOrUnlinkOneToOne(boolean doLink, Object obj, ObjectReferenceDescriptor
ord, boolean insert)
  +    {
  +        ClassDescriptor cld = m_broker.getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(obj));
  +
  +        if (doLink)
  +        {
  +            m_broker.linkOneToOne(obj, cld, ord, insert);
  +        }
  +        else
  +        {
  +            m_broker.unlinkFK(obj, cld, ord);
  +            // in 1:1 relation we have to set relation to null
  +            ord.getPersistentField().set(obj, null);
  +        }        
  +    }
  +    
  +    
       /**
        * Add a Criteria for Concrete Class to the copied Query.
        * @param aQuery
  
  
  

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