001/*-
002 * #%L
003 * HAPI FHIR Storage api
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.dao.r4;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.jpa.dao.ITransactionProcessorVersionAdapter;
025import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
026import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
027import org.hl7.fhir.exceptions.FHIRException;
028import org.hl7.fhir.instance.model.api.IBaseExtension;
029import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
030import org.hl7.fhir.instance.model.api.IBaseResource;
031import org.hl7.fhir.r4.model.Bundle;
032import org.hl7.fhir.r4.model.OperationOutcome;
033import org.hl7.fhir.r4.model.Resource;
034
035import java.util.Date;
036import java.util.List;
037import java.util.Optional;
038
039public class TransactionProcessorVersionAdapterR4
040                implements ITransactionProcessorVersionAdapter<Bundle, Bundle.BundleEntryComponent> {
041        @Override
042        public void setResponseStatus(Bundle.BundleEntryComponent theBundleEntry, String theStatus) {
043                theBundleEntry.getResponse().setStatus(theStatus);
044        }
045
046        @Override
047        public void setResponseLastModified(Bundle.BundleEntryComponent theBundleEntry, Date theLastModified) {
048                theBundleEntry.getResponse().setLastModified(theLastModified);
049        }
050
051        @Override
052        public void setResource(Bundle.BundleEntryComponent theBundleEntry, IBaseResource theResource) {
053                theBundleEntry.setResource((Resource) theResource);
054        }
055
056        @Override
057        public IBaseResource getResource(Bundle.BundleEntryComponent theBundleEntry) {
058                return theBundleEntry.getResource();
059        }
060
061        @Override
062        public String getBundleType(Bundle theRequest) {
063                if (theRequest.getType() == null) {
064                        return null;
065                }
066                return theRequest.getTypeElement().getValue().toCode();
067        }
068
069        @Override
070        public void populateEntryWithOperationOutcome(
071                        BaseServerResponseException theCaughtEx, Bundle.BundleEntryComponent theEntry) {
072                OperationOutcome oo = new OperationOutcome();
073                oo.addIssue()
074                                .setSeverity(OperationOutcome.IssueSeverity.ERROR)
075                                .setDiagnostics(theCaughtEx.getMessage())
076                                .setCode(OperationOutcome.IssueType.EXCEPTION);
077                theEntry.getResponse().setOutcome(oo);
078        }
079
080        @Override
081        public Bundle createBundle(String theBundleType) {
082                Bundle resp = new Bundle();
083                try {
084                        resp.setType(Bundle.BundleType.fromCode(theBundleType));
085                } catch (FHIRException theE) {
086                        throw new InternalErrorException(Msg.code(552) + "Unknown bundle type: " + theBundleType);
087                }
088                return resp;
089        }
090
091        @Override
092        public List<Bundle.BundleEntryComponent> getEntries(Bundle theRequest) {
093                return theRequest.getEntry();
094        }
095
096        @Override
097        public void addEntry(Bundle theBundle, Bundle.BundleEntryComponent theEntry) {
098                theBundle.addEntry(theEntry);
099        }
100
101        @Override
102        public Bundle.BundleEntryComponent addEntry(Bundle theBundle) {
103                return theBundle.addEntry();
104        }
105
106        @Override
107        public String getEntryRequestVerb(FhirContext theContext, Bundle.BundleEntryComponent theEntry) {
108                String retVal = null;
109                Bundle.HTTPVerb value = theEntry.getRequest().getMethodElement().getValue();
110                if (value != null) {
111                        retVal = value.toCode();
112                }
113                return retVal;
114        }
115
116        @Override
117        public String getFullUrl(Bundle.BundleEntryComponent theEntry) {
118                return theEntry.getFullUrl();
119        }
120
121        @Override
122        public void setFullUrl(Bundle.BundleEntryComponent theEntry, String theFullUrl) {
123                theEntry.setFullUrl(theFullUrl);
124        }
125
126        @Override
127        public String getEntryIfNoneExist(Bundle.BundleEntryComponent theEntry) {
128                return theEntry.getRequest().getIfNoneExist();
129        }
130
131        @Override
132        public String getEntryRequestUrl(Bundle.BundleEntryComponent theEntry) {
133                return theEntry.getRequest().getUrl();
134        }
135
136        @Override
137        public void setResponseLocation(Bundle.BundleEntryComponent theEntry, String theResponseLocation) {
138                theEntry.getResponse().setLocation(theResponseLocation);
139        }
140
141        @Override
142        public void setResponseETag(Bundle.BundleEntryComponent theEntry, String theEtag) {
143                theEntry.getResponse().setEtag(theEtag);
144        }
145
146        @Override
147        public String getEntryRequestIfMatch(Bundle.BundleEntryComponent theEntry) {
148                return theEntry.getRequest().getIfMatch();
149        }
150
151        @Override
152        public String getEntryRequestIfNoneExist(Bundle.BundleEntryComponent theEntry) {
153                return theEntry.getRequest().getIfNoneExist();
154        }
155
156        @Override
157        public String getEntryRequestIfNoneMatch(Bundle.BundleEntryComponent theEntry) {
158                return theEntry.getRequest().getIfNoneMatch();
159        }
160
161        @Override
162        public void setResponseOutcome(Bundle.BundleEntryComponent theEntry, IBaseOperationOutcome theOperationOutcome) {
163                theEntry.getResponse().setOutcome((Resource) theOperationOutcome);
164        }
165
166        @Override
167        public void setRequestVerb(Bundle.BundleEntryComponent theEntry, String theVerb) {
168                theEntry.getRequest().setMethod(Bundle.HTTPVerb.fromCode(theVerb));
169        }
170
171        @Override
172        public void setRequestUrl(Bundle.BundleEntryComponent theEntry, String theUrl) {
173                theEntry.getRequest().setUrl(theUrl);
174        }
175
176        @Override
177        public Optional<IBaseExtension<?, ?>> getEntryRequestExtensionByUrl(
178                        Bundle.BundleEntryComponent theEntry, String theUrl) {
179                return Optional.ofNullable(theEntry.getRequest().getExtensionByUrl(theUrl));
180        }
181}