001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.record;
016    
017    import java.util.ArrayList;
018    import java.util.Collection;
019    import java.util.HashMap;
020    import java.util.Iterator;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.hivemind.ApplicationRuntimeException;
025    import org.apache.tapestry.engine.ServiceEncoding;
026    
027    /**
028     * Implementation of the
029     * <code>tapestry.persist.PropertyPersistenceStrategySource</code> service.
030     * Allows access to other services, that implement the
031     * {@link org.apache.tapestry.record.PropertyPersistenceStrategy} interface.
032     * 
033     * @author Howard M. Lewis Ship
034     * @since 4.0
035     */
036    public class PropertyPersistenceStrategySourceImpl implements
037            PropertyPersistenceStrategySource
038    {
039    
040        // Set from tapestry.props.PersistenceStrategy
041        private List _contributions;
042    
043        private Map _strategies = new HashMap();
044    
045        public void initializeService()
046        {
047            Iterator i = _contributions.iterator();
048            while(i.hasNext())
049            {
050                PropertyPersistenceStrategyContribution c = (PropertyPersistenceStrategyContribution) i
051                        .next();
052    
053                _strategies.put(c.getName(), c.getStrategy());
054            }
055        }
056    
057        public PropertyPersistenceStrategy getStrategy(String name)
058        {
059            if (!_strategies.containsKey(name))
060                throw new ApplicationRuntimeException(RecordMessages
061                        .unknownPersistenceStrategy(name));
062    
063            return (PropertyPersistenceStrategy) _strategies.get(name);
064        }
065    
066        public Collection getAllStoredChanges(String pageName)
067        {
068            Collection result = new ArrayList();
069    
070            Iterator i = _strategies.values().iterator();
071    
072            while(i.hasNext())
073            {
074                PropertyPersistenceStrategy s = (PropertyPersistenceStrategy) i
075                        .next();
076    
077                result.addAll(s.getStoredChanges(pageName));
078            }
079    
080            return result;
081        }
082    
083        public void discardAllStoredChanged(String pageName)
084        {
085            Iterator i = _strategies.values().iterator();
086    
087            while(i.hasNext())
088            {
089                PropertyPersistenceStrategy s = (PropertyPersistenceStrategy) i
090                        .next();
091    
092                s.discardStoredChanges(pageName);
093            }
094        }
095    
096        public void addParametersForPersistentProperties(ServiceEncoding encoding,
097                boolean post)
098        {
099            Iterator i = _strategies.values().iterator();
100    
101            while(i.hasNext())
102            {
103                PropertyPersistenceStrategy s = (PropertyPersistenceStrategy) i
104                        .next();
105    
106                s.addParametersForPersistentProperties(encoding, post);
107            }
108        }
109    
110        public void setContributions(List contributions)
111        {
112            _contributions = contributions;
113        }
114    }