001package ca.uhn.fhir.mdm.util;
002
003/*-
004 * #%L
005 * HAPI FHIR - Master Data Management
006 * %%
007 * Copyright (C) 2014 - 2022 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.context.FhirContext;
024import org.apache.commons.lang3.StringUtils;
025import org.hl7.fhir.instance.model.api.IBase;
026import org.hl7.fhir.r4.model.HumanName;
027import org.hl7.fhir.r4.model.PrimitiveType;
028
029import java.util.List;
030import java.util.stream.Collectors;
031
032public final class NameUtil {
033
034        private NameUtil() {
035        }
036
037        public static List<String> extractGivenNames(FhirContext theFhirContext, IBase theBase) {
038                switch(theFhirContext.getVersion().getVersion()) {
039                        case R4:
040                                HumanName humanNameR4 = (HumanName)theBase;
041                                return humanNameR4.getGiven().stream().map(PrimitiveType::getValueAsString).filter(s -> !StringUtils.isEmpty(s)).collect(Collectors.toList());
042                        case DSTU3:
043                                org.hl7.fhir.dstu3.model.HumanName humanNameDSTU3 = (org.hl7.fhir.dstu3.model.HumanName) theBase;
044                                return humanNameDSTU3.getGiven().stream().map(given -> given.toString()).filter(s -> !StringUtils.isEmpty(s)).collect(Collectors.toList());
045                        default:
046                                throw new UnsupportedOperationException("Version not supported: " + theFhirContext.getVersion().getVersion());
047
048                }
049        }
050
051        public static String extractFamilyName(FhirContext theFhirContext, IBase theBase) {
052                switch(theFhirContext.getVersion().getVersion()) {
053                        case R4:
054                                HumanName humanNameR4 = (HumanName)theBase;
055                                return humanNameR4.getFamily();
056                        case DSTU3:
057                                org.hl7.fhir.dstu3.model.HumanName humanNameDSTU3 = (org.hl7.fhir.dstu3.model.HumanName)theBase;
058                                return humanNameDSTU3.getFamily();
059                        default:
060                                throw new UnsupportedOperationException("Version not supported: " + theFhirContext.getVersion().getVersion());
061
062                }
063        }
064}