1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.record; |
16 |
| |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.Collection; |
19 |
| import java.util.Collections; |
20 |
| import java.util.Iterator; |
21 |
| |
22 |
| import org.apache.hivemind.util.Defense; |
23 |
| import org.apache.tapestry.IRequestCycle; |
24 |
| import org.apache.tapestry.TapestryUtils; |
25 |
| import org.apache.tapestry.engine.ServiceEncoding; |
26 |
| import org.apache.tapestry.web.WebRequest; |
27 |
| import org.apache.tapestry.web.WebSession; |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| public class SessionPropertyPersistenceStrategy implements PropertyPersistenceStrategy |
37 |
| { |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| private String _applicationId; |
43 |
| |
44 |
| private WebRequest _request; |
45 |
| |
46 |
23
| public void store(String pageName, String idPath, String propertyName, Object newValue)
|
47 |
| { |
48 |
23
| Defense.notNull(pageName, "pageName");
|
49 |
23
| Defense.notNull(propertyName, "propertyName");
|
50 |
| |
51 |
23
| WebSession session = _request.getSession(true);
|
52 |
| |
53 |
23
| StringBuffer buffer = new StringBuffer();
|
54 |
| |
55 |
23
| buffer.append(_applicationId);
|
56 |
23
| buffer.append(",");
|
57 |
23
| buffer.append(pageName);
|
58 |
| |
59 |
23
| if (idPath != null)
|
60 |
| { |
61 |
1
| buffer.append(",");
|
62 |
1
| buffer.append(idPath);
|
63 |
| } |
64 |
| |
65 |
23
| buffer.append(",");
|
66 |
23
| buffer.append(propertyName);
|
67 |
| |
68 |
23
| String key = buffer.toString();
|
69 |
| |
70 |
23
| session.setAttribute(key, newValue);
|
71 |
| } |
72 |
| |
73 |
193
| public Collection getStoredChanges(String pageName, IRequestCycle cycle)
|
74 |
| { |
75 |
193
| Defense.notNull(pageName, "pageName");
|
76 |
| |
77 |
193
| WebSession session = _request.getSession(false);
|
78 |
| |
79 |
193
| if (session == null)
|
80 |
145
| return Collections.EMPTY_LIST;
|
81 |
| |
82 |
48
| Collection result = new ArrayList();
|
83 |
| |
84 |
48
| String prefix = _applicationId + "," + pageName + ",";
|
85 |
| |
86 |
48
| Iterator i = session.getAttributeNames().iterator();
|
87 |
48
| while (i.hasNext())
|
88 |
| { |
89 |
82
| String key = (String) i.next();
|
90 |
| |
91 |
82
| if (key.startsWith(prefix))
|
92 |
| { |
93 |
13
| PropertyChange change = buildChange(key, session.getAttribute(key));
|
94 |
| |
95 |
13
| result.add(change);
|
96 |
| } |
97 |
| } |
98 |
| |
99 |
48
| return result;
|
100 |
| } |
101 |
| |
102 |
13
| private PropertyChange buildChange(String key, Object value)
|
103 |
| { |
104 |
13
| String[] tokens = TapestryUtils.split(key);
|
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
13
| String idPath = (tokens.length == 4) ? tokens[2] : null;
|
110 |
13
| String propertyName = tokens[tokens.length - 1];
|
111 |
| |
112 |
13
| return new PropertyChangeImpl(idPath, propertyName, value);
|
113 |
| } |
114 |
| |
115 |
3
| public void discardStoredChanges(String pageName, IRequestCycle cycle)
|
116 |
| { |
117 |
3
| Defense.notNull(pageName, "pageName");
|
118 |
| |
119 |
3
| WebSession session = _request.getSession(false);
|
120 |
| |
121 |
3
| if (session == null)
|
122 |
1
| return;
|
123 |
| |
124 |
2
| String prefix = _applicationId + "," + pageName + ",";
|
125 |
| |
126 |
2
| Iterator i = session.getAttributeNames().iterator();
|
127 |
2
| while (i.hasNext())
|
128 |
| { |
129 |
2
| String key = (String) i.next();
|
130 |
| |
131 |
2
| if (key.startsWith(prefix))
|
132 |
1
| session.setAttribute(key, null);
|
133 |
| } |
134 |
| } |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
138
| public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle, boolean post)
|
141 |
| { |
142 |
| } |
143 |
| |
144 |
48
| public void setApplicationId(String applicationName)
|
145 |
| { |
146 |
48
| _applicationId = applicationName;
|
147 |
| } |
148 |
| |
149 |
51
| public void setRequest(WebRequest request)
|
150 |
| { |
151 |
51
| _request = request;
|
152 |
| } |
153 |
| } |