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