From 29a601ca1f3f5c0c72c244f57fbca2e15122ad15 Mon Sep 17 00:00:00 2001 From: jecihjoy Date: Wed, 18 Jan 2023 14:34:54 +0300 Subject: [PATCH] IM-04: NHDD mapping prototype --- api/pom.xml | 11 +++ .../NhddMapping/CsvUtil/CsvParser.java | 34 +++++++ .../CsvUtil/NHDDConceptMapDTO.java | 36 +++++++ .../InteropConceptTranslatorImpl.java | 96 +++++++++++++++++++ .../resources/metadata/NHDDConceptMapping.csv | 3 + pom.xml | 1 + 6 files changed, 181 insertions(+) create mode 100644 api/src/main/java/org/openmrs/module/interop/NhddMapping/CsvUtil/CsvParser.java create mode 100644 api/src/main/java/org/openmrs/module/interop/NhddMapping/CsvUtil/NHDDConceptMapDTO.java create mode 100644 api/src/main/java/org/openmrs/module/interop/NhddMapping/translations/InteropConceptTranslatorImpl.java create mode 100644 api/src/main/resources/metadata/NHDDConceptMapping.csv diff --git a/api/pom.xml b/api/pom.xml index c46b631..6c0b7a0 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -40,6 +40,17 @@ + + com.opencsv + opencsv + ${opencsv.version} + + + commons-logging + commons-logging + + + diff --git a/api/src/main/java/org/openmrs/module/interop/NhddMapping/CsvUtil/CsvParser.java b/api/src/main/java/org/openmrs/module/interop/NhddMapping/CsvUtil/CsvParser.java new file mode 100644 index 0000000..4341f98 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/interop/NhddMapping/CsvUtil/CsvParser.java @@ -0,0 +1,34 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.interop.NhddMapping.CsvUtil; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.util.List; + +import com.opencsv.bean.CsvToBeanBuilder; + +public class CsvParser { + + private File fileName; + + public CsvParser(File fileName) { + this.fileName = fileName; + } + + public List getConcepts() throws FileNotFoundException { + List concepts = new CsvToBeanBuilder(new FileReader(fileName)) + .withType(NHDDConceptMapDTO.class).build().parse(); + + return concepts; + } + +} diff --git a/api/src/main/java/org/openmrs/module/interop/NhddMapping/CsvUtil/NHDDConceptMapDTO.java b/api/src/main/java/org/openmrs/module/interop/NhddMapping/CsvUtil/NHDDConceptMapDTO.java new file mode 100644 index 0000000..af36b55 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/interop/NhddMapping/CsvUtil/NHDDConceptMapDTO.java @@ -0,0 +1,36 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.interop.NhddMapping.CsvUtil; + +import com.opencsv.bean.CsvBindByName; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Setter +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class NHDDConceptMapDTO { + + @CsvBindByName(column = "CIELId") + private String CIELId; + + @CsvBindByName(column = "CIELDatatype") + private String CIELDatatype; + + @CsvBindByName(column = "NhddId") + private String NhddId; + + @CsvBindByName(column = "NhddDatatype") + private String NhddDatatype; + +} diff --git a/api/src/main/java/org/openmrs/module/interop/NhddMapping/translations/InteropConceptTranslatorImpl.java b/api/src/main/java/org/openmrs/module/interop/NhddMapping/translations/InteropConceptTranslatorImpl.java new file mode 100644 index 0000000..c6b746a --- /dev/null +++ b/api/src/main/java/org/openmrs/module/interop/NhddMapping/translations/InteropConceptTranslatorImpl.java @@ -0,0 +1,96 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.interop.NhddMapping.translations; + +import javax.annotation.Nonnull; + +import java.io.File; +import java.io.FileNotFoundException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import lombok.extern.slf4j.Slf4j; +import org.hl7.fhir.r4.model.CodeableConcept; +import org.hl7.fhir.r4.model.Coding; +import org.openmrs.Concept; +import org.openmrs.module.fhir2.api.translators.impl.ConceptTranslatorImpl; +import org.openmrs.module.interop.NhddMapping.CsvUtil.CsvParser; +import org.openmrs.module.interop.NhddMapping.CsvUtil.NHDDConceptMapDTO; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; + +@Slf4j +@Primary +@Component +public class InteropConceptTranslatorImpl extends ConceptTranslatorImpl { + + List concepts = new ArrayList<>(); + + @Override + public CodeableConcept toFhirResource(@Nonnull Concept concept) { + CodeableConcept codeableConcept = super.toFhirResource(concept); + if (codeableConcept == null) { + return null; + } + initConcepts(); + + if (!codeableConcept.getCoding().isEmpty()) { + List cielCode = codeableConcept.getCoding().stream().filter(ci -> { + if (ci.getSystem() != null) { + return ci.getSystem().equals("https://openconceptlab.org/orgs/CIEL/sources/CIEL"); + } + return false; + }).collect(Collectors.toList()); + if (!cielCode.isEmpty()) { + concepts = concepts.stream().filter((i) -> i.getCIELId().equals(cielCode.get(0).getCode())) + .collect(Collectors.toList()); + if (!concepts.isEmpty()) { + NHDDConceptMapDTO c = concepts.get(0); + codeableConcept.setCoding(new ArrayList<>()); + Coding coding = new Coding(); + coding.setCode(c.getNhddId()); + coding.setDisplay(cielCode.get(0).getDisplay()); + coding.setSystem("https://nhdd.health.go.ke/"); + codeableConcept.getCoding().add(coding); + } + + } + } + return codeableConcept; + } + + @Override + public Concept toOpenmrsType(@Nonnull CodeableConcept concept) { + return super.toOpenmrsType(concept); + } + + private void initConcepts() { + try { + URL resource = this.getClass().getClassLoader().getResource("metadata/NHDDConceptMapping.csv"); + File file = Paths.get(resource.toURI()).toFile(); + + CsvParser parser = new CsvParser(file); + + try { + concepts = parser.getConcepts(); + } + catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + } + catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } +} diff --git a/api/src/main/resources/metadata/NHDDConceptMapping.csv b/api/src/main/resources/metadata/NHDDConceptMapping.csv new file mode 100644 index 0000000..8221783 --- /dev/null +++ b/api/src/main/resources/metadata/NHDDConceptMapping.csv @@ -0,0 +1,3 @@ +CIELId,CIELDatatype,NhddId,NhddDatatype +1054,Text,49315,Text +1086,Text,9000,Text diff --git a/pom.xml b/pom.xml index c592b5d..93ece09 100644 --- a/pom.xml +++ b/pom.xml @@ -212,5 +212,6 @@ 4.2.0 2.3.4 2.4.0 + 5.7.0