Util.kt 3.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * drupal-sync-service
 * Copyright (C) 2020 Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package org.memobase

import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.StringWriter
import kotlin.system.exitProcess
import org.apache.jena.rdf.model.Model
import org.apache.jena.riot.RDFDataMgr
import org.apache.jena.riot.RDFFormat
import org.apache.kafka.streams.KeyValue
import org.apache.logging.log4j.LogManager
import org.memobase.model.IdLabels
import org.memobase.rdf.NS

object Util {
    const val languageSourceFilePathPropertyName = "path.languages"

    const val memoriavUri = NS.mbcb + "mrv"


    private val wikidataNamespace = "http://www.wikidata.org/entity/"
    private val log = LogManager.getLogger("DrupalSyncHelpers")

    fun getMunicipalities(): Map<String, IdLabels> {
        val stream = ClassLoader.getSystemResourceAsStream("municipalities.tsv")
        if (stream != null) {
            return stream.bufferedReader().lineSequence().filterNot {
                it.startsWith("code")
            }.map {
                val values = it.split("\t")
                val codes = values[0].split(",").map { code -> code.trim() }
                codes.flatMap { code ->
                    code.split("-").map { c -> c.trim() }
                }.map { code ->
                    Pair(
                        code, IdLabels(
                            values[1].trim(),
                            values[2].trim(),
                            values[3].trim(),
                            values[4].trim()
                        )
                    )
                }
            }.flatten().toMap()
        } else {
            log.error("Could not load municipalities.tsv from classpath!")
            exitProcess(1)
        }
    }

    fun loadLanguages(path: String): Map<String, IdLabels> {
        return try {
            val stream = FileInputStream(File(path))
            stream.bufferedReader().lineSequence().filterNot { it.startsWith("code") }
                .map {
                    val values = it.split(",")
                    Pair(
                        values[0].trim(),
                        IdLabels(NS.wd + values[1].trim(), values[2].trim(), values[3].trim(), values[4].trim())
                    )
                }.toMap()
        } catch (ex: FileNotFoundException) {
            log.error("Could not find language labels in path $path.")
            exitProcess(1)
        }
    }

    fun getCantons(): Map<String, IdLabels> {
        val stream = ClassLoader.getSystemResourceAsStream("cantons.csv")
        if (stream != null) {
            return stream.bufferedReader().lineSequence().filterNot {
                it.startsWith("code")
            }.map {
                val values = it.split(",")
                Pair(values[0].trim(), IdLabels(values[1].trim(), values[2].trim(), values[3].trim(), values[4].trim()))
            }.toMap()
        } else {
            log.error("Could not load cantons.csv from classpath!")
            exitProcess(1)
        }
    }

    fun writeModel(uri: String, model: Model): KeyValue<String, String> {
        return StringWriter().use { writer ->
            RDFDataMgr.write(writer, model, RDFFormat.NTRIPLES_UTF8)
            return@use KeyValue(uri, writer.toString().trim())
        }
    }
}