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.term.api.ITermLoaderSvc; 025import org.apache.commons.csv.CSVRecord; 026import org.hl7.fhir.r4.model.ConceptMap; 027import org.hl7.fhir.r4.model.ValueSet; 028 029import java.util.List; 030import java.util.Map; 031import java.util.Properties; 032 033import static ca.uhn.fhir.jpa.term.loinc.LoincUploadPropertiesEnum.*; 034import static org.apache.commons.lang3.StringUtils.isNotBlank; 035import static org.apache.commons.lang3.StringUtils.trim; 036 037public class LoincAnswerListHandler extends BaseLoincHandler { 038 039 private final Map<String, TermConcept> myCode2Concept; 040 private final TermCodeSystemVersion myCodeSystemVersion; 041 042 public LoincAnswerListHandler( 043 TermCodeSystemVersion theCodeSystemVersion, 044 Map<String, TermConcept> theCode2concept, 045 List<ValueSet> theValueSets, 046 List<ConceptMap> theConceptMaps, 047 Properties theUploadProperties, 048 String theCopyrightStatement) { 049 super(theCode2concept, theValueSets, theConceptMaps, theUploadProperties, theCopyrightStatement); 050 myCodeSystemVersion = theCodeSystemVersion; 051 myCode2Concept = theCode2concept; 052 } 053 054 @Override 055 public void accept(CSVRecord theRecord) { 056 057 // this is the code for the list (will repeat) 058 String answerListId = trim(theRecord.get("AnswerListId")); 059 String answerListName = trim(theRecord.get("AnswerListName")); 060 String answerListOid = trim(theRecord.get("AnswerListOID")); 061 String externallyDefined = trim(theRecord.get("ExtDefinedYN")); 062 String extenrallyDefinedCs = trim(theRecord.get("ExtDefinedAnswerListCodeSystem")); 063 String externallyDefinedLink = trim(theRecord.get("ExtDefinedAnswerListLink")); 064 // this is the code for the actual answer (will not repeat) 065 String answerString = trim(theRecord.get("AnswerStringId")); 066 String sequenceNumber = trim(theRecord.get("SequenceNumber")); 067 String displayText = trim(theRecord.get("DisplayText")); 068 String extCodeId = trim(theRecord.get("ExtCodeId")); 069 String extCodeDisplayName = trim(theRecord.get("ExtCodeDisplayName")); 070 String extCodeSystem = trim(theRecord.get("ExtCodeSystem")); 071 String extCodeSystemVersion = trim(theRecord.get("ExtCodeSystemVersion")); 072 073 // Answer list code 074 if (!myCode2Concept.containsKey(answerListId)) { 075 TermConcept concept = new TermConcept(myCodeSystemVersion, answerListId); 076 concept.setDisplay(answerListName); 077 myCode2Concept.put(answerListId, concept); 078 } 079 080 // Answer list ValueSet 081 String valueSetId; 082 String codeSystemVersionId = myUploadProperties.getProperty(LOINC_CODESYSTEM_VERSION.getCode()); 083 if (codeSystemVersionId != null) { 084 valueSetId = answerListId + "-" + codeSystemVersionId; 085 } else { 086 valueSetId = answerListId; 087 } 088 ValueSet vs = getValueSet( 089 valueSetId, "http://loinc.org/vs/" + answerListId, answerListName, LOINC_ANSWERLIST_VERSION.getCode()); 090 if (vs.getIdentifier().isEmpty()) { 091 vs.addIdentifier().setSystem("urn:ietf:rfc:3986").setValue("urn:oid:" + answerListOid); 092 } 093 094 if (isNotBlank(answerString)) { 095 096 // Answer code 097 if (!myCode2Concept.containsKey(answerString)) { 098 TermConcept concept = new TermConcept(myCodeSystemVersion, answerString); 099 concept.setDisplay(displayText); 100 if (isNotBlank(sequenceNumber) && sequenceNumber.matches("^[0-9]$")) { 101 concept.setSequence(Integer.parseInt(sequenceNumber)); 102 } 103 myCode2Concept.put(answerString, concept); 104 } 105 106 vs.getCompose() 107 .getIncludeFirstRep() 108 .setSystem(ITermLoaderSvc.LOINC_URI) 109 .setVersion(codeSystemVersionId) 110 .addConcept() 111 .setCode(answerString) 112 .setDisplay(displayText); 113 } 114 } 115}