GlobalTransformsLoader.kt 3.81 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.exceptions.InvalidMappingException
Jonas Waeber's avatar
Jonas Waeber committed
19
20
import ch.memobase.helpers.GlobalTransformException
import ch.memobase.helpers.ValidationError
Jonas Waeber's avatar
Jonas Waeber committed
21
22
23
24
25
26
import ch.memobase.model.GlobalTransform
import ch.memobase.transform.ITransformer
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
27
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
28
29
import java.io.File
import java.io.FileInputStream
Jonas Waeber's avatar
Jonas Waeber committed
30
import java.io.IOException
Jonas Waeber's avatar
Jonas Waeber committed
31

Jonas Waeber's avatar
Jonas Waeber committed
32
class GlobalTransformsLoader(private val configuration: String) {
33
    private val log = LogManager.getLogger(this::class.java)
Jonas Waeber's avatar
Jonas Waeber committed
34
    private val objectMapper = ObjectMapper(YAMLFactory()).registerKotlinModule()
Jonas Waeber's avatar
Jonas Waeber committed
35
    private val transforms = mutableListOf<ITransformer>()
Jonas Waeber's avatar
Jonas Waeber committed
36

Jonas Waeber's avatar
Jonas Waeber committed
37
38
39
40
41
42
    fun parse() {
        val globalTransform = try {
            objectMapper.readValue(FileInputStream(File(configuration)), GlobalTransform::class.java)
        } catch (ex: IOException) {
            log.error("File Not Found: $configuration.")
            throw GlobalTransformException("File Not Found: $configuration.")
Jonas Waeber's avatar
Jonas Waeber committed
43
        } catch (ex: MismatchedInputException) {
Jonas Waeber's avatar
Jonas Waeber committed
44
45
            log.error("YamlParserError: ${ex.localizedMessage}")
            throw GlobalTransformException("YamlParserError: ${ex.localizedMessage}")
Jonas Waeber's avatar
Jonas Waeber committed
46
        } catch (ex: Exception) {
Jonas Waeber's avatar
Jonas Waeber committed
47
48
            log.error("${ex}: ${ex.localizedMessage}")
            throw GlobalTransformException("${ex}: ${ex.localizedMessage}")
Jonas Waeber's avatar
Jonas Waeber committed
49
        }
Jonas Waeber's avatar
Jonas Waeber committed
50
51
52
53
54
        try {
            globalTransform.let {
                it.normalizeDate.let { date ->
                    if (date != null)
                        transforms.add(date.generate())
Jonas Waeber's avatar
Jonas Waeber committed
55
                }
Jonas Waeber's avatar
Jonas Waeber committed
56
57
58
59
                it.normalizeCarrierType.let { carrierType ->
                    if (carrierType != null) {
                        transforms.add(carrierType.generate())
                    }
Jonas Waeber's avatar
Jonas Waeber committed
60
                }
Jonas Waeber's avatar
Jonas Waeber committed
61
62
63
64
65
66
67
68
69
                it.normalizeLanguages.let { normalizeLanguages ->
                    if (normalizeLanguages != null) {
                        transforms.add(normalizeLanguages.generate())
                    }
                }
                it.normalizeGenre.let { normalizeGenre ->
                    if (normalizeGenre != null) {
                        transforms.add(normalizeGenre.generate())
                    }
Jonas Waeber's avatar
Jonas Waeber committed
70
71
                }
            }
Jonas Waeber's avatar
Jonas Waeber committed
72
73
74
75
76
77
78
79
80
        } catch (ex: InvalidMappingException) {
            log.error("Invalid Mapping: ${ex.localizedMessage}")
            throw GlobalTransformException("Invalid Mapping: ${ex.localizedMessage}")
        } catch (ex: ValidationError) {
            log.error("Validation Error: ${ex.localizedMessage}")
            throw GlobalTransformException("Validation Error: ${ex.localizedMessage}")
        } catch (ex: Exception) {
            log.error("${ex}: ${ex.localizedMessage}")
            throw GlobalTransformException("${ex}: ${ex.localizedMessage}")
Jonas Waeber's avatar
Jonas Waeber committed
81
        }
Jonas Waeber's avatar
Jonas Waeber committed
82
83
84
        if (transforms.isEmpty()) {
            log.error("Empty configuration: $configuration!")
            throw GlobalTransformException("Empty configuration: $configuration!")
Jonas Waeber's avatar
Jonas Waeber committed
85
        }
Jonas Waeber's avatar
Jonas Waeber committed
86
87
88
89
    }

    fun get(): List<ITransformer> {
        return transforms
Jonas Waeber's avatar
Jonas Waeber committed
90
91
    }
}