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.ArrayList; |
18 |
|
import java.util.HashMap; |
19 |
|
import java.util.Iterator; |
20 |
|
import java.util.List; |
21 |
|
import java.util.Map; |
22 |
|
|
23 |
|
import org.apache.hivemind.util.Defense; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
public class PersistentPropertyData |
34 |
|
{ |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
private Map _changes; |
42 |
|
|
43 |
|
private String _encoded; |
44 |
|
|
45 |
|
private final PersistentPropertyDataEncoder _encoder; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
public PersistentPropertyData(PersistentPropertyDataEncoder encoder) |
53 |
8 |
{ |
54 |
8 |
Defense.notNull(encoder, "encoder"); |
55 |
|
|
56 |
8 |
_encoder = encoder; |
57 |
8 |
_changes = new HashMap(); |
58 |
8 |
} |
59 |
|
|
60 |
|
public String getEncoded() |
61 |
|
{ |
62 |
4 |
if (_encoded == null) _encoded = encode(); |
63 |
|
|
64 |
4 |
return _encoded; |
65 |
|
} |
66 |
|
|
67 |
|
public List getPageChanges() |
68 |
|
{ |
69 |
6 |
if (_changes == null) |
70 |
|
{ |
71 |
2 |
List pageChanges = _encoder.decodePageChanges(_encoded); |
72 |
|
|
73 |
2 |
_changes = decode(pageChanges); |
74 |
|
|
75 |
2 |
return pageChanges; |
76 |
|
} |
77 |
|
|
78 |
4 |
return createPageChangeList(); |
79 |
|
} |
80 |
|
|
81 |
|
public void store(String componentPath, String propertyName, Object newValue) |
82 |
|
{ |
83 |
4 |
Defense.notNull(propertyName, "propertyName"); |
84 |
|
|
85 |
4 |
if (_changes == null) |
86 |
0 |
_changes = decode(_encoder.decodePageChanges(_encoded)); |
87 |
|
|
88 |
4 |
ChangeKey key = new ChangeKey(componentPath, propertyName); |
89 |
|
|
90 |
4 |
_changes.put(key, newValue); |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
4 |
_encoded = null; |
96 |
4 |
} |
97 |
|
|
98 |
|
public void storeEncoded(String encoded) |
99 |
|
{ |
100 |
5 |
Defense.notNull(encoded, "encoded"); |
101 |
|
|
102 |
5 |
_encoded = encoded; |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
5 |
_changes = null; |
107 |
5 |
} |
108 |
|
|
109 |
|
private List createPageChangeList() |
110 |
|
{ |
111 |
5 |
List result = new ArrayList(); |
112 |
|
|
113 |
5 |
Iterator i = _changes.entrySet().iterator(); |
114 |
|
|
115 |
10 |
while(i.hasNext()) |
116 |
|
{ |
117 |
5 |
Map.Entry me = (Map.Entry) i.next(); |
118 |
|
|
119 |
5 |
ChangeKey changeKey = (ChangeKey) me.getKey(); |
120 |
5 |
Object value = me.getValue(); |
121 |
|
|
122 |
5 |
PropertyChange change = new PropertyChangeImpl(changeKey.getComponentPath(), changeKey.getPropertyName(), value); |
123 |
|
|
124 |
5 |
result.add(change); |
125 |
5 |
} |
126 |
|
|
127 |
5 |
return result; |
128 |
|
} |
129 |
|
|
130 |
|
private String encode() |
131 |
|
{ |
132 |
1 |
List changes = createPageChangeList(); |
133 |
|
|
134 |
1 |
return _encoder.encodePageChanges(changes); |
135 |
|
} |
136 |
|
|
137 |
|
private Map decode(List pageChanges) |
138 |
|
{ |
139 |
2 |
Map result = new HashMap(); |
140 |
|
|
141 |
2 |
Iterator i = pageChanges.iterator(); |
142 |
4 |
while(i.hasNext()) |
143 |
|
{ |
144 |
2 |
PropertyChange pc = (PropertyChange) i.next(); |
145 |
|
|
146 |
2 |
String propertyName = pc.getPropertyName(); |
147 |
2 |
String componentPath = pc.getComponentPath(); |
148 |
|
|
149 |
2 |
ChangeKey key = new ChangeKey(componentPath, propertyName); |
150 |
|
|
151 |
2 |
result.put(key, pc.getNewValue()); |
152 |
2 |
} |
153 |
|
|
154 |
2 |
return result; |
155 |
|
} |
156 |
|
} |