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 |
| public static final String PREFIX = "state:"; |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| private final Map _data = new HashMap(); |
52 |
| |
53 |
| private final PersistentPropertyDataEncoder _encoder; |
54 |
| |
55 |
| private WebRequest _request; |
56 |
| |
57 |
133
| public ClientPropertyPersistenceStrategy()
|
58 |
| { |
59 |
133
| this(new PersistentPropertyDataEncoderImpl());
|
60 |
| } |
61 |
| |
62 |
| |
63 |
134
| ClientPropertyPersistenceStrategy(PersistentPropertyDataEncoder encoder)
|
64 |
| { |
65 |
134
| _encoder = encoder;
|
66 |
| } |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
132
| public void initializeService()
|
77 |
| { |
78 |
132
| List names = _request.getParameterNames();
|
79 |
132
| Iterator i = names.iterator();
|
80 |
132
| while (i.hasNext())
|
81 |
| { |
82 |
371
| String name = (String) i.next();
|
83 |
| |
84 |
371
| if (!name.startsWith(PREFIX))
|
85 |
369
| continue;
|
86 |
| |
87 |
2
| String pageName = name.substring(PREFIX.length());
|
88 |
2
| String encoded = _request.getParameterValue(name);
|
89 |
| |
90 |
2
| PersistentPropertyData data = new PersistentPropertyData(_encoder);
|
91 |
2
| data.storeEncoded(encoded);
|
92 |
| |
93 |
2
| _data.put(pageName, data);
|
94 |
| } |
95 |
| } |
96 |
| |
97 |
1
| public void store(String pageName, String idPath, String propertyName, Object newValue)
|
98 |
| { |
99 |
1
| PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
|
100 |
1
| if (data == null)
|
101 |
| { |
102 |
1
| data = new PersistentPropertyData(_encoder);
|
103 |
1
| _data.put(pageName, data);
|
104 |
| } |
105 |
| |
106 |
1
| data.store(idPath, propertyName, newValue);
|
107 |
| } |
108 |
| |
109 |
191
| public Collection getStoredChanges(String pageName, IRequestCycle cycle)
|
110 |
| { |
111 |
191
| PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
|
112 |
| |
113 |
191
| if (data == null)
|
114 |
189
| return Collections.EMPTY_LIST;
|
115 |
| |
116 |
2
| return data.getPageChanges();
|
117 |
| } |
118 |
| |
119 |
1
| public void discardStoredChanges(String pageName, IRequestCycle cycle)
|
120 |
| { |
121 |
1
| _data.remove(pageName);
|
122 |
| } |
123 |
| |
124 |
140
| public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle)
|
125 |
| { |
126 |
140
| Defense.notNull(encoding, "encoding");
|
127 |
140
| Defense.notNull(cycle, "cycle");
|
128 |
| |
129 |
140
| Iterator i = _data.entrySet().iterator();
|
130 |
140
| while (i.hasNext())
|
131 |
| { |
132 |
1
| Map.Entry e = (Map.Entry) i.next();
|
133 |
| |
134 |
1
| String pageName = (String) e.getKey();
|
135 |
1
| PersistentPropertyData data = (PersistentPropertyData) e.getValue();
|
136 |
| |
137 |
1
| encoding.setParameterValue(PREFIX + pageName, data.getEncoded());
|
138 |
| } |
139 |
| } |
140 |
| |
141 |
132
| public void setRequest(WebRequest request)
|
142 |
| { |
143 |
132
| _request = request;
|
144 |
| } |
145 |
| } |