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;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.context.phonetic.PhoneticEncoderEnum;
024import ca.uhn.fhir.jpa.nickname.INicknameSvc;
025import ca.uhn.fhir.mdm.api.IMdmSettings;
026import ca.uhn.fhir.mdm.log.Logs;
027import ca.uhn.fhir.mdm.rules.matcher.fieldmatchers.EmptyFieldMatcher;
028import ca.uhn.fhir.mdm.rules.matcher.fieldmatchers.ExtensionMatcher;
029import ca.uhn.fhir.mdm.rules.matcher.fieldmatchers.HapiDateMatcher;
030import ca.uhn.fhir.mdm.rules.matcher.fieldmatchers.HapiStringMatcher;
031import ca.uhn.fhir.mdm.rules.matcher.fieldmatchers.IdentifierMatcher;
032import ca.uhn.fhir.mdm.rules.matcher.fieldmatchers.MdmNameMatchModeEnum;
033import ca.uhn.fhir.mdm.rules.matcher.fieldmatchers.NameMatcher;
034import ca.uhn.fhir.mdm.rules.matcher.fieldmatchers.NicknameMatcher;
035import ca.uhn.fhir.mdm.rules.matcher.fieldmatchers.NumericMatcher;
036import ca.uhn.fhir.mdm.rules.matcher.fieldmatchers.PhoneticEncoderMatcher;
037import ca.uhn.fhir.mdm.rules.matcher.fieldmatchers.SubstringStringMatcher;
038import ca.uhn.fhir.mdm.rules.matcher.models.IMdmFieldMatcher;
039import ca.uhn.fhir.mdm.rules.matcher.models.MatchTypeEnum;
040import org.slf4j.Logger;
041
042public class MdmMatcherFactory implements IMatcherFactory {
043        private static final Logger ourLog = Logs.getMdmTroubleshootingLog();
044
045        private final FhirContext myFhirContext;
046        private final IMdmSettings myMdmSettings;
047
048        private final INicknameSvc myNicknameSvc;
049
050        public MdmMatcherFactory(FhirContext theFhirContext, IMdmSettings theSettings, INicknameSvc theNicknameSvc) {
051                myFhirContext = theFhirContext;
052                myMdmSettings = theSettings;
053                myNicknameSvc = theNicknameSvc;
054        }
055
056        @Override
057        public IMdmFieldMatcher getFieldMatcherForMatchType(MatchTypeEnum theMdmMatcherEnum) {
058                String matchTypeName;
059                if (theMdmMatcherEnum != null) {
060                        switch (theMdmMatcherEnum) {
061                                case CAVERPHONE1:
062                                        return new PhoneticEncoderMatcher(PhoneticEncoderEnum.CAVERPHONE1);
063                                case CAVERPHONE2:
064                                        return new PhoneticEncoderMatcher(PhoneticEncoderEnum.CAVERPHONE2);
065                                case COLOGNE:
066                                        return new PhoneticEncoderMatcher(PhoneticEncoderEnum.COLOGNE);
067                                case DOUBLE_METAPHONE:
068                                        return new PhoneticEncoderMatcher(PhoneticEncoderEnum.DOUBLE_METAPHONE);
069                                case MATCH_RATING_APPROACH:
070                                        return new PhoneticEncoderMatcher(PhoneticEncoderEnum.MATCH_RATING_APPROACH);
071                                case METAPHONE:
072                                        return new PhoneticEncoderMatcher(PhoneticEncoderEnum.METAPHONE);
073                                case NYSIIS:
074                                        return new PhoneticEncoderMatcher(PhoneticEncoderEnum.NYSIIS);
075                                case REFINED_SOUNDEX:
076                                        return new PhoneticEncoderMatcher(PhoneticEncoderEnum.REFINED_SOUNDEX);
077                                case SOUNDEX:
078                                        return new PhoneticEncoderMatcher(PhoneticEncoderEnum.SOUNDEX);
079                                case NICKNAME:
080                                        return new NicknameMatcher(myNicknameSvc);
081                                case STRING:
082                                        return new HapiStringMatcher();
083                                case SUBSTRING:
084                                        return new SubstringStringMatcher();
085                                case DATE:
086                                        return new HapiDateMatcher(myFhirContext);
087                                case NAME_ANY_ORDER:
088                                        return new NameMatcher(myFhirContext, MdmNameMatchModeEnum.ANY_ORDER);
089                                case NAME_FIRST_AND_LAST:
090                                        return new NameMatcher(myFhirContext, MdmNameMatchModeEnum.FIRST_AND_LAST);
091                                case IDENTIFIER:
092                                        return new IdentifierMatcher();
093                                case EXTENSION_ANY_ORDER:
094                                        return new ExtensionMatcher();
095                                case NUMERIC:
096                                        return new NumericMatcher();
097                                case EMPTY_FIELD:
098                                        return new EmptyFieldMatcher();
099                                default:
100                                        break;
101                        }
102                        matchTypeName = theMdmMatcherEnum.name();
103                } else {
104                        matchTypeName = "null";
105                }
106
107                // This is odd, but it's a valid code path
108                ourLog.warn("Unrecognized field type {}. Returning null", matchTypeName);
109                return null;
110        }
111}