<?xml version="1.0"?>
<dc-workflow xmlns:i18n="http://xml.zope.org/namespaces/i18n"
             i18n:domain="senaite.health"
             workflow_id="senaite_health_patient_workflow"
             title="Senaite Health Patient Workflow"
             state_variable="review_state"
             initial_state="active"
             manager_bypass="False">

  <permission>Access contents information</permission>
  <permission>Delete objects</permission>
  <permission>List folder contents</permission>
  <permission>Modify portal content</permission>
  <permission>View</permission>

  <!-- Senaite permissions for transitions (govern guards) -->
  <permission>senaite.health: Transition: Activate Patient</permission>
  <permission>senaite.health: Transition: Deactivate Patient</permission>
  <permission>senaite.health: Transition: Share Patient</permission>
  <permission>senaite.health: Transition: Unshare Patient</permission>

  <!-- State: active (external client) -->
  <state state_id="active" title="Active" i18n:attributes="title">

    <!-- Possible transitions from this state -->
    <exit-transition transition_id="share" />
    <exit-transition transition_id="deactivate" />

    <!-- Transition permissions -->
    <permission-map name="senaite.health: Transition: Share Patient" acquired="True" />
    <permission-map name="senaite.health: Transition: Deactivate Patient" acquired="True" />

    <permission-map name="Access contents information" acquired="False">
     <permission-role>Doctor</permission-role>
     <permission-role>LabClerk</permission-role>
     <permission-role>LabManager</permission-role>
     <permission-role>Manager</permission-role>
     <permission-role>Owner</permission-role>
    </permission-map>
    <permission-map name="Delete objects" acquired="False">
      <!-- Same roles as those that have Modify Portal Content permission
       This is necessary, cause Batches that are created inside Patient are
       moved by ObjectModifyEvent to parent folder. This trick is used to
       auto-fill the Patient in the Batch on creation
       -->
      <permission-role>LabClerk</permission-role>
      <permission-role>LabManager</permission-role>
      <permission-role>Manager</permission-role>
      <permission-role>Owner</permission-role>
    </permission-map>

    <permission-map name="List folder contents" acquired="False">
     <permission-role>Doctor</permission-role>
     <permission-role>LabClerk</permission-role>
     <permission-role>LabManager</permission-role>
     <permission-role>Manager</permission-role>
     <permission-role>Owner</permission-role>
    </permission-map>
    <permission-map name="Modify portal content" acquired="False">
     <permission-role>Doctor</permission-role>
     <permission-role>LabClerk</permission-role>
     <permission-role>LabManager</permission-role>
     <permission-role>Manager</permission-role>
     <permission-role>Owner</permission-role>
    </permission-map>
    <permission-map name="View" acquired="False">
     <permission-role>Doctor</permission-role>
     <permission-role>LabClerk</permission-role>
     <permission-role>LabManager</permission-role>
     <permission-role>Manager</permission-role>
     <permission-role>Owner</permission-role>
    </permission-map>
  </state>

  <!-- State: shared (internal client) -->
  <state state_id="shared" title="Active (shared)" i18n:attributes="title">

    <!-- Possible transitions from this state -->
    <exit-transition transition_id="unshare" />
    <exit-transition transition_id="deactivate" />

    <!-- Transition permissions -->
    <permission-map name="senaite.health: Transition: Unshare Patient" acquired="True" />
    <permission-map name="senaite.health: Transition: Deactivate Patient" acquired="True" />

    <permission-map name="Access contents information" acquired="False">
     <permission-role>Doctor</permission-role>
     <permission-role>LabClerk</permission-role>
     <permission-role>LabManager</permission-role>
     <permission-role>Manager</permission-role>
     <permission-role>Owner</permission-role>
     <permission-role>InternalClient</permission-role>
    </permission-map>
    <permission-map name="Delete objects" acquired="False">
      <!-- Same roles as those that have Modify Portal Content permission
       This is necessary, cause Batches that are created inside Patient are
       moved by ObjectModifyEvent to parent folder. This trick is used to
       auto-fill the Patient in the Batch on creation
       -->
      <permission-role>LabClerk</permission-role>
      <permission-role>LabManager</permission-role>
      <permission-role>Manager</permission-role>
      <permission-role>Owner</permission-role>
      <permission-role>InternalClient</permission-role>
    </permission-map>

    <permission-map name="List folder contents" acquired="False">
     <permission-role>Doctor</permission-role>
     <permission-role>LabClerk</permission-role>
     <permission-role>LabManager</permission-role>
     <permission-role>Manager</permission-role>
     <permission-role>Owner</permission-role>
     <permission-role>InternalClient</permission-role>
    </permission-map>
    <permission-map name="Modify portal content" acquired="False">
     <permission-role>Doctor</permission-role>
     <permission-role>LabClerk</permission-role>
     <permission-role>LabManager</permission-role>
     <permission-role>Manager</permission-role>
     <permission-role>Owner</permission-role>
     <permission-role>InternalClient</permission-role>
    </permission-map>
    <permission-map name="View" acquired="False">
     <permission-role>Doctor</permission-role>
     <permission-role>LabClerk</permission-role>
     <permission-role>LabManager</permission-role>
     <permission-role>Manager</permission-role>
     <permission-role>Owner</permission-role>
     <permission-role>InternalClient</permission-role>
    </permission-map>
  </state>

  <!-- State: inactive -->
  <state state_id="inactive" title="Inactive" i18n:attributes="title">

    <!-- Possible transitions from this state -->
    <exit-transition transition_id="activate" />

    <!-- Transition permissions -->
    <permission-map name="senaite.health: Transition: Activate Patient" acquired="True" />

    <!-- Plone's permissions -->
    <permission-map name="Access contents information" acquired="False">
     <permission-role>LabClerk</permission-role>
     <permission-role>LabManager</permission-role>
     <permission-role>Manager</permission-role>
     <permission-role>Owner</permission-role>
    </permission-map>
    <permission-map name="Delete objects" acquired="False"/>
    <permission-map name="List folder contents" acquired="False">
     <permission-role>LabClerk</permission-role>
     <permission-role>LabManager</permission-role>
     <permission-role>Manager</permission-role>
     <permission-role>Owner</permission-role>
    </permission-map>
    <permission-map name="Modify portal content" acquired="False"/>
    <permission-map name="View" acquired="False">
     <permission-role>LabClerk</permission-role>
     <permission-role>LabManager</permission-role>
     <permission-role>Manager</permission-role>
     <permission-role>Owner</permission-role>
    </permission-map>
  </state>

  <!-- Transition: activate -->
  <transition transition_id="activate" title="Activate" new_state="active" trigger="USER" before_script="" after_script="" i18n:attributes="title">
    <action url="" category="workflow" icon="">Activate</action>
    <guard>
      <guard-permission>senaite.health: Transition: Activate Patient</guard-permission>
      <guard-expression>python:here.guard_handler("activate")</guard-expression>
    </guard>
  </transition>

  <!-- Transition: deactivate -->
  <transition transition_id="deactivate" title="Deactivate" new_state="inactive" trigger="USER" before_script="" after_script="" i18n:attributes="title">
    <action url="" category="workflow" icon="">Deactivate</action>
    <guard>
      <guard-permission>senaite.health: Transition: Deactivate Patient</guard-permission>
      <guard-expression>python:here.guard_handler("deactivate")</guard-expression>
    </guard>
  </transition>

  <!-- Transition: share -->
  <transition transition_id="share" title="Share" new_state="shared" trigger="USER" before_script="" after_script="" i18n:attributes="title">
    <action url="" category="workflow" icon="">Share</action>
    <guard>
      <guard-permission>senaite.health: Transition: Share Patient</guard-permission>
      <guard-expression>python:here.guard_handler("share")</guard-expression>
    </guard>
  </transition>

  <!-- Transition: unshare -->
  <transition transition_id="unshare" title="Unshare" new_state="active" trigger="USER" before_script="" after_script="" i18n:attributes="title">
    <action url="" category="workflow" icon="">Unshare</action>
    <guard>
      <guard-permission>senaite.health: Transition: Unshare Patient</guard-permission>
      <guard-expression>python:here.guard_handler("unshare")</guard-expression>
    </guard>
  </transition>

  <variable variable_id="action" for_catalog="False" for_status="True" update_always="True">
   <description i18n:attributes="title">Previous transition</description>
   <default>
    <expression>transition/getId|nothing</expression>
   </default>
   <guard>
   </guard>
  </variable>

  <variable variable_id="actor" for_catalog="False" for_status="True" update_always="True">
   <description i18n:attributes="title">The ID of the user who performed the previous transition</description>
   <default>
    <expression>user/getId</expression>
   </default>
   <guard>
   </guard>
  </variable>

  <variable variable_id="comments" for_catalog="False" for_status="True" update_always="True">
   <description i18n:attributes="title">Comment about the last transition</description>
   <default>
    <expression>python:state_change.kwargs.get('comment', '')</expression>
   </default>
   <guard>
   </guard>
  </variable>

  <variable variable_id="review_history" for_catalog="False" for_status="False" update_always="False">
   <description i18n:attributes="title">Provides access to workflow history</description>
   <default>
    <expression>state_change/getHistory</expression>
   </default>
   <guard>
    <guard-permission>Request review</guard-permission>
    <guard-permission>Review portal content</guard-permission>
   </guard>
  </variable>

  <variable variable_id="time" for_catalog="False" for_status="True" update_always="True">
   <description i18n:attributes="title">When the previous transition was performed</description>
   <default>
    <expression>state_change/getDateTime</expression>
   </default>
   <guard>
   </guard>
  </variable>

</dc-workflow>
