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 Mon, 10 Mar 2003 19:23:29 GMT
brj         2003/03/10 11:23:29

  Modified:    src/java/org/apache/ojb/broker/util BrokerHelper.java
  Log:
  prevent double conversion (patch by dave marquard)
  
  Revision  Changes    Path
  1.15      +35 -77    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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- BrokerHelper.java	8 Mar 2003 19:22:56 -0000	1.14
  +++ BrokerHelper.java	10 Mar 2003 19:23:28 -0000	1.15
  @@ -54,9 +54,12 @@
    * <http://www.apache.org/>.
    */
   
  +import java.lang.reflect.Field;
  +import java.lang.reflect.Proxy;
  +import java.util.StringTokenizer;
  +
   import org.apache.commons.lang.SystemUtils;
   import org.apache.ojb.broker.Identity;
  -import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerException;
  @@ -70,12 +73,6 @@
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
   import org.apache.ojb.broker.singlevm.PersistenceBrokerConfiguration;
   import org.apache.ojb.broker.util.sequence.SequenceManagerException;
  -
  -import java.lang.reflect.Field;
  -import java.util.StringTokenizer;
  -
  -//#ifdef JDK13
  -import java.lang.reflect.Proxy;
   //#else
   /*
   import com.develop.java.lang.reflect.Proxy;
  @@ -211,14 +208,15 @@
                   FieldDescriptor fmd = pkFields[i];
                   PersistentField f = fmd.getPersistentField();
                   Object cv = f.get(objectOrProxy);
  -
  +                
                   // handle autoincrement attributes if not filled
                   if (fmd.isAutoIncrement())
                   {
  +                    // getAutoIncrementValue returns a value that already
  +                    // has a field conversion run on it
                       cv = getAutoIncrementValue(fmd, objectOrProxy, cv);
                   }
  -
  -                if (convertToSql)
  +                else if (convertToSql)
                   {
                       // BRJ : apply type and value mapping
                       cv = fmd.getFieldConversion().javaToSql(cv);
  @@ -298,6 +296,8 @@
   
   
       /**
  +     * Get an autoincremented value that has already
  +     * had a field conversion run on it.
        * @throws MetadataException if there is an erros accessing obj field values
        * @deprecated
        */
  @@ -309,73 +309,21 @@
               Object result = cv;
               try
               {
  -                // int
  -                if ((f.getType() == int.class) || (f.getType() == Integer.class))
  -                {
  -                    if ((f.get(obj) == null) || (((Number) f.get(obj)).intValue() == 0))
  -                    {
  -                    	// lookup SeqMan for a value matching db column an fieldconversion
  -                        result = broker.serviceSequenceManager().getUniqueValue(fmd);
  -                        // reflect autoincrement value back into object
  -                        f.set(obj, result);
  -                    }
  -                }
  -                // long
  -                else if ((f.getType() == long.class) || (f.getType() == Long.class))
  -                {
  -                    if ((f.get(obj) == null) || (((Number) f.get(obj)).longValue() == 0))
  -                    {
  -                    	// lookup SeqMan for a value matching to the DB column and field-conversion
  -                        result = broker.serviceSequenceManager().getUniqueValue(fmd);
  -                        // reflect autoincrement value back into object
  -                        f.set(obj, result);
  -                    }
  -                }
  -                // String
  -                else if (String.class.isAssignableFrom(f.getType()))
  -                {
  -                    if (f.get(obj) == null)
  -                    {
  -                    	// lookup SeqMan for a value matching to the DB column and field-conversion
  -                        result = broker.serviceSequenceManager().getUniqueValue(fmd);
  -                        // reflect autoincrement value back into object
  -                        f.set(obj, result);
  -                    }
  -                }
  -                // Object
  -                else if (!f.getType().isPrimitive())
  -                {
  -                    // only assign a value if attribute == null
  -                    if (f.get(obj) == null)
  -                    {
  -                    	// lookup SeqMan for a value matching to the DB column and field-conversion
  -                        result = broker.serviceSequenceManager().getUniqueValue(fmd);
  -                        if (result != null)
  -                        {
  -                            // reflect autoincrement value back into object
  -                            f.set(obj, result);
  -                        }
  -                        else
  -                        {
  -                            throw new OJBRuntimeException(
  -                                    "OJB ERROR: Dont know how to autoincrement field "
+
  -                                    f.getDeclaringClass() + "#" + f.getName());
  -                        }
  -                    }
  -                }
  -                else
  +                Object currentValue = f.get(obj);
  +                if ((currentValue == null) || ((cv instanceof Number) && ((Number)
currentValue).intValue() == 0))
                   {
  -                    throw new OJBRuntimeException(
  -                            "OJB ERROR: Dont know how to autoincrement field " +
  -                            f.getDeclaringClass() + "#" + f.getName());
  +                    // lookup SeqMan for a value matching db column an fieldconversion
  +                    result = broker.serviceSequenceManager().getUniqueValue(fmd);
  +                    // 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);
  +                    "Error while trying to autoincrement field " +
  +                    f.getDeclaringClass() + "#" + f.getName(), e);
               }
               catch (SequenceManagerException e)
               {
  @@ -403,15 +351,20 @@
               FieldDescriptor fmd = nonPkFields[i];
               PersistentField f = fmd.getPersistentField();
               Object cv = f.get(o);
  -
  +            
               // handle autoincrement attributes if not filled
               if (fmd.isAutoIncrement())
               {
  +                // getAutoIncrementValue returns a value that already
  +                // has a field conversion run on it
                   cv = getAutoIncrementValue(fmd, o, cv);
               }
  -
  -            // apply type and value conversion
  -            cv = fmd.getFieldConversion().javaToSql(cv);
  +            else 
  +            {
  +                // apply type and value conversion
  +                cv = fmd.getFieldConversion().javaToSql(cv);
  +            }
  +            
               result[i] = cv;
           }
           return result;
  @@ -435,11 +388,16 @@
               // handle autoincrement attributes if not filled
               if (fmd.isAutoIncrement())
               {
  +                // getAutoIncrementValue returns a value that already
  +                // has a field conversion run on it
                   cv = getAutoIncrementValue(fmd, obj, cv);
               }
  +            else
  +            {
  +                // apply type and value mapping
  +                cv = fmd.getFieldConversion().javaToSql(cv);
  +            }
   
  -            // apply type and value mapping
  -            cv = fmd.getFieldConversion().javaToSql(cv);
               result[i] = cv;
           }
           return result;
  
  
  

Mime
View raw message