|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
PageRecorderImpl.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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.IRequestCycle;
|
|
27 |
import org.apache.tapestry.engine.IPageRecorder;
|
|
28 |
import org.apache.tapestry.event.ObservedChangeEvent;
|
|
29 |
import org.apache.tapestry.spec.IPropertySpecification;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* @author Howard M. Lewis Ship
|
|
33 |
* @since 4.0
|
|
34 |
*/
|
|
35 |
public class PageRecorderImpl implements IPageRecorder |
|
36 |
{ |
|
37 |
private String _pageName;
|
|
38 |
|
|
39 |
private IRequestCycle _requestCycle;
|
|
40 |
|
|
41 |
private PropertyPersistenceStrategySource _strategySource;
|
|
42 |
|
|
43 |
private boolean _locked = false; |
|
44 |
|
|
45 |
private ErrorLog _log;
|
|
46 |
|
|
47 | 219 |
public PageRecorderImpl(String pageName, IRequestCycle requestCycle,
|
48 |
PropertyPersistenceStrategySource strategySource, ErrorLog log) |
|
49 |
{ |
|
50 | 219 |
Defense.notNull(pageName, "pageName");
|
51 | 219 |
Defense.notNull(requestCycle, "requestCycle");
|
52 | 219 |
Defense.notNull(strategySource, "strategySource");
|
53 | 219 |
Defense.notNull(log, "log");
|
54 |
|
|
55 | 219 |
_pageName = pageName; |
56 | 219 |
_requestCycle = requestCycle; |
57 | 219 |
_strategySource = strategySource; |
58 | 219 |
_log = log; |
59 |
} |
|
60 |
|
|
61 | 224 |
public void commit() |
62 |
{ |
|
63 | 224 |
_locked = true;
|
64 |
} |
|
65 |
|
|
66 | 215 |
public Collection getChanges()
|
67 |
{ |
|
68 | 215 |
return _strategySource.getAllStoredChanges(_pageName, _requestCycle);
|
69 |
} |
|
70 |
|
|
71 | 214 |
public void rollback(IPage page) |
72 |
{ |
|
73 | 214 |
Collection changes = getChanges(); |
74 |
|
|
75 | 214 |
Iterator i = changes.iterator(); |
76 |
|
|
77 | 214 |
while (i.hasNext())
|
78 |
{ |
|
79 | 24 |
PropertyChange change = (PropertyChange) i.next(); |
80 |
|
|
81 | 24 |
applyChange(page, change); |
82 |
} |
|
83 |
} |
|
84 |
|
|
85 | 24 |
private void applyChange(IPage page, PropertyChange change) |
86 |
{ |
|
87 | 24 |
String idPath = change.getComponentPath(); |
88 |
|
|
89 | 24 |
IComponent component = (idPath == null) ? page : page.getNestedComponent(idPath);
|
90 |
|
|
91 | 24 |
PropertyUtils.write(component, change.getPropertyName(), change.getNewValue()); |
92 |
} |
|
93 |
|
|
94 | 33 |
public void observeChange(ObservedChangeEvent event) |
95 |
{ |
|
96 | 33 |
IComponent component = event.getComponent(); |
97 | 33 |
String propertyName = event.getPropertyName(); |
98 |
|
|
99 | 33 |
if (_locked)
|
100 |
{ |
|
101 | 1 |
_log.error(RecordMessages.recorderLocked(propertyName, component), null, null); |
102 | 1 |
return;
|
103 |
} |
|
104 |
|
|
105 | 32 |
PropertyPersistenceStrategy strategy = findStrategy(component, propertyName); |
106 |
|
|
107 | 32 |
if (strategy != null) |
108 | 31 |
strategy.store(_pageName, component.getIdPath(), propertyName, event.getNewValue()); |
109 |
} |
|
110 |
|
|
111 |
// package private for testing
|
|
112 |
|
|
113 | 33 |
PropertyPersistenceStrategy findStrategy(IComponent component, String propertyName) |
114 |
{ |
|
115 |
// So much for Law of Demeter!
|
|
116 |
|
|
117 | 33 |
IPropertySpecification propertySpecification = component.getSpecification() |
118 |
.getPropertySpecification(propertyName); |
|
119 |
|
|
120 | 33 |
if (propertySpecification == null) |
121 |
{ |
|
122 | 1 |
_log.error( |
123 |
RecordMessages.missingPropertySpecification(propertyName, component), |
|
124 |
null,
|
|
125 |
null);
|
|
126 | 1 |
return null; |
127 |
} |
|
128 |
|
|
129 | 32 |
String name = propertySpecification.getPersistence(); |
130 |
|
|
131 |
// Should check for nulls, but the architecture of the framework pretty much
|
|
132 |
// ensures that we won't get here unless there is a property
|
|
133 |
// and a persistence value for the property.
|
|
134 |
|
|
135 | 32 |
try
|
136 |
{ |
|
137 | 32 |
return _strategySource.getStrategy(name);
|
138 |
} |
|
139 |
catch (ApplicationRuntimeException ex)
|
|
140 |
{ |
|
141 | 1 |
_log.error(ex.getMessage(), propertySpecification.getLocation(), ex); |
142 |
|
|
143 | 1 |
return null; |
144 |
} |
|
145 |
} |
|
146 |
|
|
147 |
} |
|