001/*-
002 * #%L
003 * HAPI FHIR - Master Data Management
004 * %%
005 * Copyright (C) 2014 - 2023 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.json;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.mdm.api.MdmMatchEvaluation;
025import ca.uhn.fhir.mdm.rules.matcher.MdmMatcherEnum;
026import ca.uhn.fhir.model.api.IModelJson;
027import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
028import com.fasterxml.jackson.annotation.JsonProperty;
029import org.hl7.fhir.instance.model.api.IBase;
030
031import javax.annotation.Nonnull;
032
033/**
034 * Contains all business data for determining if a match exists on a particular field, given:
035 * <p></p>
036 * 1. A {@link MdmMatcherEnum} which determines the actual similarity values.
037 * 2. A given resource type (e.g. Patient)
038 * 3. A given FHIRPath expression for finding the particular primitive to be used for comparison. (e.g. name.given)
039 */
040public class MdmFieldMatchJson implements IModelJson {
041        @JsonProperty(value = "name", required = true)
042        String myName;
043
044        @JsonProperty(value = "resourceType", required = true)
045        String myResourceType;
046
047        @JsonProperty(value = "resourcePath", required = false)
048        String myResourcePath;
049
050        @JsonProperty(value = "fhirPath", required = false)
051        String myFhirPath;
052
053        @JsonProperty(value = "matcher", required = false)
054        MdmMatcherJson myMatcher;
055
056        @JsonProperty(value = "similarity", required = false)
057        MdmSimilarityJson mySimilarity;
058
059        public String getResourceType() {
060                return myResourceType;
061        }
062
063        public MdmFieldMatchJson setResourceType(String theResourceType) {
064                myResourceType = theResourceType;
065                return this;
066        }
067
068        public String getResourcePath() {
069                return myResourcePath;
070        }
071
072        public MdmFieldMatchJson setResourcePath(String theResourcePath) {
073                myResourcePath = theResourcePath;
074                return this;
075        }
076
077        public String getName() {
078                return myName;
079        }
080
081        public MdmFieldMatchJson setName(@Nonnull String theName) {
082                myName = theName;
083                return this;
084        }
085
086        public MdmMatcherJson getMatcher() {
087                return myMatcher;
088        }
089
090        public boolean isMatcherSupportingEmptyFields() {
091                return (getMatcher() == null) ? false : getMatcher().isMatchingEmptyFields();
092        }
093
094        public MdmFieldMatchJson setMatcher(MdmMatcherJson theMatcher) {
095                myMatcher = theMatcher;
096                return this;
097        }
098
099        public MdmSimilarityJson getSimilarity() {
100                return mySimilarity;
101        }
102
103        public MdmFieldMatchJson setSimilarity(MdmSimilarityJson theSimilarity) {
104                mySimilarity = theSimilarity;
105                return this;
106        }
107
108        public MdmMatchEvaluation match(FhirContext theFhirContext, IBase theLeftValue, IBase theRightValue) {
109                if (myMatcher != null) {
110                        boolean result = myMatcher.match(theFhirContext, theLeftValue, theRightValue);
111                        return new MdmMatchEvaluation(result, result ? 1.0 : 0.0);
112                }
113                if (mySimilarity != null) {
114                        return mySimilarity.match(theFhirContext, theLeftValue, theRightValue);
115                }
116                throw new InternalErrorException(Msg.code(1522) + "Field Match " + myName + " has neither a matcher nor a similarity.");
117        }
118
119        public String getFhirPath() {
120                return myFhirPath;
121        }
122
123        public MdmFieldMatchJson setFhirPath(String theFhirPath) {
124                myFhirPath = theFhirPath;
125                return this;
126        }
127}