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 org.apache.tapestry.IPage;
018    import org.apache.tapestry.IRequestCycle;
019    import org.apache.tapestry.engine.ServiceEncoding;
020    
021    /**
022     * Defines the 'page' scope for persisting client properties. Persist the properties only if the
023     * current page name is the same as that of the property.
024     * 
025     * @author Mindbridge
026     * @since 4.0
027     * @see org.apache.tapestry.record.ClientPropertyPersistenceScope
028     */
029    public class PageClientPropertyPersistenceScope extends
030            AbstractPrefixedClientPropertyPersistenceScope
031    {
032        private IRequestCycle _requestCycle;
033    
034        public PageClientPropertyPersistenceScope()
035        {
036            super("state:");
037        }
038    
039        /**
040         * Returns true if the active page name matches the page for this property. This means that
041         * <em>after a new page has been activated</em>, the state is discarded.
042         */
043    
044        public boolean shouldEncodeState(ServiceEncoding encoding, String pageName,
045                PersistentPropertyData data)
046        {
047            IPage page = _requestCycle.getPage();
048    
049            // TAPESTRY-701: if you try to generate a link using, say, page or external service,
050            // from inside PageValidateListener.pageValidate(), then there may not be an active
051            // page yet. Seems like the right thing to do is hold onto any properties until
052            // we know what the active page is.  I know this one is going to cause a fight
053            // since its not clear whether keeping or discarding is the right way to go.
054            
055            if (page == null)
056                return true;
057    
058            return pageName.equals(page.getPageName());
059        }
060    
061        public void setRequestCycle(IRequestCycle requestCycle)
062        {
063            _requestCycle = requestCycle;
064        }
065    
066    }