001package ca.uhn.fhir.jpa.mdm.svc;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server - 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.context.FhirContext;
024import ca.uhn.fhir.mdm.api.IMdmSettings;
025import ca.uhn.fhir.mdm.log.Logs;
026import ca.uhn.fhir.mdm.rules.json.MdmResourceSearchParamJson;
027import ca.uhn.fhir.mdm.svc.MdmSearchParamSvc;
028import ca.uhn.fhir.mdm.util.MdmResourceUtil;
029import org.hl7.fhir.instance.model.api.IAnyResource;
030import org.slf4j.Logger;
031import org.springframework.beans.factory.annotation.Autowired;
032import org.springframework.stereotype.Service;
033
034import java.util.List;
035
036@Service
037public class MdmResourceFilteringSvc {
038        private static final Logger ourLog = Logs.getMdmTroubleshootingLog();
039
040        @Autowired
041        private IMdmSettings myMdmSettings;
042        @Autowired
043        MdmSearchParamSvc myMdmSearchParamSvc;
044        @Autowired
045        FhirContext myFhirContext;
046
047        /**
048         * Given a resource from the MDM Channel, determine whether or not MDM processing should occur on it.
049         *
050         * MDM processing should occur if for any {@link MdmResourceSearchParamJson ) Search Param, the resource contains a value.
051         *
052         * If the resource has no attributes that appear in the candidate search params, processing should be skipped, as there is not
053         * sufficient information to perform meaningful MDM processing. (For example, how can MDM processing occur on a patient that has _no_ attributes?)
054         *
055         * @param theResource the resource that you wish to check against MDM rules.
056         *
057         * @return whether or not MDM processing should proceed
058         */
059        public boolean shouldBeProcessed(IAnyResource theResource) {
060                if (MdmResourceUtil.isMdmManaged(theResource)) {
061                        ourLog.trace("MDM Message handler is dropping [{}] as it is MDM-managed.", theResource.getId());
062                        return false;
063                }
064
065                String resourceType = myFhirContext.getResourceType(theResource);
066                List<MdmResourceSearchParamJson> candidateSearchParams = myMdmSettings.getMdmRules().getCandidateSearchParams();
067
068                if (candidateSearchParams.isEmpty()) {
069                        return true;
070                }
071
072                boolean containsValueForSomeSearchParam = candidateSearchParams.stream()
073                        .filter(csp -> myMdmSearchParamSvc.searchParamTypeIsValidForResourceType(csp.getResourceType(), resourceType))
074                        .flatMap(csp -> csp.getSearchParams().stream())
075                        .map(searchParam -> myMdmSearchParamSvc.getValueFromResourceForSearchParam(theResource, searchParam))
076                        .anyMatch(valueList -> !valueList.isEmpty());
077
078                ourLog.trace("Is {} suitable for MDM processing? : {}", theResource.getId(), containsValueForSomeSearchParam);
079                return containsValueForSomeSearchParam;
080        }
081}