# Thu, 20 Aug 2026 13:11:52 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.
          If the value is a date it will be used as is. All other values will be hashed.
    scriptLanguage: "GROOVY"
    legacyScript: false
    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 data.
         * data       : 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.
         * *****************************************************************************
         */
        
        import java.time.LocalDate
        import java.time.format.DateTimeFormatter
        import java.time.OffsetDateTime
        import de.ubs.xdm3.batch.modification.HashUtils
        import de.ubs.xdm3.batch.modification.ModificationException
        
        dateFormatter = null
        
        /*
         * This function is called once before the processing starts. The method
         * is called once per table.
         */
        def init() {
            dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
            return null
        }
        
        /*
         * This function should modify the row that is provided by the data. The
         * method is called for each single row of the table.
         */
        def apply() {
            def birthdayValue = modificationMethod.birthday.toString()
            def regex = /^(\d{4}-\d{2}-\d{2})-(\d{2})\.(\d{2})\.(\d{2})\..*\+(\d{2}):(\d{2})$/
            def m = birthdayValue =~ regex
            if (m.matches()) {
                def normalized = "${m[0][1]}T${m[0][2]}:${m[0][3]}:${m[0][4]}+${m[0][5]}:${m[0][6]}"
                def odt = OffsetDateTime.parse(normalized)
                birthdayValue = odt.toLocalDate().format(dateFormatter)
            }
        
            LocalDate date
            try {
                    date = LocalDate.parse(birthdayValue, dateFormatter)
                } catch (Exception e1) {
                    try {
                        def dateOnly = birthdayValue.substring(0, 10)
                        date = LocalDate.parse(dateOnly, dateFormatter)
                        birthdayValue = dateOnly
                    } catch (Exception e2) {
                        long hash = HashUtils.hashInt(HashUtils.hashStringLikeInt(birthdayValue.toString()) % 1_400_000_000)
                        long val = hash / (24*60*60*1000)
                        date = LocalDate.ofEpochDay(val)
                    }
            }
        
        
            def year = date.year % 100
            def month = date.monthValue
            def day = date.dayOfMonth
            var number = 100 + HashUtils.hashInt(HashUtils.hashStringLikeInt(birthdayValue.toString()), 900)
        
            /* Creating swedish identity number without checksum */
            def swedishIDNumber = ""
            if (year < 10) swedishIDNumber += "0"
            swedishIDNumber += year.toString()
            if (month < 10) swedishIDNumber += "0"
            swedishIDNumber += month.toString()
            if (day < 10) swedishIDNumber += "0"
            swedishIDNumber += day.toString()
        
            def checkNumber = swedishIDNumber
            if (modificationMethod.delimiter != null && modificationMethod.delimiter != "") {
                swedishIDNumber += modificationMethod.delimiter
            }
            swedishIDNumber += number
            checkNumber += number
        
            /* Calculating and adding checksum */
            int digitSum = 0
            for (int i = 0; i < checkNumber.length(); i++) {
                int digit
                if (i % 2 == 0) {
                    digit = 2 * Integer.parseInt(checkNumber.charAt(i).toString())
                } else {
                    digit = Integer.parseInt(checkNumber.charAt(i).toString())
                }
                if (digit > 9) {
                    digitSum += 1 + (digit % 10)
                } else {
                    digitSum += digit
                }
            }
            swedishIDNumber += ((10 - (digitSum % 10)) % 10).toString()
            data[columnIndex] = swedishIDNumber
            return true
        }
        
        /*
         * This function is executed once after the modification of a table is complete. The method
         * is called once per table.
         */
        def 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
