---
version: 36
data:

  - type: "TaskStageHook"
    variation: null
    lastChangedDate: "2025-02-03T15:40:23"
    lastChangedBy: "andreas.kottmann"
    replacePermissions: false
    replaceObject: true
    attributes:
      code: "importPackage(java.sql);\nimportPackage(java.io);\n\nvar report = Packages.org.apache.commons.io.FileUtils.readFileToString(new\
      \ File (properties.get('root_dir') + \"/reports/Stage1_0_objectSelectorTableReport_Source.rpd\"\
      )); \nvar reportData = Packages.de.ubs.xdm.utils.reportingutils.ReportUtils.deserializeReportData(report);\n\
      var query1;\nvar pStmt1;\nvar rs1;\nvar query2;\nvar pStmt2;\nvar rs2;\n\nfor\
      \ (i = 0; i < reportData.getRowCount(); i++) {\n    tableSchema = reportData.getRow(i)[2];\n\
      \    tableName = reportData.getRow(i)[3];\n    query1 = \"SELECT COLS.name AS\
      \ COLNAME FROM sys.identity_columns COLS INNER JOIN sys.tables TABS ON COLS.object_id=TABS.object_id\
      \ INNER JOIN sys.schemas SCH ON SCH.schema_id=TABS.schema_id WHERE (SCH.name\
      \ = '\" + tableSchema + \"') AND (TABS.name = '\" + tableName + \"') AND COLS.is_computed=0\
      \ ORDER BY COLS.column_id;\"\n    pStmt1 = taskStageHook.connection.jdbcConnection.prepareStatement(query1);\n\
      \    rs1 = pStmt1.executeQuery();\n    if (rs1.next()) {\n          column =\
      \ rs1.getString(\"COLNAME\");\n          query2 = 'SELECT MAX(' + column + ')\
      \ AS VALUE FROM ' + tableSchema +'.' + tableName;\n          pStmt2 = taskStageHook.connection.jdbcConnection.prepareStatement(query2);\n\
      \          rs2 = pStmt2.executeQuery();\n          if (rs2.next()) {\n     \
      \            result = rs2.getString(\"VALUE\");\n                 print(\"Max\
      \ value for identity column in table \" + tableSchema + \".\" + tableName +\
      \ \": \" + result);\n                 if (result != null) {\n              \
      \   startValue = parseInt(result) + 1;\n                 } else {\n        \
      \             startValue =1;\n                 }\n                 print(\"\
      Start value for identity column in table \" + tableSchema + \".\" + tableName\
      \ + \": \" + startValue);\n                 properties.put(tableName, startValue);\n\
      \                 }\n          }\n    }  \n\nrs1.close;\npStmt1.close();\nrs2.close;\n\
      pStmt2.close();\n"
      codeType: "JAVA_SCRIPT"
      description: ""
      displayName: "Calculate Start Value"
      libraries: []
      mappingTableAccessMode: "READ_WRITE"
      mappingTableContainers: []
      parameters:
        - possibleValues: []
          displayName: "connection"
          defaultValue: null
          dataType: "CONNECTION"
          description: ""
      tags: []