diff options
| author | Paul-Christian Volkmer | 2023-10-05 12:41:49 +0200 |
|---|---|---|
| committer | GitHub | 2023-10-05 12:41:49 +0200 |
| commit | 0eee1908df1a975824f002cff548456286e9a22a (patch) | |
| tree | a92fd2f24f155efdc1ebd924d103641f9b8c4c35 /src/main/kotlin/dev/dnpm | |
| parent | 3f5c5e28fafa4aa35cb0744c28743074346e0a9c (diff) | |
| parent | ffea9343c87f15357e83167af4a4a2f7a03d71fc (diff) | |
Merge pull request #13 from CCC-MF/issue_12
Transformation of MTBFile data based on rules
Diffstat (limited to 'src/main/kotlin/dev/dnpm')
4 files changed, 108 insertions, 3 deletions
diff --git a/src/main/kotlin/dev/dnpm/etl/processor/config/AppConfigProperties.kt b/src/main/kotlin/dev/dnpm/etl/processor/config/AppConfigProperties.kt index 06e730b..6b85603 100644 --- a/src/main/kotlin/dev/dnpm/etl/processor/config/AppConfigProperties.kt +++ b/src/main/kotlin/dev/dnpm/etl/processor/config/AppConfigProperties.kt @@ -24,7 +24,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties @ConfigurationProperties(AppConfigProperties.NAME) data class AppConfigProperties( var bwhcUri: String?, - var generator: PseudonymGenerator = PseudonymGenerator.BUILDIN + var generator: PseudonymGenerator = PseudonymGenerator.BUILDIN, + var transformations: List<TransformationProperties> = listOf() ) { companion object { const val NAME = "app" @@ -78,4 +79,10 @@ data class KafkaTargetProperties( enum class PseudonymGenerator { BUILDIN, GPAS -}
\ No newline at end of file +} + +data class TransformationProperties( + val path: String, + val from: String, + val to: String +)
\ No newline at end of file diff --git a/src/main/kotlin/dev/dnpm/etl/processor/config/AppConfiguration.kt b/src/main/kotlin/dev/dnpm/etl/processor/config/AppConfiguration.kt index 6b15fc0..c8e86fb 100644 --- a/src/main/kotlin/dev/dnpm/etl/processor/config/AppConfiguration.kt +++ b/src/main/kotlin/dev/dnpm/etl/processor/config/AppConfiguration.kt @@ -25,6 +25,9 @@ import dev.dnpm.etl.processor.pseudonym.AnonymizingGenerator import dev.dnpm.etl.processor.pseudonym.Generator import dev.dnpm.etl.processor.pseudonym.GpasPseudonymGenerator import dev.dnpm.etl.processor.pseudonym.PseudonymizeService +import dev.dnpm.etl.processor.services.Transformation +import dev.dnpm.etl.processor.services.TransformationService +import org.slf4j.LoggerFactory import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean @@ -41,6 +44,8 @@ import reactor.core.publisher.Sinks ) class AppConfiguration { + private val logger = LoggerFactory.getLogger(AppConfiguration::class.java) + @ConditionalOnProperty(value = ["app.pseudonymizer"], havingValue = "GPAS") @Bean fun gpasPseudonymGenerator(configProperties: GPasConfigProperties): Generator { @@ -71,5 +76,16 @@ class AppConfiguration { return Sinks.many().multicast().directBestEffort() } + @Bean + fun transformationService( + objectMapper: ObjectMapper, + configProperties: AppConfigProperties + ): TransformationService { + logger.info("Apply ${configProperties.transformations.size} transformation rules") + return TransformationService(objectMapper, configProperties.transformations.map { + Transformation.of(it.path) from it.from to it.to + }) + } + } diff --git a/src/main/kotlin/dev/dnpm/etl/processor/services/RequestProcessor.kt b/src/main/kotlin/dev/dnpm/etl/processor/services/RequestProcessor.kt index 3cd912c..fd9a3f5 100644 --- a/src/main/kotlin/dev/dnpm/etl/processor/services/RequestProcessor.kt +++ b/src/main/kotlin/dev/dnpm/etl/processor/services/RequestProcessor.kt @@ -38,6 +38,7 @@ import java.util.* @Service class RequestProcessor( private val pseudonymizeService: PseudonymizeService, + private val transformationService: TransformationService, private val sender: MtbFileSender, private val requestService: RequestService, private val objectMapper: ObjectMapper, @@ -50,7 +51,7 @@ class RequestProcessor( mtbFile pseudonymizeWith pseudonymizeService - val request = MtbFileSender.MtbFileRequest(requestId, mtbFile) + val request = MtbFileSender.MtbFileRequest(requestId, transformationService.transform(mtbFile)) requestService.save( Request( diff --git a/src/main/kotlin/dev/dnpm/etl/processor/services/TransformationService.kt b/src/main/kotlin/dev/dnpm/etl/processor/services/TransformationService.kt new file mode 100644 index 0000000..26de550 --- /dev/null +++ b/src/main/kotlin/dev/dnpm/etl/processor/services/TransformationService.kt @@ -0,0 +1,81 @@ +/* + * This file is part of ETL-Processor + * + * Copyright (c) 2023 Comprehensive Cancer Center Mainfranken, Datenintegrationszentrum Philipps-Universität Marburg and Contributors + * + * 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 dev.dnpm.etl.processor.services + +import com.fasterxml.jackson.databind.ObjectMapper +import com.jayway.jsonpath.JsonPath +import com.jayway.jsonpath.PathNotFoundException +import de.ukw.ccc.bwhc.dto.MtbFile + +class TransformationService(private val objectMapper: ObjectMapper, private val transformations: List<Transformation>) { + fun transform(mtbFile: MtbFile): MtbFile { + var json = objectMapper.writeValueAsString(mtbFile) + + transformations.forEach { transformation -> + val jsonPath = JsonPath.parse(json) + + try { + val before = transformation.path.substringBeforeLast(".") + val last = transformation.path.substringAfterLast(".") + + val existingValue = if (transformation.existingValue is Number) transformation.existingValue else transformation.existingValue.toString() + val newValue = if (transformation.newValue is Number) transformation.newValue else transformation.newValue.toString() + + jsonPath.set("$.$before.[?]$last", newValue, { + it.item(HashMap::class.java)[last] == existingValue + }) + } catch (e: PathNotFoundException) { + // Ignore + } + + json = jsonPath.jsonString() + } + + return objectMapper.readValue(json, MtbFile::class.java) + } + +} + +class Transformation private constructor(internal val path: String) { + + lateinit var existingValue: Any + private set + lateinit var newValue: Any + private set + + infix fun from(value: Any): Transformation { + this.existingValue = value + return this + } + + infix fun to(value: Any): Transformation { + this.newValue = value + return this + } + + companion object { + + fun of(path: String): Transformation { + return Transformation(path) + } + + } + +} |
