1 16 package org.springframework.webflow.engine; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.springframework.core.style.ToStringCreator; 21 import org.springframework.util.Assert; 22 import org.springframework.webflow.definition.TransitionDefinition; 23 import org.springframework.webflow.execution.Event; 24 import org.springframework.webflow.execution.FlowExecutionException; 25 import org.springframework.webflow.execution.RequestContext; 26 import org.springframework.webflow.execution.ViewSelection; 27 28 63 public class Transition extends AnnotatedObject implements TransitionDefinition { 64 65 68 protected final Log logger = LogFactory.getLog(Transition.class); 69 70 74 private TransitionCriteria matchingCriteria; 75 76 80 private TransitionCriteria executionCriteria = WildcardTransitionCriteria.INSTANCE; 81 82 86 private TargetStateResolver targetStateResolver; 87 88 97 public Transition(TargetStateResolver targetStateResolver) { 98 this(WildcardTransitionCriteria.INSTANCE, targetStateResolver); 99 } 100 101 110 public Transition(TransitionCriteria matchingCriteria, TargetStateResolver targetStateResolver) { 111 setMatchingCriteria(matchingCriteria); 112 setTargetStateResolver(targetStateResolver); 113 } 114 115 117 public String getId() { 118 return matchingCriteria.toString(); 119 } 120 121 public String getTargetStateId() { 122 return targetStateResolver.toString(); 123 } 124 125 130 protected TransitionCriteria getMatchingCriteria() { 131 return matchingCriteria; 132 } 133 134 139 public void setMatchingCriteria(TransitionCriteria matchingCriteria) { 140 Assert.notNull(matchingCriteria, "The matching criteria is required"); 141 this.matchingCriteria = matchingCriteria; 142 } 143 144 149 protected TransitionCriteria getExecutionCriteria() { 150 return executionCriteria; 151 } 152 153 158 public void setExecutionCriteria(TransitionCriteria executionCriteria) { 159 Assert.notNull(executionCriteria, "The execution criteria is required"); 160 this.executionCriteria = executionCriteria; 161 } 162 163 166 protected TargetStateResolver getTargetStateResolver() { 167 return targetStateResolver; 168 } 169 170 175 public void setTargetStateResolver(TargetStateResolver targetStateResolver) { 176 Assert.notNull(targetStateResolver, "The target state resolver is required"); 177 this.targetStateResolver = targetStateResolver; 178 } 179 180 186 public boolean matches(RequestContext context) { 187 return matchingCriteria.test(context); 188 } 189 190 198 public ViewSelection execute(State sourceState, RequestControlContext context) throws FlowExecutionException { 199 ViewSelection selectedView; 200 if (canExecute(context)) { 201 if (sourceState != null) { 202 if (logger.isDebugEnabled()) { 203 logger.debug("Executing " + this + " out of state '" + sourceState.getId() + "'"); 204 } 205 if (sourceState instanceof TransitionableState) { 206 ((TransitionableState)sourceState).exit(context); 208 } 209 } 210 else { 211 if (logger.isDebugEnabled()) { 212 logger.debug("Executing " + this); 213 } 214 } 215 State targetState = targetStateResolver.resolveTargetState(this, sourceState, context); 216 context.setLastTransition(this); 217 selectedView = targetState.enter(context); 219 } 220 else { 221 if (sourceState != null && sourceState instanceof TransitionableState) { 222 selectedView = ((TransitionableState)sourceState).reenter(context); 224 } 225 else { 226 throw new IllegalStateException ( 227 "Execution of '" + this + "' was blocked by '" + getExecutionCriteria() 228 + "', " + "; however, no source state is set at runtime. " 229 + "This is an illegal situation: check your flow definition."); 230 } 231 } 232 if (logger.isDebugEnabled()) { 233 if (context.getFlowExecutionContext().isActive()) { 234 logger.debug("Completed execution of " + this + ", as a result the new state is '" 235 + context.getCurrentState().getId() + "' in flow '" + context.getActiveFlow().getId() + "'"); 236 } 237 else { 238 logger.debug("Completed execution of " + this + ", as a result the flow execution has ended"); 239 } 240 } 241 return selectedView; 242 } 243 244 251 protected boolean canExecute(RequestContext context) { 252 return executionCriteria.test(context); 253 } 254 255 public String toString() { 256 return new ToStringCreator(this).append("on", getMatchingCriteria()).append("to", getTargetStateResolver()) 257 .toString(); 258 } 259 } | Popular Tags |