LocalTransformsLoader.kt 3.17 KB
Newer Older
1
/*
Jonas Waeber's avatar
Jonas Waeber committed
2
   Copyright 2020-2021 Jonas Waeber
3
4
5
6
7
8
9
10
11
12
13
14
15

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
Jonas Waeber's avatar
Jonas Waeber committed
16
17
package ch.memobase.configs

18
import ch.memobase.helpers.ValidationError
Jonas Waeber's avatar
Jonas Waeber committed
19
import ch.memobase.model.LocalTransform
20
21
import ch.memobase.reporting.Report
import ch.memobase.reporting.ReportStatus
22
import ch.memobase.transform.ITransformer
Jonas Waeber's avatar
Jonas Waeber committed
23
24
25
26
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.exc.MismatchedInputException
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
Jonas Waeber's avatar
Jonas Waeber committed
27
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
28
import java.io.ByteArrayInputStream
29
import java.util.regex.PatternSyntaxException
Jonas Waeber's avatar
Jonas Waeber committed
30

Jonas Waeber's avatar
Jonas Waeber committed
31
class LocalTransformsLoader(private val data: ByteArray, private val step: String) {
Jonas Waeber's avatar
Jonas Waeber committed
32
    private val log = LogManager.getLogger(this::class.java)
Jonas Waeber's avatar
Jonas Waeber committed
33
    private val objectMapper = ObjectMapper(YAMLFactory()).registerKotlinModule()
34
    private val transforms = mutableListOf<ITransformer>()
Jonas Waeber's avatar
Jonas Waeber committed
35

36
37
38
    fun parse(key: String): Report {
        return try {
            if (data.isEmpty())
Jonas Waeber's avatar
Jonas Waeber committed
39
40
41
42
                Report(
                    key,
                    ReportStatus.ignored,
                    "[Local Transform] Ignored empty local transformation file.",
Jonas Waeber's avatar
Jonas Waeber committed
43
                    step
Jonas Waeber's avatar
Jonas Waeber committed
44
                )
45
46
47
48
49
50
51
52
53
54
            else {
                val results = objectMapper.readValue(ByteArrayInputStream(data), LocalTransform::class.java)
                results.splitEntity?.forEach {
                    transforms.add(it.generate())
                }
                results.normalizePerson?.generate().let {
                    if (it != null) {
                        transforms.addAll(it)
                    }
                }
Jonas Waeber's avatar
Jonas Waeber committed
55
                Report(key, ReportStatus.success, "", step)
56
            }
Jonas Waeber's avatar
Jonas Waeber committed
57
        } catch (ex: MismatchedInputException) {
58
59
            val message = "[Local Transform] YamlParserError: ${ex.localizedMessage}"
            log.error(message)
Jonas Waeber's avatar
Jonas Waeber committed
60
            Report(key, ReportStatus.fatal, message, step)
61
62
        } catch (ex: ValidationError) {
            val message = "[Local Transform] ValidationError: ${ex.localizedMessage}"
63
            log.error(message)
Jonas Waeber's avatar
Jonas Waeber committed
64
            Report(key, ReportStatus.fatal, message, step)
65
66
        } catch (ex: PatternSyntaxException) {
            val message = "[Local Transform] RegexError: ${ex.localizedMessage}"
67
            log.error(message)
Jonas Waeber's avatar
Jonas Waeber committed
68
            Report(key, ReportStatus.fatal, message, step)
Jonas Waeber's avatar
Jonas Waeber committed
69
        } catch (ex: Exception) {
70
            val message = "[Local Transform] ${ex.javaClass.name}: ${ex.localizedMessage}"
71
            log.error(message)
Jonas Waeber's avatar
Jonas Waeber committed
72
            Report(key, ReportStatus.fatal, message, step)
Jonas Waeber's avatar
Jonas Waeber committed
73
74
75
76
        }
    }

    fun get(): List<ITransformer> {
77
        return transforms
Jonas Waeber's avatar
Jonas Waeber committed
78
79
    }
}