Helpers.kt 1.29 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
package org.memobase

import org.apache.logging.log4j.LogManager
4
5
import org.memobase.model.Institution
import org.memobase.model.Municipality
Jonas Waeber's avatar
Jonas Waeber committed
6
7
import kotlin.system.exitProcess

8
object Helpers {
Jonas Waeber's avatar
Jonas Waeber committed
9

10
    private val log = LogManager.getLogger("Helper")
Jonas Waeber's avatar
Jonas Waeber committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

    fun getMunicipalities(): Map<String, Municipality> {
        val stream = ClassLoader.getSystemResourceAsStream("municipalities.tsv")
        if (stream != null) {
            return stream.bufferedReader().lineSequence().filterNot {
                it.startsWith("item")
            }.map {
                val values = it.split("\t")
                Municipality(
                    values[1].trim('"').split(", "),
                    values[2].trim('<', '>'),
                    values[0].trim('<', '>'),
                    values[3].replace("@de", "").trim('"'),
                    values[4].replace("@fr", "").trim('"'),
                    values[5].replace("@it", "").trim('"'),
                    values[6].split(",")
                )
            }.map { municipality ->
                municipality.postalCodes.map { code ->
                    Pair(code, municipality)
                }
            }.flatten().toMap()
        } else {
            log.error("Could not load municipalities.tsv from classpath!")
            exitProcess(1)
        }
    }
}