1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.application.impl; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.io.Serializable; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import javax.faces.component.UIViewRoot; |
24 | |
import javax.faces.context.FacesContext; |
25 | |
import javax.faces.render.ResponseStateManager; |
26 | |
|
27 | |
import org.seasar.teeda.core.application.TeedaStateManager; |
28 | |
import org.seasar.teeda.core.application.TreeStructure; |
29 | |
import org.seasar.teeda.core.util.PortletUtil; |
30 | |
import org.seasar.teeda.core.util.ResponseStateManagerUtil; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class TeedaStateManagerImpl extends TeedaStateManager implements |
37 | |
Serializable { |
38 | |
|
39 | |
static final long serialVersionUID = 0L; |
40 | |
|
41 | 19 | private final Map serializedViews = new HashMap(); |
42 | |
|
43 | 19 | public TeedaStateManagerImpl() { |
44 | 19 | } |
45 | |
|
46 | |
public synchronized UIViewRoot restoreView(final FacesContext context, |
47 | |
final String viewId, final String renderKitId) { |
48 | 3 | assertRenderKitIdNotNull(renderKitId); |
49 | 2 | final UIViewRoot viewRoot = restoreTreeStructure(context, viewId, |
50 | |
renderKitId); |
51 | 2 | if (viewRoot != null) { |
52 | 2 | viewRoot.setViewId(viewId); |
53 | 2 | restoreComponentState(context, viewRoot, renderKitId); |
54 | |
|
55 | 2 | if (PortletUtil.isPortlet(context)) { |
56 | 0 | removeSerializedView(viewId); |
57 | |
} |
58 | |
} |
59 | 2 | return viewRoot; |
60 | |
} |
61 | |
|
62 | |
public synchronized SerializedView saveSerializedView( |
63 | |
final FacesContext context) throws IllegalStateException { |
64 | 4 | if (isSavingStateInClient(context)) { |
65 | 1 | return createSerializedView(context); |
66 | |
} |
67 | 3 | if (!hasSerializedViewInServer(context.getViewRoot().getViewId())) { |
68 | 3 | saveViewToServer(context, context.getViewRoot()); |
69 | |
} |
70 | 3 | return null; |
71 | |
} |
72 | |
|
73 | |
protected synchronized void saveSerializedViewToServer( |
74 | |
FacesContext context, String viewId, SerializedView serializedView) { |
75 | 3 | saveSerializedViewToServer(viewId, serializedView); |
76 | 3 | } |
77 | |
|
78 | |
public synchronized void removeSerializedView(final String viewId) { |
79 | |
|
80 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
81 | 0 | if (context != null && PortletUtil.isPortlet(context)) { |
82 | 0 | if (!isSavingStateInClient(context)) { |
83 | 0 | context.getExternalContext().getSessionMap().remove( |
84 | |
SERIALIZED_VIEW_ATTR + "-" + viewId); |
85 | 0 | return; |
86 | |
} |
87 | |
} |
88 | |
|
89 | 0 | serializedViews.remove(viewId); |
90 | 0 | } |
91 | |
|
92 | |
public synchronized void writeState(final FacesContext context, |
93 | |
final SerializedView serializedView) throws IOException { |
94 | 0 | if (isSavingStateInClient(context)) { |
95 | 0 | final UIViewRoot viewRoot = context.getViewRoot(); |
96 | 0 | final ResponseStateManager responseStateManager = getResponseStateManager( |
97 | |
context, viewRoot.getRenderKitId()); |
98 | 0 | responseStateManager.writeState(context, serializedView); |
99 | |
} |
100 | 0 | } |
101 | |
|
102 | |
public Object getComponentStateToSave(final FacesContext context) { |
103 | 3 | final UIViewRoot viewRoot = context.getViewRoot(); |
104 | 3 | if (viewRoot.isTransient()) { |
105 | 1 | return null; |
106 | |
} |
107 | 2 | return viewRoot.processSaveState(context); |
108 | |
} |
109 | |
|
110 | |
public Object getTreeStructureToSave(final FacesContext context) { |
111 | 3 | final UIViewRoot viewRoot = context.getViewRoot(); |
112 | 3 | if (viewRoot.isTransient()) { |
113 | 1 | return null; |
114 | |
} |
115 | 2 | return getTreeStructureManager().buildTreeStructure(viewRoot); |
116 | |
} |
117 | |
|
118 | |
protected void restoreComponentState(final FacesContext context, |
119 | |
final UIViewRoot viewRoot, final String renderKitId) { |
120 | 3 | assertRenderKitIdNotNull(renderKitId); |
121 | 2 | if (viewRoot.getRenderKitId() == null) { |
122 | 2 | viewRoot.setRenderKitId(renderKitId); |
123 | |
} |
124 | 2 | if (isSavingStateInClient(context)) { |
125 | 0 | restoreComponentStateFromClient(context, viewRoot, renderKitId); |
126 | |
} else { |
127 | 2 | restoreComponentStateFromServer(context, viewRoot); |
128 | |
} |
129 | 2 | } |
130 | |
|
131 | |
protected UIViewRoot restoreTreeStructure(final FacesContext context, |
132 | |
final String viewId, final String renderKitId) { |
133 | 3 | assertRenderKitIdNotNull(renderKitId); |
134 | 2 | if (isSavingStateInClient(context)) { |
135 | 0 | return restoreTreeStructureFromClient(context, viewId, renderKitId); |
136 | |
} |
137 | 2 | return restoreTreeStructureFromServer(context, viewId); |
138 | |
} |
139 | |
|
140 | |
protected void restoreComponentStateFromClient(final FacesContext context, |
141 | |
final UIViewRoot viewRoot, final String renderKitId) { |
142 | 1 | final ResponseStateManager responseStateManager = getResponseStateManager( |
143 | |
context, renderKitId); |
144 | 1 | final Object state = responseStateManager |
145 | |
.getComponentStateToRestore(context); |
146 | 1 | viewRoot.processRestoreState(context, state); |
147 | 1 | } |
148 | |
|
149 | |
protected void restoreComponentStateFromServer(final FacesContext context, |
150 | |
final UIViewRoot viewRoot) { |
151 | 5 | final SerializedView serializedView = getSerializedViewFromServer(viewRoot |
152 | |
.getViewId()); |
153 | 5 | if (serializedView == null) { |
154 | 2 | return; |
155 | |
} |
156 | 3 | final Object state = serializedView.getState(); |
157 | 3 | if (state == null) { |
158 | 0 | return; |
159 | |
} |
160 | 3 | viewRoot.processRestoreState(context, state); |
161 | 3 | } |
162 | |
|
163 | |
protected UIViewRoot restoreTreeStructureFromClient( |
164 | |
final FacesContext context, final String viewId, |
165 | |
final String renderKitId) { |
166 | 2 | final ResponseStateManager responseStateManager = getResponseStateManager( |
167 | |
context, renderKitId); |
168 | 2 | final TreeStructure struct = (TreeStructure) responseStateManager |
169 | |
.getTreeStructureToRestore(context, viewId); |
170 | 2 | if (struct == null) { |
171 | 1 | return null; |
172 | |
} |
173 | 1 | return (UIViewRoot) getTreeStructureManager().restoreTreeStructure( |
174 | |
struct); |
175 | |
} |
176 | |
|
177 | |
protected UIViewRoot restoreTreeStructureFromServer( |
178 | |
final FacesContext context, final String viewId) { |
179 | 4 | final SerializedView serializedView = getSerializedViewFromServer(viewId); |
180 | 4 | if (serializedView == null) { |
181 | 1 | return null; |
182 | |
} |
183 | 3 | final TreeStructure struct = (TreeStructure) serializedView |
184 | |
.getStructure(); |
185 | 3 | return (UIViewRoot) getTreeStructureManager().restoreTreeStructure( |
186 | |
struct); |
187 | |
} |
188 | |
|
189 | |
private static void assertRenderKitIdNotNull(final String renderKitId) { |
190 | 9 | if (renderKitId == null) { |
191 | 3 | throw new IllegalArgumentException(); |
192 | |
} |
193 | 6 | } |
194 | |
|
195 | |
protected SerializedView getSerializedViewFromServer(final String viewId) { |
196 | |
|
197 | 11 | FacesContext context = FacesContext.getCurrentInstance(); |
198 | 11 | if (context != null && PortletUtil.isPortlet(context)) { |
199 | 0 | return (SerializedView) context.getExternalContext() |
200 | |
.getSessionMap().get(SERIALIZED_VIEW_ATTR + "-" + viewId); |
201 | |
} |
202 | |
|
203 | 11 | return (SerializedView) serializedViews.get(viewId); |
204 | |
} |
205 | |
|
206 | |
protected boolean hasSerializedViewInServer(final String viewId) { |
207 | |
|
208 | 3 | FacesContext context = FacesContext.getCurrentInstance(); |
209 | 3 | if (context != null && PortletUtil.isPortlet(context)) { |
210 | 0 | return context.getExternalContext().getSessionMap().containsKey( |
211 | |
SERIALIZED_VIEW_ATTR + "-" + viewId); |
212 | |
} |
213 | |
|
214 | 3 | return serializedViews.containsKey(viewId); |
215 | |
} |
216 | |
|
217 | |
protected void saveSerializedViewToServer(final String viewId, |
218 | |
final SerializedView serializedView) { |
219 | |
|
220 | 6 | FacesContext context = FacesContext.getCurrentInstance(); |
221 | 6 | if (context != null && PortletUtil.isPortlet(context)) { |
222 | 0 | context.getExternalContext().getSessionMap().put( |
223 | |
SERIALIZED_VIEW_ATTR + "-" + viewId, serializedView); |
224 | 0 | return; |
225 | |
} |
226 | |
|
227 | 6 | serializedViews.put(viewId, serializedView); |
228 | 6 | } |
229 | |
|
230 | |
private ResponseStateManager getResponseStateManager( |
231 | |
final FacesContext context, final String renderKitId) { |
232 | 3 | return ResponseStateManagerUtil.getResponseStateManager(context, |
233 | |
renderKitId); |
234 | |
} |
235 | |
|
236 | |
public boolean hasSerializedView(final FacesContext context, |
237 | |
final String viewId) { |
238 | 0 | if (isSavingStateInClient(context)) { |
239 | 0 | return false; |
240 | |
} |
241 | 0 | return hasSerializedViewInServer(viewId); |
242 | |
} |
243 | |
|
244 | |
} |