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