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