1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces.application; |
20 | |
|
21 | |
import java.net.MalformedURLException; |
22 | |
import java.net.URL; |
23 | |
import java.util.List; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import javax.el.ExpressionFactory; |
27 | |
import javax.el.ValueExpression; |
28 | |
import javax.faces.context.ExternalContext; |
29 | |
import javax.faces.context.FacesContext; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public class NavigationCase |
35 | |
{ |
36 | |
private String _condition; |
37 | |
private String _fromAction; |
38 | |
private String _fromOutcome; |
39 | |
private String _fromViewId; |
40 | |
private String _toViewId; |
41 | |
private String _toFlowDocumentId; |
42 | |
private boolean _includeViewParams; |
43 | |
private boolean _redirect; |
44 | |
private Map<String,List<String>> _parameters; |
45 | |
|
46 | |
private ValueExpression _conditionExpression; |
47 | |
private ValueExpression _toViewIdExpression; |
48 | |
|
49 | |
public NavigationCase(String fromViewId, String fromAction, String fromOutcome, String condition, String toViewId, |
50 | |
Map<String,List<String>> parameters, boolean redirect, boolean includeViewParams) |
51 | 0 | { |
52 | 0 | _condition = condition; |
53 | 0 | _fromViewId = fromViewId; |
54 | 0 | _fromAction = fromAction; |
55 | 0 | _fromOutcome = fromOutcome; |
56 | 0 | _toViewId = toViewId; |
57 | 0 | _toFlowDocumentId = null; |
58 | 0 | _redirect = redirect; |
59 | 0 | _includeViewParams = includeViewParams; |
60 | 0 | _parameters = parameters; |
61 | 0 | } |
62 | |
|
63 | |
public NavigationCase(String fromViewId, String fromAction, String fromOutcome, String condition, String toViewId, |
64 | |
String toFlowDocumentId, Map<String,List<String>> parameters, boolean redirect, |
65 | |
boolean includeViewParams) |
66 | 0 | { |
67 | 0 | _condition = condition; |
68 | 0 | _fromViewId = fromViewId; |
69 | 0 | _fromAction = fromAction; |
70 | 0 | _fromOutcome = fromOutcome; |
71 | 0 | _toViewId = toViewId; |
72 | 0 | _toFlowDocumentId = toFlowDocumentId; |
73 | 0 | _redirect = redirect; |
74 | 0 | _includeViewParams = includeViewParams; |
75 | 0 | _parameters = parameters; |
76 | |
|
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
@Override |
83 | |
public boolean equals(Object o) |
84 | |
{ |
85 | 0 | if (o == this) |
86 | |
{ |
87 | 0 | return true; |
88 | |
} |
89 | 0 | else if (o instanceof NavigationCase) |
90 | |
{ |
91 | 0 | NavigationCase c = (NavigationCase) o; |
92 | |
|
93 | 0 | return equals(_fromViewId, c._fromViewId) && equals(_fromAction, c._fromAction) |
94 | |
&& equals(_fromOutcome, c._fromOutcome) && equals(_condition, c._condition) |
95 | |
&& equals(_toViewId, c._toViewId) && _redirect == c._redirect |
96 | |
&& _includeViewParams == c._includeViewParams; |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 0 | return false; |
101 | |
} |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
@Override |
108 | |
public int hashCode() |
109 | |
{ |
110 | 0 | return hash(_fromViewId) << 4 ^ hash(_fromAction) << 8 ^ hash(_fromOutcome) << 12 ^ hash(_condition) << 16 |
111 | |
^ hash(_toViewId) << 20 ^ hash(Boolean.valueOf(_redirect)) << 24 |
112 | |
^ hash(Boolean.valueOf(_includeViewParams)); |
113 | |
} |
114 | |
|
115 | |
public URL getActionURL(FacesContext context) throws MalformedURLException |
116 | |
{ |
117 | 0 | ExternalContext externalContext = context.getExternalContext(); |
118 | 0 | return new URL(externalContext.getRequestScheme(), externalContext.getRequestServerName(), |
119 | |
externalContext.getRequestServerPort(), |
120 | |
context.getApplication().getViewHandler().getActionURL(context, getToViewId(context))); |
121 | |
} |
122 | |
|
123 | |
public Boolean getCondition(FacesContext context) |
124 | |
{ |
125 | 0 | if (_condition == null) |
126 | |
{ |
127 | 0 | return null; |
128 | |
} |
129 | |
|
130 | 0 | ValueExpression expression = _getConditionExpression(context); |
131 | |
|
132 | 0 | return Boolean.TRUE.equals(expression.getValue(context.getELContext())); |
133 | |
} |
134 | |
|
135 | |
public String getFromAction() |
136 | |
{ |
137 | 0 | return _fromAction; |
138 | |
} |
139 | |
|
140 | |
public String getFromOutcome() |
141 | |
{ |
142 | 0 | return _fromOutcome; |
143 | |
} |
144 | |
|
145 | |
public String getFromViewId() |
146 | |
{ |
147 | 0 | return _fromViewId; |
148 | |
} |
149 | |
|
150 | |
public URL getBookmarkableURL(FacesContext context) throws MalformedURLException |
151 | |
{ |
152 | 0 | ExternalContext externalContext = context.getExternalContext(); |
153 | 0 | return new URL(externalContext.getRequestScheme(), |
154 | |
externalContext.getRequestServerName(), |
155 | |
externalContext.getRequestServerPort(), |
156 | |
context.getApplication().getViewHandler().getBookmarkableURL(context, getToViewId(context), |
157 | |
_NavigationUtils.getEvaluatedNavigationParameters(context, |
158 | |
getParameters()), isIncludeViewParams())); |
159 | |
} |
160 | |
|
161 | |
public URL getResourceURL(FacesContext context) throws MalformedURLException |
162 | |
{ |
163 | 0 | ExternalContext externalContext = context.getExternalContext(); |
164 | 0 | return new URL(externalContext.getRequestScheme(), externalContext.getRequestServerName(), |
165 | |
externalContext.getRequestServerPort(), |
166 | |
context.getApplication().getViewHandler().getResourceURL(context, getToViewId(context))); |
167 | |
} |
168 | |
|
169 | |
public URL getRedirectURL(FacesContext context) throws MalformedURLException |
170 | |
{ |
171 | 0 | ExternalContext externalContext = context.getExternalContext(); |
172 | 0 | return new URL(externalContext.getRequestScheme(), |
173 | |
externalContext.getRequestServerName(), |
174 | |
externalContext.getRequestServerPort(), |
175 | |
context.getApplication().getViewHandler().getRedirectURL(context, getToViewId(context), |
176 | |
_NavigationUtils.getEvaluatedNavigationParameters(context, |
177 | |
getParameters()), isIncludeViewParams())); |
178 | |
} |
179 | |
|
180 | |
public Map<String,List<String>> getParameters() |
181 | |
{ |
182 | 0 | return _parameters; |
183 | |
} |
184 | |
|
185 | |
public String getToViewId(FacesContext context) |
186 | |
{ |
187 | 0 | if (_toViewId == null) |
188 | |
{ |
189 | 0 | return null; |
190 | |
} |
191 | |
|
192 | 0 | ValueExpression expression = _getToViewIdExpression(context); |
193 | |
|
194 | 0 | return (String)( (expression.isLiteralText()) ? |
195 | |
expression.getExpressionString() : expression.getValue(context.getELContext()) ); |
196 | |
} |
197 | |
|
198 | |
public boolean hasCondition() |
199 | |
{ |
200 | 0 | return _condition != null && _condition.length() > 0; |
201 | |
} |
202 | |
|
203 | |
public boolean isIncludeViewParams() |
204 | |
{ |
205 | 0 | return _includeViewParams; |
206 | |
} |
207 | |
|
208 | |
public boolean isRedirect() |
209 | |
{ |
210 | 0 | return _redirect; |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
public String getToFlowDocumentId() |
218 | |
{ |
219 | 0 | return _toFlowDocumentId; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
@Override |
226 | |
public String toString() |
227 | |
{ |
228 | 0 | StringBuilder builder = new StringBuilder(128); |
229 | 0 | builder.append("<navigation-case>\n"); |
230 | |
|
231 | 0 | if (_fromViewId != null) |
232 | |
{ |
233 | 0 | builder.append(" "); |
234 | 0 | builder.append("<from-view-id>"); |
235 | 0 | builder.append(_fromViewId); |
236 | 0 | builder.append("</from-view-id>\n"); |
237 | |
} |
238 | |
|
239 | 0 | if (_fromAction != null) |
240 | |
{ |
241 | 0 | builder.append(" "); |
242 | 0 | builder.append("<from-action>"); |
243 | 0 | builder.append(_fromAction); |
244 | 0 | builder.append("</from-action>\n"); |
245 | |
} |
246 | |
|
247 | 0 | if (_fromOutcome != null) |
248 | |
{ |
249 | 0 | builder.append(" "); |
250 | 0 | builder.append("<from-outcome>"); |
251 | 0 | builder.append(_fromOutcome); |
252 | 0 | builder.append("</from-outcome>\n"); |
253 | |
} |
254 | |
|
255 | 0 | if (_condition != null) |
256 | |
{ |
257 | 0 | builder.append(" "); |
258 | 0 | builder.append("<if>"); |
259 | 0 | builder.append(_condition); |
260 | 0 | builder.append("</if>\n"); |
261 | |
} |
262 | |
|
263 | 0 | builder.append(" "); |
264 | 0 | builder.append("<to-view-id>"); |
265 | 0 | builder.append(_toViewId); |
266 | 0 | builder.append("</to-view-id>\n"); |
267 | |
|
268 | 0 | if (_toFlowDocumentId != null) |
269 | |
{ |
270 | 0 | builder.append(" "); |
271 | 0 | builder.append("<to-flow-document-id>"); |
272 | 0 | builder.append(_toFlowDocumentId); |
273 | 0 | builder.append("</to-flow-document-id>\n"); |
274 | |
} |
275 | |
|
276 | 0 | if (_redirect) |
277 | |
{ |
278 | 0 | builder.append(" "); |
279 | 0 | builder.append("<redirect include-view-params=\""); |
280 | 0 | builder.append(_includeViewParams); |
281 | 0 | if (_parameters != null && _parameters.size() != 0) |
282 | |
{ |
283 | 0 | builder.append("\">\n"); |
284 | 0 | for (Map.Entry<String, List<String>> entry : _parameters.entrySet()) |
285 | |
{ |
286 | 0 | String name = entry.getKey(); |
287 | 0 | for (String value : entry.getValue()) |
288 | |
{ |
289 | 0 | builder.append(" <view-param>\n"); |
290 | 0 | builder.append(" <name>"); |
291 | 0 | builder.append(name); |
292 | 0 | builder.append("</name>\n"); |
293 | 0 | builder.append(" <value>"); |
294 | 0 | builder.append(value); |
295 | 0 | builder.append("</value>\n"); |
296 | 0 | builder.append(" </view-param>\n"); |
297 | 0 | } |
298 | 0 | } |
299 | 0 | builder.append(" </redirect>\n"); |
300 | |
} |
301 | |
else |
302 | |
{ |
303 | 0 | builder.append("\"/>\n"); |
304 | |
} |
305 | |
} |
306 | |
|
307 | 0 | builder.append("</navigation-case>"); |
308 | |
|
309 | 0 | return builder.toString(); |
310 | |
} |
311 | |
|
312 | |
private ValueExpression _getConditionExpression(FacesContext context) |
313 | |
{ |
314 | 0 | assert _condition != null; |
315 | |
|
316 | 0 | if (_conditionExpression == null) |
317 | |
{ |
318 | 0 | ExpressionFactory factory = context.getApplication().getExpressionFactory(); |
319 | 0 | _conditionExpression = factory.createValueExpression(context.getELContext(), _condition, Boolean.class); |
320 | |
} |
321 | |
|
322 | 0 | return _conditionExpression; |
323 | |
} |
324 | |
|
325 | |
private ValueExpression _getToViewIdExpression(FacesContext context) |
326 | |
{ |
327 | 0 | assert _toViewId != null; |
328 | |
|
329 | 0 | if (_toViewIdExpression == null) |
330 | |
{ |
331 | 0 | ExpressionFactory factory = context.getApplication().getExpressionFactory(); |
332 | 0 | _toViewIdExpression = factory.createValueExpression(context.getELContext(), _toViewId, String.class); |
333 | |
} |
334 | |
|
335 | 0 | return _toViewIdExpression; |
336 | |
} |
337 | |
|
338 | |
private boolean equals(Object o1, Object o2) |
339 | |
{ |
340 | 0 | if (o1 == null) |
341 | |
{ |
342 | 0 | return o2 == null; |
343 | |
} |
344 | |
else |
345 | |
{ |
346 | 0 | return o1.equals(o2); |
347 | |
} |
348 | |
} |
349 | |
|
350 | |
private int hash(Object o) |
351 | |
{ |
352 | 0 | return o == null ? 0 : o.hashCode(); |
353 | |
} |
354 | |
} |