---
version: 43
data:

  - type: "TaskStageHook"
    variation: null
    lastChangedDate: "2026-03-27T13:39:15"
    lastChangedBy: "admin"
    replacePermissions: false
    replaceObject: true
    attributes:
      code: |-
        import java.sql.*
        import java.io.*
        import org.apache.commons.io.FileUtils
        import de.ubs.xdm.utils.reportingutils.ReportUtils
        
        def reportFile = new File(properties.get('root_dir') + "/reports/Stage1_0_objectSelectorTableReport_Source.rpd")
        def report = FileUtils.readFileToString(reportFile)
        def reportData = ReportUtils.deserializeReportData(report)
        
        PreparedStatement pStmt1
        ResultSet rs1
        PreparedStatement pStmt2
        ResultSet rs2
        
        try {
            for (int i = 0; i < reportData.rowCount; i++) {
                def row = reportData.getRow(i)
                def tableSchema = row[1]
                def tableName  = row[2]
        
                def 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
                """.trim()
        
                println "Executing statement: $query1"
                pStmt1 = taskStageHook.connection.jdbcConnection.prepareStatement(query1)
                rs1 = pStmt1.executeQuery()
        
                if (rs1.next()) {
                    def column = rs1.getString("COLNAME")
                    def query2 = "SELECT MAX(${column}) AS VALUE FROM ${tableSchema}.${tableName}"
        
                    pStmt2 = taskStageHook.connection.jdbcConnection.prepareStatement(query2)
                    rs2 = pStmt2.executeQuery()
        
                    if (rs2.next()) {
                        def result = rs2.getString("VALUE")
                        println "Max value for identity column in table ${tableSchema}.${tableName}: ${result}"
        
                        int startValue
                        if (result != null) {
                            startValue = Integer.parseInt(result) + 1
                        } else {
                            startValue = 1
                        }
        
                        println "Start value for identity column in table ${tableSchema}.${tableName}: ${startValue}"
                        properties.put(tableName, startValue)
                    }
                }
                if (rs2 != null) { rs2.close(); rs2 = null }
                if (pStmt2 != null) { pStmt2.close(); pStmt2 = null }
                if (rs1 != null) { rs1.close(); rs1 = null }
                if (pStmt1 != null) { pStmt1.close(); pStmt1 = null }
            }
        } finally {
            if (rs2 != null)  rs2.close()
            if (pStmt2 != null) pStmt2.close()
            if (rs1 != null)  rs1.close()
            if (pStmt1 != null) pStmt1.close()
        }
      codeType: "GROOVY"
      description: ""
      displayName: "Calculate Start Value"
      libraries: []
      mappingTableAccessMode: "READ_WRITE"
      mappingTableContainers: []
      parameters:
        - possibleValues: []
          displayName: "connection"
          defaultValue: null
          dataType: "CONNECTION"
          description: ""
      tags: []