
001package ca.uhn.fhir.jpa.term.loinc; 002 003/*- 004 * #%L 005 * HAPI FHIR JPA Server 006 * %% 007 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import ca.uhn.fhir.jpa.entity.TermCodeSystemVersion; 024import ca.uhn.fhir.jpa.entity.TermConcept; 025import ca.uhn.fhir.jpa.entity.TermConceptParentChildLink; 026import ca.uhn.fhir.jpa.term.api.ITermLoaderSvc; 027import ca.uhn.fhir.jpa.term.IZipContentsHandlerCsv; 028import org.apache.commons.csv.CSVRecord; 029 030import java.util.Map; 031 032import static org.apache.commons.lang3.StringUtils.isNotBlank; 033import static org.apache.commons.lang3.StringUtils.trim; 034 035public class LoincHierarchyHandler implements IZipContentsHandlerCsv { 036 037 private Map<String, TermConcept> myCode2Concept; 038 private TermCodeSystemVersion myCodeSystemVersion; 039 040 public LoincHierarchyHandler(TermCodeSystemVersion theCodeSystemVersion, Map<String, TermConcept> theCode2concept) { 041 myCodeSystemVersion = theCodeSystemVersion; 042 myCode2Concept = theCode2concept; 043 } 044 045 @Override 046 public void accept(CSVRecord theRecord) { 047 String parentCode = trim(theRecord.get("IMMEDIATE_PARENT")); 048 String childCode = trim(theRecord.get("CODE")); 049 String childCodeText = trim(theRecord.get("CODE_TEXT")); 050 051 if (isNotBlank(parentCode) && isNotBlank(childCode)) { 052 TermConcept parent = getOrCreate(parentCode, "(unknown)"); 053 TermConcept child = getOrCreate(childCode, childCodeText); 054 055 parent.addChild(child, TermConceptParentChildLink.RelationshipTypeEnum.ISA); 056 057 parent.addPropertyCoding( 058 "child", 059 ITermLoaderSvc.LOINC_URI, 060 child.getCode(), 061 child.getDisplay()); 062 063 child.addPropertyCoding( 064 "parent", 065 ITermLoaderSvc.LOINC_URI, 066 parent.getCode(), 067 parent.getDisplay()); 068 } 069 } 070 071 private TermConcept getOrCreate(String theCode, String theDisplay) { 072 TermConcept retVal = myCode2Concept.get(theCode); 073 if (retVal == null) { 074 retVal = new TermConcept(); 075 retVal.setCodeSystemVersion(myCodeSystemVersion); 076 retVal.setCode(theCode); 077 retVal.setDisplay(theDisplay); 078 myCode2Concept.put(theCode, retVal); 079 } 080 return retVal; 081 } 082 083}