001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2025 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.provider.merge;
021
022import ca.uhn.fhir.batch2.jobs.chunk.FhirIdJson;
023import ca.uhn.fhir.batch2.jobs.merge.MergeJobParameters;
024import ca.uhn.fhir.batch2.jobs.replacereferences.ProvenanceAgentJson;
025import ca.uhn.fhir.context.FhirContext;
026import ca.uhn.fhir.interceptor.model.RequestPartitionId;
027import ca.uhn.fhir.jpa.api.config.JpaStorageSettings;
028import ca.uhn.fhir.model.api.IProvenanceAgent;
029import org.hl7.fhir.instance.model.api.IBaseResource;
030import org.hl7.fhir.r4.model.Patient;
031
032import java.util.List;
033
034/**
035 * See <a href="https://build.fhir.org/patient-operation-merge.html">Patient $merge spec</a>
036 */
037public class MergeOperationInputParameters extends MergeOperationsCommonInputParameters {
038
039        private boolean myPreview;
040        private boolean myDeleteSource;
041        private IBaseResource myResultResource;
042        private List<IProvenanceAgent> myProvenanceAgents;
043        private boolean myCreateProvenance = true;
044        private IBaseResource myOriginalInputParameters;
045
046        protected MergeOperationInputParameters(int theResourceLimit) {
047                super(theResourceLimit);
048        }
049
050        public boolean getPreview() {
051                return myPreview;
052        }
053
054        public void setPreview(boolean thePreview) {
055                this.myPreview = thePreview;
056        }
057
058        public boolean getDeleteSource() {
059                return myDeleteSource;
060        }
061
062        public void setDeleteSource(boolean theDeleteSource) {
063                this.myDeleteSource = theDeleteSource;
064        }
065
066        public IBaseResource getResultResource() {
067                return myResultResource;
068        }
069
070        public void setResultResource(IBaseResource theResultResource) {
071                this.myResultResource = theResultResource;
072        }
073
074        public boolean getCreateProvenance() {
075                return myCreateProvenance;
076        }
077
078        public void setCreateProvenance(boolean theCreateProvenance) {
079                this.myCreateProvenance = theCreateProvenance;
080        }
081
082        public List<IProvenanceAgent> getProvenanceAgents() {
083                return myProvenanceAgents;
084        }
085
086        public void setProvenanceAgents(List<IProvenanceAgent> theProvenanceAgents) {
087                this.myProvenanceAgents = theProvenanceAgents;
088        }
089
090        public IBaseResource getOriginalInputParameters() {
091                return myOriginalInputParameters;
092        }
093
094        public void setOriginalInputParameters(IBaseResource theOriginalInputParameters) {
095                myOriginalInputParameters = theOriginalInputParameters;
096        }
097
098        public MergeJobParameters asMergeJobParameters(
099                        FhirContext theFhirContext,
100                        JpaStorageSettings theStorageSettings,
101                        Patient theSourceResource,
102                        Patient theTargetResource,
103                        RequestPartitionId thePartitionId) {
104                MergeJobParameters retval = new MergeJobParameters();
105                retval.setOriginalInputParameters(
106                                theFhirContext.newJsonParser().encodeResourceToString(myOriginalInputParameters));
107                retval.setBatchSize(theStorageSettings.getDefaultTransactionEntriesForWrite());
108                retval.setSourceId(new FhirIdJson(theSourceResource.getIdElement().toVersionless()));
109                retval.setTargetId(new FhirIdJson(theTargetResource.getIdElement().toVersionless()));
110                retval.setPartitionId(thePartitionId);
111                retval.setProvenanceAgents(ProvenanceAgentJson.from(myProvenanceAgents, theFhirContext));
112                retval.setCreateProvenance(myCreateProvenance);
113                return retval;
114        }
115}