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 |
11 |
public class SessionPropertyPersistenceStrategy implements |
34 |
|
PropertyPersistenceStrategy |
35 |
|
{ |
36 |
|
|
37 |
|
public static final String STRATEGY_ID = "session"; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
private String _applicationId; |
45 |
|
|
46 |
|
private WebRequest _request; |
47 |
|
|
48 |
|
public void store(String pageName, String idPath, String propertyName, Object newValue) |
49 |
|
{ |
50 |
3 |
Defense.notNull(pageName, "pageName"); |
51 |
3 |
Defense.notNull(propertyName, "propertyName"); |
52 |
|
|
53 |
3 |
WebSession session = _request.getSession(true); |
54 |
|
|
55 |
3 |
String attributeName = RecordUtils.buildChangeKey(STRATEGY_ID, _applicationId, pageName, idPath, propertyName); |
56 |
|
|
57 |
3 |
session.setAttribute(attributeName, newValue); |
58 |
3 |
} |
59 |
|
|
60 |
|
public Collection getStoredChanges(String pageName) |
61 |
|
{ |
62 |
4 |
Defense.notNull(pageName, "pageName"); |
63 |
|
|
64 |
4 |
WebSession session = _request.getSession(false); |
65 |
|
|
66 |
4 |
if (session == null) |
67 |
1 |
return Collections.EMPTY_LIST; |
68 |
|
|
69 |
3 |
final Collection result = new ArrayList(); |
70 |
|
|
71 |
3 |
WebSessionAttributeCallback callback = new WebSessionAttributeCallback() |
72 |
3 |
{ |
73 |
|
public void handleAttribute(WebSession sess, String name) |
74 |
|
{ |
75 |
2 |
PropertyChange change = RecordUtils.buildChange(name, sess.getAttribute(name)); |
76 |
|
|
77 |
2 |
result.add(change); |
78 |
2 |
} |
79 |
|
}; |
80 |
|
|
81 |
3 |
RecordUtils.iterateOverMatchingAttributes(STRATEGY_ID, _applicationId, pageName, session, callback); |
82 |
|
|
83 |
3 |
return result; |
84 |
|
} |
85 |
|
|
86 |
|
public void discardStoredChanges(String pageName) |
87 |
|
{ |
88 |
3 |
WebSession session = _request.getSession(false); |
89 |
|
|
90 |
3 |
if (session == null) return; |
91 |
|
|
92 |
2 |
WebSessionAttributeCallback callback = new WebSessionAttributeCallback() |
93 |
2 |
{ |
94 |
|
public void handleAttribute(WebSession sess, String name) |
95 |
|
{ |
96 |
1 |
sess.setAttribute(name, null); |
97 |
1 |
} |
98 |
|
}; |
99 |
|
|
100 |
2 |
RecordUtils.iterateOverMatchingAttributes(STRATEGY_ID, _applicationId, |
101 |
|
pageName, session, callback); |
102 |
2 |
} |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
public void addParametersForPersistentProperties(ServiceEncoding encoding, |
109 |
|
boolean post) |
110 |
|
{ |
111 |
1 |
} |
112 |
|
|
113 |
|
public void setApplicationId(String applicationName) |
114 |
|
{ |
115 |
8 |
_applicationId = applicationName; |
116 |
8 |
} |
117 |
|
|
118 |
|
public void setRequest(WebRequest request) |
119 |
|
{ |
120 |
10 |
_request = request; |
121 |
10 |
} |
122 |
|
} |