mindbridge    2005/08/14 07:06:37

  Modified:    framework/src/java/org/apache/tapestry/components
                        ForBean.java
  Log:
  Updated the For component algorithm once again
  
  Revision  Changes    Path
  1.7       +75 -58    
jakarta-tapestry/framework/src/java/org/apache/tapestry/components/ForBean.java
  
  Index: ForBean.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/components/ForBean.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ForBean.java      11 Aug 2005 13:17:25 -0000      1.6
  +++ ForBean.java      14 Aug 2005 14:06:37 -0000      1.7
  @@ -80,6 +80,10 @@
   
       protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
       {
  +     // Clear the cache between rewind and render.
  +     // This allows the value of 'source' to be changed by the form 
listeners.
  +     setSavedSourceData(null);
  +     
           // form may be null if component is not located in a form
           IForm form = (IForm) 
cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE);
   
  @@ -179,17 +183,6 @@
        return sourceData;
       }
       
  -    
  -    protected Iterator getSources(String parameter)
  -    {
  -     IBinding binding = getBinding(parameter);
  -             if (binding == null)
  -                     return null;
  -             
  -             Object data = binding.getObject();
  -             return (Iterator) getValueConverter().coerceValue(data, 
Iterator.class);
  -    }
  -    
       /**
        *  Returns a list of the values stored as Hidden fields in the form.
        *  A conversion is performed if the primary key of the value is stored.
  @@ -249,7 +242,7 @@
                if (sourceData == null)
                        return null;
                
  -     List sourcePrimaryKeys = generateSourcePrimaryKeys();
  +     List sourcePrimaryKeys = evaluateSourcePrimaryKeys();
                if (sourcePrimaryKeys == null)
                        return null;
        
  @@ -277,7 +270,7 @@
        * of the array is a string describing whether a particular element is
        * a primary key or a value.
        */
  -    private List generateSourcePrimaryKeys()
  +    private List evaluateSourcePrimaryKeys()
       {
        // check if the result is already cached to avoid evaluating again
        List sourcePrimaryKeys = getSourcePrimaryKeys();
  @@ -288,10 +281,6 @@
                if (sourceData == null)
                        return null;
        
  -     Map primaryKeyMap = getPrimaryKeyMap();
  -     if (primaryKeyMap == null)
  -             primaryKeyMap = new HashMap();
  -     
                // extract primary keys from data
                StringBuffer pkDesc = new StringBuffer(sourceData.size());
                sourcePrimaryKeys = new ArrayList(sourceData.size()+1);
  @@ -306,60 +295,45 @@
                        }
                        else {
                                pkDesc.append(DESC_PRIMARY_KEY);
  -                             primaryKeyMap.put(pk, value);
                        }
                        sourcePrimaryKeys.add(pk);
                }
        
                setSourcePrimaryKeys(sourcePrimaryKeys);
  -             setPrimaryKeyMap(primaryKeyMap);
                
                return sourcePrimaryKeys;
       }
       
       /**
  -     * Iterates over the fullSource parameter until a value with a matching 
primary key is found.
  -     * The primary keys generated and the fullSource iterator are stored in 
properties 
  -     * to avoid repeated evaluation.
  -     * 
  -     * @param primaryKey the primary key to be matched
  -     * @param fullSource the provided list of objects
  -     * @return an object with a matching primary key, or null if such is not 
found
  +     * Converts the values in the 'source' parameter to primary keys if 
possible.
  +     * Stores the evaluated primary keys in a map to determine the value 
  +     * that a particular primary key represents.  
  +     *  
  +     * @return the map from primary keys to their corresponding objects 
        */
  -    private Object findPrimaryKeyMatchInFullSource(Object primaryKey, Object 
fullSource)
  +    private Map fillSourcePrimaryKeysMap()
       {
  +     // check if the result is already cached to avoid evaluating again
        Map primaryKeyMap = getPrimaryKeyMap();
  -     if (primaryKeyMap == null)
  -             primaryKeyMap = new HashMap();
  +     if (primaryKeyMap != null)
  +             return primaryKeyMap;
        
  -             Iterator it = getFullSourceIterator();
  -             if (it == null) {
  -                     it = (Iterator) 
getValueConverter().coerceValue(fullSource, Iterator.class);
  -                     if (it == null)
  -                             it = Collections.EMPTY_LIST.iterator();
  -             }
  -             
  -             try { 
  -                     while (it.hasNext()) {
  -                     Object sourceValue = it.next();
  -                     if (sourceValue == null)
  -                             continue;
  -                     
  -                     Object sourcePrimaryKey = 
getPrimaryKeyFromValue(sourceValue);
  -                     if (sourcePrimaryKey != null)
  -                             primaryKeyMap.put(sourcePrimaryKey, 
sourceValue);
  -                     
  -                     if (primaryKey.equals(sourcePrimaryKey)) {
  -                             return sourceValue;
  -                     }
  -                     }
  -                     
  +     List sourceData = getSourceData();
  +             if (sourceData == null)
                        return null;
  +     
  +             // extract primary keys from data
  +             primaryKeyMap = new HashMap();
  +             for (Iterator it = sourceData.iterator(); it.hasNext();) {
  +                     Object value = it.next();
  +                     Object pk = getPrimaryKeyFromValue(value);
  +                     if (pk != null)
  +                             primaryKeyMap.put(pk, value);
                }
  -             finally {
  -                     setFullSourceIterator(it);
  -                     setPrimaryKeyMap(primaryKeyMap);
  -             }
  +     
  +             setPrimaryKeyMap(primaryKeyMap);
  +             
  +             return primaryKeyMap;
       }
       
       /**
  @@ -402,9 +376,7 @@
       private Object getValueFromPrimaryKey(Object primaryKey) {
        Object value = null;
   
  -     generateSourcePrimaryKeys();
  -     
  -     Map primaryKeyMap = getPrimaryKeyMap();
  +     Map primaryKeyMap = fillSourcePrimaryKeysMap();
        if (primaryKeyMap != null)
                value = primaryKeyMap.get(primaryKey);
        
  @@ -428,6 +400,51 @@
       }
       
       /**
  +     * Iterates over the fullSource parameter until a value with a matching 
primary key is found.
  +     * The primary keys generated and the fullSource iterator are stored in 
properties 
  +     * to avoid repeated evaluation.
  +     * 
  +     * @param primaryKey the primary key to be matched
  +     * @param fullSource the provided list of objects
  +     * @return an object with a matching primary key, or null if such is not 
found
  +     */
  +    private Object findPrimaryKeyMatchInFullSource(Object primaryKey, Object 
fullSource)
  +    {
  +     Map primaryKeyMap = getPrimaryKeyMap();
  +     if (primaryKeyMap == null)
  +             primaryKeyMap = new HashMap();
  +     
  +             Iterator it = getFullSourceIterator();
  +             if (it == null) {
  +                     it = (Iterator) 
getValueConverter().coerceValue(fullSource, Iterator.class);
  +                     if (it == null)
  +                             it = Collections.EMPTY_LIST.iterator();
  +             }
  +             
  +             try { 
  +                     while (it.hasNext()) {
  +                     Object sourceValue = it.next();
  +                     if (sourceValue == null)
  +                             continue;
  +                     
  +                     Object sourcePrimaryKey = 
getPrimaryKeyFromValue(sourceValue);
  +                     if (sourcePrimaryKey != null)
  +                             primaryKeyMap.put(sourcePrimaryKey, 
sourceValue);
  +                     
  +                     if (primaryKey.equals(sourcePrimaryKey)) {
  +                             return sourceValue;
  +                     }
  +                     }
  +                     
  +                     return null;
  +             }
  +             finally {
  +                     setFullSourceIterator(it);
  +                     setPrimaryKeyMap(primaryKeyMap);
  +             }
  +    }
  +    
  +    /**
        * Updates the index and value output parameters if bound.
        */
       private void updateOutputParameters()
  
  
  

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

Reply via email to