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 2004/05/04 19:37:55 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/otm/copy MetadataObjectCopyStrategy.java

arminw      2004/05/04 10:37:55

  Modified:    src/java/org/apache/ojb/broker/util BrokerHelper.java
               src/java/org/apache/ojb/otm/copy
                        MetadataObjectCopyStrategy.java
  Log:
  - hide BrokerHelper#getAutoincrementValue method
  - refactoring of BrokerHelper methods
  - add more javadoc
  
  Revision  Changes    Path
  1.50      +54 -51    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.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- BrokerHelper.java	22 Apr 2004 19:23:45 -0000	1.49
  +++ BrokerHelper.java	4 May 2004 17:37:54 -0000	1.50
  @@ -31,8 +31,9 @@
   import org.apache.ojb.broker.accesslayer.sql.SqlExistStatement;
   import org.apache.ojb.broker.core.PersistenceBrokerImpl;
   import org.apache.ojb.broker.core.ValueContainer;
  -import org.apache.ojb.broker.core.proxy.*;
  +import org.apache.ojb.broker.core.RuntimeBinder;
   import org.apache.ojb.broker.core.proxy.IndirectionHandler;
  +import org.apache.ojb.broker.core.proxy.ProxyHelper;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
  @@ -52,14 +53,12 @@
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   import org.apache.ojb.broker.util.sequence.SequenceManagerException;
   
  -//#ifdef JDK13
  -//#else
  -/*
  -import com.develop.java.lang.reflect.Proxy;
  -*/
  -//#endif
  -
   /**
  + * This class contains helper methods primarily used by the {@link org.apache.ojb.broker.PersistenceBroker}
  + * implementation (e.g. contains methods to assign the the values of 'autoincrement' fields).
  + * <br/>
  + * Furthermore it was used to introduce new features related to {@link org.apache.ojb.broker.PersistenceBroker} - these
  + * new features and services (if they stand the test of time) will be moved to separate services in future.
    *
    * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
    * @version $Id$
  @@ -151,7 +150,7 @@
       }
   
       /**
  -     * returns an Array with an Objects PK VALUES if convertToSql is true, any
  +     * Returns an Array with an Objects PK VALUES if convertToSql is true, any
        * associated java-to-sql conversions are applied. If the Object is a Proxy
        * or a VirtualProxy NO conversion is necessary.
        *
  @@ -176,7 +175,8 @@
       }
   
       /**
  -     * Return key Values of an Identity
  +     * Return primary key values of given Identity object.
  +     *
        * @param cld
        * @param oid
        * @return Object[]
  @@ -242,7 +242,7 @@
        * - If given value is 'null' itself, true will be returned<br/>
        *
        * - If given value is instance of Number with value 0 and the field-descriptor
  -     * is a primary key and represents a primitive field, true will be returned<br/>
  +     * represents a primitive field, true will be returned<br/>
        *
        * - If given value is instance of String with length 0 and the field-descriptor
        * is a primary key, true will be returned<br/>
  @@ -254,8 +254,10 @@
           boolean result = false;
           if(((aValue instanceof Number) && (((Number) aValue).longValue() == 0)))
           {
  -            result = fld.getPersistentField().getType().isPrimitive() && fld.isPrimaryKey();
  +            result = fld.getPersistentField().getType().isPrimitive();
           }
  +        // TODO: Do we need this check?? String could be nullified, why should we assume
  +        // it's 'null' on empty string?
           else if((aValue instanceof String) && (((String) aValue).length() == 0))
           {
               result = fld.isPrimaryKey();
  @@ -281,44 +283,38 @@
       }
   
       /**
  -     * Get an autoincremented value that has already
  -     * had a field conversion run on it.
  +     * Set an autoincremented value in given object field that has already
  +     * had a field conversion run on it, if an value for the given field is
  +     * already set, it will be overridden - no further checks are done.
        * <p>
        * The data type of the value that is returned by this method is
        * compatible with the java-world.  The return value has <b>NOT</b>
        * been run through a field conversion and converted to a corresponding
        * sql-type.
        *
  -     * @throws MetadataException if there is an erros accessing obj field values
  +     * @return the autoincremented value set on given object
  +     * @throws PersistenceBrokerException if there is an erros accessing obj field values
        */
  -    public Object getAutoIncrementValue(FieldDescriptor fd, Object obj, Object cv)
  +    private Object setAutoIncrementValue(FieldDescriptor fd, Object obj, Object cv)
       {
  -        if(representsNull(fd, cv))
  +        PersistentField f = fd.getPersistentField();
  +        try
           {
  -            PersistentField f = fd.getPersistentField();
  -            try
  -            {
  -// lookup SeqMan for a value matching db column an
  -                // fieldconversion
  -                Object result = m_broker.serviceSequenceManager().getUniqueValue(fd);
  -// reflect autoincrement value back into object
  -                f.set(obj, result);
  -                return result;
  -            }
  -            catch(MetadataException e)
  -            {
  -                throw new PersistenceBrokerException(
  -                        "Error while trying to autoincrement field " + f.getDeclaringClass() + "#" + f.getName(),
  -                        e);
  -            }
  -            catch(SequenceManagerException e)
  -            {
  -                throw new PersistenceBrokerException("Could not get key value", e);
  -            }
  +            // lookup SeqMan for a value matching db column an
  +            Object result = m_broker.serviceSequenceManager().getUniqueValue(fd);
  +            // reflect autoincrement value back into object
  +            f.set(obj, result);
  +            return result;
           }
  -        else
  +        catch(MetadataException e)
  +        {
  +            throw new PersistenceBrokerException(
  +                    "Error while trying to autoincrement field " + f.getDeclaringClass() + "#" + f.getName(),
  +                    e);
  +        }
  +        catch(SequenceManagerException e)
           {
  -            return cv;
  +            throw new PersistenceBrokerException("Could not get key value", e);
           }
       }
   
  @@ -337,15 +333,22 @@
               FieldDescriptor fd = fields[i];
               Object cv = fd.getPersistentField().get(obj);
   
  -            // handle autoincrement attributes if not filled
  -            if(fd.isAutoIncrement())
  -            {
  -                // getAutoIncrementValue returns a value that is
  -                // properly typed for the java-world.  This value
  -                // needs to be converted to it's corresponding
  -                // sql type so that the entire result array contains
  -                // objects that are properly typed for sql.
  -                cv = getAutoIncrementValue(fd, obj, cv);
  +            /*
  +            handle autoincrement attributes if
  +            - is a autoincrement field
  +            - field represents a 'null' value, is nullified
  +            and generate a new value
  +            */
  +            if(fd.isAutoIncrement() && representsNull(fd, cv))
  +            {
  +                /*
  +                setAutoIncrementValue returns a value that is
  +                properly typed for the java-world.  This value
  +                needs to be converted to it's corresponding
  +                sql type so that the entire result array contains
  +                objects that are properly typed for sql.
  +                */
  +                cv = setAutoIncrementValue(fd, obj, cv);
               }
               if(convertToSql)
               {
  @@ -578,8 +581,8 @@
               {
                   countQuery.setPathOuterJoin(path);
               }
  -        }     
  -        
  +        }
  +
           return countQuery;
       }
   
  
  
  
  1.21      +12 -7     db-ojb/src/java/org/apache/ojb/otm/copy/MetadataObjectCopyStrategy.java
  
  Index: MetadataObjectCopyStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/copy/MetadataObjectCopyStrategy.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- MetadataObjectCopyStrategy.java	12 Apr 2004 16:32:40 -0000	1.20
  +++ MetadataObjectCopyStrategy.java	4 May 2004 17:37:55 -0000	1.21
  @@ -103,18 +103,23 @@
            * fields are not mapped objects (ie ObjectReferenceDescriptors)
            */
           final FieldDescriptor[] fieldDescs = cld.getFieldDescriptions();
  -        final BrokerHelper brokerHelper = broker.serviceBrokerHelper();
  +//        final BrokerHelper brokerHelper = broker.serviceBrokerHelper();
           for (int i = 0; i < fieldDescs.length; i++)
           {
               final FieldDescriptor fd = fieldDescs[i];
               final PersistentField f = fd.getPersistentField();
               Object fieldValue = f.get(toCopy);
  -
  -            // If the field is auto increment, assign its value before copying!
  -            if (fd.isAutoIncrement())
  -            {
  -                fieldValue = brokerHelper.getAutoIncrementValue(fd, toCopy, fieldValue);
  -            }
  +/*
  +arminw:
  +TODO: ensure that the autoincrement values be assigned before the copy was done
  +If possible we should avoid to declare BrokerHelper#getAutoIncrementValue public and
  +if we copy an object user don't expect the change of fields.
  +*/
  +//            // If the field is auto increment, assign its value before copying!
  +//            if (fd.isAutoIncrement())
  +//            {
  +//                fieldValue = brokerHelper.getAutoIncrementValue(fd, toCopy, fieldValue);
  +//            }
   
               f.set(retval, fieldValue);
           }
  
  
  

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