1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.record; |
16 |
| |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.Collection; |
19 |
| import java.util.Collections; |
20 |
| |
21 |
| import org.apache.hivemind.util.Defense; |
22 |
| import org.apache.tapestry.engine.ServiceEncoding; |
23 |
| import org.apache.tapestry.web.WebRequest; |
24 |
| import org.apache.tapestry.web.WebSession; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| public class SessionPropertyPersistenceStrategy implements PropertyPersistenceStrategy |
34 |
| { |
35 |
| public static final String STRATEGY_ID = "session"; |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| private String _applicationId; |
42 |
| |
43 |
| private WebRequest _request; |
44 |
| |
45 |
23
| public void store(String pageName, String idPath, String propertyName, Object newValue)
|
46 |
| { |
47 |
23
| Defense.notNull(pageName, "pageName");
|
48 |
23
| Defense.notNull(propertyName, "propertyName");
|
49 |
| |
50 |
23
| WebSession session = _request.getSession(true);
|
51 |
| |
52 |
23
| String attributeName = RecordUtils.buildChangeKey(
|
53 |
| STRATEGY_ID, |
54 |
| _applicationId, |
55 |
| pageName, |
56 |
| idPath, |
57 |
| propertyName); |
58 |
| |
59 |
23
| session.setAttribute(attributeName, newValue);
|
60 |
| } |
61 |
| |
62 |
179
| public Collection getStoredChanges(String pageName)
|
63 |
| { |
64 |
179
| Defense.notNull(pageName, "pageName");
|
65 |
| |
66 |
179
| WebSession session = _request.getSession(false);
|
67 |
| |
68 |
179
| if (session == null)
|
69 |
131
| return Collections.EMPTY_LIST;
|
70 |
| |
71 |
48
| final Collection result = new ArrayList();
|
72 |
| |
73 |
48
| WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
|
74 |
| { |
75 |
13
| public void handleAttribute(WebSession session, String name)
|
76 |
| { |
77 |
13
| PropertyChange change = RecordUtils.buildChange(name, session.getAttribute(name));
|
78 |
| |
79 |
13
| result.add(change);
|
80 |
| } |
81 |
| }; |
82 |
| |
83 |
48
| RecordUtils.iterateOverMatchingAttributes(
|
84 |
| STRATEGY_ID, |
85 |
| _applicationId, |
86 |
| pageName, |
87 |
| session, |
88 |
| callback); |
89 |
| |
90 |
48
| return result;
|
91 |
| } |
92 |
| |
93 |
3
| public void discardStoredChanges(String pageName)
|
94 |
| { |
95 |
3
| WebSession session = _request.getSession(false);
|
96 |
| |
97 |
3
| if (session == null)
|
98 |
1
| return;
|
99 |
| |
100 |
2
| WebSessionAttributeCallback callback = new WebSessionAttributeCallback()
|
101 |
| { |
102 |
1
| public void handleAttribute(WebSession session, String name)
|
103 |
| { |
104 |
1
| session.setAttribute(name, null);
|
105 |
| } |
106 |
| }; |
107 |
| |
108 |
2
| RecordUtils.iterateOverMatchingAttributes(
|
109 |
| STRATEGY_ID, |
110 |
| _applicationId, |
111 |
| pageName, |
112 |
| session, |
113 |
| callback); |
114 |
| } |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
114
| public void addParametersForPersistentProperties(ServiceEncoding encoding, boolean post)
|
121 |
| { |
122 |
| } |
123 |
| |
124 |
47
| public void setApplicationId(String applicationName)
|
125 |
| { |
126 |
47
| _applicationId = applicationName;
|
127 |
| } |
128 |
| |
129 |
49
| public void setRequest(WebRequest request)
|
130 |
| { |
131 |
49
| _request = request;
|
132 |
| } |
133 |
| } |