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.IRequestCycle;
026    import org.apache.tapestry.engine.ServiceEncoding;
027    
028    /**
029     * Implementation of the <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 PropertyPersistenceStrategySource
037    {
038        // Set from tapestry.props.PersistenceStrategy
039        private List _contributions;
040    
041        private Map _strategies = new HashMap();
042    
043        public void initializeService()
044        {
045            Iterator i = _contributions.iterator();
046            while (i.hasNext())
047            {
048                PropertyPersistenceStrategyContribution c = (PropertyPersistenceStrategyContribution) i
049                        .next();
050    
051                _strategies.put(c.getName(), c.getStrategy());
052            }
053        }
054    
055        public PropertyPersistenceStrategy getStrategy(String name)
056        {
057            if (!_strategies.containsKey(name))
058                throw new ApplicationRuntimeException(RecordMessages.unknownPersistenceStrategy(name));
059    
060            return (PropertyPersistenceStrategy) _strategies.get(name);
061        }
062    
063        public Collection getAllStoredChanges(String pageName, IRequestCycle cycle)
064        {
065            Collection result = new ArrayList();
066    
067            Iterator i = _strategies.values().iterator();
068    
069            while (i.hasNext())
070            {
071                PropertyPersistenceStrategy s = (PropertyPersistenceStrategy) i.next();
072    
073                result.addAll(s.getStoredChanges(pageName, cycle));
074            }
075    
076            return result;
077        }
078    
079        public void discardAllStoredChanged(String pageName, IRequestCycle cycle)
080        {
081            Iterator i = _strategies.values().iterator();
082    
083            while (i.hasNext())
084            {
085                PropertyPersistenceStrategy s = (PropertyPersistenceStrategy) i.next();
086    
087                s.discardStoredChanges(pageName, cycle);
088            }
089        }
090    
091        public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle, boolean post)
092        {
093            Iterator i = _strategies.values().iterator();
094    
095            while (i.hasNext())
096            {
097                PropertyPersistenceStrategy s = (PropertyPersistenceStrategy) i.next();
098    
099                s.addParametersForPersistentProperties(encoding, cycle, post);
100            }
101        }
102    
103        public void setContributions(List contributions)
104        {
105            _contributions = contributions;
106        }
107    }