Coverage Report - com.javaforge.tapestry.flash.FlashPropertyPersistenceStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
FlashPropertyPersistenceStrategy
100%
26/26
N/A
1.5
FlashPropertyPersistenceStrategy$1
100%
5/5
N/A
1.5
FlashPropertyPersistenceStrategy$2
100%
3/3
N/A
1.5
 
 1  
 // Copyright 2005 Howard M. Lewis Ship
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // 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 com.javaforge.tapestry.flash;
 16  
 
 17  
 import java.util.ArrayList;
 18  
 import java.util.Collection;
 19  
 import java.util.Collections;
 20  
 
 21  
 import org.apache.hivemind.util.Defense;
 22  
 import org.apache.tapestry.engine.ServiceEncoding;
 23  
 import org.apache.tapestry.record.PropertyChange;
 24  
 import org.apache.tapestry.record.PropertyPersistenceStrategy;
 25  
 import org.apache.tapestry.record.RecordUtils;
 26  
 import org.apache.tapestry.record.WebSessionAttributeCallback;
 27  
 import org.apache.tapestry.web.WebRequest;
 28  
 import org.apache.tapestry.web.WebSession;
 29  
 
 30  
 /**
 31  
  * A variation of {@link org.apache.tapestry.record.SessionPropertyPersistenceStrategy} where values
 32  
  * are stored in the session just until used to rewind a page's state, at which point they are
 33  
  * removed. This concept is borrowed from Ruby-On-Rails, in effect.
 34  
  * 
 35  
  * @author Howard M. Lewis Ship
 36  
  */
 37  11
 public class FlashPropertyPersistenceStrategy implements PropertyPersistenceStrategy
 38  
 {
 39  
     public static final String STRATEGY_ID = "flash";
 40  
 
 41  
     // Really, the name of the servlet; used as a prefix on all HttpSessionAttribute keys
 42  
     // to keep things straight if multiple Tapestry apps are deployed
 43  
     // in the same WAR.
 44  
 
 45  
     private String _applicationId;
 46  
 
 47  
     private WebRequest _request;
 48  
 
 49  
     public void store(String pageName, String idPath, String propertyName, Object newValue)
 50  
     {
 51  3
         Defense.notNull(pageName, "pageName");
 52  3
         Defense.notNull(propertyName, "propertyName");
 53  
 
 54  3
         WebSession session = _request.getSession(true);
 55  
 
 56  3
         String attributeName = RecordUtils.buildChangeKey(
 57  
                 STRATEGY_ID,
 58  
                 _applicationId,
 59  
                 pageName,
 60  
                 idPath,
 61  
                 propertyName);
 62  
 
 63  3
         session.setAttribute(attributeName, newValue);
 64  3
     }
 65  
 
 66  
     public Collection getStoredChanges(String pageName)
 67  
     {
 68  4
         Defense.notNull(pageName, "pageName");
 69  
 
 70  4
         WebSession session = _request.getSession(false);
 71  
 
 72  4
         if (session == null)
 73  1
             return Collections.EMPTY_LIST;
 74  
 
 75  3
         final Collection result = new ArrayList();
 76  
 
 77  3
         WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
 78  
         {
 79  3
             public void handleAttribute(WebSession session, String name)
 80  
             {
 81  2
                 PropertyChange change = RecordUtils.buildChange(name, session.getAttribute(name));
 82  
 
 83  2
                 result.add(change);
 84  
 
 85  
                 // The only significant change between SessionPropertyPersistenceStorage and this
 86  
                 // class as we retrieve PropertyChanges to be used to rollback page state, we remove
 87  
                 // those changes from the WebSession
 88  
 
 89  2
                 session.setAttribute(name, null);
 90  2
             }
 91  
         };
 92  
 
 93  3
         RecordUtils.iterateOverMatchingAttributes(
 94  
                 STRATEGY_ID,
 95  
                 _applicationId,
 96  
                 pageName,
 97  
                 session,
 98  
                 callback);
 99  
 
 100  3
         return result;
 101  
     }
 102  
 
 103  
     public void discardStoredChanges(String pageName)
 104  
     {
 105  3
         WebSession session = _request.getSession(false);
 106  
 
 107  3
         if (session == null)
 108  1
             return;
 109  
 
 110  2
         WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
 111  
         {
 112  2
             public void handleAttribute(WebSession session, String name)
 113  
             {
 114  1
                 session.setAttribute(name, null);
 115  1
             }
 116  
         };
 117  
 
 118  2
         RecordUtils.iterateOverMatchingAttributes(
 119  
                 STRATEGY_ID,
 120  
                 _applicationId,
 121  
                 pageName,
 122  
                 session,
 123  
                 callback);
 124  2
     }
 125  
 
 126  
     /**
 127  
      * Does nothing; session persistence does not make use of query parameters.
 128  
      */
 129  
 
 130  
     public void addParametersForPersistentProperties(ServiceEncoding encoding, boolean post)
 131  
     {
 132  1
     }
 133  
 
 134  
     public void setApplicationId(String applicationName)
 135  
     {
 136  8
         _applicationId = applicationName;
 137  8
     }
 138  
 
 139  
     public void setRequest(WebRequest request)
 140  
     {
 141  10
         _request = request;
 142  10
     }
 143  
 }