<?xml version="1.0" encoding="UTF-8"?>
<flow
    xmlns="http://www.springframework.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/webflow 
                        http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"
    parent="common-transitions"
    start-state="openTemplate">

    <input
        name="itemId"
        value="flowScope.itemId"
        type="string" />

    <input
        name="itemType"
        value="flowScope.itemType"
        type="gov.va.med.pharmacy.peps.common.vo.EntityType" />

    <input
        name="templateMethod"
        value="flowScope.templateMethod"
        type="string"
        required="true" />

    <!-- Boolean attribute that signifies if this conversation was started as a sub-flow. -->
    <!-- Used by Cancel buttons to know if they should send an event (if sub-flow) or history.back() (if not sub-flow). -->
    <!-- Save the old value to be reset on-exit in cases where this flow is called recursively. -->
    <on-start>
        <set
            name="flowScope.oldTemplateSubFlow"
            value="conversationScope.templateSubFlow"
            type="java.lang.Boolean" />
        <set
            name="conversationScope.templateSubFlow"
            value="flowScope.subFlow"
            type="java.lang.Boolean" />
    </on-start>

    <!-- Create the template by calling the ManagedItemAction method that corresponds to the templateMethod attribute -->
    <!-- passed in as an input to the flow. -->
    <action-state id="openTemplate">
        <evaluate expression="springWebFlowActionAdapter">
            <attribute
                name="actionName"
                value="managedItem" />
            <attribute
                name="actionMethod"
                value="${flowScope.templateMethod}" />
        </evaluate>
        <transition
            on="success"
            to="wizardAdd" />
    </action-state>

    <subflow-state
        id="wizardAdd"
        subflow="${flowScope.itemType.getViewName()}WizardAdd">
        <input
            name="item"
            value="flowScope.item"
            type="gov.va.med.pharmacy.peps.common.vo.ManagedItemVo" />
        <input
            name="itemType"
            value="flowScope.itemType"
            type="gov.va.med.pharmacy.peps.common.vo.EntityType" />
        <transition
            on="end"
            to="chooseEnd">
            <set
                name="flowScope.item"
                value="currentEvent.attributes.item"
                type="gov.va.med.pharmacy.peps.common.vo.ManagedItemVo" />
            <set
                name="flowScope.itemId"
                value="currentEvent.attributes.itemId"
                type="string" />
            <set
                name="flowScope.itemType"
                value="currentEvent.attributes.itemType"
                type="gov.va.med.pharmacy.peps.common.vo.EntityType" />
        </transition>
        <transition
            on="cancel"
            to="cancel" />
    </subflow-state>

    <decision-state id="chooseEnd">
        <if
            test="flowScope.subFlow"
            then="end" />
        <if
            test="flowScope.itemType.isEntityType()"
            then="restartItemFlow" />
        <if
            test="flowScope.itemType.isDomainType()"
            then="restartDomainFlow"
            else="restartItemFlow" />
    </decision-state>

    <end-state id="cancel" />

    <end-state id="end">
        <output
            name="itemId"
            value="flowScope.itemId"
            type="string" />
        <output
            name="itemType"
            value="flowScope.itemType"
            type="gov.va.med.pharmacy.peps.common.vo.EntityType" />
        <output
            name="item"
            value="flowScope.item"
            type="gov.va.med.pharmacy.peps.common.vo.ManagedItemVo" />
    </end-state>

    <end-state
        id="restartDomainFlow"
        view="flowRedirect:manageDomain" />

    <end-state
        id="restartItemFlow"
        view="flowRedirect:manageItem" />

    <!-- Reset the conversation scoped templateSubFlow attribute in cases where this flow was called recursively. -->
    <on-end>
        <set
            name="conversationScope.templateSubFlow"
            value="flowScope.oldTemplateSubFlow"
            type="java.lang.Boolean" />
    </on-end>
</flow>