1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.record; |
16 |
|
|
17 |
|
import org.apache.hivemind.util.Defense; |
18 |
|
import org.apache.tapestry.engine.ServiceEncoding; |
19 |
|
import org.apache.tapestry.web.WebRequest; |
20 |
|
|
21 |
|
import java.util.*; |
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
5 |
public class ClientPropertyPersistenceStrategy implements PropertyPersistenceStrategy |
34 |
|
{ |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
5 |
protected final Map _data = new LinkedHashMap(); |
40 |
|
|
41 |
|
protected PersistentPropertyDataEncoder _encoder; |
42 |
|
|
43 |
|
protected WebRequest _request; |
44 |
|
|
45 |
|
protected ClientPropertyPersistenceScope _scope; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
public void initializeService() |
56 |
|
{ |
57 |
3 |
List names = _request.getParameterNames(); |
58 |
3 |
Iterator i = names.iterator(); |
59 |
10 |
while (i.hasNext()) |
60 |
|
{ |
61 |
7 |
String name = (String) i.next(); |
62 |
|
|
63 |
7 |
if (!_scope.isParameterForScope(name)) |
64 |
3 |
continue; |
65 |
|
|
66 |
4 |
String pageName = _scope.extractPageName(name); |
67 |
|
|
68 |
4 |
String encoded = _request.getParameterValue(name); |
69 |
|
|
70 |
4 |
PersistentPropertyData data = new PersistentPropertyData(_encoder); |
71 |
4 |
data.storeEncoded(encoded); |
72 |
|
|
73 |
4 |
_data.put(pageName, data); |
74 |
4 |
} |
75 |
3 |
} |
76 |
|
|
77 |
|
public void store(String pageName, String idPath, String propertyName, Object newValue) |
78 |
|
{ |
79 |
1 |
PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName); |
80 |
1 |
if (data == null) |
81 |
|
{ |
82 |
1 |
data = new PersistentPropertyData(_encoder); |
83 |
1 |
_data.put(pageName, data); |
84 |
|
} |
85 |
|
|
86 |
1 |
data.store(idPath, propertyName, newValue); |
87 |
1 |
} |
88 |
|
|
89 |
|
public Collection getStoredChanges(String pageName) |
90 |
|
{ |
91 |
4 |
PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName); |
92 |
|
|
93 |
4 |
if (data == null) |
94 |
2 |
return Collections.EMPTY_LIST; |
95 |
|
|
96 |
2 |
return data.getPageChanges(); |
97 |
|
} |
98 |
|
|
99 |
|
public void discardStoredChanges(String pageName) |
100 |
|
{ |
101 |
1 |
_data.remove(pageName); |
102 |
1 |
} |
103 |
|
|
104 |
|
public void addParametersForPersistentProperties(ServiceEncoding encoding, boolean post) |
105 |
|
{ |
106 |
2 |
Defense.notNull(encoding, "encoding"); |
107 |
|
|
108 |
2 |
Iterator i = _data.entrySet().iterator(); |
109 |
5 |
while (i.hasNext()) |
110 |
|
{ |
111 |
3 |
Map.Entry e = (Map.Entry) i.next(); |
112 |
|
|
113 |
3 |
String pageName = (String) e.getKey(); |
114 |
3 |
PersistentPropertyData data = (PersistentPropertyData) e.getValue(); |
115 |
|
|
116 |
3 |
ClientPropertyPersistenceScope scope = getScope(); |
117 |
|
|
118 |
3 |
if (scope.shouldEncodeState(encoding, pageName, data)) |
119 |
|
{ |
120 |
2 |
String parameterName = _scope.constructParameterName(pageName); |
121 |
2 |
encoding.setParameterValue(parameterName, data.getEncoded()); |
122 |
|
} |
123 |
3 |
} |
124 |
2 |
} |
125 |
|
|
126 |
|
public void setRequest(WebRequest request) |
127 |
|
{ |
128 |
3 |
_request = request; |
129 |
3 |
} |
130 |
|
|
131 |
|
public ClientPropertyPersistenceScope getScope() |
132 |
|
{ |
133 |
3 |
return _scope; |
134 |
|
} |
135 |
|
|
136 |
|
public void setScope(ClientPropertyPersistenceScope scope) |
137 |
|
{ |
138 |
3 |
_scope = scope; |
139 |
3 |
} |
140 |
|
|
141 |
|
public void setEncoder(PersistentPropertyDataEncoder encoder) |
142 |
|
{ |
143 |
4 |
_encoder = encoder; |
144 |
4 |
} |
145 |
|
} |