{
    "schema": "1.0",
    "key": "search-items-by-option-across-partitions",
    "title": "Search items by option across all partitions",
    "description": "Searches every partition of a table for the items whose LIFE_CYCLE field matches the selected option, and displays one access link per partition holding matches.",
    "userInputs": [
        {
            "key": "lifeCycle",
            "title": "Life cycle",
            "description": "Life cycle option to search for",
            "required": true,
            "mode": "SINGLE",
            "type": "SELECT",
            "options": [
                {
                    "key": "NEW",
                    "title": "New"
                },
                {
                    "key": "RENEWED",
                    "title": "Renewed"
                },
                {
                    "key": "ENDED",
                    "title": "Ended"
                }
            ],
            "default": "NEW"
        },
        {
            "key": "screen",
            "title": "Screen",
            "description": "Screen to use when opening items",
            "required": false,
            "type": "SCREEN",
            "default": ""
        }
    ],
    "tasks": [
        {
            "name": "data-table-find",
            "taskReferenceName": "find_table",
            "description": "Find the PRODUCTS table",
            "type": "SUB_WORKFLOW",
            "optional": false,
            "inputParameters": {
                "findMode": "REQUEST",
                "request": {
                    "method": "INLINE",
                    "contentType": "application/json",
                    "model": "default",
                    "json": {
                        "criteria": {
                            "type": "eq",
                            "field": "key",
                            "value": "PRODUCTS"
                        }
                    }
                }
            }
        },
        {
            "name": "switch",
            "taskReferenceName": "check_table_found",
            "description": "Stop if table not found",
            "type": "SWITCH",
            "optional": false,
            "evaluatorType": "javascript",
            "expression": "$.count > 0 ? 'OK' : 'NOT_FOUND'",
            "inputParameters": {
                "count": "${find_table.output.totalElements}"
            },
            "decisionCases": {
                "NOT_FOUND": [
                    {
                        "name": "terminate",
                        "taskReferenceName": "terminate_no_table",
                        "description": "Table not found",
                        "type": "TERMINATE",
                        "inputParameters": {
                            "terminationStatus": "FAILED",
                            "outputParameters": {
                                "message": "Table 'PRODUCTS' not found."
                            }
                        }
                    }
                ]
            },
            "defaultCase": []
        },
        {
            "name": "data-partition-find",
            "taskReferenceName": "find_partitions",
            "description": "List the partitions of the table, to resolve partition ids into partition keys",
            "type": "SUB_WORKFLOW",
            "optional": false,
            "inputParameters": {
                "findMode": "ALL",
                "request": {
                    "method": "INLINE",
                    "contentType": "application/json",
                    "model": "default",
                    "json": {
                        "context": {
                            "tableId": "${find_table.output.result.json[0].id}"
                        }
                    }
                }
            }
        },
        {
            "name": "data-item-find",
            "taskReferenceName": "find_items",
            "description": "Find the items matching the selected option, across every partition of the table",
            "type": "SUB_WORKFLOW",
            "optional": false,
            "inputParameters": {
                "findMode": "REQUEST",
                "findOptions": {
                    "partitionSelection": "TABLE",
                    "tableKey": "PRODUCTS"
                },
                "request": {
                    "method": "INLINE",
                    "contentType": "application/json",
                    "model": "default",
                    "json": {
                        "criteria": {
                            "type": "eq",
                            "field": {
                                "target": "item.fields",
                                "key": "LIFE_CYCLE"
                            },
                            "value": "${workflow.input.lifeCycle}"
                        }
                    }
                }
            }
        },
        {
            "name": "code-execute-javascript",
            "taskReferenceName": "build_message_metadata",
            "description": "Group the items by partition and build the HTML message with access links",
            "type": "SUB_WORKFLOW",
            "optional": false,
            "inputParameters": {
                "expressionFile": "file://assets/build-message.js",
                "data": {
                    "items": "${find_items.output.result.json}",
                    "partitions": "${find_partitions.output.result.json}",
                    "fieldKey": "LIFE_CYCLE",
                    "optionKey": "${workflow.input.lifeCycle}",
                    "tableKey": "PRODUCTS",
                    "screenKey": "${workflow.input.screen.key}",
                    "appDomain": "app.product-live.com"
                }
            }
        },
        {
            "name": "code-execute-javascript",
            "taskReferenceName": "build_message_file",
            "description": "Write the HTML message to a file",
            "type": "SUB_WORKFLOW",
            "optional": false,
            "inputParameters": {
                "expression": "return $.data.html;",
                "data": {
                    "html": "${build_message_metadata.output.result.html}"
                },
                "response": "FILE",
                "outputFileName": "message.html"
            }
        },
        {
            "name": "notification-display-message",
            "taskReferenceName": "display_results",
            "description": "Display the search results with access links",
            "type": "SUB_WORKFLOW",
            "optional": false,
            "inputParameters": {
                "request": "${build_message_file.output.result}",
                "height": "${build_message_metadata.output.result.messageHeight}"
            }
        }
    ],
    "outputParameters": {
        "message": "${display_results.output.message}"
    }
}
