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.IMdmSurvivorshipService;
024import ca.uhn.fhir.mdm.model.MdmTransactionContext;
025import ca.uhn.fhir.util.TerserUtil;
026import org.hl7.fhir.instance.model.api.IBase;
027import org.hl7.fhir.instance.model.api.IBaseResource;
028import org.springframework.beans.factory.annotation.Autowired;
029
030public class MdmSurvivorshipSvcImpl implements IMdmSurvivorshipService {
031
032        @Autowired
033        private FhirContext myFhirContext;
034
035        /**
036         * Merges two golden resources by overwriting all field values on theGoldenResource param for CREATE_RESOURCE,
037         * UPDATE_RESOURCE, SUBMIT_RESOURCE_TO_MDM, UPDATE_LINK (when setting to MATCH) and MANUAL_MERGE_GOLDEN_RESOURCES.
038         * PID, identifiers and meta values are not affected by this operation.
039         *
040         * @param theTargetResource        Target resource to retrieve fields from
041         * @param theGoldenResource        Golden resource to merge fields into
042         * @param theMdmTransactionContext Current transaction context
043         * @param <T>
044         */
045        @Override
046        public <T extends IBase> void applySurvivorshipRulesToGoldenResource(
047                        T theTargetResource, T theGoldenResource, MdmTransactionContext theMdmTransactionContext) {
048                switch (theMdmTransactionContext.getRestOperation()) {
049                        case MERGE_GOLDEN_RESOURCES:
050                                TerserUtil.mergeFields(
051                                                myFhirContext,
052                                                (IBaseResource) theTargetResource,
053                                                (IBaseResource) theGoldenResource,
054                                                TerserUtil.EXCLUDE_IDS_AND_META);
055                                break;
056                        default:
057                                TerserUtil.replaceFields(
058                                                myFhirContext,
059                                                (IBaseResource) theTargetResource,
060                                                (IBaseResource) theGoldenResource,
061                                                TerserUtil.EXCLUDE_IDS_AND_META);
062                                break;
063                }
064        }
065}