001/*-
002 * #%L
003 * HAPI FHIR - Master Data Management
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.mdm.rules.matcher.fieldmatchers;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.mdm.rules.json.MdmMatcherJson;
024import ca.uhn.fhir.mdm.rules.matcher.models.IMdmFieldMatcher;
025import ca.uhn.fhir.mdm.util.NameUtil;
026import ca.uhn.fhir.util.StringUtil;
027import org.apache.commons.lang3.StringUtils;
028import org.hl7.fhir.instance.model.api.IBase;
029
030import java.util.List;
031import java.util.stream.Collectors;
032
033/**
034 * Similarity measure for two IBase name fields
035 */
036public class NameMatcher implements IMdmFieldMatcher {
037
038        private final MdmNameMatchModeEnum myMatchMode;
039
040        private final FhirContext myFhirContext;
041
042        public NameMatcher(FhirContext theFhirContext, MdmNameMatchModeEnum theMatchMode) {
043                myMatchMode = theMatchMode;
044                myFhirContext = theFhirContext;
045        }
046
047        @Override
048        public boolean matches(IBase theLeftBase, IBase theRightBase, MdmMatcherJson theParams) {
049                String leftFamilyName = NameUtil.extractFamilyName(myFhirContext, theLeftBase);
050                String rightFamilyName = NameUtil.extractFamilyName(myFhirContext, theRightBase);
051                if (StringUtils.isEmpty(leftFamilyName) || StringUtils.isEmpty(rightFamilyName)) {
052                        return false;
053                }
054
055                boolean match = false;
056
057                List<String> leftGivenNames = NameUtil.extractGivenNames(myFhirContext, theLeftBase);
058                List<String> rightGivenNames = NameUtil.extractGivenNames(myFhirContext, theRightBase);
059
060                if (!theParams.getExact()) {
061                        leftFamilyName = StringUtil.normalizeStringForSearchIndexing(leftFamilyName);
062                        rightFamilyName = StringUtil.normalizeStringForSearchIndexing(rightFamilyName);
063                        leftGivenNames = leftGivenNames.stream()
064                                        .map(StringUtil::normalizeStringForSearchIndexing)
065                                        .collect(Collectors.toList());
066                        rightGivenNames = rightGivenNames.stream()
067                                        .map(StringUtil::normalizeStringForSearchIndexing)
068                                        .collect(Collectors.toList());
069                }
070
071                for (String leftGivenName : leftGivenNames) {
072                        for (String rightGivenName : rightGivenNames) {
073                                match |= leftGivenName.equals(rightGivenName) && leftFamilyName.equals(rightFamilyName);
074                                if (myMatchMode == MdmNameMatchModeEnum.ANY_ORDER) {
075                                        match |= leftGivenName.equals(rightFamilyName) && leftFamilyName.equals(rightGivenName);
076                                }
077                        }
078                }
079
080                return match;
081        }
082}