001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2023 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 static org.apache.commons.lang3.StringUtils.isBlank;
023import static org.apache.commons.lang3.StringUtils.trim;
024import static org.apache.commons.lang3.StringUtils.trimToEmpty;
025
026import java.util.Map;
027
028import org.apache.commons.csv.CSVRecord;
029
030import ca.uhn.fhir.jpa.entity.TermConcept;
031import ca.uhn.fhir.jpa.term.IZipContentsHandlerCsv;
032import ca.uhn.fhir.jpa.term.api.ITermLoaderSvc;
033
034public class LoincLinguisticVariantHandler implements IZipContentsHandlerCsv {
035
036        private final Map<String, TermConcept> myCode2Concept;
037        private final String myLanguageCode;
038
039        public LoincLinguisticVariantHandler(Map<String, TermConcept> theCode2Concept, String theLanguageCode) {
040                myCode2Concept = theCode2Concept;
041                myLanguageCode = theLanguageCode;
042        }
043
044        @Override
045        public void accept(CSVRecord theRecord) {
046                
047                String loincNumber = trim(theRecord.get("LOINC_NUM"));
048                if (isBlank(loincNumber)) {
049                        return;
050                }
051
052                TermConcept concept = myCode2Concept.get(loincNumber);
053                if (concept == null) {
054                        return;
055                }
056        
057                // The following should be created as designations for each term:
058        // COMPONENT:PROPERTY:TIME_ASPCT:SYSTEM:SCALE_TYP:METHOD_TYP (as colon-separated concatenation - FormalName)
059        // SHORTNAME
060        // LONG_COMMON_NAME
061        // LinguisticVariantDisplayName
062                        
063                //-- add formalName designation
064                StringBuilder fullySpecifiedName = new StringBuilder();
065                fullySpecifiedName.append(trimToEmpty(theRecord.get("COMPONENT") + ":"));
066                fullySpecifiedName.append(trimToEmpty(theRecord.get("PROPERTY") + ":"));
067                fullySpecifiedName.append(trimToEmpty(theRecord.get("TIME_ASPCT") + ":"));
068                fullySpecifiedName.append(trimToEmpty(theRecord.get("SYSTEM") + ":"));
069                fullySpecifiedName.append(trimToEmpty(theRecord.get("SCALE_TYP") + ":"));
070                fullySpecifiedName.append(trimToEmpty(theRecord.get("METHOD_TYP")));
071                
072                String fullySpecifiedNameStr = fullySpecifiedName.toString();
073                
074                // skip if COMPONENT, PROPERTY, TIME_ASPCT, SYSTEM, SCALE_TYP and METHOD_TYP are all empty
075                if (!fullySpecifiedNameStr.equals(":::::")) {
076                        concept.addDesignation()
077                                .setLanguage(myLanguageCode)
078                                .setUseSystem(ITermLoaderSvc.LOINC_URI)
079                                .setUseCode("FullySpecifiedName")
080                                .setUseDisplay("FullySpecifiedName")
081                                .setValue(fullySpecifiedNameStr);
082                }
083                
084                //-- other designations
085                addDesignation(theRecord, concept, "SHORTNAME");
086                addDesignation(theRecord, concept, "LONG_COMMON_NAME");         
087                addDesignation(theRecord, concept, "LinguisticVariantDisplayName");
088                
089        }
090
091        private void addDesignation(CSVRecord theRecord, TermConcept concept, String fieldName) {
092                
093                String field = trim(theRecord.get(fieldName));
094                if (isBlank(field)) {
095                        return;
096                }
097                
098                concept.addDesignation()
099                  .setLanguage(myLanguageCode)
100                  .setUseSystem(ITermLoaderSvc.LOINC_URI)
101                  .setUseCode(fieldName)
102              .setUseDisplay(fieldName)
103              .setValue(field);
104        }
105}