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