<?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">

  <input name="pageTitleCode" required="true"/>
  <input name="lastSubmitCode" required="true"/>
  <input name="community" required="true"/>

  <view-state id="codeForm" view="community.Form" model="community">
    <binder>
      <binding property="name"/>
    </binder>
    <on-render>
      <set name="flashScope.canChangeToSingleThreadCommunity" value="domainFactory.prototype(community).canChangeTo('org.unitedfront2.domain.communication.SingleThreadCommunity')"/>
      <set name="flashScope.canChangeToMultiThreadCommunity" value="domainFactory.prototype(community).canChangeTo('org.unitedfront2.domain.communication.MultiThreadCommunity')"/>
      <set name="flashScope.validator" value="communityValidator"/>
    </on-render>
    <transition on="submit" to="readAccessControlForm">
     <set name="flowScope.community" value="domainFactory.prototype(community).changeTo('org.unitedfront2.domain.communication.' + requestParameters.type)"/>
    </transition>
  </view-state>

  <subflow-state id="readAccessControlForm" subflow="accesscontrol.form-sub-flow">
    <input name="pageTitleCode" required="true"/>
    <input name="pageDescriptionCode" value="'community.FormAction.formReadAccessControl.description'"/>
    <input name="formSubmitCode" value="'toNext'"/>
    <input name="accessControl" value="flowScope.community.readAccessControl" required="true"/>
    <output name="accessControl" value="flowScope.community.readAccessControl" required="true"/>
    <transition on="finish" to="postAccessControlForm"/>
  </subflow-state>

  <subflow-state id="postAccessControlForm" subflow="accesscontrol.form-sub-flow">
    <input name="pageTitleCode" required="true"/>
    <input name="pageDescriptionCode" value="'community.FormAction.formPostAccessControl.description'"/>
    <input name="formSubmitCode" value="'toNext'"/>
    <input name="accessControl" value="flowScope.community.postAccessControl" required="true"/>
    <output name="accessControl" value="flowScope.community.postAccessControl" required="true"/>
    <transition on="finish" to="messageForm"/>
  </subflow-state>

  <subflow-state id="messageForm" subflow="message.form-sub-flow">
    <input name="pageTitleCode" required="true"/>
    <input name="pageDescriptionCode" value="'community.FormAction.formMessage.description'"/>
    <input name="formSubmitCode" value="flowScope.lastSubmitCode" required="true"/>
    <input name="subjectRequired" value="true"/>
    <input name="message" value="flowScope.community.overview" required="true"/>
    <output name="message" value="flowScope.community.overview" required="true"/>
    <transition on="finish" to="store"/>
  </subflow-state>

  <action-state id="store">
    <evaluate expression="domainFactory.prototype(community).store()"/>
    <transition on="success" to="finish"/>
  </action-state>

  <end-state id="finish">
    <output name="community" required="true"/>
  </end-state>

</flow>
