Coverage Report - org.apache.tapestry.record.RecordUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
RecordUtils
0%
0/32
0%
0/8
2
 
 1  
 // Copyright 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.record;
 16  
 
 17  
 import java.util.Iterator;
 18  
 
 19  
 import org.apache.hivemind.util.Defense;
 20  
 import org.apache.tapestry.TapestryUtils;
 21  
 import org.apache.tapestry.web.WebSession;
 22  
 
 23  
 /**
 24  
  * Utility methods to support implementations of
 25  
  * {@link org.apache.tapestry.record.PropertyPersistenceStrategy}. This consists of code refactored
 26  
  * out of {@link org.apache.tapestry.record.SessionPropertyPersistenceStrategy} to support other,
 27  
  * similar, persistence types with different rules for how long values are stored in the session.
 28  
  * 
 29  
  * @author Howard M. Lewis Ship
 30  
  * @since 4.0
 31  
  */
 32  
 public final class RecordUtils
 33  
 {
 34  
     /* defeat instantiation */
 35  0
     private RecordUtils() { }
 36  
     
 37  
     /**
 38  
      * Builds a {@link PropertyChange} instance for the given key and value pulled from the
 39  
      * {@link org.apache.tapestry.web.WebSession}.
 40  
      * 
 41  
      * @param key
 42  
      *            a key, previously created by
 43  
      *            {@link #buildChangeKey(String, String, String, String, String)}, consisting of a
 44  
      *            strategy id, application id, page name, id path (optional), and a property name,
 45  
      *            all seperated by commas.
 46  
      * @param value
 47  
      *            the value stored in the session with this key
 48  
      * @return a {@link PropertyChange} storing the property name and id path (if any), and the
 49  
      *         value
 50  
      */
 51  
     public static PropertyChange buildChange(String key, Object value)
 52  
     {
 53  0
         String[] tokens = TapestryUtils.split(key);
 54  
 
 55  
         // Either strategy-id, app-name,page-name,id-path,property
 56  
         // or strategy-id,app-name,page-name,property
 57  
 
 58  0
         String idPath = (tokens.length == 5) ? tokens[3] : null;
 59  0
         String propertyName = tokens[tokens.length - 1];
 60  
 
 61  0
         return new PropertyChangeImpl(idPath, propertyName, value);
 62  
     }
 63  
 
 64  
     /**
 65  
      * Iterates over the attributes stored in the session, invoking a callback on each one that
 66  
      * matches the given prefix, applicationid and page name. This is used to operate over all
 67  
      * stored data for a particular combination of strategy, applicationId and page.
 68  
      * 
 69  
      * @param strategyId
 70  
      *            a unique identifier for a particular implementation of
 71  
      *            {@link PropertyPersistenceStrategy}
 72  
      * @param applicationId
 73  
      *            a unique id for the application
 74  
      * @param pageName
 75  
      *            the name of the page
 76  
      * @param session
 77  
      *            the session to search
 78  
      * @param callback
 79  
      *            the callback to invoke on each matching attibute name
 80  
      */
 81  
     public static void iterateOverMatchingAttributes(String strategyId, String applicationId,
 82  
             String pageName, WebSession session, WebSessionAttributeCallback callback)
 83  
     {
 84  0
         Defense.notNull(strategyId, "strategyId");
 85  0
         Defense.notNull(applicationId, "applicationId");
 86  0
         Defense.notNull(pageName, "pageName");
 87  0
         Defense.notNull(session, "session");
 88  
 
 89  0
         String prefix = strategyId + "," + applicationId + "," + pageName + ",";
 90  
 
 91  0
         Iterator i = session.getAttributeNames().iterator();
 92  0
         while (i.hasNext())
 93  
         {
 94  0
             String name = (String) i.next();
 95  
 
 96  0
             if (name.startsWith(prefix))
 97  0
                 callback.handleAttribute(session, name);
 98  0
         }
 99  0
     }
 100  
 
 101  
     /**
 102  
      * Builds a change key, used to identify the change within the {@link WebSession}. A change key
 103  
      * can be used as a session attribute name, without reasonable fear of conflict.
 104  
      * 
 105  
      * @param strategyId
 106  
      *            a unique identifier for a particular implementation of
 107  
      *            {@link PropertyPersistenceStrategy}
 108  
      * @param applicationId
 109  
      *            a unique identifier for the application
 110  
      * @param pageName
 111  
      *            the name of the page containing the change
 112  
      * @param idPath
 113  
      *            the id path of the component within the page containing the page, possibly null
 114  
      * @param propertyName
 115  
      *            the name of the property
 116  
      * @return the above values, seperated by commas (well, no comma between the prefix and the
 117  
      *         application id)
 118  
      */
 119  
     public static String buildChangeKey(String strategyId, String applicationId, String pageName,
 120  
             String idPath, String propertyName)
 121  
     {
 122  0
         Defense.notNull(strategyId, "strategyId");
 123  0
         Defense.notNull(applicationId, "applicationId");
 124  0
         Defense.notNull(pageName, "pageName");
 125  0
         Defense.notNull(propertyName, "propertyName");
 126  
 
 127  0
         StringBuffer buffer = new StringBuffer(strategyId);
 128  
 
 129  0
         buffer.append(",");
 130  0
         buffer.append(applicationId);
 131  0
         buffer.append(",");
 132  0
         buffer.append(pageName);
 133  
 
 134  0
         if (idPath != null)
 135  
         {
 136  0
             buffer.append(",");
 137  0
             buffer.append(idPath);
 138  
         }
 139  
 
 140  0
         buffer.append(",");
 141  0
         buffer.append(propertyName);
 142  
 
 143  0
         return buffer.toString();
 144  
     }
 145  
 }