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