1 16 package org.springframework.webflow.engine.builder; 17 18 import org.springframework.binding.expression.Expression; 19 import org.springframework.binding.mapping.AttributeMapper; 20 import org.springframework.binding.mapping.Mapping; 21 import org.springframework.binding.mapping.MappingBuilder; 22 import org.springframework.binding.method.MethodSignature; 23 import org.springframework.webflow.action.AbstractBeanInvokingAction; 24 import org.springframework.webflow.action.ActionResultExposer; 25 import org.springframework.webflow.action.BeanInvokingActionFactory; 26 import org.springframework.webflow.action.EvaluateAction; 27 import org.springframework.webflow.action.MultiAction; 28 import org.springframework.webflow.core.collection.AttributeMap; 29 import org.springframework.webflow.core.collection.CollectionUtils; 30 import org.springframework.webflow.engine.AnnotatedAction; 31 import org.springframework.webflow.engine.Flow; 32 import org.springframework.webflow.engine.FlowAttributeMapper; 33 import org.springframework.webflow.engine.FlowExecutionExceptionHandler; 34 import org.springframework.webflow.engine.State; 35 import org.springframework.webflow.engine.TargetStateResolver; 36 import org.springframework.webflow.engine.Transition; 37 import org.springframework.webflow.engine.TransitionCriteria; 38 import org.springframework.webflow.engine.ViewSelector; 39 import org.springframework.webflow.engine.support.ActionTransitionCriteria; 40 import org.springframework.webflow.execution.Action; 41 import org.springframework.webflow.execution.ScopeType; 42 import org.springframework.webflow.execution.support.EventFactorySupport; 43 44 142 public abstract class AbstractFlowBuilder extends BaseFlowBuilder { 143 144 148 private EventFactorySupport eventFactorySupport = new EventFactorySupport(); 149 150 153 protected AbstractFlowBuilder() { 154 super(); 155 } 156 157 163 protected AbstractFlowBuilder(FlowServiceLocator flowServiceLocator) { 164 super(flowServiceLocator); 165 } 166 167 171 public EventFactorySupport getEventFactorySupport() { 172 return eventFactorySupport; 173 } 174 175 179 public void setEventFactorySupport(EventFactorySupport eventFactorySupport) { 180 this.eventFactorySupport = eventFactorySupport; 181 } 182 183 public void init(String flowId, AttributeMap attributes) throws FlowBuilderException { 184 setFlow(getFlowArtifactFactory().createFlow(flowId, flowAttributes().union(attributes))); 185 } 186 187 193 protected AttributeMap flowAttributes() { 194 return CollectionUtils.EMPTY_ATTRIBUTE_MAP; 195 } 196 197 199 206 protected State addViewState(String stateId, String viewName, Transition transition) { 207 return getFlowArtifactFactory().createViewState(stateId, getFlow(), null, viewSelector(viewName), null, 208 new Transition[] { transition }, null, null, null); 209 } 210 211 218 protected State addViewState(String stateId, String viewName, Transition[] transitions) { 219 return getFlowArtifactFactory().createViewState(stateId, getFlow(), null, viewSelector(viewName), null, 220 transitions, null, null, null); 221 } 222 223 232 protected State addViewState(String stateId, String viewName, Action renderAction, Transition transition) { 233 return getFlowArtifactFactory().createViewState(stateId, getFlow(), null, viewSelector(viewName), 234 new Action[] { renderAction }, new Transition[] { transition }, null, null, null); 235 } 236 237 246 protected State addViewState(String stateId, String viewName, Action renderAction, Transition[] transitions) { 247 return getFlowArtifactFactory().createViewState(stateId, getFlow(), null, viewSelector(viewName), 248 new Action[] { renderAction }, transitions, null, null, null); 249 } 250 251 266 protected State addViewState(String stateId, Action[] entryActions, ViewSelector viewSelector, 267 Action[] renderActions, Transition[] transitions, FlowExecutionExceptionHandler[] exceptionHandlers, 268 Action[] exitActions, AttributeMap attributes) { 269 return getFlowArtifactFactory().createViewState(stateId, getFlow(), entryActions, viewSelector, renderActions, 270 transitions, exceptionHandlers, exitActions, attributes); 271 } 272 273 275 282 protected State addActionState(String stateId, Action action, Transition transition) { 283 return getFlowArtifactFactory().createActionState(stateId, getFlow(), null, new Action[] { action }, 284 new Transition[] { transition }, null, null, null); 285 } 286 287 294 protected State addActionState(String stateId, Action action, Transition[] transitions) { 295 return getFlowArtifactFactory().createActionState(stateId, getFlow(), null, new Action[] { action }, 296 transitions, null, null, null); 297 } 298 299 308 protected State addActionState(String stateId, Action action, Transition transition, 309 FlowExecutionExceptionHandler exceptionHandler) { 310 return getFlowArtifactFactory().createActionState(stateId, getFlow(), null, new Action[] { action }, 311 new Transition[] { transition }, new FlowExecutionExceptionHandler[] { exceptionHandler }, null, null); 312 } 313 314 328 protected State addActionState(String stateId, Action[] entryActions, Action[] actions, Transition[] transitions, 329 FlowExecutionExceptionHandler[] exceptionHandlers, Action[] exitActions, AttributeMap attributes) { 330 return getFlowArtifactFactory().createActionState(stateId, getFlow(), entryActions, actions, transitions, 331 exceptionHandlers, exitActions, attributes); 332 } 333 334 336 342 protected State addDecisionState(String stateId, Transition[] transitions) { 343 return getFlowArtifactFactory().createDecisionState(stateId, getFlow(), null, transitions, null, null, null); 344 } 345 346 354 protected State addDecisionState(String stateId, TransitionCriteria decisionCriteria, String trueStateId, 355 String falseStateId) { 356 Transition thenTransition = getFlowArtifactFactory() 357 .createTransition(to(trueStateId), decisionCriteria, null, null); 358 Transition elseTransition = getFlowArtifactFactory().createTransition(to(falseStateId), null, null, null); 359 return getFlowArtifactFactory().createDecisionState(stateId, getFlow(), null, 360 new Transition[] { thenTransition, elseTransition }, null, null, null); 361 } 362 363 375 protected State addDecisionState(String stateId, Action[] entryActions, Transition[] transitions, 376 FlowExecutionExceptionHandler[] exceptionHandlers, Action[] exitActions, AttributeMap attributes) { 377 return getFlowArtifactFactory().createDecisionState(stateId, getFlow(), entryActions, transitions, 378 exceptionHandlers, exitActions, attributes); 379 } 380 381 383 392 protected State addSubflowState(String stateId, Flow subflow, FlowAttributeMapper attributeMapper, 393 Transition transition) { 394 return getFlowArtifactFactory().createSubflowState(stateId, getFlow(), null, subflow, attributeMapper, 395 new Transition[] { transition }, null, null, null); 396 } 397 398 407 protected State addSubflowState(String stateId, Flow subflow, FlowAttributeMapper attributeMapper, 408 Transition[] transitions) { 409 return getFlowArtifactFactory().createSubflowState(stateId, getFlow(), null, subflow, attributeMapper, 410 transitions, null, null, null); 411 } 412 413 428 protected State addSubflowState(String stateId, Action[] entryActions, Flow subflow, 429 FlowAttributeMapper attributeMapper, Transition[] transitions, 430 FlowExecutionExceptionHandler[] exceptionHandlers, Action[] exitActions, AttributeMap attributes) { 431 return getFlowArtifactFactory().createSubflowState(stateId, getFlow(), entryActions, subflow, attributeMapper, 432 transitions, exceptionHandlers, exitActions, attributes); 433 } 434 435 437 442 protected State addEndState(String stateId) { 443 return getFlowArtifactFactory().createEndState(stateId, getFlow(), null, null, null, null, null); 444 } 445 446 452 protected State addEndState(String stateId, String viewName) { 453 return getFlowArtifactFactory().createEndState(stateId, getFlow(), null, viewSelector(viewName), null, 454 null, null); 455 } 456 457 465 protected State addEndState(String stateId, String viewName, AttributeMapper outputMapper) { 466 return getFlowArtifactFactory().createEndState(stateId, getFlow(), null, viewSelector(viewName), 467 outputMapper, null, null); 468 } 469 470 483 protected State addEndState(String stateId, Action[] entryActions, ViewSelector viewSelector, 484 AttributeMapper outputMapper, FlowExecutionExceptionHandler[] exceptionHandlers, AttributeMap attributes) { 485 return getFlowArtifactFactory().createEndState(stateId, getFlow(), entryActions, viewSelector, outputMapper, 486 exceptionHandlers, attributes); 487 } 488 489 491 498 public ViewSelector viewSelector(String viewName) { 499 return (ViewSelector)fromStringTo(ViewSelector.class).execute(viewName); 500 } 501 502 509 protected Action action(String id) throws FlowArtifactLookupException { 510 return getFlowServiceLocator().getAction(id); 511 } 512 513 522 protected Action action(String beanId, MethodSignature methodSignature) throws FlowArtifactLookupException { 523 return getBeanInvokingActionFactory().createBeanInvokingAction(beanId, 524 getFlowServiceLocator().getBeanFactory(), methodSignature, null, 525 getFlowServiceLocator().getConversionService(), null); 526 } 527 528 537 protected Action action(String beanId, MethodSignature methodSignature, ActionResultExposer resultExposer) 538 throws FlowArtifactLookupException { 539 return getBeanInvokingActionFactory().createBeanInvokingAction(beanId, 540 getFlowServiceLocator().getBeanFactory(), methodSignature, resultExposer, 541 getFlowServiceLocator().getConversionService(), null); 542 } 543 544 548 protected Action action(Expression expression) { 549 return action(expression, null); 550 } 551 552 557 protected Action action(Expression expression, ActionResultExposer resultExposer) { 558 return new EvaluateAction(expression, resultExposer); 559 } 560 561 567 protected Expression expression(String expressionString) { 568 return getFlowServiceLocator().getExpressionParser().parseExpression(expressionString); 569 } 570 571 592 protected MethodSignature method(String method) { 593 return (MethodSignature)fromStringTo(MethodSignature.class).execute(method); 594 } 595 596 604 protected ActionResultExposer result(String resultName) { 605 return result(resultName, ScopeType.REQUEST); 606 } 607 608 617 protected ActionResultExposer result(String resultName, ScopeType resultScope) { 618 return new ActionResultExposer(resultName, resultScope); 619 } 620 621 633 protected AnnotatedAction invoke(String methodName, MultiAction multiAction) throws FlowArtifactLookupException { 634 AnnotatedAction action = new AnnotatedAction(multiAction); 635 action.setMethod(methodName); 636 return action; 637 } 638 639 649 protected FlowAttributeMapper attributeMapper(String id) throws FlowArtifactLookupException { 650 return getFlowServiceLocator().getAttributeMapper(id); 651 } 652 653 663 protected Flow flow(String id) throws FlowArtifactLookupException { 664 return getFlowServiceLocator().getSubflow(id); 665 } 666 667 674 protected TransitionCriteria on(String transitionCriteriaExpression) { 675 return (TransitionCriteria)fromStringTo(TransitionCriteria.class).execute(transitionCriteriaExpression); 676 } 677 678 683 protected TargetStateResolver to(String targetStateIdExpression) { 684 return (TargetStateResolver)fromStringTo(TargetStateResolver.class).execute(targetStateIdExpression); 685 } 686 687 694 protected Transition transition(TransitionCriteria matchingCriteria, TargetStateResolver targetStateResolver) { 695 return getFlowArtifactFactory().createTransition(targetStateResolver, matchingCriteria, null, null); 696 } 697 698 707 protected Transition transition(TransitionCriteria matchingCriteria, TargetStateResolver targetStateResolver, 708 TransitionCriteria executionCriteria) { 709 return getFlowArtifactFactory().createTransition(targetStateResolver, matchingCriteria, executionCriteria, null); 710 } 711 712 722 protected Transition transition(TransitionCriteria matchingCriteria, TargetStateResolver targetStateResolver, 723 TransitionCriteria executionCriteria, AttributeMap attributes) { 724 return getFlowArtifactFactory() 725 .createTransition(targetStateResolver, matchingCriteria, executionCriteria, attributes); 726 } 727 728 739 protected TransitionCriteria ifReturnedSuccess(Action action) { 740 return new ActionTransitionCriteria(action); 741 } 742 743 748 protected String success() { 749 return eventFactorySupport.getSuccessEventId(); 750 } 751 752 757 protected String error() { 758 return eventFactorySupport.getErrorEventId(); 759 } 760 761 766 protected String submit() { 767 return "submit"; 768 } 769 770 775 protected String back() { 776 return "back"; 777 } 778 779 784 protected String cancel() { 785 return "cancel"; 786 } 787 788 793 protected String finish() { 794 return "finish"; 795 } 796 797 802 protected String select() { 803 return "select"; 804 } 805 806 811 protected String edit() { 812 return "edit"; 813 } 814 815 820 protected String add() { 821 return "add"; 822 } 823 824 829 protected String delete() { 830 return "delete"; 831 } 832 833 838 protected String yes() { 839 return eventFactorySupport.getYesEventId(); 840 } 841 842 847 protected String no() { 848 return eventFactorySupport.getNoEventId(); 849 } 850 851 857 protected MappingBuilder mapping() { 858 MappingBuilder mapping = new MappingBuilder(getFlowServiceLocator().getExpressionParser()); 859 mapping.setConversionService(getFlowServiceLocator().getConversionService()); 860 return mapping; 861 } 862 863 865 private FlowArtifactFactory getFlowArtifactFactory() { 866 return getFlowServiceLocator().getFlowArtifactFactory(); 867 } 868 869 private BeanInvokingActionFactory getBeanInvokingActionFactory() { 870 return getFlowServiceLocator().getBeanInvokingActionFactory(); 871 } 872 } | Popular Tags |