01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.internal.services;
16:
17: import java.util.Collection;
18: import java.util.Map;
19:
20: import org.apache.tapestry.ComponentResources;
21: import org.apache.tapestry.ioc.internal.util.CollectionFactory;
22: import org.apache.tapestry.ioc.internal.util.InternalUtils;
23: import org.apache.tapestry.model.ComponentModel;
24: import org.apache.tapestry.services.MetaDataLocator;
25: import org.apache.tapestry.services.PersistentFieldBundle;
26: import org.apache.tapestry.services.PersistentFieldChange;
27: import org.apache.tapestry.services.PersistentFieldManager;
28: import org.apache.tapestry.services.PersistentFieldStrategy;
29:
30: public class PersistentFieldManagerImpl implements
31: PersistentFieldManager {
32: public static final String META_KEY = "tapestry.persistence-strategy";
33:
34: public static final String DEFAULT_STRATEGY = "session";
35:
36: private final MetaDataLocator _metaDataLocator;
37:
38: private final Map<String, PersistentFieldStrategy> _strategies;
39:
40: public PersistentFieldManagerImpl(MetaDataLocator locator,
41: Map<String, PersistentFieldStrategy> strategies) {
42: _metaDataLocator = locator;
43:
44: _strategies = strategies;
45: }
46:
47: private PersistentFieldStrategy getStrategy(String strategyName) {
48: PersistentFieldStrategy result = _strategies.get(strategyName);
49:
50: if (result == null)
51: throw new RuntimeException(ServicesMessages
52: .unknownPersistentFieldStrategy(strategyName,
53: _strategies.keySet()));
54:
55: return result;
56: }
57:
58: public PersistentFieldBundle gatherChanges(String pageName) {
59: Collection<PersistentFieldChange> allChanges = CollectionFactory
60: .newList();
61:
62: for (PersistentFieldStrategy strategy : _strategies.values()) {
63: allChanges.addAll(strategy.gatherFieldChanges(pageName));
64: }
65:
66: return new PersistentFieldBundleImpl(allChanges);
67: }
68:
69: public void postChange(String pageName,
70: ComponentResources resources, String fieldName,
71: Object newValue) {
72: String strategyName = findStrategy(resources, fieldName);
73: PersistentFieldStrategy strategy = getStrategy(strategyName);
74:
75: strategy.postChange(pageName, resources.getNestedId(),
76: fieldName, newValue);
77: }
78:
79: private String findStrategy(ComponentResources resources,
80: String fieldName) {
81: ComponentModel model = resources.getComponentModel();
82:
83: String strategy = model.getFieldPersistenceStrategy(fieldName);
84:
85: if (InternalUtils.isNonBlank(strategy))
86: return strategy;
87:
88: return _metaDataLocator.findMeta(META_KEY, resources);
89: }
90: }
|