<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<object-views xmlns="http://axelor.com/xml/ns/object-views"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://axelor.com/xml/ns/object-views http://axelor.com/xml/ns/object-views/object-views_5.2.xsd">
  
  <grid name="wkf-grid" title="Process studio" model="com.axelor.studio.db.Wkf">
      <field name="name"/>
      <field name="model"/>
      <field name="appBuilder" grid-view="app-builder-grid" form-view="app-builder-form"/>
  </grid>
    
  <form name="wkf-form" title="Process studio" model="com.axelor.studio.db.Wkf" width="large" 
  	onSave="action-wkf-group-on-save" onNew="action-wkf-set-default">
     <panel name="mainPanel">
        <field name="name"/>
        <field name="appBuilder" grid-view="app-builder-grid" form-view="app-builder-form" onChange="action-wkf-reset-model" />
        <field name="isJson" colSpan="1" onChange="action-wkf-reset-model"/>
        <panel name="modelPanel" colSpan="5">
	        <field name="model" colSpan="12" showIf="isJson" widget="ref-text" x-target="com.axelor.meta.db.MetaJsonModel" x-target-name="name" onSelect="action-wkf-attrs-set-model-domain" />
	        <field name="model" colSpan="12" hideIf="isJson" widget="ref-text" x-target="com.axelor.meta.db.MetaModel" x-target-name="fullName" onSelect="action-wkf-attrs-set-model-domain" />
	    </panel>
	    <field name="jsonField" colSpan="4" requiredIf="model != null &amp;&amp; !isJson" readonlyIf="model == null &amp;&amp; !isJson" hideIf="isJson" widget="ref-text" 
	    	x-target="com.axelor.meta.db.MetaField" x-target-name="name" x-domain="self.metaModel.fullName = :model and self.json = true" onChange="action-wkf-attrs-reset-status-field"/>
	    <field name="isTrackFlow" colSpan="2"/>
        <field name="statusField" form-view="studio-meta-json-field-form" grid-view="studio-meta-json-field-grid" canNew="true" colSpan="4"
          onSelect="action-wkf-attrs-set-status-field-domain" onChange="action-wkf-validate-status-field,action-wkf-method-set-default-nodes" />
      	<field name="displayTypeSelect" colSpan="4" />
      	<button name="openDashboardBtn" title="Dashboard" onClick="open.wkf.dashboard" colSpan="4" />
      </panel>
      <panel-tabs>
      	<panel title="Process">
      	<field name="bpmnXml" showTitle="false" colSpan="12" widget="BpmnEditor" />
        <field name="description" widget="html" colSpan="12" />
      	<field name="$bpmnDefault" hidden="true" type="string" />
      	</panel>
      	<panel-related name="conditionsPanel" field="conditions" colSpan="12" editable="true" onNew="action-wkf-transition-filter-set-defaults">
	        <field name="logicOp" />
	        <field name="metaJsonField" required="true" widget="SuggestBox" onSelect="action-wkf-filter-meta-json-field-domain" onChange="action-filter-method-update-target-field" />
	        <field name="targetField" readonlyIf="metaJsonField.targetModel == null &amp;&amp; metaJsonField.targetJsonModel == null" requiredIf="metaJsonField.targetModel != null || metaJsonField.targetJsonModel != null"  />
	        <field name="operator"/>
	        <field name="value" onChange="action-studio-filter-validate-values" />
	        <field name="metaJsonField.targetModel" hidden="true" />
	        <field name="metaJsonField.targetJsonModel" hidden="true" />
	        <field name="isJson" hidden="true" />
	        <field name="targetType" hidden="true" /> 
	    </panel-related>
      </panel-tabs>
  </form>
  
  <action-group name="action-wkf-group-on-save">
      <action name="action-wkf-validate-bpmn-xml" />
      <action name="save" />
      <action name="action-wfk-method-process-xml" />
      <action name="action-wfk-method-process-wkf" />
  </action-group>
  
  <action-record name="action-wkf-set-default" model="com.axelor.studio.db.Wkf">
  	<field name="name" expr="eval:_jsonModel + ' workflow'" if="_jsonModel != null"/>
  	<field name="model" expr="eval:_jsonModel"/>
  </action-record>
  
  <action-method name="action-wfk-method-process-xml">
  	<call class="com.axelor.studio.web.WkfController" method="processXml"/>
  </action-method>
  
  <action-method name="action-wfk-method-process-wkf">
  	<call class="com.axelor.studio.web.WkfController" method="processWkf"/>
  </action-method>
  
  <action-validate name="action-wkf-validate-bpmn-xml">
    <error message="Please correct the workflow diagram." if="bpmnXml == null"/>
  </action-validate>
  
  <action-validate name="action-wkf-validate-status-field">
  	<alert message="Do you want to reset workflow ?" if="bpmnXml != null"/>
  </action-validate>
  
  <action-method name="action-wkf-method-set-default-nodes">
  	<call class="com.axelor.studio.web.WkfController" method="setDefaultNodes"/>
  </action-method>

  <action-attrs name="action-wkf-attrs-set-model-domain">
    <attribute name="domain" for="model" expr="eval:&quot;self.appBuilder.id = ${appBuilder.id} &quot;" if="isJson &amp;&amp; appBuilder != null" />
    <attribute name="domain" for="model" expr="eval: null" if="!isJson" />
  </action-attrs>
  
  <action-attrs name="action-wkf-attrs-set-status-field-domain">
  	<attribute name="domain" for="statusField" expr="eval:&quot;self.model = '${model}' and self.modelField = '${jsonField}' and (self.type = 'string' or self.type = 'integer') and self.appBuilder.id = ${appBuilder?.id} &quot;" if="!isJson" />
  	<attribute name="domain" for="statusField" expr="eval:&quot;self.jsonModel.name = '${model}' and (self.type = 'string' or self.type = 'integer') and (self.appBuilder.id = ${appBuilder?.id} or self.jsonModel.appBuilder.id = ${appBuilder?.id}) &quot;" if="isJson"/>
  </action-attrs>
  
  <action-attrs name="action-wkf-reset-model">
    <attribute name="value" for="jsonField" expr="eval: null" />
    <attribute name="value" for="model" expr="eval: null" />
    <attribute name="domain" for="model" expr="eval: null" />
  </action-attrs>
  
  <action-attrs name="action-wkf-attrs-reset-status-field">
  	<attribute name="value" for="statusField" expr="eval:null"/>
  </action-attrs>
  
  <action-attrs name="action-wkf-filter-meta-json-field-domain">
  	<attribute for="metaJsonField" name="domain" expr="eval:&quot;(self.jsonModel.name = '${_parent.model}' OR self.model = '${_parent.model}' ) AND (self.type != 'json-one-to-many' OR self.type != 'one-to-many' OR self.type != 'many-to-many' OR self.type != 'json-many-to-many') AND self.isWkf = false &quot;"/>
  </action-attrs>
  
  <action-view name="open.wkf.dashboard" title="Workflow dashboard">
    <view type="dashboard" name="dasbhoard-wkf" />
    <context name="_wkfId" expr="eval:id" if="_model == 'com.axelor.studio.db.Wkf'" />
    <context name="_wkfId" expr="eval:wkf.id" if="_model == 'com.axelor.studio.db.WkfTracking'" />
  </action-view>
  
</object-views>
