KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > support > FlowDefinitionRedirectSelector


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.engine.support;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.springframework.binding.expression.Expression;
22 import org.springframework.util.StringUtils;
23 import org.springframework.webflow.engine.ViewSelector;
24 import org.springframework.webflow.execution.RequestContext;
25 import org.springframework.webflow.execution.ViewSelection;
26 import org.springframework.webflow.execution.support.FlowDefinitionRedirect;
27
28 /**
29  * Makes a {@link FlowDefinitionRedirect} selection when requested, calculating the
30  * <code>flowDefinitionId</code> and <code>executionInput</code> by
31  * evaluating an expression against the request context.
32  *
33  * @see org.springframework.webflow.execution.support.FlowDefinitionRedirect
34  *
35  * @author Keith Donald
36  */

37 public class FlowDefinitionRedirectSelector implements ViewSelector {
38
39     /**
40      * The parsed flow expression, evaluatable to the string format:
41      * flowDefinitionId?param1Name=parmValue&param2Name=paramValue.
42      */

43     private Expression expression;
44
45     /**
46      * Creates a new launch flow redirect selector.
47      * @param expression the parsed flow redirect expression, evaluatable to the
48      * string format: flowDefinitionId?param1Name=parmValue&param2Name=paramValue
49      */

50     public FlowDefinitionRedirectSelector(Expression expression) {
51         this.expression = expression;
52     }
53
54     public boolean isEntrySelectionRenderable(RequestContext context) {
55         return true;
56     }
57
58     public ViewSelection makeEntrySelection(RequestContext context) {
59         String JavaDoc encodedRedirect = (String JavaDoc)expression.evaluate(context, null);
60         if (encodedRedirect == null) {
61             throw new IllegalStateException JavaDoc(
62                     "Flow definition redirect expression evaluated to [null], the expression was " + expression);
63         }
64         // the encoded FlowDefinitionRedirect should look something like
65
// "flowDefinitionId?param0=value0&param1=value1"
66
// now parse that and build a corresponding view selection
67
int index = encodedRedirect.indexOf('?');
68         String JavaDoc flowDefinitionId;
69         Map JavaDoc executionInput = null;
70         if (index != -1) {
71             flowDefinitionId = encodedRedirect.substring(0, index);
72             String JavaDoc[] parameters = StringUtils.delimitedListToStringArray(encodedRedirect.substring(index + 1), "&");
73             executionInput = new HashMap JavaDoc(parameters.length, 1);
74             for (int i = 0; i < parameters.length; i++) {
75                 String JavaDoc nameAndValue = parameters[i];
76                 index = nameAndValue.indexOf('=');
77                 if (index != -1) {
78                     executionInput.put(nameAndValue.substring(0, index), nameAndValue.substring(index + 1));
79                 }
80                 else {
81                     executionInput.put(nameAndValue, "");
82                 }
83             }
84         }
85         else {
86             flowDefinitionId = encodedRedirect;
87         }
88         if (!StringUtils.hasText(flowDefinitionId)) {
89             // equivalent to restart
90
flowDefinitionId = context.getFlowExecutionContext().getDefinition().getId();
91         }
92         return new FlowDefinitionRedirect(flowDefinitionId, executionInput);
93     }
94
95     public ViewSelection makeRefreshSelection(RequestContext context) {
96         return makeEntrySelection(context);
97     }
98 }
Popular Tags