Coverage Report - org.trails.record.AbstractSessionPropertyPersistenceStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSessionPropertyPersistenceStrategy
0% 
0% 
1.444
 
 1  0
 // Copyright 2005 The Apache Software Foundation
 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 org.trails.record;
 16  
 
 17  
 import org.apache.commons.logging.Log;
 18  
 import org.apache.commons.logging.LogFactory;
 19  
 import org.apache.hivemind.util.Defense;
 20  
 import org.apache.tapestry.engine.ServiceEncoding;
 21  
 import org.apache.tapestry.record.PropertyChange;
 22  
 import org.apache.tapestry.record.PropertyPersistenceStrategy;
 23  
 import org.apache.tapestry.record.RecordUtils;
 24  
 import org.apache.tapestry.record.WebSessionAttributeCallback;
 25  
 import org.apache.tapestry.web.WebRequest;
 26  
 import org.apache.tapestry.web.WebSession;
 27  
 
 28  
 import java.util.ArrayList;
 29  
 import java.util.Collection;
 30  
 import java.util.Collections;
 31  
 
 32  
 /**
 33  
  * A basic {@link org.apache.tapestry.record.PropertyPersistenceStrategy},
 34  
  * which stores properties in the HttpSession as attributes. This is a clone of
 35  
  * {@link org.apache.tapestry.record.SessionPropertyPersistenceStrategy} with one difference, it allows you to specify
 36  
  * a different strategyId
 37  
  */
 38  0
 public abstract class AbstractSessionPropertyPersistenceStrategy implements PropertyPersistenceStrategy
 39  
 {
 40  0
         private static final Log LOG = LogFactory.getLog(AbstractSessionPropertyPersistenceStrategy.class);
 41  
 
 42  
         private String applicationId;
 43  
         private WebRequest request;
 44  
 
 45  
         protected abstract String getStrategyId();
 46  
 
 47  
         // Really, the name of the servlet; used as a prefix on all
 48  
         // HttpSessionAttribute keys
 49  
         // to keep things straight if multiple Tapestry apps are deployed
 50  
         // in the same WAR.
 51  
 
 52  
         public void store(String pageName, String idPath, String propertyName, Object newValue)
 53  
         {
 54  0
                 Defense.notNull(pageName, "pageName");
 55  0
                 Defense.notNull(propertyName, "propertyName");
 56  0
                 WebSession session = request.getSession(true);
 57  0
                 String attributeName = RecordUtils.buildChangeKey(getStrategyId(), applicationId, pageName, idPath, propertyName);
 58  0
                 session.setAttribute(attributeName, newValue);
 59  0
         }
 60  
 
 61  
         public Collection getStoredChanges(String pageName)
 62  
         {
 63  0
                 Defense.notNull(pageName, "pageName");
 64  
 
 65  0
                 WebSession session = request.getSession(false);
 66  
 
 67  0
                 if (session == null)
 68  0
                         return Collections.EMPTY_LIST;
 69  
 
 70  0
                 final Collection result = new ArrayList();
 71  
 
 72  0
                 WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
 73  
                 {
 74  
                         public void handleAttribute(WebSession sess, String name)
 75  
                         {
 76  0
                                 PropertyChange change = RecordUtils.buildChange(name, sess.getAttribute(name));
 77  
 
 78  0
                                 result.add(change);
 79  0
                         }
 80  
                 };
 81  
 
 82  0
                 RecordUtils.iterateOverMatchingAttributes(getStrategyId(), applicationId, pageName, session, callback);
 83  
 
 84  0
                 return result;
 85  
         }
 86  
 
 87  
         public void discardStoredChanges(String pageName)
 88  
         {
 89  0
                 WebSession session = request.getSession(false);
 90  
 
 91  0
                 if (session == null) return;
 92  
 
 93  0
                 WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
 94  
                 {
 95  
                         public void handleAttribute(WebSession sess, String name)
 96  
                         {
 97  0
                                 sess.setAttribute(name, null);
 98  0
                         }
 99  
                 };
 100  
 
 101  0
                 RecordUtils.iterateOverMatchingAttributes(getStrategyId(), applicationId, pageName, session, callback);
 102  0
         }
 103  
 
 104  
         /**
 105  
          * Does nothing; session persistence does not make use of query parameters.
 106  
          */
 107  
 
 108  
         public void addParametersForPersistentProperties(ServiceEncoding encoding,
 109  
                                                                                                          boolean post)
 110  
         {
 111  0
         }
 112  
 
 113  
         public void setApplicationId(String applicationName)
 114  
         {
 115  0
                 this.applicationId = applicationName;
 116  0
         }
 117  
 
 118  
         public void setRequest(WebRequest request)
 119  
         {
 120  0
                 this.request = request;
 121  0
         }
 122  
 }