001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2025 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 org.apache.commons.csv.CSVRecord;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028import org.springframework.util.CollectionUtils;
029
030import java.util.ArrayList;
031import java.util.List;
032import java.util.Map;
033import java.util.Objects;
034
035import static org.apache.commons.lang3.StringUtils.isNotBlank;
036import static org.apache.commons.lang3.StringUtils.trim;
037
038public class PropertyHandler implements IZipContentsHandlerCsv {
039
040        private static final Logger ourLog = LoggerFactory.getLogger(PropertyHandler.class);
041
042        public static final String CODE = "CODE";
043        public static final String KEY = "KEY";
044        public static final String VALUE = "VALUE";
045        public static final String TYPE = "TYPE";
046        private final Map<String, List<TermConceptProperty>> myCode2Properties;
047
048        public PropertyHandler(Map<String, List<TermConceptProperty>> theCode2concept) {
049                myCode2Properties = theCode2concept;
050        }
051
052        @Override
053        public void accept(CSVRecord theRecord) {
054                String code = trim(theRecord.get(CODE));
055                String key = trim(theRecord.get(KEY));
056
057                if (isNotBlank(code) && isNotBlank(key)) {
058                        String value = trim(theRecord.get(VALUE));
059                        String type = trim(theRecord.get(TYPE));
060
061                        List<TermConceptProperty> conceptProperties = myCode2Properties.get(code);
062                        if (conceptProperties == null) conceptProperties = new ArrayList<>();
063
064                        if (isDuplicateConceptProperty(conceptProperties, key, value, type)) {
065                                ourLog.info(
066                                                "Duplicate concept property found for code {}, key {}, value {}, type {}. Skipping entry.",
067                                                code,
068                                                key,
069                                                value,
070                                                type);
071                                return;
072                        }
073
074                        TermConceptProperty conceptProperty = new TermConceptProperty();
075                        conceptProperty.setKey(key);
076                        conceptProperty.setValue(value);
077                        // TODO: check this for different types, other types should be added once TermConceptPropertyTypeEnum
078                        // contain different types
079                        conceptProperty.setType(TermConceptPropertyTypeEnum.STRING);
080                        conceptProperties.add(conceptProperty);
081                        myCode2Properties.put(code, conceptProperties);
082                }
083        }
084
085        public boolean isDuplicateConceptProperty(
086                        List<TermConceptProperty> conceptProperties, String key, String value, String typeString) {
087                if (CollectionUtils.isEmpty(conceptProperties)) {
088                        return false;
089                }
090                return conceptProperties.stream()
091                                .anyMatch(p -> Objects.equals(p.getKey(), key)
092                                                && Objects.equals(p.getValue(), value)
093                                                && Objects.equals(p.getType(), TermConceptPropertyTypeEnum.fromString(typeString)));
094        }
095}