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