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.mdm.rules.json.MdmMatcherJson;
023import ca.uhn.fhir.mdm.rules.matcher.models.IMdmFieldMatcher;
024import ca.uhn.fhir.mdm.util.IdentifierUtil;
025import ca.uhn.fhir.model.primitive.StringDt;
026import ca.uhn.fhir.util.CanonicalIdentifier;
027import org.hl7.fhir.instance.model.api.IBase;
028
029public class IdentifierMatcher implements IMdmFieldMatcher {
030
031        private boolean isEmpty(StringDt theValue) {
032                if (theValue == null) {
033                        return true;
034                }
035                return theValue.isEmpty();
036        }
037
038        /**
039         * @return true if the two fhir identifiers are the same.  If @param theIdentifierSystem is not null, then the
040         * matcher only returns true if the identifier systems also match this system.
041         * @throws UnsupportedOperationException if either Base is not an Identifier instance
042         */
043        @Override
044        public boolean matches(IBase theLeftBase, IBase theRightBase, MdmMatcherJson theParams) {
045                CanonicalIdentifier left = IdentifierUtil.identifierDtFromIdentifier(theLeftBase);
046                if (theParams.getIdentifierSystem() != null) {
047                        if (!theParams.getIdentifierSystem().equals(left.getSystemElement().getValueAsString())) {
048                                return false;
049                        }
050                }
051                CanonicalIdentifier right = IdentifierUtil.identifierDtFromIdentifier(theRightBase);
052                if (isEmpty(left.getValueElement()) || isEmpty(right.getValueElement())) {
053                        return false;
054                }
055                return left.equals(right);
056        }
057}