<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:flowable="http://flowable.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.flowable.org/processdef">
    <process id="{{workflow.id}}" isClosed="false" isExecutable="true" name="{{workflow.name}}" processType="None">
        <extensionElements>
            <flowable:eventListener delegateExpression="${processInstanceListener}"  />
            {{#if workflow.refGroups}}
            <flowable:field name="refgroups">
                <flowable:string>{{#each workflow.refGroups as |refGroup|}}{{@key}}{{#unless @last}},{{/unless}}{{/each}}</flowable:string>
            </flowable:field>
            {{/if}}
            {{#if workflow.bookings}}
            <flowable:field name="bookings">
                <flowable:string>{{#each workflow.bookings as |booking|}}{{pluralize booking.codeName}}{{#unless @last}},{{/unless}}{{/each}}</flowable:string>
            </flowable:field>    
            {{/if}}
            {{#if workflow.mobApps}}
            {{#each workflow.mobApps as |mobApp|}}
            <flowable:field name="bookingmobs_{{pluralize @key}}">
                <flowable:string>{{#each this as |app|}}{{app.codeName}}{{#unless @last}},{{/unless}}{{/each}}</flowable:string>
            </flowable:field>       
            {{/each}}
            {{/if}}
            {{#if workflow.webApps}}
            {{#each workflow.webApps as |webApp|}}
            <flowable:field name="bookingapps_{{pluralize @key}}">
                <flowable:string>{{#each this as |app|}}{{app.codeName}}{{#unless @last}},{{/unless}}{{/each}}</flowable:string>
            </flowable:field>       
            {{/each}}
            {{/if}}

            {{#each workflow.psWorkflow.psWFDEs as |wfde|}}
            {{#if wfde.wFStepPSDEField}}
            <flowable:field name="wfstepfield_{{pluralize wfde.psDataEntity.codeName}}">
                <flowable:string>{{lowerCase wfde.wFStepPSDEField.codeName}}</flowable:string>
            </flowable:field>    
            {{/if}}
            {{#if wfde.wFInstPSDEField}}
            <flowable:field name="wfinstfield_{{pluralize wfde.psDataEntity.codeName}}">
                <flowable:string>{{lowerCase wfde.wFInstPSDEField.codeName}}</flowable:string>
            </flowable:field>    
            {{/if}}
            {{#if wfde.uDStatePSDEField}}
            <flowable:field name="udstatefield_{{pluralize wfde.psDataEntity.codeName}}">
                <flowable:string>{{lowerCase wfde.uDStatePSDEField.codeName}}</flowable:string>
            </flowable:field>    
            {{/if}}
            {{#if workflow.psWorkflow.entityWFState}}
            <flowable:field name="udstateingval_{{pluralize wfde.psDataEntity.codeName}}">
                <flowable:string>{{workflow.psWorkflow.entityWFState}}</flowable:string>
            </flowable:field>
            {{/if}}
            {{#if workflow.psWorkflow.entityWFFinishState}}
                {{#neq workflow.psWorkflow.entityWFFinishState ""}}
            <flowable:field name="wffinishval_{{pluralize wfde.psDataEntity.codeName}}">
                <flowable:string>{{workflow.psWorkflow.entityWFFinishState}}</flowable:string>
            </flowable:field>
                {{/neq}}
            {{/if}}
            {{#if workflow.psWorkflow.entityWFErrorState}}
                {{#neq workflow.psWorkflow.entityWFErrorState ""}}
            <flowable:field name="wferrorval_{{pluralize wfde.psDataEntity.codeName}}">
                <flowable:string>{{workflow.psWorkflow.entityWFErrorState}}</flowable:string>
            </flowable:field>
                {{/neq}}
            {{/if}}
            {{#if wfde.wFStatePSDEField}}
            <flowable:field name="wfstatefield_{{pluralize wfde.psDataEntity.codeName}}">
                <flowable:string>{{lowerCase wfde.wFStatePSDEField.codeName}}</flowable:string>
            </flowable:field>    
            {{/if}}
            {{#if wfde.wFVerPSDEField}}
            <flowable:field name="wfverfield_{{pluralize wfde.psDataEntity.codeName}}">
                <flowable:string>{{lowerCase wfde.wFVerPSDEField.codeName}}</flowable:string>
            </flowable:field>    
            {{/if}}
            {{#if wfde.psDataEntity.majorPSDEField}}
            <flowable:field name="majortext_{{pluralize wfde.psDataEntity.codeName}}">
                <flowable:string>{{lowerCase wfde.psDataEntity.majorPSDEField.codeName}}</flowable:string>
            </flowable:field>    
            {{/if}}
            {{/each}}
            {{#each workflow.bookings as |booking|}}
            {{#if booking.orgField}}
            <flowable:field name="orgfield_{{pluralize booking.codeName}}">
                <flowable:string>{{lowerCase booking.orgField.codeName}}</flowable:string>
            </flowable:field>    
            {{/if}}
            {{/each}}
            <flowable:field name="isvalid"><flowable:string>{{#if workflow.valid}}1{{else}}0{{/if}}</flowable:string></flowable:field>
        </extensionElements>
            {{#if workflow.psWFProcesses}}
                {{#each workflow.psWFProcesses as |WFProcess|}}
                    {{#eq WFProcess.wFProcessType 'START'}}
        <startEvent id="sid-{{WFProcess.codeName}}" name="{{WFProcess.name}}">
            {{#if (or WFProcess.formCodeName WFProcess.mobFormCodeName)}}
            <extensionElements>
                <flowable:form {{#if WFProcess.formCodeName}}process-form="{{WFProcess.formCodeName}}"{{/if}} {{#if WFProcess.mobFormCodeName}}process-mobform="{{WFProcess.mobFormCodeName}}"{{/if}} {{#if WFProcess.wFVersion}}wfversion="{{WFProcess.wFVersion}}"{{/if}}/>    
            </extensionElements>    
            {{/if}}
        </startEvent>
                    {{/eq}}
                    {{#eq WFProcess.wFProcessType 'END'}}
        <endEvent id="sid-{{WFProcess.codeName}}" name="{{WFProcess.name}}">
            {{#if (or (and WFProcess.userData (neq WFProcess.userData "")) (and WFProcess.userData2 (neq WFProcess.userData2 "")))}}
            <extensionElements>
                <flowable:form {{#if (and WFProcess.userData (neq WFProcess.userData ""))}}userdata="{{WFProcess.userData}}"{{/if}} {{#if (and WFProcess.userData2 (neq WFProcess.userData2 ""))}}userdata2="{{WFProcess.userData2}}"{{/if}} />
            </extensionElements>    
            {{/if}}
        </endEvent>
                    {{/eq}}
                    {{#eq WFProcess.wFProcessType 'PARALLELGATEWAY'}}
        <parallelGateway id="sid-{{WFProcess.codeName}}"/>
                    {{/eq}}
                    {{#eq WFProcess.wFProcessType 'INCLUSIVEGATEWAY'}}
        <inclusiveGateway id="sid-{{WFProcess.codeName}}"/>
                    {{/eq}}
                    {{#eq WFProcess.wFProcessType 'EXCLUSIVEGATEWAY'}}
        <exclusiveGateway id="sid-{{WFProcess.codeName}}"/>
                    {{/eq}}
                    {{#eq WFProcess.wFProcessType 'PROCESS'}}
        <serviceTask id="sid-{{WFProcess.codeName}}" name="{{WFProcess.name}}" flowable:expression="${wfCoreService.execute(execution, activedata)}" >
            <extensionElements>
                <flowable:field name="service-entity"><flowable:string>{{pluralize WFProcess.psDataEntity.codeName}}</flowable:string></flowable:field>
                <flowable:field name="service-deaction"><flowable:string>{{lowerCase WFProcess.psDEAction.codeName}}</flowable:string></flowable:field>
                            {{#if WFProcess.psWFProcessParams}}
                                {{#each WFProcess.psWFProcessParams as |processparams|}}
                                {{#if processparams.dstField (neq processparams.dstField "")}}
                                    {{#if (and processparams.srcValueType (neq processparams.srcValueType ""))}}
                                        {{#eq processparams.srcValue "CURTIME"}}
                <flowable:field name="params-{{lowerCase processparams.dstField}}"><flowable:expression><![CDATA[${wfCoreService.getnow()}]]></flowable:expression></flowable:field>
                                        {{/eq}}
                                        {{#eq processparams.srcValue "OPERATOR"}}
                <flowable:field name="params-{{lowerCase processparams.dstField}}"><flowable:expression><![CDATA[${curuser.userid}]]></flowable:expression></flowable:field>
                                        {{/eq}}
                                        {{#eq processparams.srcValue "OPERATORNAME"}}
                <flowable:field name="params-{{lowerCase processparams.dstField}}"><flowable:expression><![CDATA[${curuser.personname}]]></flowable:expression></flowable:field>
                                        {{/eq}}
                                        {{#eq processparams.srcValue "CONTEXT"}}
                <flowable:field name="params-{{lowerCase processparams.dstField}}"><flowable:expression><![CDATA[${activedata.{{lowerCase processparams.srcValue}} }]]></flowable:expression></flowable:field>
                                        {{/eq}}
                                        {{#eq processparams.srcValue "SESSION"}}
                <flowable:field name="params-{{lowerCase processparams.dstField}}"><flowable:expression><![CDATA[${curuser.sessionParams.{{lowerCase processparams.srcValue}} }]]></flowable:expression></flowable:field>
                                        {{/eq}}
                                    {{else}}
                                        {{#if (and processparams.srcValue (neq processparams.srcValue ""))}}
                <flowable:field name="params-{{lowerCase processparams.dstField}}"><flowable:string>{{processparams.srcValue}}</flowable:string></flowable:field>                                
                                        {{/if}}
                                    {{/if}}
                                {{/if}}
                                {{/each}}
                            {{/if}}
            </extensionElements>
        </serviceTask>
                    {{/eq}}
                {{/each}}
                {{#each workflow.userTasks as |userTask|}}
        <userTask flowable:category="${businessKey}" {{#if userTask.timeOut}}flowable:dueDate="{{userTask.timeoutStrategy}}" {{/if}}flowable:candidateUsers="{{#if userTask.multiInstanceLoopCharacteristics}}${candidateUsers}{{else}}{{userTask.assignCond}}{{/if}}" flowable:exclusive="true" 
         id="tid-{{userTask.process.wFStepValue}}-{{userTask.process.codeName}}" 
         name="{{userTask.process.name}}" {{#if (or (eq userTask.process.wFProcessType "CALLORGACTIVITY") (eq userTask.process.wFProcessType "EMBED"))}}flowable:formKey="{{userTask.process.wFProcessType}}"{{/if}}>
            <documentation>${majortext}</documentation>
            {{#if userTask.formParam}}
            <extensionElements>
                <flowable:form {{#each userTask.formParam as |itemParam|}}{{itemParam}} {{/each}}/> 
            </extensionElements>
            {{/if}}
            {{#if userTask.multiInstanceLoopCharacteristics}}
            <multiInstanceLoopCharacteristics flowable:collection="candidateUsersList" flowable:elementVariable="candidateUsers" isSequential="{{userTask.sequential}}">
                <completionCondition><![CDATA[${nrOfCompletedInstances/nrOfInstances == 1}]]></completionCondition>
            </multiInstanceLoopCharacteristics>   
            {{/if}}
        </userTask>
        {{#if userTask.timeOut}}
        <boundaryEvent id="bid-{{userTask.process.wFStepValue}}-{{userTask.process.codeName}}" name="timeout-{{userTask.process.name}}" attachedToRef="tid-{{userTask.process.wFStepValue}}-{{userTask.process.codeName}}" cancelActivity="true">
            <timerEventDefinition>
                <timeDate>{{userTask.timeoutStrategy}}</timeDate>
            </timerEventDefinition>
        </boundaryEvent>    
        {{/if}}            
                {{/each}}
            {{/if}}

        {{#each workflow.sequenceFlows as |sequenceFlow|}}
        <sequenceFlow id="{{sequenceFlow.flowId}}" sourceRef="{{sequenceFlow.sourceProcessId}}" targetRef="{{sequenceFlow.targetProcessId}}" name="{{sequenceFlow.link.logicName}}">
            {{#if sequenceFlow.linkCond}}
            <conditionExpression  xsi:type="tFormalExpression" >{{sequenceFlow.linkCond}}</conditionExpression>
            {{/if}}
            {{#if sequenceFlow.linkParam}}
            <extensionElements>
                <flowable:form {{#each sequenceFlow.linkParam as |itemParam|}}{{itemParam}} {{/each}}/> 
            </extensionElements>    
            {{/if}}
        </sequenceFlow>    
        {{/each}}
    </process>
    
    <bpmndi:BPMNDiagram id="BPMNDiagram_{{workflow.id}}">
        <bpmndi:BPMNPlane id="BPMNPlane_{{workflow.id}}" bpmnElement="{{workflow.id}}">
            {{#each workflow.processes as |task|}}
            <bpmndi:BPMNShape id="BPMNShape-{{task.processId}}" bpmnElement="{{task.processId}}">
                <omgdi:Bounds x="{{task.process.leftPos}}" y="{{task.process.topPos}}" width="{{task.process.width}}" height="{{task.process.height}}" />
            </bpmndi:BPMNShape>
            {{/each}}
            {{#each workflow.sequenceFlows as |sequenceFlow|}}
            <bpmndi:BPMNEdge id="BPMNEdge-{{sequenceFlow.flowId}}" bpmnElement="{{sequenceFlow.flowId}}">
                <omgdi:waypoint x="0" y="0" />
                <omgdi:waypoint x="0" y="0" />
            </bpmndi:BPMNEdge>
            {{/each}}
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>