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 <code>tapestry.persist.PropertyPersistenceStrategySource</code> service.
029     * Allows access to other services, that implement the
030     * {@link org.apache.tapestry.record.PropertyPersistenceStrategy} interface.
031     * 
032     * @author Howard M. Lewis Ship
033     * @since 4.0
034     */
035    public class PropertyPersistenceStrategySourceImpl implements PropertyPersistenceStrategySource
036    {
037        // Set from tapestry.props.PersistenceStrategy
038        private List _contributions;
039    
040        private Map _strategies = new HashMap();
041    
042        public void initializeService()
043        {
044            Iterator i = _contributions.iterator();
045            while (i.hasNext())
046            {
047                PropertyPersistenceStrategyContribution c = (PropertyPersistenceStrategyContribution) i
048                        .next();
049    
050                _strategies.put(c.getName(), c.getStrategy());
051            }
052        }
053    
054        public PropertyPersistenceStrategy getStrategy(String name)
055        {
056            if (!_strategies.containsKey(name))
057                throw new ApplicationRuntimeException(RecordMessages.unknownPersistenceStrategy(name));
058    
059            return (PropertyPersistenceStrategy) _strategies.get(name);
060        }
061    
062        public Collection getAllStoredChanges(String pageName)
063        {
064            Collection result = new ArrayList();
065    
066            Iterator i = _strategies.values().iterator();
067    
068            while (i.hasNext())
069            {
070                PropertyPersistenceStrategy s = (PropertyPersistenceStrategy) i.next();
071    
072                result.addAll(s.getStoredChanges(pageName));
073            }
074    
075            return result;
076        }
077    
078        public void discardAllStoredChanged(String pageName)
079        {
080            Iterator i = _strategies.values().iterator();
081    
082            while (i.hasNext())
083            {
084                PropertyPersistenceStrategy s = (PropertyPersistenceStrategy) i.next();
085    
086                s.discardStoredChanges(pageName);
087            }
088        }
089    
090        public void addParametersForPersistentProperties(ServiceEncoding encoding, boolean post)
091        {
092            Iterator i = _strategies.values().iterator();
093    
094            while (i.hasNext())
095            {
096                PropertyPersistenceStrategy s = (PropertyPersistenceStrategy) i.next();
097    
098                s.addParametersForPersistentProperties(encoding, post);
099            }
100        }
101    
102        public void setContributions(List contributions)
103        {
104            _contributions = contributions;
105        }
106    }