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