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