# Mon, 22 Dec 2025 14:57:12 GMT
---
version: 23
data:


- type: Credential
  attributes:
    displayName: XDM Lookup Table Credential
    description: Credential for XDM Lookup Table
    user: sa
    password: ""

- type: MappingTableContainer
  attributes:
    displayName: XDM Lookup Table
    description: Mapping table to use the xdm lookup tables
    variableName: lookupTable
    credential: XDM Lookup Table Credential

- type: ModificationMethod
  attributes:
    displayName: Auto increment
    description: |
          This method generates a new value each time it is called. Internally it uses a database SEQUENCE that is responsible to provided incrementing values.
    inputType: COLUMN
    mappingTableContainers:
      - "XDM Lookup Table"
    mappingTableAccessMode: READ_WRITE
    code: |
        /*
         * *****************************************************************************
         * Auto Increment
         * *****************************************************************************
         *
         * The following variables are available in this script:
         * =====================================================
         * ctx             : A context object that offers a number of methods.
         *                   The method indexOf() can be used to get the index of a
         *                   specific column within the dataArray.
         * dataArray       : The data array. This array contains the contents of all
         *                   columns of the current row of the table that is being
         *                   masked by this method.
         *
         * Additionally for column based modification methods the following two
         * variables are available:
         * columnName      : The name of the column that matched the pattern.
         * columnIndex     : The position of the column that matched the pattern within
         *                   the data array. This value is zero-based.
         * *****************************************************************************
         */
        importClass(Packages.de.ubs.xdm3.batch.modification.MappingTableUtils);
        var mappingTableUtils = new MappingTableUtils(lookupTable);
        
        /*
         * Creates the internal sequence AUTO_INCREMENT if it does not yet exist.
         */
        function init() {
            if (sequenceName == null || sequenceName.length <= 0) {
                sequenceName = "AUTO_INCREMENT";
            }
            var statement = "CREATE SEQUENCE IF NOT EXISTS " + sequenceName;
        
            if (startValue != null && startValue != "null" && parseFloat(startValue) != "NaN") {
                statement += " START WITH " + Math.floor(startValue);
            }
            if (incrementBy != null && incrementBy != "null" && parseFloat(incrementBy) != "NaN") {
                statement += " INCREMENT BY " + Math.floor(incrementBy);
            }
            if (minValue != null && minValue != "null" && parseFloat(minValue) != "NaN") {
                statement += " MINVALUE " + Math.floor(minValue);
            } else {
                statement += " NOMINVALUE";
            }
            if (maxValue != null && maxValue != "null" && parseFloat(maxValue) != "NaN") {
                statement += " MAXVALUE " + Math.floor(maxValue);
            } else {
                statement += " NOMAXVALUE";
            }
            if (cycle == "true") {
                statement += " CYCLE";
            } else {
                statement += " NOCYCLE";
            }
            ctx.print(statement);
        
            ctx.print("DROP: " + dropSequence);
            if (dropSequence == true) {
                mappingTableUtils.executeStatement("DROP SEQUENCE IF EXISTS " + sequenceName);
                ctx.print("DROP SEQUENCE IF EXISTS " + sequenceName);
            }
        	mappingTableUtils.executeStatement(statement);
        }
        
        /*
         * This method is called for each row. It will get the next value from the sequence
         * AUTO_INCREMENT and stores it as the value of the current row of the specified column.
         */
        function apply() {
        	dataArray[columnIndex] = mappingTableUtils.getNextValueFor(sequenceName);
        }
        
        /*
         * Close internal resources of the modification utils.
         */
        function close() {
            if (dropSequence == true) {
                mappingTableUtils.executeStatement("DROP SEQUENCE IF EXISTS " + sequenceName);
                ctx.print("DROP SEQUENCE IF EXISTS " + sequenceName);
            }
        	mappingTableUtils.close();
        }
    parameters:
    - displayName: Sequence Name
      description: |
          Specifies the name of the sequence.
      variableName: sequenceName
      defaultValue: "AUTO_INCREMENT"
      dataType: STRING
    - displayName: IncrementBy
      description: |
          Specifies the step size of the sequence. Negative values will lead to a down counting sequence.
      variableName: incrementBy
      dataType: STRING
    - displayName: Start Value
      description: |
          Specifies the value the sequence is starting with.
      variableName: startValue
      dataType: STRING
    - displayName: Min Value
      description: |
          Specifies the lower limit of the sequence. This will be used as start value or at cycling.
      variableName: minValue
      dataType: STRING
    - displayName: Max Value
      description: |
          Specifies the maximum value for the sequence. This will be used as start value for down counting sequences.
      variableName: maxValue
      dataType: STRING
    - displayName: Cycle
      description: |
          Specifies whether the sequence cycles at the borders or causing an error.
      variableName: cycle
      defaultValue: "FALSE"
      dataType: BOOLEAN
    - displayName: Drop Sequence
      description: |
          Specifies if the sequence is dropped at the end of the modification method finishes.
      variableName: dropSequence
      defaultValue: "FALSE"
      dataType: BOOLEAN
