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