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.TermConceptProperty;
023import ca.uhn.fhir.jpa.entity.TermConceptPropertyTypeEnum;
024import ca.uhn.fhir.jpa.term.IZipContentsHandlerCsv;
025import ca.uhn.fhir.jpa.term.TermLoaderSvcImpl;
026import ca.uhn.fhir.util.ValidateUtil;
027import org.apache.commons.csv.CSVRecord;
028
029import java.util.ArrayList;
030import java.util.List;
031import java.util.Map;
032
033import static org.apache.commons.lang3.StringUtils.isNotBlank;
034import static org.apache.commons.lang3.StringUtils.trim;
035
036public class PropertyHandler implements IZipContentsHandlerCsv {
037
038        public static final String CODE = "CODE";
039        public static final String KEY = "KEY";
040        public static final String VALUE = "VALUE";
041        public static final String TYPE = "TYPE";
042        private final Map<String, List<TermConceptProperty>> myCode2Properties;
043
044        public PropertyHandler(Map<String, List<TermConceptProperty>> theCode2concept) {
045                myCode2Properties = theCode2concept;
046        }
047
048        @Override
049        public void accept(CSVRecord theRecord) {
050                String code = trim(theRecord.get(CODE));
051                String key = trim(theRecord.get(KEY));
052
053                if (isNotBlank(code) && isNotBlank(KEY)) {
054                        String value = trim(theRecord.get(VALUE));
055                        String type = trim(theRecord.get(TYPE));
056
057                        List<TermConceptProperty> conceptProperties = myCode2Properties.get(code);
058                        if (conceptProperties == null) conceptProperties = new ArrayList<>();
059
060                        TermConceptProperty conceptProperty =
061                                        TermLoaderSvcImpl.getOrCreateConceptProperty(myCode2Properties, code, key);
062                        ValidateUtil.isNotNullOrThrowUnprocessableEntity(
063                                        conceptProperty, "Concept property %s not found in file", conceptProperty);
064
065                        conceptProperty.setKey(key);
066                        conceptProperty.setValue(value);
067                        // TODO: check this for different types, other types should be added once TermConceptPropertyTypeEnum
068                        // contain different types
069                        conceptProperty.setType(TermConceptPropertyTypeEnum.STRING);
070                        conceptProperties.add(conceptProperty);
071                        myCode2Properties.put(code, conceptProperties);
072                }
073        }
074}