01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 org.springframework.util.Assert;
19: import org.springframework.webflow.engine.ActionExecutor;
20: import org.springframework.webflow.engine.TransitionCriteria;
21: import org.springframework.webflow.execution.Action;
22: import org.springframework.webflow.execution.Event;
23: import org.springframework.webflow.execution.RequestContext;
24:
25: /**
26: * A transition criteria that will execute an action when tested and return
27: * <code>true</code> if the action's result is equal to the 'trueEventId',
28: * <code>false</code> otherwise.
29: * <p>
30: * This effectively adapts an <code>Action</code> to a <code>TransitionCriteria</code>.
31: *
32: * @see org.springframework.webflow.execution.Action
33: * @see org.springframework.webflow.engine.TransitionCriteria
34: *
35: * @author Keith Donald
36: * @author Erwin Vervaet
37: */
38: public class ActionTransitionCriteria implements TransitionCriteria {
39:
40: /**
41: * The result event id that should map to a <code>true</code>
42: * return value.
43: */
44: private String trueEventId = "success";
45:
46: /**
47: * The action to execute when the criteria is tested, annotated with
48: * usage attributes.
49: */
50: private Action action;
51:
52: /**
53: * Create action transition criteria delegating to the specified action.
54: * @param action the action
55: */
56: public ActionTransitionCriteria(Action action) {
57: this .action = action;
58: }
59:
60: /**
61: * Returns the action result <code>eventId</code> that should cause this
62: * criteria to return true (it will return false otherwise). Defaults to
63: * "success".
64: */
65: public String getTrueEventId() {
66: return trueEventId;
67: }
68:
69: /**
70: * Sets the action result <code>eventId</code> that should cause this
71: * precondition to return true (it will return false otherwise).
72: * @param trueEventId the true result event ID
73: */
74: public void setTrueEventId(String trueEventId) {
75: Assert.notNull(trueEventId, "The trueEventId is required");
76: this .trueEventId = trueEventId;
77: }
78:
79: /**
80: * Returns the action wrapped by this object.
81: * @return the action
82: */
83: protected Action getAction() {
84: return action;
85: }
86:
87: public boolean test(RequestContext context) {
88: Event result = ActionExecutor.execute(getAction(), context);
89: return result != null
90: && getTrueEventId().equals(result.getId());
91: }
92: }
|