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