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 final PersistentPropertyDataEncoder _encoder; |
48 |
| |
49 |
| private WebRequest _request; |
50 |
| |
51 |
| private ClientPropertyPersistenceScope _scope; |
52 |
| |
53 |
256
| public ClientPropertyPersistenceStrategy()
|
54 |
| { |
55 |
256
| this(new PersistentPropertyDataEncoderImpl());
|
56 |
| } |
57 |
| |
58 |
| |
59 |
257
| ClientPropertyPersistenceStrategy(PersistentPropertyDataEncoder encoder)
|
60 |
| { |
61 |
257
| _encoder = encoder;
|
62 |
| } |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
255
| public void initializeService()
|
73 |
| { |
74 |
255
| List names = _request.getParameterNames();
|
75 |
255
| Iterator i = names.iterator();
|
76 |
255
| while (i.hasNext())
|
77 |
| { |
78 |
723
| String name = (String) i.next();
|
79 |
| |
80 |
723
| if (!_scope.isParameterForScope(name))
|
81 |
719
| continue;
|
82 |
| |
83 |
4
| String pageName = _scope.extractPageName(name);
|
84 |
| |
85 |
4
| String encoded = _request.getParameterValue(name);
|
86 |
| |
87 |
4
| PersistentPropertyData data = new PersistentPropertyData(_encoder);
|
88 |
4
| data.storeEncoded(encoded);
|
89 |
| |
90 |
4
| _data.put(pageName, data);
|
91 |
| } |
92 |
| } |
93 |
| |
94 |
1
| public void store(String pageName, String idPath, String propertyName, Object newValue)
|
95 |
| { |
96 |
1
| PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
|
97 |
1
| if (data == null)
|
98 |
| { |
99 |
1
| data = new PersistentPropertyData(_encoder);
|
100 |
1
| _data.put(pageName, data);
|
101 |
| } |
102 |
| |
103 |
1
| data.store(idPath, propertyName, newValue);
|
104 |
| } |
105 |
| |
106 |
547
| public Collection getStoredChanges(String pageName, IRequestCycle cycle)
|
107 |
| { |
108 |
547
| PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
|
109 |
| |
110 |
547
| if (data == null)
|
111 |
545
| return Collections.EMPTY_LIST;
|
112 |
| |
113 |
2
| return data.getPageChanges();
|
114 |
| } |
115 |
| |
116 |
1
| public void discardStoredChanges(String pageName, IRequestCycle cycle)
|
117 |
| { |
118 |
1
| _data.remove(pageName);
|
119 |
| } |
120 |
| |
121 |
374
| public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle, boolean post)
|
122 |
| { |
123 |
374
| Defense.notNull(encoding, "encoding");
|
124 |
374
| Defense.notNull(cycle, "cycle");
|
125 |
| |
126 |
374
| Iterator i = _data.entrySet().iterator();
|
127 |
374
| while (i.hasNext())
|
128 |
| { |
129 |
3
| Map.Entry e = (Map.Entry) i.next();
|
130 |
| |
131 |
3
| String pageName = (String) e.getKey();
|
132 |
3
| PersistentPropertyData data = (PersistentPropertyData) e.getValue();
|
133 |
| |
134 |
3
| ClientPropertyPersistenceScope scope = getScope();
|
135 |
| |
136 |
3
| if (scope.shouldEncodeState(encoding, cycle, pageName, data))
|
137 |
| { |
138 |
2
| String parameterName = _scope.constructParameterName(pageName);
|
139 |
2
| encoding.setParameterValue(parameterName, data.getEncoded());
|
140 |
| } |
141 |
| } |
142 |
| } |
143 |
| |
144 |
255
| public void setRequest(WebRequest request)
|
145 |
| { |
146 |
255
| _request = request;
|
147 |
| } |
148 |
| |
149 |
3
| public ClientPropertyPersistenceScope getScope()
|
150 |
| { |
151 |
3
| return _scope;
|
152 |
| } |
153 |
| |
154 |
255
| public void setScope(ClientPropertyPersistenceScope scope)
|
155 |
| { |
156 |
255
| _scope = scope;
|
157 |
| } |
158 |
| } |