ExtractCreationRelationName.kt 907 Bytes
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
package ch.memobase.model

import org.memobase.exceptions.InvalidMappingException
Jonas Waeber's avatar
Jonas Waeber committed
4
import ch.memobase.transform.ExtractCreationRelationNameTransform
Jonas Waeber's avatar
Jonas Waeber committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

data class ExtractCreationRelationName(
    val pattern: String,
    val language: String
) {
    private val validLanguageTags = listOf(
        "de", "fr", "it", "NONE"
    )

    init {
        if (!pattern.contains("(?<relation>")) {
            throw InvalidMappingException("The pattern of the 'extractRelationName' transform requires a named group 'relation'.")
        }
        if (!validLanguageTags.contains(language)) {
            throw InvalidMappingException("The language of the 'extractRelationName' transform must be one of $validLanguageTags.")
        }
    }

Jonas Waeber's avatar
Jonas Waeber committed
23
24
    fun generate(): ExtractCreationRelationNameTransform {
        return ExtractCreationRelationNameTransform(Regex(pattern), if (language == "NONE") "" else language)
Jonas Waeber's avatar
Jonas Waeber committed
25
26
    }
}