#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

apiVersion: sonataflow.org/v1alpha08
kind: SonataFlow
metadata:
  annotations:
    sonataflow.org/description: Order Workflow Sample
    sonataflow.org/expressionLang: jq
    sonataflow.org/profile: dev
    sonataflow.org/version: "1.0"
  creationTimestamp: null
  labels:
    app: order
  name: order
  namespace: my-workflows
spec:
  flow:
    events:
    - correlation:
      - contextAttributeName: orderid
      dataOnly: true
      kind: consumed
      name: orderEvent
      source: Client
      type: OrderEventType
    - correlation:
      - contextAttributeName: orderid
      dataOnly: true
      kind: consumed
      name: shippingEvent
      source: Shipper
      type: ShippingEventType
    - correlation:
      - contextAttributeName: orderid
      dataOnly: true
      kind: consumed
      name: cancelEvent
      source: Client
      type: CancelEventType
    functions:
    - name: printMessage
      operation: sysout
      type: custom
    - name: sendOrder
      operation: specs/supplier.yaml#sendOrder
      type: rest
    - name: cancelOrder
      operation: specs/supplier.yaml#cancelOrder
      type: rest
    start:
      stateName: Order Received
    states:
    - exclusive: true
      name: Order Received
      onEvents:
      - actionMode: sequential
        eventDataFilter:
          useData: true
        eventRefs:
        - orderEvent
      transition:
        nextState: Check Inventory
      type: event
    - actionMode: sequential
      actions:
      - actionDataFilter:
          fromStateData: .
          results: '{inventory: .item | test("0+") }'
          useResults: true
        functionRef:
          arguments:
            message: '"Check Inventory " + .orderId'
          invoke: sync
          refName: printMessage
        name: printAction
      name: Check Inventory
      transition:
        nextState: Item Available?
      type: operation
    - dataConditions:
      - condition: .inventory
        transition:
          nextState: Prepare for Shipping
      defaultCondition:
        transition:
          nextState: Forward to External Supplier
      name: Item Available?
      type: switch
    - actionMode: sequential
      actions:
      - actionDataFilter:
          useResults: true
        functionRef:
          arguments:
            message: '"Prepare for Shipping"'
          invoke: sync
          refName: printMessage
        name: printAction
      name: Prepare for Shipping
      transition:
        nextState: Order Shipped or Cancelled
      type: operation
    - actionMode: sequential
      actions:
      - actionDataFilter:
          useResults: true
        functionRef:
          arguments:
            content: .orderId
            supplier-id: '"1"'
          invoke: sync
          refName: sendOrder
        name: sendOrderRestCall
      name: Forward to External Supplier
      transition:
        nextState: Order Shipped or Cancelled
      type: operation
    - exclusive: true
      name: Order Shipped or Cancelled
      onEvents:
      - actionMode: sequential
        eventDataFilter:
          useData: true
        eventRefs:
        - shippingEvent
      - actionMode: sequential
        eventDataFilter:
          data: '{cancel:true}'
          useData: true
        eventRefs:
        - cancelEvent
      transition:
        nextState: Is Shipped?
      type: event
    - dataConditions:
      - condition: .cancel == true
        name: order cancelled
        transition:
          nextState: Compensate Order
      defaultCondition:
        transition:
          nextState: Notify Customer
      name: Is Shipped?
      type: switch
    - actionMode: sequential
      actions:
      - actionDataFilter:
          useResults: true
        functionRef:
          arguments:
            message: '"Compensate Order"'
          invoke: sync
          refName: printMessage
        name: printAction
      end:
        compensate: true
        terminate: true
      name: Compensate Order
      type: operation
    - actionMode: sequential
      actions:
      - actionDataFilter:
          useResults: true
        functionRef:
          arguments:
            message: '"Notify Customer"'
          invoke: sync
          refName: printMessage
        name: printAction
      end:
        terminate: true
      name: Notify Customer
      type: operation
  resources:
    configMaps:
    - configMap:
        name: 01-order-resources
      workflowPath: specs
status:
  address: {}
  lastTimeRecoverAttempt: null
