001package ca.uhn.fhir.jpa.term.custom;
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.TermConcept;
024import ca.uhn.fhir.jpa.term.IZipContentsHandlerCsv;
025import ca.uhn.fhir.jpa.term.TermLoaderSvcImpl;
026import org.apache.commons.csv.CSVRecord;
027import org.apache.commons.lang3.Validate;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import java.util.Map;
032
033import static org.apache.commons.lang3.StringUtils.isNotBlank;
034import static org.apache.commons.lang3.StringUtils.trim;
035
036public class ConceptHandler implements IZipContentsHandlerCsv {
037
038        private static final Logger ourLog = LoggerFactory.getLogger(ConceptHandler.class);
039        public static final String CODE = "CODE";
040        public static final String DISPLAY = "DISPLAY";
041        private final Map<String, TermConcept> myCode2Concept;
042
043        public ConceptHandler(Map<String, TermConcept> theCode2concept) {
044                myCode2Concept = theCode2concept;
045        }
046
047        @Override
048        public void accept(CSVRecord theRecord) {
049                String code = trim(theRecord.get(CODE));
050                if (isNotBlank(code)) {
051                        String display = trim(theRecord.get(DISPLAY));
052
053                        Validate.isTrue(!myCode2Concept.containsKey(code), "The code %s has appeared more than once", code);
054
055                        TermConcept concept = TermLoaderSvcImpl.getOrCreateConcept(myCode2Concept, code);
056                        concept.setCode(code);
057                        concept.setDisplay(display);
058
059                        myCode2Concept.put(code, concept);
060                }
061        }
062}