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