Coverage Report - org.apache.tapestry.record.ClientPropertyPersistenceStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
ClientPropertyPersistenceStrategy
100% 
100% 
1.778
 
 1  
 // 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.apache.tapestry.record;
 16  
 
 17  
 import org.apache.hivemind.util.Defense;
 18  
 import org.apache.tapestry.engine.ServiceEncoding;
 19  
 import org.apache.tapestry.web.WebRequest;
 20  
 
 21  
 import java.util.*;
 22  
 
 23  
 /**
 24  
  * Service tapestry.persist.ClientPropertyPersistenceStrategy. Encodes persistent page properties on
 25  
  * the client as query parameters.
 26  
  * <p>
 27  
  * Uses the threaded model.
 28  
  * 
 29  
  * @author Howard M. Lewis Ship
 30  
  * @since 4.0
 31  
  * @see org.apache.tapestry.engine.ILink
 32  
  */
 33  5
 public class ClientPropertyPersistenceStrategy implements PropertyPersistenceStrategy
 34  
 {
 35  
     /**
 36  
      * Keyed on page name (String), values are
 37  
      * {@link org.apache.tapestry.record.PersistentPropertyData}.
 38  
      */
 39  5
     protected final Map _data = new LinkedHashMap();
 40  
 
 41  
     protected PersistentPropertyDataEncoder _encoder;
 42  
 
 43  
     protected WebRequest _request;
 44  
 
 45  
     protected ClientPropertyPersistenceScope _scope;
 46  
 
 47  
     /**
 48  
      * Initializer for this service, invoked every time a service instance is created. This
 49  
      * initializer pulls out of the request and query parameters whose prefix is "client:" and
 50  
      * expects them to be encoded {@link PersistentPropertyData}, which are stored internally.
 51  
      * Because the service model is threaded, this information is specific to a single request, and
 52  
      * will be discarded at the end of the request.
 53  
      */
 54  
 
 55  
     public void initializeService()
 56  
     {
 57  3
         List names = _request.getParameterNames();
 58  3
         Iterator i = names.iterator();
 59  10
         while (i.hasNext())
 60  
         {
 61  7
             String name = (String) i.next();
 62  
 
 63  7
             if (!_scope.isParameterForScope(name))
 64  3
                 continue;
 65  
 
 66  4
             String pageName = _scope.extractPageName(name);
 67  
 
 68  4
             String encoded = _request.getParameterValue(name);
 69  
 
 70  4
             PersistentPropertyData data = new PersistentPropertyData(_encoder);
 71  4
             data.storeEncoded(encoded);
 72  
 
 73  4
             _data.put(pageName, data);
 74  4
         }
 75  3
     }
 76  
 
 77  
     public void store(String pageName, String idPath, String propertyName, Object newValue)
 78  
     {
 79  1
         PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
 80  1
         if (data == null)
 81  
         {
 82  1
             data = new PersistentPropertyData(_encoder);
 83  1
             _data.put(pageName, data);
 84  
         }
 85  
 
 86  1
         data.store(idPath, propertyName, newValue);
 87  1
     }
 88  
 
 89  
     public Collection getStoredChanges(String pageName)
 90  
     {
 91  4
         PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
 92  
 
 93  4
         if (data == null)
 94  2
             return Collections.EMPTY_LIST;
 95  
 
 96  2
         return data.getPageChanges();
 97  
     }
 98  
 
 99  
     public void discardStoredChanges(String pageName)
 100  
     {
 101  1
         _data.remove(pageName);
 102  1
     }
 103  
 
 104  
     public void addParametersForPersistentProperties(ServiceEncoding encoding, boolean post)
 105  
     {
 106  2
         Defense.notNull(encoding, "encoding");
 107  
  
 108  2
         Iterator i = _data.entrySet().iterator();
 109  5
         while (i.hasNext())
 110  
         {
 111  3
             Map.Entry e = (Map.Entry) i.next();
 112  
 
 113  3
             String pageName = (String) e.getKey();
 114  3
             PersistentPropertyData data = (PersistentPropertyData) e.getValue();
 115  
 
 116  3
             ClientPropertyPersistenceScope scope = getScope();
 117  
 
 118  3
             if (scope.shouldEncodeState(encoding, pageName, data))
 119  
             {
 120  2
                 String parameterName = _scope.constructParameterName(pageName);
 121  2
                 encoding.setParameterValue(parameterName, data.getEncoded());
 122  
             }
 123  3
         }
 124  2
     }
 125  
 
 126  
     public void setRequest(WebRequest request)
 127  
     {
 128  3
         _request = request;
 129  3
     }
 130  
 
 131  
     public ClientPropertyPersistenceScope getScope()
 132  
     {
 133  3
         return _scope;
 134  
     }
 135  
 
 136  
     public void setScope(ClientPropertyPersistenceScope scope)
 137  
     {
 138  3
         _scope = scope;
 139  3
     }
 140  
 
 141  
     public void setEncoder(PersistentPropertyDataEncoder encoder)
 142  
     {
 143  4
         _encoder = encoder;
 144  4
     }
 145  
 }