Coverage Report - org.apache.tapestry5.internal.services.AbstractSessionPersistentFieldStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSessionPersistentFieldStrategy
100%
42/42
94%
17/18
0
 
 1  
 // Copyright 2007, 2008 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.apache.tapestry5.internal.services;
 16  
 
 17  
 import static org.apache.tapestry5.ioc.internal.util.CollectionFactory.newList;
 18  
 import static org.apache.tapestry5.ioc.internal.util.Defense.notBlank;
 19  
 import org.apache.tapestry5.services.PersistentFieldChange;
 20  
 import org.apache.tapestry5.services.PersistentFieldStrategy;
 21  
 import org.apache.tapestry5.services.Request;
 22  
 import org.apache.tapestry5.services.Session;
 23  
 
 24  
 import java.util.Collection;
 25  
 import java.util.Collections;
 26  
 import java.util.List;
 27  
 
 28  
 /**
 29  
  * Base class for strategies that store their values as keys in the session. Implements a uniform format for the keys,
 30  
  * based on a prefix to identify the particular strategy.
 31  
  */
 32  
 public abstract class AbstractSessionPersistentFieldStrategy implements PersistentFieldStrategy
 33  
 {
 34  
     private final String prefix;
 35  
 
 36  
     private final Request request;
 37  
 
 38  
     protected AbstractSessionPersistentFieldStrategy(String prefix, Request request)
 39  56
     {
 40  56
         this.prefix = prefix;
 41  56
         this.request = request;
 42  56
     }
 43  
 
 44  
     public final Collection<PersistentFieldChange> gatherFieldChanges(String pageName)
 45  
     {
 46  3638
         Session session = request.getSession(false);
 47  
 
 48  3638
         if (session == null) return Collections.emptyList();
 49  
 
 50  3520
         List<PersistentFieldChange> result = newList();
 51  
 
 52  3520
         String fullPrefix = prefix + pageName + ":";
 53  
 
 54  3520
         for (String name : session.getAttributeNames(fullPrefix))
 55  
         {
 56  1604
             Object persistedValue = session.getAttribute(name);
 57  
 
 58  1604
             Object applicationValue = persistedValue == null ? null : convertPersistedToApplicationValue(
 59  
                     persistedValue);
 60  
 
 61  1604
             PersistentFieldChange change = buildChange(name, applicationValue);
 62  
 
 63  1604
             result.add(change);
 64  
 
 65  1604
             didReadChange(session, name);
 66  1604
         }
 67  
 
 68  3520
         return result;
 69  
     }
 70  
 
 71  
     public void discardChanges(String pageName)
 72  
     {
 73  8
         Session session = request.getSession(false);
 74  
 
 75  8
         if (session == null) return;
 76  
 
 77  6
         String fullPrefix = prefix + pageName + ":";
 78  
 
 79  6
         for (String name : session.getAttributeNames(fullPrefix))
 80  
         {
 81  4
             session.setAttribute(name, null);
 82  
         }
 83  6
     }
 84  
 
 85  
     /**
 86  
      * Called after each key is read by {@link #gatherFieldChanges(String)}. This implementation does nothing,
 87  
      * subclasses may override.
 88  
      *
 89  
      * @param session       the session from which a value was just read
 90  
      * @param attributeName the name of the attribute used to read a value
 91  
      */
 92  
     protected void didReadChange(Session session, String attributeName)
 93  
     {
 94  656
     }
 95  
 
 96  
     private PersistentFieldChange buildChange(String name, Object newValue)
 97  
     {
 98  1604
         String[] chunks = name.split(":");
 99  
 
 100  
         // Will be empty string for the root component
 101  1604
         String componentId = chunks[2];
 102  1604
         String fieldName = chunks[3];
 103  
 
 104  1604
         return new PersistentFieldChangeImpl(componentId, fieldName, newValue);
 105  
     }
 106  
 
 107  
     public final void postChange(String pageName, String componentId, String fieldName,
 108  
                                  Object newValue)
 109  
     {
 110  774
         notBlank(pageName, "pageName");
 111  774
         notBlank(fieldName, "fieldName");
 112  
 
 113  774
         Object persistedValue = newValue == null ? null : convertApplicationValueToPersisted(newValue);
 114  
 
 115  774
         StringBuilder builder = new StringBuilder(prefix);
 116  774
         builder.append(pageName);
 117  774
         builder.append(':');
 118  
 
 119  774
         if (componentId != null) builder.append(componentId);
 120  
 
 121  774
         builder.append(':');
 122  774
         builder.append(fieldName);
 123  
 
 124  774
         Session session = request.getSession(persistedValue != null);
 125  
 
 126  
         // TAPESTRY-2308: The session will be false when newValue is null and the session
 127  
         // does not already exist.
 128  
 
 129  774
         if (session != null)
 130  
         {
 131  774
             session.setAttribute(builder.toString(), persistedValue);
 132  
         }
 133  774
     }
 134  
 
 135  
     /**
 136  
      * Hook that allows a value to be converted as it is written to the session. Passed the new value provided by the
 137  
      * application, returns the object to be stored in the session. This implementation simply returns the provided
 138  
      * value.
 139  
      *
 140  
      * @param newValue non-null value
 141  
      * @return persisted value
 142  
      * @see #convertPersistedToApplicationValue(Object)
 143  
      */
 144  
     protected Object convertApplicationValueToPersisted(Object newValue)
 145  
     {
 146  742
         return newValue;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Converts a persisted value stored in the session back into an application value.   This implementation returns
 151  
      * the persisted value as is.
 152  
      *
 153  
      * @param persistedValue non-null persisted value
 154  
      * @return application value
 155  
      * @see #convertPersistedToApplicationValue(Object)
 156  
      */
 157  
     protected Object convertPersistedToApplicationValue(Object persistedValue)
 158  
     {
 159  916
         return persistedValue;
 160  
     }
 161  
 }