---
version: 16
data:

  - type: "CustomParameter"
    variation: null
    replacePermissions: true
    replaceObject: true
    attributes:
      defaultValue: ""
      description: ""
      displayName: "XDM Id List"
      parameterType: "STRING"
      requirementLevel: "NONE"
      validationExpression: null
      validationType: "NONE"
      variableName: "xdmIdList"

  - type: "TaskStageHook"
    variation: null
    replacePermissions: true
    replaceObject: true
    attributes:
      code: "importClass(java.lang.RuntimeException);\n\nif(generation != null) {\n\
      \  var parameterNameList = parameterNameList.split(\",\");\n  for (var i = 0;\
      \ i < parameterNameList.length; i++) {\n    var parameterName = new java.lang.String(parameterNameList[i]).trim();\n\
      \    var parm = generation.getParameter(parameterName);\n\tif (parm != null)\
      \ {\n      var value = parm.getValue();\n\t  properties.put(parameterName, value);\n\
      \t  utils.log(\"Extract: \" + parameterName + \": \" + value);\n\t} else {\n\
      \t\tthrow new RuntimeException(\"Parameter '\" + parameterName + \"' not found\
      \ in generation \" + generation.generation + \" createdOn: \" + generation.createdOn\
      \ + \" createdBy: \" + generation.createdBy);\n\t}\n  }\n} else {\n    utils.log(\"\
      Hook was called for a non from icebox task\");\n}"
      codeType: "JAVA_SCRIPT"
      description: ""
      displayName: "ExtractIceboxGenerationInfo"
      parameters:
        - displayName: "parameterNameList"
          defaultValue: ""
          dataType: "STRING"
          description: "List of parameter names to extract"

  - type: "TaskTemplate"
    variation: "subset-restore-task-template"
    replacePermissions: true
    replaceObject: true
    attributes:
      backupExpression: null
      backupSelection: "RELATIVE"
      description: ""
      displayName: "XDM Example from Icebox to delete"
      dropOption: "LEAVE"
      fillMode: "DELETE"
      generation: "0"

  - type: "TaskTemplate"
    variation: "subset-delete-task-template"
    replacePermissions: true
    replaceObject: true
    attributes:
      description: ""
      displayName: "XDM Example Delete"
      xdmIdList: null
      rowSelectionMode: "QUERY"
      startCondition: |-
        SELECT ${uniqueRowIdentifier}
        FROM "${startTableSchema}"."${startTableName}" "T"
        WHERE "T"."ID" IN (${xdmIdList})

  - type: "TaskTemplate"
    variation: "subset-restore-task-template"
    replacePermissions: true
    replaceObject: true
    attributes:
      backupExpression: null
      backupSelection: "RELATIVE"
      description: ""
      displayName: "XDM Example from Icebox"
      dropOption: "LEAVE"
      fillMode: "INSERT"
      generation: "0"
    children:
      - type: "TaskStageHookUsage"
        variation: null
        replacePermissions: true
        replaceObject: true
        attributes:
          active: true
          description: ""
          origin: "PRE"
          parameterNameList: "startCondition, xdmIdList"
          parameterNameListValueType: "CONSTANT"
          stage: "Stage3"
          taskStageHook: "ExtractIceboxGenerationInfo"

  - type: "TaskTemplate"
    variation: "subset-backup-task-template"
    replacePermissions: true
    replaceObject: true
    attributes:
      description: ""
      displayName: "XDM Example to Icebox"
      retention: "-1"
      rowSelectionMode: "QUERY"
      startCondition: |-
        SELECT ${uniqueRowIdentifier}
        FROM "${startTableSchema}"."${startTableName}" "T"
        WHERE "T"."ID" in (${xdmIdList})
      xdmIdList: "1,2,3"

  - type: "WorkflowTemplate"
    variation: null
    replacePermissions: true
    replaceObject: true
    attributes:
      description: ""
      displayName: "XDM Example Workflow Template"
      workflow: |-
        // Save an environment in an icebox generation
        TaskTemplateRunner()
           .taskTemplate("XDM Example to Icebox")
           .taskNamePattern("Workflow of ${taskTemplate.displayName} at ${.now?iso_local}")
           .parameter("xdmIdList", "1,3")
           .run();

        // Select a specific icebox generation and extract it's selection criteria
        // For this task the stage hook usage is required to extract the selection criteria from the icebox generation
        var load = TaskTemplateRunner()
           .taskTemplate("XDM Example from Icebox")
           .taskNamePattern("Workflow of ${taskTemplate.displayName} at ${.now?iso_local}")
           .stages("Stage1", "Stage2", "Stage3")
           .run();

        // Use the selection criteria from the icebox select and delete the data in the target environment
        TaskTemplateRunner()
           .taskTemplate("XDM Example Delete")
           .taskNamePattern("Workflow of ${taskTemplate.displayName} at ${.now?iso_local}")
           .parameter("startCondition", load.get("startCondition"))
           .parameter("xdmIdList", load.get("xdmIdList"))
           .run();

        // Use the data from the icebox generation to delete also remaining data in the target environment.
        // This might be necessary, when working with the test data causes inconsistent data which might not be
        // reachable by the delete task any longer.
        TaskTemplateRunner()
           .taskTemplate("XDM Example from Icebox to delete")
           .taskNamePattern("Workflow of ${taskTemplate.displayName} at ${.now?iso_local}")
           .run();

        TaskRunner()
            .execution(load)
            .stages("Stage4")
            .run();