db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/core QueryReferenceBroker.java
Date Tue, 12 Apr 2005 20:53:20 GMT
tomdz       2005/04/12 13:53:20

  Modified:    src/java/org/apache/ojb/broker/accesslayer
                        MtoNCollectionPrefetcher.java
                        CollectionPrefetcher.java
               src/java/org/apache/ojb/broker/core
                        QueryReferenceBroker.java
  Added:       src/java/org/apache/ojb/broker/util/collections
                        TrackingSetImpl.java ManageableTrackingSetImpl.java
                        RemovalAwareSet.java
  Log:
  Forward port of OJB 1.0 fix for issue OJB-19 including support for using collections classes
that implement ManageableCollection directly as the field type without having to specify collection-class
  
  Revision  Changes    Path
  1.1                  db-ojb/src/java/org/apache/ojb/broker/util/collections/TrackingSetImpl.java
  
  Index: TrackingSetImpl.java
  ===================================================================
  package org.apache.ojb.broker.util.collections;
  
  /* Copyright 2003-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import java.util.Set;
  
  /**
   * A Wrapper to track modifications on a set.
   *
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
   * @version $Id: TrackingSetImpl.java,v 1.1 2005/04/12 20:53:20 tomdz Exp $
   */
  public class TrackingSetImpl extends TrackingCollectionImpl implements Set, TrackingCollection
  {
      private static final long serialVersionUID = 3256721779866022455L;
  
      /**
       * Creates a new tracking set object that wraps around the given set.
       * 
       * @param set The set to be wrapped 
       */
      public TrackingSetImpl(Set set)
      {
          super(set);
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableTrackingSetImpl.java
  
  Index: ManageableTrackingSetImpl.java
  ===================================================================
  package org.apache.ojb.broker.util.collections;
  
  /* Copyright 2003-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import java.util.Collection;
  import java.util.HashSet;
  
  /**
   * Manageable set that keeps track of additions and deletions.
   *
   * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
   * @version $Id: ManageableTrackingSetImpl.java,v 1.1 2005/04/12 20:53:20 tomdz Exp $
   */
  public class ManageableTrackingSetImpl extends ManageableSetImpl implements TrackingCollection
  {
      private static final long serialVersionUID = 6009335074727417445L;
  
      /**
       * Default constructor, which results in the wrapping of a hash set. 
       */
      public ManageableTrackingSetImpl()
      {
          super(new TrackingSetImpl(new HashSet()));
      }
  
      /**
       * Returns the tracking collection.
       * 
       * @return The tracking collection
       */
      private TrackingCollection getTrackingCollection()
      {
          return (TrackingCollection)getCollection();
      }    
  
      /**
       * @see org.apache.ojb.broker.util.collections.TrackingCollection#clearDeletedObjects()
       */
      public void clearDeletedObjects()
      {
          getTrackingCollection().clearDeletedObjects();
      }
      
      /**
       * @see org.apache.ojb.broker.util.collections.TrackingCollection#clearNewObjects()
       */
      public void clearNewObjects()
      {
          getTrackingCollection().clearNewObjects();
      }
      /**
       * @see org.apache.ojb.broker.util.collections.TrackingCollection#getDeletedObjects()
       */
      public Collection getDeletedObjects()
      {
          return getTrackingCollection().getDeletedObjects();
      }
      /**
       * @see org.apache.ojb.broker.util.collections.TrackingCollection#getNewObjects()
       */
      public Collection getNewObjects()
      {
          return getTrackingCollection().getNewObjects();
      }
  }
  
  
  
  1.2       +29 -0     db-ojb/src/java/org/apache/ojb/broker/util/collections/RemovalAwareSet.java
  
  
  
  
  1.19      +2 -2      db-ojb/src/java/org/apache/ojb/broker/accesslayer/MtoNCollectionPrefetcher.java
  
  Index: MtoNCollectionPrefetcher.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/MtoNCollectionPrefetcher.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- MtoNCollectionPrefetcher.java	1 Apr 2005 20:28:09 -0000	1.18
  +++ MtoNCollectionPrefetcher.java	12 Apr 2005 20:53:20 -0000	1.19
  @@ -481,7 +481,7 @@
               }
               else
               {
  -                ManageableCollection col = createCollection(collectionClass);
  +                ManageableCollection col = createCollection(cds, collectionClass);
   
                   for (Iterator it2 = list.iterator(); it2.hasNext();)
                   {
  
  
  
  1.36      +43 -10    db-ojb/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java
  
  Index: CollectionPrefetcher.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java,v
  retrieving revision 1.35
  retrieving revision 1.36
  diff -u -r1.35 -r1.36
  --- CollectionPrefetcher.java	21 Feb 2005 19:14:58 -0000	1.35
  +++ CollectionPrefetcher.java	12 Apr 2005 20:53:20 -0000	1.36
  @@ -35,11 +35,14 @@
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.metadata.FieldHelper;
  +import org.apache.ojb.broker.metadata.MetadataException;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
  +import org.apache.ojb.broker.util.collections.RemovalAwareList;
  +import org.apache.ojb.broker.util.collections.RemovalAwareSet;
   
   /**
    * Relationship Prefetcher for Collections.
  @@ -194,7 +197,7 @@
               }
               else
               {
  -                ManageableCollection col = createCollection(collectionClass);
  +                ManageableCollection col = createCollection(cds, collectionClass);
                   for (Iterator it2 = list.iterator(); it2.hasNext();)
                   {
                       col.ojbAdd(pb, it2.next());
  @@ -215,28 +218,58 @@
       }
   
       /**
  -     * Create a Collection of class collectionClass
  -     * if collectionClass is null return a RemovalAwareCollection
  -     * @param collectionClass
  -     * @return
  +     * Create a collection object of the given collection type. If none has been given,
  +     * OJB uses RemovalAwareList, RemovalAwareSet, or RemovalAwareCollection depending
  +     * on the field type. 
  +     * 
  +     * @param desc            The collection descriptor
  +     * @param collectionClass The collection class specified in the collection-descriptor
  +     * @return The collection object
        */
  -    protected ManageableCollection createCollection(Class collectionClass)
  +    protected ManageableCollection createCollection(CollectionDescriptor desc, Class collectionClass)
       {
  +        Class                fieldType = desc.getPersistentField().getType();
           ManageableCollection col;
   
           if (collectionClass == null)
           {
  -            col = new RemovalAwareCollection();
  +            if (ManageableCollection.class.isAssignableFrom(fieldType))
  +            {
  +                try
  +                {
  +                    col = (ManageableCollection)fieldType.newInstance();
  +                }
  +                catch (Exception e)
  +                {
  +                    throw new OJBRuntimeException("Cannot instantiate the default collection
type "+fieldType.getName()+" of collection "+desc.getAttributeName()+" in type "+desc.getClassDescriptor().getClassNameOfObject());
  +                }
  +            }
  +            else if (fieldType.isAssignableFrom(RemovalAwareCollection.class))
  +            {
  +                col = new RemovalAwareCollection();
  +            }
  +            else if (fieldType.isAssignableFrom(RemovalAwareList.class))
  +            {
  +                col = new RemovalAwareList();
  +            }
  +            else if (fieldType.isAssignableFrom(RemovalAwareSet.class))
  +            {
  +                col = new RemovalAwareSet();
  +            }
  +            else
  +            {
  +                throw new MetadataException("Cannot determine a default collection type
for collection "+desc.getAttributeName()+" in type "+desc.getClassDescriptor().getClassNameOfObject());
  +            }
           }
           else
           {
               try
               {
  -                col = (ManageableCollection) collectionClass.newInstance();
  +                col = (ManageableCollection)collectionClass.newInstance();
               }
               catch (Exception e)
               {
  -                throw new OJBRuntimeException("Can't create new Collection for owner",
e);
  +                throw new OJBRuntimeException("Cannot instantiate the collection class
"+collectionClass.getName()+" of collection "+desc.getAttributeName()+" in type "+desc.getClassDescriptor().getClassNameOfObject());
               }
           }
           return col;
  
  
  
  1.31      +27 -4     db-ojb/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java
  
  Index: QueryReferenceBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- QueryReferenceBroker.java	2 Apr 2005 09:44:39 -0000	1.30
  +++ QueryReferenceBroker.java	12 Apr 2005 20:53:20 -0000	1.31
  @@ -41,6 +41,7 @@
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.metadata.FieldHelper;
  +import org.apache.ojb.broker.metadata.MetadataException;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
   import org.apache.ojb.broker.metadata.fieldaccess.AnonymousPersistentField;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
  @@ -50,7 +51,10 @@
   import org.apache.ojb.broker.query.QueryFactory;
   import org.apache.ojb.broker.util.BrokerHelper;
   import org.apache.ojb.broker.util.collections.ManageableListImpl;
  +import org.apache.ojb.broker.util.collections.ManageableSetImpl;
   import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
  +import org.apache.ojb.broker.util.collections.RemovalAwareList;
  +import org.apache.ojb.broker.util.collections.RemovalAwareSet;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   
  @@ -252,6 +256,7 @@
           // 1. ManageableVector implements Collection (will be returned if lazy == false)
           // 2. CollectionProxy implements Collection (will be returned if lazy == true)
           return (Collection) getCollectionByQuery(RemovalAwareCollection.class, query, lazy);
  +
       }
   
       /**
  @@ -261,14 +266,32 @@
       {
           // BRJ: do not use RemovalAwareCollection for m:n relationships
           // see http://db.apache.org/ojb/docu/guides/basic-technique.html#Mapping+m%3An+associations
  -        if (cds.isMtoNRelation())
  +
  +        Class fieldType = cds.getPersistentField().getType();
  +        Class collType  = null;
  +
  +        if (fieldType.isArray() || fieldType.isAssignableFrom(RemovalAwareCollection.class))
  +        {
  +            collType = cds.isMtoNRelation() ? ManageableListImpl.class : RemovalAwareCollection.class;
  +        }
  +        else if (fieldType.isAssignableFrom(RemovalAwareList.class))
           {
  -            return (Collection) getCollectionByQuery(ManageableListImpl.class, query, cds.isLazy());
  +            collType = cds.isMtoNRelation() ? ManageableListImpl.class : RemovalAwareList.class;
  +        }
  +        else if (fieldType.isAssignableFrom(RemovalAwareSet.class))
  +        {
  +            collType = cds.isMtoNRelation() ? ManageableSetImpl.class : RemovalAwareSet.class;
  +        }
  +        else if (ManageableCollection.class.isAssignableFrom(fieldType))
  +        {
  +            collType = fieldType;
           }
           else
           {
  -            return (Collection) getCollectionByQuery(RemovalAwareCollection.class, query,
cds.isLazy());
  +            throw new MetadataException("Cannot determine a default collection type for
collection "+cds.getAttributeName()+" in type "+cds.getClassDescriptor().getClassNameOfObject());
           }
  +
  +        return (Collection)getCollectionByQuery(collType, query, cds.isLazy());
       }
       
       /**
  
  
  

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