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

  <secured attributes="ROLE_USER, ROLE_ADMIN"/>

  <var name="thread" class="org.unitedfront2.domain.communication.Thread"/>
  <var name="writeAccessControl" class="org.unitedfront2.domain.accesscontrol.AuthorOnly"/>

  <on-start>
    <set name="requestScope.communityCode" value="requestParameters.communityCode"/>
    <set name="requestScope.user" value="externalContext.requestMap._user"/>
    <set name="flowScope.community" value="communityTable.findByCodeForPost(communityCode, user)"/>
  </on-start>

  <subflow-state id="createForm" subflow="message.form-sub-flow">
    <input name="pageTitleCode" value="'community.ThreadCreateForm'"/>
    <input name="formSubmitCode" value="'doCreate'"/>
    <input name="subjectRequired" value="true"/>
    <output name="message" value="flowScope.overview" required="true"/>
    <transition on="finish" to="create"/>
  </subflow-state>

  <action-state id="create">
    <on-entry>
      <set name="requestScope.user" value="externalContext.requestMap._user"/>
      <set name="overview.ownerId" value="community.ownerId"/>
      <set name="overview.authorId" value="user.id"/>
      <set name="overview.readAccessControl" value="community.readAccessControl"/>
      <set name="overview.writeAccessControl" value="community.writeAccessControl"/>
      <set name="thread.overview" value="overview"/>
      <set name="thread.status" value="'AVAILABLE'"/>
      <set name="thread.ownerId" value="user.id"/>
      <set name="thread.readAccessControl" value="community.readAccessControl"/>
      <set name="thread.writeAccessControl" value="writeAccessControl"/>
      <set name="thread.postAccessControl" value="community.postAccessControl"/>
    </on-entry>
    <evaluate expression="domainFactory.prototype(community).registerThread(domainFactory.prototype(thread))" result="flowScope.thread"/>
    <transition to="finish"/>
  </action-state>

  <end-state id="finish" view="externalRedirect:contextRelative:/community/${flowScope.community.code}/${flowScope.thread.id}/index.html"/>

</flow>
