|
|||||||||||||||||||
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 | 26 | public static PropertyChange buildChange(String key, Object value) |
49 | { | |
50 | 26 | 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 | 26 | String idPath = (tokens.length == 5) ? tokens[3] : null; |
56 | 26 | String propertyName = tokens[tokens.length - 1]; |
57 | ||
58 | 26 | 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 | 100 | public static void iterateOverMatchingAttributes(String strategyId, String applicationId, |
79 | String pageName, WebSession session, WebSessionAttributeCallback callback) | |
80 | { | |
81 | 100 | Defense.notNull(strategyId, "strategyId"); |
82 | 100 | Defense.notNull(applicationId, "applicationId"); |
83 | 100 | Defense.notNull(pageName, "pageName"); |
84 | 100 | Defense.notNull(session, "session"); |
85 | ||
86 | 100 | String prefix = strategyId + "," + applicationId + "," + pageName + ","; |
87 | ||
88 | 100 | Iterator i = session.getAttributeNames().iterator(); |
89 | 100 | while (i.hasNext()) |
90 | { | |
91 | 168 | String name = (String) i.next(); |
92 | ||
93 | 168 | if (name.startsWith(prefix)) |
94 | 28 | 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 | 46 | public static String buildChangeKey(String strategyId, String applicationId, String pageName, |
117 | String idPath, String propertyName) | |
118 | { | |
119 | 46 | Defense.notNull(strategyId, "strategyId"); |
120 | 46 | Defense.notNull(applicationId, "applicationId"); |
121 | 46 | Defense.notNull(pageName, "pageName"); |
122 | 46 | Defense.notNull(propertyName, "propertyName"); |
123 | ||
124 | 46 | StringBuffer buffer = new StringBuffer(strategyId); |
125 | ||
126 | 46 | buffer.append(","); |
127 | 46 | buffer.append(applicationId); |
128 | 46 | buffer.append(","); |
129 | 46 | buffer.append(pageName); |
130 | ||
131 | 46 | if (idPath != null) |
132 | { | |
133 | 2 | buffer.append(","); |
134 | 2 | buffer.append(idPath); |
135 | } | |
136 | ||
137 | 46 | buffer.append(","); |
138 | 46 | buffer.append(propertyName); |
139 | ||
140 | 46 | return buffer.toString(); |
141 | } | |
142 | } |
|