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


- type: ModificationMethod
  attributes:
    displayName: Swedish id
    description: |
          Generates a Swedish id based on the birthday parameter. The generated ID is returned as a string.
          'RAND' will generate a random value. If the value is a date it will be used as is. All other values will be hashed.
    inputType: COLUMN
    mappingTableContainers: []
    mappingTableAccessMode: READ
    code: |
        /*
         * *****************************************************************************
         * Swedish ID
         * *****************************************************************************
         *
         * 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.
         * *****************************************************************************
         */
        importPackage(Packages.java.text);
        importPackage(Packages.java.util);
        importClass(Packages.de.ubs.xdm3.batch.modification.HashUtils);
        importClass(Packages.de.ubs.xdm3.batch.modification.ModificationException);
        importClass(Packages.de.ubs.xdm.utils.core.HighPrecisionTimestamp);
        
        var cal = null;
        var dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        
        /*
         * This function is called once before the processing starts. The method
         * is called once per table.
         */
        function init() {
            cal = new GregorianCalendar();
        }
        
        /*
         * This function should modify the row that is provided by the dataArray. The
         * method is called for each single row of the table.
         */
        function apply() {
            var birthdayValue = "";
            if (ctx.indexOf(birthday) != -1) {
                birthdayValue = dataArray[ctx.indexOf(birthday)];
                if (birthdayValue instanceof HighPrecisionTimestamp) {
                    birthdayValue = dateFormat.format(birthdayValue.getTime());
                }
            } else {
                birthdayValue = birthday;
            }
        
            if (birthday == "RAND") {
                cal.setTime(new Date(Math.random() * 1400000000));
            } else {
                try {
                    cal.setTime(dateFormat.parse(birthdayValue));
                } catch (e) {
                    cal.setTime(new Date(HashUtils.hashStringLikeInt(HashUtils.hashStringLikeInt(birthdayValue) % 1400000000)));
                }
            }
        
            var year = cal.get(Calendar.YEAR) % 100;
            var month = cal.get(Calendar.MONTH) + 1;
            var day = cal.get(Calendar.DAY_OF_MONTH);
            var number = 100 + HashUtils.hashStringLikeInt(HashUtils.hashStringLikeInt(birthdayValue), 900);
        /* Creating swedish identity number without checksum */
            var swedishIDNumber = "";
            if (year < 10) {
                swedishIDNumber = swedishIDNumber + '0';
            }
            swedishIDNumber = swedishIDNumber + year.toString();
            if (month < 10) {
                swedishIDNumber = swedishIDNumber + '0';
            }
            swedishIDNumber = swedishIDNumber + month.toString();
            if (day < 10) {
                swedishIDNumber = swedishIDNumber + '0';
            }
            swedishIDNumber = swedishIDNumber + day.toString();
            var checkNumber = swedishIDNumber;
            if ((delimiter != null) && (delimiter != "")) {
                swedishIDNumber = swedishIDNumber + delimiter;
            }
            swedishIDNumber = swedishIDNumber + number;
            checkNumber = checkNumber + number;
            /* Calculating and adding checksum */
            var i;
            var digit;
            var digitSum = 0;
            for (i = 0; i < checkNumber.length; i++) {
                if ((i % 2) === 0) {
                    digit = 2 * (parseInt(checkNumber.charAt(i)));
                } else {
                    digit = parseInt(checkNumber.charAt(i));
                }
                if (digit > 9) {
                    digitSum = digitSum + 1 + (digit % 10);
                } else {
                    digitSum = digitSum + digit;
                }
            }
            swedishIDNumber = swedishIDNumber + ((10 - (digitSum % 10)) % 10) ;
            dataArray[columnIndex] = swedishIDNumber;
        }
        
        /*
         * This function is executed once after the modification of a table is complete. The method
         * is called once per table.
         */
        function close() {
        }
    parameters:
    - displayName: Birthday
      description: |
          Specify a value that is used to calculate the hash.
      variableName: birthday
      dataType: STRING
    - displayName: Delimiter
      description: |
          The delimiter character used to separate the groups of digits. May be empty.
      variableName: delimiter
      defaultValue: "-"
      dataType: STRING
