|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| RecordUtils.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 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 class RecordUtils | |
| 33 | { | |
| 34 | /** | |
| 35 | * Builds a {@link PropertyChange} instance for the given key and value pulled from the | |
| 36 | * {@link org.apache.tapestry.web.WebSession}. | |
| 37 | * | |
| 38 | * @param key | |
| 39 | * a key, previously created by | |
| 40 | * {@link #buildChangeKey(String, String, String, String, String)}, consisting of a | |
| 41 | * strategy id, application id, page name, id path (optional), and a property name, | |
| 42 | * all seperated by commas. | |
| 43 | * @param value | |
| 44 | * the value stored in the session with this key | |
| 45 | * @return a {@link PropertyChange} storing the property name and id path (if any), and the | |
| 46 | * value | |
| 47 | */ | |
| 48 | 39 | public static PropertyChange buildChange(String key, Object value) |
| 49 | { | |
| 50 | 39 | String[] tokens = TapestryUtils.split(key); |
| 51 | ||
| 52 | // Either strategy-id, app-name,page-name,id-path,property | |
| 53 | // or strategy-id,app-name,page-name,property | |
| 54 | ||
| 55 | 39 | String idPath = (tokens.length == 5) ? tokens[3] : null; |
| 56 | 39 | String propertyName = tokens[tokens.length - 1]; |
| 57 | ||
| 58 | 39 | return new PropertyChangeImpl(idPath, propertyName, value); |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * Iterates over the attributes stored in the session, invoking a callback on each one that | |
| 63 | * matches the given prefix, applicationid and page name. This is used to operate over all | |
| 64 | * stored data for a particular combination of strategy, applicationId and page. | |
| 65 | * | |
| 66 | * @param strategyId | |
| 67 | * a unique identifier for a particular implementation of | |
| 68 | * {@link PropertyPersistenceStrategy} | |
| 69 | * @param applicationId | |
| 70 | * a unique id for the application | |
| 71 | * @param pageName | |
| 72 | * the name of the page | |
| 73 | * @param session | |
| 74 | * the session to search | |
| 75 | * @param callback | |
| 76 | * the callback to invoke on each matching attibute name | |
| 77 | */ | |
| 78 | 150 | public static void iterateOverMatchingAttributes(String strategyId, String applicationId, |
| 79 | String pageName, WebSession session, WebSessionAttributeCallback callback) | |
| 80 | { | |
| 81 | 150 | Defense.notNull(strategyId, "strategyId"); |
| 82 | 150 | Defense.notNull(applicationId, "applicationId"); |
| 83 | 150 | Defense.notNull(pageName, "pageName"); |
| 84 | 150 | Defense.notNull(session, "session"); |
| 85 | ||
| 86 | 150 | String prefix = strategyId + "," + applicationId + "," + pageName + ","; |
| 87 | ||
| 88 | 150 | Iterator i = session.getAttributeNames().iterator(); |
| 89 | 150 | while (i.hasNext()) |
| 90 | { | |
| 91 | 252 | String name = (String) i.next(); |
| 92 | ||
| 93 | 252 | if (name.startsWith(prefix)) |
| 94 | 42 | callback.handleAttribute(session, name); |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Builds a change key, used to identify the change within the {@link WebSession}. A change key | |
| 100 | * can be used as a session attribute name, without reasonable fear of conflict. | |
| 101 | * | |
| 102 | * @param strategyId | |
| 103 | * a unique identifier for a particular implementation of | |
| 104 | * {@link PropertyPersistenceStrategy} | |
| 105 | * @param applicationId | |
| 106 | * a unique identifier for the application | |
| 107 | * @param pageName | |
| 108 | * the name of the page containing the change | |
| 109 | * @param idPath | |
| 110 | * the id path of the component within the page containing the page, possibly null | |
| 111 | * @param propertyName | |
| 112 | * the name of the property | |
| 113 | * @return the above values, seperated by commas (well, no comma between the prefix and the | |
| 114 | * application id) | |
| 115 | */ | |
| 116 | 69 | public static String buildChangeKey(String strategyId, String applicationId, String pageName, |
| 117 | String idPath, String propertyName) | |
| 118 | { | |
| 119 | 69 | Defense.notNull(strategyId, "strategyId"); |
| 120 | 69 | Defense.notNull(applicationId, "applicationId"); |
| 121 | 69 | Defense.notNull(pageName, "pageName"); |
| 122 | 69 | Defense.notNull(propertyName, "propertyName"); |
| 123 | ||
| 124 | 69 | StringBuffer buffer = new StringBuffer(strategyId); |
| 125 | ||
| 126 | 69 | buffer.append(","); |
| 127 | 69 | buffer.append(applicationId); |
| 128 | 69 | buffer.append(","); |
| 129 | 69 | buffer.append(pageName); |
| 130 | ||
| 131 | 69 | if (idPath != null) |
| 132 | { | |
| 133 | 3 | buffer.append(","); |
| 134 | 3 | buffer.append(idPath); |
| 135 | } | |
| 136 | ||
| 137 | 69 | buffer.append(","); |
| 138 | 69 | buffer.append(propertyName); |
| 139 | ||
| 140 | 69 | return buffer.toString(); |
| 141 | } | |
| 142 | } |
|
||||||||||