# 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: Latitude
    description: |
          Generates the latitude component of geographical coordinates based on the seed parameter.
          'RAND' will generate a random value. All other values are used as they are for the seed.
          The country parameter specifies the language of origin for the first name
          ('US', 'DE', 'FR', 'IT', 'NL', 'AT', 'BE', 'CA', 'CH', 'UK'). It is possible to specify one to all of
          them separated by a comma. If more than one value is specified one of them is used based on the seed value.
          'RAND' can be used to use a random value.
          *This method requires the installation of the lookup tables*.
    inputType: COLUMN
    mappingTableContainers:
      - "XDM Lookup Table"
    mappingTableAccessMode: READ
    code: |
        /*
         * *****************************************************************************
         * Latitude
         * *****************************************************************************
         *
         * 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);
        importClass(Packages.de.ubs.xdm3.batch.modification.ModificationException);
        importClass(Packages.de.ubs.xdm3.batch.modification.CompositeKey);
        importClass(Packages.de.ubs.xdm3.batch.modification.HashUtils);
        
        var TABLE_NAME = 'ADDRESSES';
        var countMap = {};
        var mappingTableUtils = new MappingTableUtils(lookupTable);
        var countries = ['US', 'DE', 'FR', 'IT', 'NL', 'AT', 'BE', 'CA', 'CH', 'UK'];
        
        /*
         * This function is called once before the processing starts. The method
         * is called once per table.
         */
        function init() {
            countMap = mappingTableUtils.count(TABLE_NAME, "KEY_01");
        
            var availableCountries = [];
            for (var i = 0; i < countries.length; ++i) {
                if (countMap.get(new CompositeKey(countries[i])) > 0) {
                    availableCountries.push(countries[i]);
                }
            }
            if (availableCountries.length == 0) {
                throw new ModificationException("Lookup table " + TABLE_NAME + " has no entries");
            }
            countries = availableCountries;
        
            if (country != null && ctx.indexOf(country) == -1){
                if (country != "RAND") {
                    var temp = country.split(",");
                    var temp2 = [];
                    for (var i = 0; i < temp.length; ++i) {
                        if (!countries.includes(temp[i])) {
                            throw new ModificationException("Country " + temp[i] + " is not a valid value of '" + countries.join("','") + "'");
                        }
                        temp2.push(temp[i]);
                    }
                    if (temp2.length == 0) {
                        throw new ModificationException("No countries available");
                    }
                    country = temp2;
                }
            }
        }
        
        /*
         * 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 seedValue = "";
            var countryValue = "";
        
            if (ctx.indexOf(seed) != -1) {
                seedValue = dataArray[ctx.indexOf(seed)];
            } else {
                if (seed == "RAND") {
                    seedValue = Math.floor(Math.random() * 1000000000);
                } else {
                    seedValue = seed;
                }
            }
            var seedHash = HashUtils.hashInt(HashUtils.hashStringLikeInt(seedValue));
        
            if (ctx.indexOf(country) != -1) {
                countryValue = dataArray[ctx.indexOf(country)];
            } else {
                if (country == "RAND") {
                    countryValue = countries[Math.floor(Math.random() * countries.length)];
                } else {
                    countryValue = country[(seedHash % country.length)];
                }
            }
            if (!countries.includes(countryValue)) {
                throw new ModificationException("Country " + countryValue + " is not a valid value of '" + countries.join("','") + "'");
            }
        
            var count = countMap.get(new CompositeKey(countryValue));
            var rowNumber = (seedHash % count) + 1;
        
            var result = mappingTableUtils.get(TABLE_NAME, rowNumber, countryValue);
            if (result == null) {
                throw new ModificationException("Failed to retrieve data from table " + TABLE_NAME);
            }
            dataArray[columnIndex] = result[4];
        }
        
        /*
         * This function is executed once after the modification of a table is complete. The method
         * is called once per table.
         */
        function close() {
            mappingTableUtils.close()
        }
    parameters:
    - displayName: Seed
      description: |
          Column that is used to calculate the hash.
      variableName: seed
      dataType: STRING
    - displayName: Country
      description: |
          Specifies the origin of the latitude.
      variableName: country
      dataType: STRING
