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.IZipContentsHandlerCsv;
025import org.apache.commons.csv.CSVRecord;
026import org.apache.commons.lang3.Validate;
027
028import java.util.HashMap;
029import java.util.Map;
030
031import static org.apache.commons.lang3.StringUtils.isNotBlank;
032import static org.apache.commons.lang3.StringUtils.trim;
033
034public class LoincPartHandler implements IZipContentsHandlerCsv {
035
036        private final Map<String, TermConcept> myCode2Concept;
037        private final TermCodeSystemVersion myCodeSystemVersion;
038        private final Map<PartTypeAndPartName, String> myPartTypeAndPartNameToPartNumber = new HashMap<>();
039
040        public LoincPartHandler(TermCodeSystemVersion theCodeSystemVersion, Map<String, TermConcept> theCode2concept) {
041                myCodeSystemVersion = theCodeSystemVersion;
042                myCode2Concept = theCode2concept;
043        }
044
045        @Override
046        public void accept(CSVRecord theRecord) {
047
048                // this is the code for the list (will repeat)
049                String partNumber = trim(theRecord.get("PartNumber"));
050                String partTypeName = trim(theRecord.get("PartTypeName"));
051                String partName = trim(theRecord.get("PartName"));
052                String partDisplayName = trim(theRecord.get("PartDisplayName"));
053
054                // Per Dan's note, we include deprecated parts
055                //              String status = trim(theRecord.get("Status"));
056                //              if (!"ACTIVE".equals(status)) {
057                //                      return;
058                //              }
059
060                PartTypeAndPartName partTypeAndPartName = new PartTypeAndPartName(partTypeName, partName);
061                String previousValue = myPartTypeAndPartNameToPartNumber.put(partTypeAndPartName, partNumber);
062                Validate.isTrue(previousValue == null, "Already had part: " + partTypeAndPartName);
063
064                TermConcept concept = myCode2Concept.get(partNumber);
065                if (concept == null) {
066                        concept = new TermConcept(myCodeSystemVersion, partNumber);
067                        concept.setDisplay(partName);
068                        myCode2Concept.put(partNumber, concept);
069                }
070
071                if (isNotBlank(partDisplayName)) {
072                        concept.addDesignation()
073                                        .setConcept(concept)
074                                        .setUseDisplay("PartDisplayName")
075                                        .setValue(partDisplayName);
076                }
077        }
078
079        public Map<PartTypeAndPartName, String> getPartTypeAndPartNameToPartNumber() {
080                return myPartTypeAndPartNameToPartNumber;
081        }
082}