arminw      2005/10/01 06:46:51

  Modified:    src/java/org/apache/ojb/broker/util/collections
                        ManageableMapImpl.java
  Log:
  update javadoc, add check for composed PK's
  
  Revision  Changes    Path
  1.3       +33 -18    
db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableMapImpl.java
  
  Index: ManageableMapImpl.java
  ===================================================================
  RCS file: 
/home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/collections/ManageableMapImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ManageableMapImpl.java    5 Dec 2004 12:31:00 -0000       1.2
  +++ ManageableMapImpl.java    1 Oct 2005 13:46:50 -0000       1.3
  @@ -26,9 +26,15 @@
   import org.apache.ojb.broker.ManageableCollection;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
  +import org.apache.ojb.broker.metadata.FieldDescriptor;
  +import org.apache.ojb.broker.metadata.MetadataException;
   
   /**
  - * Manageable Wrapper for Map.
  + * Manageable Wrapper for Map, where the primary key is the map key, and the 
object
  + * is the map value.
  + * <br/>
  + * <strong>Note:</strong> This implementation is limited in use, only 
objects with
  + * single primary key field are allowed (composed PK's are illegal).
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jakob Braeuchi</a>
    * @version $Id$
  @@ -51,7 +57,7 @@
           }
           setMap(map);
       }
  -    
  +
       /**
        * Default Constructor. Wraps a HashMap.
        */
  @@ -59,7 +65,7 @@
       {
           this(new HashMap());
       }
  -    
  +
       /**
        * @return Returns the map.
        */
  @@ -176,13 +182,22 @@
        */
       public void ojbAdd(PersistenceBroker broker, Object anObject)
       {
  -             if (anObject != null)
  -             {
  -                     ClassDescriptor cd  = 
broker.getDescriptorRepository().getDescriptorFor(anObject.getClass());
  -                     Object          key = 
cd.getPrimaryKey().getPersistentField().get(anObject);
  -
  -            put(key,anObject);
  -             }
  +        if (anObject != null)
  +        {
  +            ClassDescriptor cd  = 
broker.getDescriptorRepository().getDescriptorFor(anObject.getClass());
  +            FieldDescriptor[] fields = cd.getPkFields();
  +            if(fields.length > 1 || fields.length == 0)
  +            {
  +                throw new MetadataException("ManageableHashMap can only be 
used for persistence capable objects with" +
  +                        " exactly one primiary key field defined in 
metadata, for " + anObject.getClass() + " the" +
  +                        " PK field count is " + fields.length);
  +            }
  +            else
  +            {
  +                Object key = fields[0].getPersistentField().get(anObject);
  +                put(key,anObject);
  +            }
  +        }
       }
   
       /**
  @@ -190,11 +205,11 @@
        */
       public void ojbAddAll(PersistenceBroker broker, ManageableCollection 
otherCollection)
       {
  -             Iterator it = otherCollection.ojbIterator(broker);
  -             while (it.hasNext())
  -             {
  -                     ojbAdd(broker, it.next());
  -             }        
  +        Iterator it = otherCollection.ojbIterator(broker);
  +        while (it.hasNext())
  +        {
  +            ojbAdd(broker, it.next());
  +        }
       }
   
       /**
  @@ -216,7 +231,7 @@
           }
           return getMap().equals(obj);
       }
  -    
  +
       /**
        * @see java.lang.Object#hashCode()
        */
  @@ -224,7 +239,7 @@
       {
           return getMap().hashCode();
       }
  -    
  +
       /**
        * @see java.lang.Object#toString()
        */
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to