Coverage Report - org.apache.tapestry.record.ClientPropertyPersistenceStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
ClientPropertyPersistenceStrategy
0%
0/46
0%
0/12
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  0
 public class ClientPropertyPersistenceStrategy implements PropertyPersistenceStrategy
 34  
 {
 35  
     /**
 36  
      * Keyed on page name (String), values are
 37  
      * {@link org.apache.tapestry.record.PersistentPropertyData}.
 38  
      */
 39  0
     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  0
         List names = _request.getParameterNames();
 58  0
         Iterator i = names.iterator();
 59  0
         while (i.hasNext())
 60  
         {
 61  0
             String name = (String) i.next();
 62  
 
 63  0
             if (!_scope.isParameterForScope(name))
 64  0
                 continue;
 65  
 
 66  0
             String pageName = _scope.extractPageName(name);
 67  
 
 68  0
             String encoded = _request.getParameterValue(name);
 69  
 
 70  0
             PersistentPropertyData data = new PersistentPropertyData(_encoder);
 71  0
             data.storeEncoded(encoded);
 72  
 
 73  0
             _data.put(pageName, data);
 74  0
         }
 75  0
     }
 76  
 
 77  
     public void store(String pageName, String idPath, String propertyName, Object newValue)
 78  
     {
 79  0
         PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
 80  0
         if (data == null)
 81  
         {
 82  0
             data = new PersistentPropertyData(_encoder);
 83  0
             _data.put(pageName, data);
 84  
         }
 85  
 
 86  0
         data.store(idPath, propertyName, newValue);
 87  0
     }
 88  
 
 89  
     public Collection getStoredChanges(String pageName)
 90  
     {
 91  0
         PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName);
 92  
 
 93  0
         if (data == null)
 94  0
             return Collections.EMPTY_LIST;
 95  
 
 96  0
         return data.getPageChanges();
 97  
     }
 98  
 
 99  
     public void discardStoredChanges(String pageName)
 100  
     {
 101  0
         _data.remove(pageName);
 102  0
     }
 103  
 
 104  
     public void addParametersForPersistentProperties(ServiceEncoding encoding, boolean post)
 105  
     {
 106  0
         Defense.notNull(encoding, "encoding");
 107  
  
 108  0
         Iterator i = _data.entrySet().iterator();
 109  0
         while (i.hasNext())
 110  
         {
 111  0
             Map.Entry e = (Map.Entry) i.next();
 112  
 
 113  0
             String pageName = (String) e.getKey();
 114  0
             PersistentPropertyData data = (PersistentPropertyData) e.getValue();
 115  
 
 116  0
             ClientPropertyPersistenceScope scope = getScope();
 117  
 
 118  0
             if (scope.shouldEncodeState(encoding, pageName, data))
 119  
             {
 120  0
                 String parameterName = _scope.constructParameterName(pageName);
 121  0
                 encoding.setParameterValue(parameterName, data.getEncoded());
 122  
             }
 123  0
         }
 124  0
     }
 125  
 
 126  
     public void setRequest(WebRequest request)
 127  
     {
 128  0
         _request = request;
 129  0
     }
 130  
 
 131  
     public ClientPropertyPersistenceScope getScope()
 132  
     {
 133  0
         return _scope;
 134  
     }
 135  
 
 136  
     public void setScope(ClientPropertyPersistenceScope scope)
 137  
     {
 138  0
         _scope = scope;
 139  0
     }
 140  
 
 141  
     public void setEncoder(PersistentPropertyDataEncoder encoder)
 142  
     {
 143  0
         _encoder = encoder;
 144  0
     }
 145  
 }