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 | 0 | public class ClientPropertyPersistenceStrategy implements PropertyPersistenceStrategy |
34 | |
{ |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | 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 | 0 | List names = _request.getParameterNames(); |
58 | 0 | Iterator i = names.iterator(); |
59 | 0 | while (i.hasNext()) |
60 | |
{ |
61 | 0 | String name = (String) i.next(); |
62 | |
|
63 | 0 | if (!_scope.isParameterForScope(name)) |
64 | 0 | continue; |
65 | |
|
66 | 0 | String pageName = _scope.extractPageName(name); |
67 | |
|
68 | 0 | String encoded = _request.getParameterValue(name); |
69 | |
|
70 | 0 | PersistentPropertyData data = new PersistentPropertyData(_encoder); |
71 | 0 | data.storeEncoded(encoded); |
72 | |
|
73 | 0 | _data.put(pageName, data); |
74 | 0 | } |
75 | 0 | } |
76 | |
|
77 | |
public void store(String pageName, String idPath, String propertyName, Object newValue) |
78 | |
{ |
79 | 0 | PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName); |
80 | 0 | if (data == null) |
81 | |
{ |
82 | 0 | data = new PersistentPropertyData(_encoder); |
83 | 0 | _data.put(pageName, data); |
84 | |
} |
85 | |
|
86 | 0 | data.store(idPath, propertyName, newValue); |
87 | 0 | } |
88 | |
|
89 | |
public Collection getStoredChanges(String pageName) |
90 | |
{ |
91 | 0 | PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName); |
92 | |
|
93 | 0 | if (data == null) |
94 | 0 | return Collections.EMPTY_LIST; |
95 | |
|
96 | 0 | return data.getPageChanges(); |
97 | |
} |
98 | |
|
99 | |
public void discardStoredChanges(String pageName) |
100 | |
{ |
101 | 0 | _data.remove(pageName); |
102 | 0 | } |
103 | |
|
104 | |
public void addParametersForPersistentProperties(ServiceEncoding encoding, boolean post) |
105 | |
{ |
106 | 0 | Defense.notNull(encoding, "encoding"); |
107 | |
|
108 | 0 | Iterator i = _data.entrySet().iterator(); |
109 | 0 | while (i.hasNext()) |
110 | |
{ |
111 | 0 | Map.Entry e = (Map.Entry) i.next(); |
112 | |
|
113 | 0 | String pageName = (String) e.getKey(); |
114 | 0 | PersistentPropertyData data = (PersistentPropertyData) e.getValue(); |
115 | |
|
116 | 0 | ClientPropertyPersistenceScope scope = getScope(); |
117 | |
|
118 | 0 | if (scope.shouldEncodeState(encoding, pageName, data)) |
119 | |
{ |
120 | 0 | String parameterName = _scope.constructParameterName(pageName); |
121 | 0 | encoding.setParameterValue(parameterName, data.getEncoded()); |
122 | |
} |
123 | 0 | } |
124 | 0 | } |
125 | |
|
126 | |
public void setRequest(WebRequest request) |
127 | |
{ |
128 | 0 | _request = request; |
129 | 0 | } |
130 | |
|
131 | |
public ClientPropertyPersistenceScope getScope() |
132 | |
{ |
133 | 0 | return _scope; |
134 | |
} |
135 | |
|
136 | |
public void setScope(ClientPropertyPersistenceScope scope) |
137 | |
{ |
138 | 0 | _scope = scope; |
139 | 0 | } |
140 | |
|
141 | |
public void setEncoder(PersistentPropertyDataEncoder encoder) |
142 | |
{ |
143 | 0 | _encoder = encoder; |
144 | 0 | } |
145 | |
} |