db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess AbstractPersistentField.java
Date Fri, 17 Oct 2003 14:12:56 GMT
arminw      2003/10/17 07:12:56

  Modified:    src/schema ojbtest-schema.xml
               src/test/org/apache/ojb/broker NestedFieldsTest.java
               src/test/org/apache/ojb repository_junit.xml
               src/java/org/apache/ojb/broker/metadata/fieldaccess
                        AbstractPersistentField.java
  Log:
  add patch by Andy Malakov to
  support collections of pc object
  collections within nested objects.
  Test cases updated to check collection support
  
  Revision  Changes    Path
  1.54      +6 -0      db-ojb/src/schema/ojbtest-schema.xml
  
  Index: ojbtest-schema.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/schema/ojbtest-schema.xml,v
  retrieving revision 1.53
  retrieving revision 1.54
  diff -u -r1.53 -r1.54
  --- ojbtest-schema.xml	4 Oct 2003 17:44:14 -0000	1.53
  +++ ojbtest-schema.xml	17 Oct 2003 14:12:55 -0000	1.54
  @@ -857,6 +857,12 @@
        <column name="NESTED_DESCRIPTION" type="VARCHAR" size="150"/>
      </table>
   
  +    <table name="NESTED_FIELDS_ENTRY">
  +     <column name="OBJ_ID" required="true" primaryKey="true" type="INTEGER"/>
  +     <column name="FK_ID" required="true" type="DECIMAL"/>
  +     <column name="NAME" type="VARCHAR" size="150"/>
  +   </table>
  +
       <!-- ************************************************* -->
       <!--      Classes for OTM dependent objects test       -->
       <!-- ************************************************* -->
  
  
  
  1.4       +108 -2    db-ojb/src/test/org/apache/ojb/broker/NestedFieldsTest.java
  
  Index: NestedFieldsTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/NestedFieldsTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- NestedFieldsTest.java	11 Jul 2003 07:43:53 -0000	1.3
  +++ NestedFieldsTest.java	17 Oct 2003 14:12:55 -0000	1.4
  @@ -3,6 +3,12 @@
   import junit.framework.TestCase;
   import org.apache.commons.lang.builder.ToStringBuilder;
   import org.apache.commons.lang.builder.ToStringStyle;
  +import org.apache.ojb.broker.query.QueryFactory;
  +import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.Query;
  +
  +import java.util.Collection;
  +import java.util.ArrayList;
   
   
   /**
  @@ -50,24 +56,35 @@
           long timestamp = System.currentTimeMillis();
           String ddName = "second_level_detail_" + timestamp;
           String ddDescription = "a real detail description" + timestamp;
  +        String entryName = "nested_entry_" + timestamp;
   
           NestedDetailDetail dd = new NestedDetailDetail(ddName, ddDescription);
           NestedDetail d = new NestedDetail(dd);
  +
  +        ArrayList entryList = new ArrayList();
  +        entryList.add(new NestedEntry(entryName));
  +        entryList.add(new NestedEntry(entryName));
  +        entryList.add(new NestedEntry(entryName));
  +
  +        d.setNestedEntryCollection(entryList);
           NestedMain nested = new NestedMain("main_object_" + timestamp, d);
  -        Identity oid = new Identity(nested, broker);
   
           broker.beginTransaction();
           broker.store(nested);
           broker.commitTransaction();
   
  +        Identity oid = new Identity(nested, broker);
  +
           assertNotNull(nested.getNestedDetail());
           assertNotNull(nested.getNestedDetail().getNestedDetailDetail());
           assertNotNull(nested.getNestedDetail().getNestedDetailDetail().getRealDetailName());
           dd = nested.getNestedDetail().getNestedDetailDetail();
           assertEquals(ddName, dd.getRealDetailName());
           assertEquals(ddDescription, dd.getRealDetailDescription());
  +        assertEquals(3, nested.getNestedDetail().getNestedEntryCollection().size());
   
           // retrieve copy of nested object
  +        // using cached version
           NestedMain nestedCopy = (NestedMain) broker.getObjectByIdentity(oid);
           assertNotNull(nestedCopy.getNestedDetail());
           assertNotNull(nestedCopy.getNestedDetail().getNestedDetailDetail());
  @@ -75,6 +92,8 @@
           dd = nested.getNestedDetail().getNestedDetailDetail();
           assertEquals(ddName, dd.getRealDetailName());
           assertEquals(ddDescription, dd.getRealDetailDescription());
  +        assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection());
  +        assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size());
   
           // clear cache and retrieve copy of nested object
           broker.clearCache();
  @@ -85,6 +104,8 @@
           dd = nested.getNestedDetail().getNestedDetailDetail();
           assertEquals(ddName, dd.getRealDetailName());
           assertEquals(ddDescription, dd.getRealDetailDescription());
  +        assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection());
  +        assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size());
       }
   
       public void testUpdateNestedField()
  @@ -92,16 +113,26 @@
           long timestamp = System.currentTimeMillis();
           String ddName = "second_level_detail_" + timestamp;
           String ddDescription = "a real detail description" + timestamp;
  +        String entryName = "nested_entry_" + timestamp;
   
           NestedDetailDetail dd = new NestedDetailDetail(ddName, ddDescription);
           NestedDetail d = new NestedDetail(dd);
  +
  +        ArrayList entryList = new ArrayList();
  +        entryList.add(new NestedEntry(entryName));
  +        entryList.add(new NestedEntry(entryName));
  +        entryList.add(new NestedEntry(entryName));
  +
  +        d.setNestedEntryCollection(entryList);
  +
           NestedMain nested = new NestedMain("main_object_" + timestamp, d);
  -        Identity oid = new Identity(nested, broker);
   
           broker.beginTransaction();
           broker.store(nested);
           broker.commitTransaction();
   
  +        Identity oid = new Identity(nested, broker);
  +
           // clear cache and retrieve copy of nested object
           broker.clearCache();
           nested = (NestedMain) broker.getObjectByIdentity(oid);
  @@ -111,6 +142,7 @@
           now change nested field and store
           */
           nested.getNestedDetail().getNestedDetailDetail().setRealDetailName("update_name_"+timestamp);
  +        nested.getNestedDetail().getNestedEntryCollection().add(new NestedEntry(entryName));
           broker.beginTransaction();
           broker.store(nested);
           broker.commitTransaction();
  @@ -119,6 +151,8 @@
           broker.clearCache();
           nested = (NestedMain) broker.getObjectByIdentity(oid);
           assertEquals("update_name_"+timestamp, nested.getNestedDetail().getNestedDetailDetail().getRealDetailName());
  +        assertNotNull(nested.getNestedDetail().getNestedEntryCollection());
  +        assertEquals(4, nested.getNestedDetail().getNestedEntryCollection().size());
       }
   
       public void testDeleteNestedField()
  @@ -126,9 +160,18 @@
           long timestamp = System.currentTimeMillis();
           String ddName = "second_level_detail_" + timestamp;
           String ddDescription = "a real detail description" + timestamp;
  +        String entryName = "nested_entry_" + timestamp;
   
           NestedDetailDetail dd = new NestedDetailDetail(ddName, ddDescription);
           NestedDetail d = new NestedDetail(dd);
  +
  +        ArrayList entryList = new ArrayList();
  +        entryList.add(new NestedEntry(entryName));
  +        entryList.add(new NestedEntry(entryName));
  +        entryList.add(new NestedEntry(entryName));
  +
  +        d.setNestedEntryCollection(entryList);
  +
           NestedMain nested = new NestedMain("main_object_" + timestamp, d);
           Identity oid = new Identity(nested, broker);
   
  @@ -146,6 +189,12 @@
   
           nested = (NestedMain) broker.getObjectByIdentity(oid);
           assertNull("Object was not deleted", nested);
  +
  +        Criteria crit = new Criteria();
  +        crit.addLike("name", entryName);
  +        Query query = QueryFactory.newQuery(NestedEntry.class, crit);
  +        Collection result = broker.getCollectionByQuery(query);
  +        assertEquals(0, result.size());
       }
   
   
  @@ -213,11 +262,22 @@
       public static class NestedDetail
       {
           private NestedDetailDetail nestedDetailDetail;
  +        private Collection nestedEntryCollection;
   
           public NestedDetail()
           {
           }
   
  +        public Collection getNestedEntryCollection()
  +        {
  +            return nestedEntryCollection;
  +        }
  +
  +        public void setNestedEntryCollection(Collection nestedEntryCollection)
  +        {
  +            this.nestedEntryCollection = nestedEntryCollection;
  +        }
  +
           public NestedDetail(NestedDetailDetail detail)
           {
               this.nestedDetailDetail = detail;
  @@ -282,6 +342,52 @@
               buf.append("realDetailName", realDetailName).
                       append("realDetailDescription", realDetailDescription);
               return buf.toString();
  +        }
  +    }
  +
  +    public static class NestedEntry
  +    {
  +        private Integer id;
  +        private Long fkId;
  +        private String name;
  +
  +        public NestedEntry()
  +        {
  +        }
  +
  +        public NestedEntry(String name)
  +        {
  +            this.name = name;
  +        }
  +
  +        public Integer getId()
  +        {
  +            return id;
  +        }
  +
  +        public void setId(Integer id)
  +        {
  +            this.id = id;
  +        }
  +
  +        public Long getFkId()
  +        {
  +            return fkId;
  +        }
  +
  +        public void setFkId(Long fkId)
  +        {
  +            this.fkId = fkId;
  +        }
  +
  +        public String getName()
  +        {
  +            return name;
  +        }
  +
  +        public void setName(String name)
  +        {
  +            this.name = name;
           }
       }
   }
  
  
  
  1.96      +35 -1     db-ojb/src/test/org/apache/ojb/repository_junit.xml
  
  Index: repository_junit.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit.xml,v
  retrieving revision 1.95
  retrieving revision 1.96
  diff -u -r1.95 -r1.96
  --- repository_junit.xml	4 Oct 2003 17:44:14 -0000	1.95
  +++ repository_junit.xml	17 Oct 2003 14:12:55 -0000	1.96
  @@ -4930,6 +4930,40 @@
               column="NESTED_DESCRIPTION"
               jdbc-type="VARCHAR"
           />
  +
  +        <collection-descriptor
  +            name="nestedDetail::nestedEntryCollection"
  +            element-class-ref="org.apache.ojb.broker.NestedFieldsTest$NestedEntry"
  +            auto-retrieve="true"
  +            auto-delete="true"
  +            auto-update="true"
  +        >
  +            <inverse-foreignkey field-ref="fkId"/>
  +        </collection-descriptor>
  +    </class-descriptor>
  +
  +
  +    <class-descriptor
  +        class="org.apache.ojb.broker.NestedFieldsTest$NestedEntry"
  +        table="NESTED_FIELDS_ENTRY">
  +
  +        <field-descriptor
  +        name="id"
  +        column="OBJ_ID"
  +        jdbc-type="INTEGER"
  +        primarykey="true"
  +        autoincrement="true"/>
  +
  +        <field-descriptor
  +        name="fkId"
  +        column="FK_ID"
  +        jdbc-type="BIGINT"/>
  +
  +        <field-descriptor
  +            name="name"
  +            column="NAME"
  +            jdbc-type="VARCHAR"
  +        />
       </class-descriptor>
   
       <!-- ************************************************* -->
  
  
  
  1.8       +45 -7     db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/AbstractPersistentField.java
  
  Index: AbstractPersistentField.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/AbstractPersistentField.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- AbstractPersistentField.java	20 Aug 2003 19:39:00 -0000	1.7
  +++ AbstractPersistentField.java	17 Oct 2003 14:12:56 -0000	1.8
  @@ -75,7 +75,6 @@
       private static final int UNKNOWN_FIELD = 0;
       private static final int NORMAL_FIELD = 1;
       private static final int NESTED_FIELD = 2;
  -    private static final Class[] METHOD_TYPES = {Class.class, String.class};
   
       protected transient Field field;
       protected String fieldName;
  @@ -170,7 +169,10 @@
       }
   
       /**
  -     * Tries to compute a Field object using getFieldRecursive.
  +     * Tries to compute a {@link java.lang.reflect.Field} object
  +     * based on given <code>Class</code> and field name - The field
  +     * name may a nested name string (e.g. stock::stockdetail::name)
  +     *
        * @throws MetadataException if there is an error computing the field
        * ( No Field was found into the class hierarchy)
        */
  @@ -178,8 +180,20 @@
       {
           try
           {
  -            Field f = getFieldRecursive(c, fieldname);
  -            if (makeAccessible()) f.setAccessible(true); // allow access to private members
  +            Field f;
  +            if (isNestedField())
  +            {
  +                f = getNestedRecursiveField(c, fieldname);
  +            }
  +            else
  +            {
  +                f = getFieldRecursive(c, fieldname);
  +            }
  +
  +            if (makeAccessible())
  +            {
  +                f.setAccessible(true);
  +            }
               return f;
           }
           catch (NoSuchFieldException e)
  @@ -214,6 +228,30 @@
           }
       }
   
  +    /**
  +     *
  +     * @param c
  +     * @param fieldName
  +     * @return
  +     * @throws NoSuchFieldException
  +     */
  +    private Field getNestedRecursiveField(Class c, String fieldName) throws NoSuchFieldException
  +    {
  +        Field result = null;
  +        int index = fieldName.indexOf(PATH_TOKEN);
  +        if (index >= 0)
  +        {
  +            String pathName = fieldName.substring(0, index);
  +            Field path = getFieldRecursive(c, pathName); // assert(path != null);
  +            result = getNestedRecursiveField(path.getType(), fieldName.substring(index
+ PATH_TOKEN.length()));
  +        }
  +        else
  +        {
  +            result = getFieldRecursive(c, fieldName);
  +        }
  +        return result;
  +    }
  +    
       protected boolean isNestedField()
       {
           if (isNestedField == UNKNOWN_FIELD) // not initialized
  @@ -238,7 +276,7 @@
       /**
        * Get nested attribute with given field name.
        * @param obj object from which the represented field's value is to be extracted
  -     * @param fieldName nested attribute name
  +     * @param aFieldName nested attribute name
        * @return Object the value of the represented field in object obj
        */
       protected Object getNestedObject(Object obj, String aFieldName)
  @@ -296,7 +334,7 @@
                       throw new MetadataException("Error getting field:"
                               + name + " in object:" + obj.getClass().getName(), e);
                   }
  -                Class type = pField.getType();
  +
                   /*
                   TODO: here we need cast to AbstractPersistentField to execute the doSet-method.
                   Find better solution without cast?
  
  
  

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