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