ConfigField.kt 5.97 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
/*
 * mapper-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 ch.memobase.mapping.fields

Jonas Waeber's avatar
Jonas Waeber committed
21
22
23
import com.beust.klaxon.TypeFor
import mapping.fields.FieldAdapter
import mapping.fields.FieldTypes
Jonas Waeber's avatar
Jonas Waeber committed
24
25
26
import org.apache.jena.rdf.model.Literal
import org.apache.jena.rdf.model.ResourceFactory

Jonas Waeber's avatar
Jonas Waeber committed
27
28
29
30
31
@TypeFor("fieldType", FieldAdapter::class)
sealed class ConfigField(val fieldType: String) {
    sealed class AnnotationField(fieldType: String) : ConfigField(fieldType) {
        sealed class SimpleAnnotationField(fieldType: String) : AnnotationField(fieldType) {
            data class ConstantField(val key: String, val constant: String) : SimpleAnnotationField(FieldTypes.constant) {
Jonas Waeber's avatar
Jonas Waeber committed
32
33
34
35
36
37
38
39
40
                fun toLiteral(): Literal {
                    return literal(constant)
                }

                fun toLangLiteral(tag: String): Literal {
                    return langLiteral(constant, tag)
                }
            }

Jonas Waeber's avatar
Jonas Waeber committed
41
            sealed class MappedAnnotationField(fieldType: String, val key: String, val field: String) : SimpleAnnotationField(fieldType) {
Jonas Waeber's avatar
Jonas Waeber committed
42
                data class PrefixField(
Jonas Waeber's avatar
Jonas Waeber committed
43
                    val prefixKey: String,
Jonas Waeber's avatar
Jonas Waeber committed
44
                    val prefix: String,
Jonas Waeber's avatar
Jonas Waeber committed
45
46
                    val prefixField: String
                ) : MappedAnnotationField(FieldTypes.prefix, prefixKey, prefixField) {
Jonas Waeber's avatar
Jonas Waeber committed
47
48
49
50
51
52
53
54
55
                    override fun toLiteral(value: String): Literal {
                        return literal(prefix + value.trim())
                    }

                    override fun toLangLiteral(value: String, tag: String): Literal {
                        return langLiteral(prefix + value.trim(), tag)
                    }
                }

Jonas Waeber's avatar
Jonas Waeber committed
56
57
                data class DirectMapField(val directKey: String, val directField: String) :
                    MappedAnnotationField(FieldTypes.direct, directKey, directField) {
Jonas Waeber's avatar
Jonas Waeber committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
                    override fun toLiteral(value: String): Literal {
                        return literal(value)
                    }

                    override fun toLangLiteral(value: String, tag: String): Literal {
                        return langLiteral(value, tag)
                    }
                }

                abstract fun toLiteral(value: String): Literal
                abstract fun toLangLiteral(value: String, tag: String): Literal
            }
        }

Jonas Waeber's avatar
Jonas Waeber committed
72
73
        sealed class ComplexAnnotationField(fieldType: String) : AnnotationField(fieldType) {
            data class ListField(val key: String, val fields: List<SimpleAnnotationField>) : ComplexAnnotationField(FieldTypes.list) {
Jonas Waeber's avatar
Jonas Waeber committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
                fun toLiterals(source: Map<String, Any>): List<Literal> {
                    return fields.mapNotNull {
                        when (it) {
                            is MappedAnnotationField ->
                                FieldParsers.unpackSource(it.field, source).let { value ->
                                    if (value != null) {
                                        it.toLiteral(value)
                                    } else {
                                        null
                                    }
                                }
                            is ConstantField -> it.toLiteral()
                        }
                    }
                }
            }

            data class LanguageField(val key: String, val fields: List<Pair<String, List<SimpleAnnotationField>>>) :
Jonas Waeber's avatar
Jonas Waeber committed
92
                ComplexAnnotationField(FieldTypes.language) {
Jonas Waeber's avatar
Jonas Waeber committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
                fun toLangLiterals(source: Map<String, Any>): List<Literal> {
                    return fields.flatMap { pairs ->
                        pairs.second.mapNotNull {
                            when (it) {
                                is MappedAnnotationField ->
                                    FieldParsers.unpackSource(it.field, source).let { value ->
                                        if (value != null) {
                                            it.toLangLiteral(value, pairs.first)
                                        } else {
                                            null
                                        }
                                    }
                                is ConstantField -> it.toLangLiteral(pairs.first)
                            }
                        }
                    }
                }
            }
        }
    }

    protected fun langLiteral(text: String, language: String): Literal =
        ResourceFactory.createLangLiteral(text.trim(), language)

    protected fun literal(text: String): Literal = ResourceFactory.createStringLiteral(text.trim())
}

typealias AnnotationField = ConfigField.AnnotationField

typealias DirectMapField = ConfigField.AnnotationField.SimpleAnnotationField.MappedAnnotationField.DirectMapField
typealias PrefixField = ConfigField.AnnotationField.SimpleAnnotationField.MappedAnnotationField.PrefixField
typealias ConstantField = ConfigField.AnnotationField.SimpleAnnotationField.ConstantField
typealias SimpleAnnotationField = ConfigField.AnnotationField.SimpleAnnotationField
typealias MappedAnnotationField = ConfigField.AnnotationField.SimpleAnnotationField.MappedAnnotationField

typealias ListField = ConfigField.AnnotationField.ComplexAnnotationField.ListField
typealias LanguageField = ConfigField.AnnotationField.ComplexAnnotationField.LanguageField
typealias ComplexAnnotationField = ConfigField.AnnotationField.ComplexAnnotationField