KafkaTopology.kt 3.27 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
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
/*
 * Table Data Import 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 com.beust.klaxon.Klaxon
import com.beust.klaxon.KlaxonException
import org.apache.kafka.streams.StreamsBuilder
import org.apache.kafka.streams.Topology
import org.apache.logging.log4j.LogManager
26
27
28
29
import org.memobase.model.LanguageString
import org.memobase.model.LanguageVariants
import org.memobase.model.MergedAddress
import org.memobase.model.MergedInstitution
Jonas Waeber's avatar
Jonas Waeber committed
30
import org.memobase.settings.SettingsLoader
Jonas Waeber's avatar
Jonas Waeber committed
31
import java.io.StringReader
Jonas Waeber's avatar
Jonas Waeber committed
32
33
34
35

class KafkaTopology(private val settings: SettingsLoader) {
    private val log = LogManager.getLogger("StreamsProcessing")

36
    private val municipalities = Helpers.getMunicipalities()
Jonas Waeber's avatar
Jonas Waeber committed
37

Jonas Waeber's avatar
Jonas Waeber committed
38
39
40
41
42
    fun build(): Topology {
        val builder = StreamsBuilder()
        val stream = builder.stream<String, String>(settings.inputTopic)
        stream
            .flatMapValues { value -> parseJson(value) }
43
44
            .mapValues { value -> mergeTranslations(value) }
            .mapValues { value -> transformJson(value) }
Jonas Waeber's avatar
Jonas Waeber committed
45
            .map { _, value -> value.write() }
Jonas Waeber's avatar
Jonas Waeber committed
46
47
48
49
50
            .to(settings.outputTopic)

        return builder.build()
    }

51
    private fun parseJson(data: String): List<LanguageVariants> {
Jonas Waeber's avatar
Jonas Waeber committed
52
        return try {
53
54
55
            val int = Klaxon().parse<LanguageVariants>(StringReader(data))
            return if (int == null) emptyList()
            else listOf(int)
Jonas Waeber's avatar
Jonas Waeber committed
56
57
58
59
60
61
        } catch (ex: KlaxonException) {
            log.error("Failed to parse source: $data.")
            emptyList()
        }
    }

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    private fun mergeTranslations(input: LanguageVariants): MergedInstitution {
        return MergedInstitution(
            input.de.field_memobase_id,
            LanguageString(input.de.title, input.fr.title, input.it.title),
            LanguageString(input.de.field_text.value, input.fr.field_text.value, input.it.field_text.value),
            input.de.field_isil,
            input.de.field_email,
            input.de.field_link_archive_catalog.uri,
            input.de.field_website.uri,
            input.de.field_address.mapIndexed { index, address ->
                MergedAddress(
                    LanguageString(address.address_line1, input.fr.field_address[index].address_line1, input.it.field_address[index].address_line1),
                    LanguageString(address.address_line2, input.fr.field_address[index].address_line2, input.it.field_address[index].address_line2),
                    address.postal_code
                )
            }
        )
    }

    private fun transformJson(input: MergedInstitution): Transform {
        return Transform(municipalities).createInstitution(input)
Jonas Waeber's avatar
Jonas Waeber committed
83
84
    }
}