|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ClientPropertyPersistenceStrategy.java | 90% | 100% | 100% | 98% |
|
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 java.util.Collection;
|
|
18 |
import java.util.Collections;
|
|
19 |
import java.util.HashMap;
|
|
20 |
import java.util.Iterator;
|
|
21 |
import java.util.List;
|
|
22 |
import java.util.Map;
|
|
23 |
|
|
24 |
import org.apache.hivemind.util.Defense;
|
|
25 |
import org.apache.tapestry.IRequestCycle;
|
|
26 |
import org.apache.tapestry.engine.ServiceEncoding;
|
|
27 |
import org.apache.tapestry.web.WebRequest;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* Encodes persistent page properties on the client as query parameters.
|
|
31 |
* <p>
|
|
32 |
* Uses the threaded model.
|
|
33 |
*
|
|
34 |
* @author Howard M. Lewis Ship
|
|
35 |
* @since 4.0
|
|
36 |
* @see org.apache.tapestry.engine.ILink
|
|
37 |
*/
|
|
38 |
public class ClientPropertyPersistenceStrategy implements PropertyPersistenceStrategy |
|
39 |
{ |
|
40 |
/**
|
|
41 |
* Query parameters consist of this prefix followed by the page name. Each page gets its own
|
|
42 |
* query parameter.
|
|
43 |
*/
|
|
44 |
public static final String PREFIX = "state:"; |
|
45 |
|
|
46 |
/**
|
|
47 |
* Keyed on page name (String), values are
|
|
48 |
* {@link org.apache.tapestry.record.PersistentPropertyData}.
|
|
49 |
*/
|
|
50 |
private final Map _data = new HashMap(); |
|
51 |
|
|
52 |
private final PersistentPropertyDataEncoder _encoder;
|
|
53 |
|
|
54 |
private WebRequest _request;
|
|
55 |
|
|
56 | 48 |
public ClientPropertyPersistenceStrategy()
|
57 |
{ |
|
58 | 48 |
this(new PersistentPropertyDataEncoderImpl()); |
59 |
} |
|
60 |
|
|
61 |
// Alternate constructor used for testing
|
|
62 | 49 |
ClientPropertyPersistenceStrategy(PersistentPropertyDataEncoder encoder) |
63 |
{ |
|
64 | 49 |
_encoder = encoder; |
65 |
} |
|
66 |
|
|
67 | 2 |
public void initializeForService() |
68 |
{ |
|
69 | 2 |
List names = _request.getParameterNames(); |
70 | 2 |
Iterator i = names.iterator(); |
71 | 2 |
while (i.hasNext())
|
72 |
{ |
|
73 | 4 |
String name = (String) i.next(); |
74 |
|
|
75 | 4 |
if (!name.startsWith(PREFIX))
|
76 | 2 |
continue;
|
77 |
|
|
78 | 2 |
String pageName = name.substring(PREFIX.length()); |
79 | 2 |
String encoded = _request.getParameterValue(name); |
80 |
|
|
81 | 2 |
PersistentPropertyData data = new PersistentPropertyData(_encoder);
|
82 | 2 |
data.storeEncoded(encoded); |
83 |
|
|
84 | 2 |
_data.put(pageName, data); |
85 |
} |
|
86 |
} |
|
87 |
|
|
88 | 1 |
public void store(String pageName, String idPath, String propertyName, Object newValue) |
89 |
{ |
|
90 | 1 |
PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName); |
91 | 1 |
if (data == null) |
92 |
{ |
|
93 | 1 |
data = new PersistentPropertyData(_encoder);
|
94 | 1 |
_data.put(pageName, data); |
95 |
} |
|
96 |
|
|
97 | 1 |
data.store(idPath, propertyName, newValue); |
98 |
} |
|
99 |
|
|
100 | 216 |
public Collection getStoredChanges(String pageName, IRequestCycle cycle)
|
101 |
{ |
|
102 | 216 |
PersistentPropertyData data = (PersistentPropertyData) _data.get(pageName); |
103 |
|
|
104 | 216 |
if (data == null) |
105 | 214 |
return Collections.EMPTY_LIST;
|
106 |
|
|
107 | 2 |
return data.getPageChanges();
|
108 |
} |
|
109 |
|
|
110 | 1 |
public void discardStoredChanges(String pageName, IRequestCycle cycle) |
111 |
{ |
|
112 | 1 |
_data.remove(pageName); |
113 |
} |
|
114 |
|
|
115 | 161 |
public void addParametersForPersistentProperties(ServiceEncoding encoding, IRequestCycle cycle) |
116 |
{ |
|
117 | 161 |
Defense.notNull(encoding, "encoding");
|
118 | 161 |
Defense.notNull(cycle, "cycle");
|
119 |
|
|
120 | 161 |
Iterator i = _data.entrySet().iterator(); |
121 | 161 |
while (i.hasNext())
|
122 |
{ |
|
123 | 1 |
Map.Entry e = (Map.Entry) i.next(); |
124 |
|
|
125 | 1 |
String pageName = (String) e.getKey(); |
126 | 1 |
PersistentPropertyData data = (PersistentPropertyData) e.getValue(); |
127 |
|
|
128 | 1 |
encoding.setParameterValue(PREFIX + pageName, data.getEncoded()); |
129 |
} |
|
130 |
} |
|
131 |
|
|
132 | 47 |
public void setRequest(WebRequest request) |
133 |
{ |
|
134 | 47 |
_request = request; |
135 |
} |
|
136 |
} |
|