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