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.Iterator; |
19 |
|
|
20 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
21 |
|
import org.apache.hivemind.ErrorLog; |
22 |
|
import org.apache.hivemind.util.Defense; |
23 |
|
import org.apache.hivemind.util.PropertyUtils; |
24 |
|
import org.apache.tapestry.IComponent; |
25 |
|
import org.apache.tapestry.IPage; |
26 |
|
import org.apache.tapestry.engine.IPageRecorder; |
27 |
|
import org.apache.tapestry.event.ObservedChangeEvent; |
28 |
|
import org.apache.tapestry.spec.IPropertySpecification; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
public class PageRecorderImpl implements IPageRecorder |
35 |
|
{ |
36 |
|
|
37 |
|
private String _pageName; |
38 |
|
|
39 |
|
private PropertyPersistenceStrategySource _strategySource; |
40 |
|
|
41 |
7 |
private boolean _locked = false; |
42 |
|
|
43 |
|
private ErrorLog _log; |
44 |
|
|
45 |
|
public PageRecorderImpl(String pageName, PropertyPersistenceStrategySource strategySource, ErrorLog log) |
46 |
7 |
{ |
47 |
7 |
Defense.notNull(pageName, "pageName"); |
48 |
7 |
Defense.notNull(strategySource, "strategySource"); |
49 |
7 |
Defense.notNull(log, "log"); |
50 |
|
|
51 |
7 |
_pageName = pageName; |
52 |
7 |
_strategySource = strategySource; |
53 |
7 |
_log = log; |
54 |
7 |
} |
55 |
|
|
56 |
|
public void commit() |
57 |
|
{ |
58 |
1 |
_locked = true; |
59 |
1 |
} |
60 |
|
|
61 |
|
public boolean isLocked() |
62 |
|
{ |
63 |
0 |
return _locked; |
64 |
|
} |
65 |
|
|
66 |
|
public Collection getChanges() |
67 |
|
{ |
68 |
3 |
return _strategySource.getAllStoredChanges(_pageName); |
69 |
|
} |
70 |
|
|
71 |
|
public void rollback(IPage page) |
72 |
|
{ |
73 |
2 |
Collection changes = getChanges(); |
74 |
|
|
75 |
2 |
Iterator i = changes.iterator(); |
76 |
|
|
77 |
4 |
while(i.hasNext()) |
78 |
|
{ |
79 |
2 |
PropertyChange change = (PropertyChange) i.next(); |
80 |
|
|
81 |
2 |
applyChange(page, change); |
82 |
2 |
} |
83 |
2 |
} |
84 |
|
|
85 |
|
private void applyChange(IPage page, PropertyChange change) |
86 |
|
{ |
87 |
2 |
String idPath = change.getComponentPath(); |
88 |
|
|
89 |
2 |
IComponent component = (idPath == null) ? page : page.getNestedComponent(idPath); |
90 |
|
|
91 |
2 |
PropertyUtils.write(component, change.getPropertyName(), change.getNewValue()); |
92 |
2 |
} |
93 |
|
|
94 |
|
public void observeChange(ObservedChangeEvent event) |
95 |
|
{ |
96 |
3 |
IComponent component = event.getComponent(); |
97 |
3 |
String propertyName = event.getPropertyName(); |
98 |
|
|
99 |
3 |
if (_locked) |
100 |
|
{ |
101 |
1 |
_log.error(RecordMessages.recorderLocked(propertyName, component), null, null); |
102 |
1 |
return; |
103 |
|
} |
104 |
|
|
105 |
2 |
PropertyPersistenceStrategy strategy = findStrategy(component, propertyName); |
106 |
|
|
107 |
2 |
if (strategy != null) |
108 |
1 |
strategy.store(_pageName, component.getIdPath(), propertyName, event.getNewValue()); |
109 |
2 |
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
PropertyPersistenceStrategy findStrategy(IComponent component, String propertyName) |
114 |
|
{ |
115 |
|
|
116 |
|
|
117 |
3 |
IPropertySpecification propertySpecification = component.getSpecification().getPropertySpecification(propertyName); |
118 |
|
|
119 |
3 |
if (propertySpecification == null) |
120 |
|
{ |
121 |
1 |
_log.error(RecordMessages.missingPropertySpecification(propertyName, component), null, null); |
122 |
1 |
return null; |
123 |
|
} |
124 |
|
|
125 |
2 |
String name = propertySpecification.getPersistence(); |
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
try |
133 |
|
{ |
134 |
2 |
return _strategySource.getStrategy(name); |
135 |
|
} |
136 |
1 |
catch (ApplicationRuntimeException ex) |
137 |
|
{ |
138 |
1 |
_log.error(ex.getMessage(), propertySpecification.getLocation(), ex); |
139 |
1 |
return null; |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
} |