View Javadoc
1   package ca.uhn.fhir.jpa.dao.r4;
2   
3   /*-
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2019 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import ca.uhn.fhir.jpa.dao.TransactionProcessor;
24  import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
25  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
26  import org.hl7.fhir.exceptions.FHIRException;
27  import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
28  import org.hl7.fhir.instance.model.api.IBaseResource;
29  import org.hl7.fhir.r4.model.Bundle;
30  import org.hl7.fhir.r4.model.OperationOutcome;
31  import org.hl7.fhir.r4.model.Resource;
32  
33  import java.util.Date;
34  import java.util.List;
35  
36  public class TransactionProcessorVersionAdapterR4 implements TransactionProcessor.ITransactionProcessorVersionAdapter<Bundle, Bundle.BundleEntryComponent> {
37  	@Override
38  	public void setResponseStatus(Bundle.BundleEntryComponent theBundleEntry, String theStatus) {
39  		theBundleEntry.getResponse().setStatus(theStatus);
40  	}
41  
42  	@Override
43  	public void setResponseLastModified(Bundle.BundleEntryComponent theBundleEntry, Date theLastModified) {
44  		theBundleEntry.getResponse().setLastModified(theLastModified);
45  	}
46  
47  	@Override
48  	public void setResource(Bundle.BundleEntryComponent theBundleEntry, IBaseResource theResource) {
49  		theBundleEntry.setResource((Resource) theResource);
50  	}
51  
52  	@Override
53  	public IBaseResource getResource(Bundle.BundleEntryComponent theBundleEntry) {
54  		return theBundleEntry.getResource();
55  	}
56  
57  	@Override
58  	public String getBundleType(Bundle theRequest) {
59  		if (theRequest.getType() == null) {
60  			return null;
61  		}
62  		return theRequest.getTypeElement().getValue().toCode();
63  	}
64  
65  	@Override
66  	public void populateEntryWithOperationOutcome(BaseServerResponseException theCaughtEx, Bundle.BundleEntryComponent theEntry) {
67  		OperationOutcome oo = new OperationOutcome();
68  		oo.addIssue()
69  			.setSeverity(OperationOutcome.IssueSeverity.ERROR)
70  			.setDiagnostics(theCaughtEx.getMessage())
71  			.setCode(OperationOutcome.IssueType.EXCEPTION);
72  		theEntry.getResponse().setOutcome(oo);
73  	}
74  
75  	@Override
76  	public Bundle createBundle(String theBundleType) {
77  		Bundle resp = new Bundle();
78  		try {
79  			resp.setType(Bundle.BundleType.fromCode(theBundleType));
80  		} catch (FHIRException theE) {
81  			throw new InternalErrorException("Unknown bundle type: " + theBundleType);
82  		}
83  		return resp;
84  	}
85  
86  	@Override
87  	public List<Bundle.BundleEntryComponent> getEntries(Bundle theRequest) {
88  		return theRequest.getEntry();
89  	}
90  
91  	@Override
92  	public void addEntry(Bundle theBundle, Bundle.BundleEntryComponent theEntry) {
93  		theBundle.addEntry(theEntry);
94  	}
95  
96  	@Override
97  	public Bundle.BundleEntryComponent addEntry(Bundle theBundle) {
98  		return theBundle.addEntry();
99  	}
100 
101 	@Override
102 	public String getEntryRequestVerb(Bundle.BundleEntryComponent theEntry) {
103 		String retVal = null;
104 		Bundle.HTTPVerb value = theEntry.getRequest().getMethodElement().getValue();
105 		if (value != null) {
106 			retVal = value.toCode();
107 		}
108 		return retVal;
109 	}
110 
111 	@Override
112 	public String getFullUrl(Bundle.BundleEntryComponent theEntry) {
113 		return theEntry.getFullUrl();
114 	}
115 
116 	@Override
117 	public String getEntryIfNoneExist(Bundle.BundleEntryComponent theEntry) {
118 		return theEntry.getRequest().getIfNoneExist();
119 	}
120 
121 	@Override
122 	public String getEntryRequestUrl(Bundle.BundleEntryComponent theEntry) {
123 		return theEntry.getRequest().getUrl();
124 	}
125 
126 	@Override
127 	public void setResponseLocation(Bundle.BundleEntryComponent theEntry, String theResponseLocation) {
128 		theEntry.getResponse().setLocation(theResponseLocation);
129 	}
130 
131 	@Override
132 	public void setResponseETag(Bundle.BundleEntryComponent theEntry, String theEtag) {
133 		theEntry.getResponse().setEtag(theEtag);
134 	}
135 
136 	@Override
137 	public String getEntryRequestIfMatch(Bundle.BundleEntryComponent theEntry) {
138 		return theEntry.getRequest().getIfMatch();
139 	}
140 
141 	@Override
142 	public String getEntryRequestIfNoneExist(Bundle.BundleEntryComponent theEntry) {
143 		return theEntry.getRequest().getIfNoneExist();
144 	}
145 
146 	@Override
147 	public String getEntryRequestIfNoneMatch(Bundle.BundleEntryComponent theEntry) {
148 		return theEntry.getRequest().getIfNoneMatch();
149 	}
150 
151 	@Override
152 	public void setResponseOutcome(Bundle.BundleEntryComponent theEntry, IBaseOperationOutcome theOperationOutcome) {
153 		theEntry.getResponse().setOutcome((Resource) theOperationOutcome);
154 	}
155 
156 	@Override
157 	public void setRequestVerb(Bundle.BundleEntryComponent theEntry, String theVerb) {
158 		theEntry.getRequest().setMethod(Bundle.HTTPVerb.fromCode(theVerb));
159 	}
160 
161 	@Override
162 	public void setRequestUrl(Bundle.BundleEntryComponent theEntry, String theUrl) {
163 		theEntry.getRequest().setUrl(theUrl);
164 	}
165 
166 }