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().setSeverity(OperationOutcome.IssueSeverity.ERROR).setDiagnostics(theCaughtEx.getMessage());
69  		theEntry.getResponse().setOutcome(oo);
70  	}
71  
72  	@Override
73  	public Bundle createBundle(String theBundleType) {
74  		Bundle resp = new Bundle();
75  		try {
76  			resp.setType(Bundle.BundleType.fromCode(theBundleType));
77  		} catch (FHIRException theE) {
78  			throw new InternalErrorException("Unknown bundle type: " + theBundleType);
79  		}
80  		return resp;
81  	}
82  
83  	@Override
84  	public List<Bundle.BundleEntryComponent> getEntries(Bundle theRequest) {
85  		return theRequest.getEntry();
86  	}
87  
88  	@Override
89  	public void addEntry(Bundle theBundle, Bundle.BundleEntryComponent theEntry) {
90  		theBundle.addEntry(theEntry);
91  	}
92  
93  	@Override
94  	public Bundle.BundleEntryComponent addEntry(Bundle theBundle) {
95  		return theBundle.addEntry();
96  	}
97  
98  	@Override
99  	public String getEntryRequestVerb(Bundle.BundleEntryComponent theEntry) {
100 		String retVal = null;
101 		Bundle.HTTPVerb value = theEntry.getRequest().getMethodElement().getValue();
102 		if (value != null) {
103 			retVal = value.toCode();
104 		}
105 		return retVal;
106 	}
107 
108 	@Override
109 	public String getFullUrl(Bundle.BundleEntryComponent theEntry) {
110 		return theEntry.getFullUrl();
111 	}
112 
113 	@Override
114 	public String getEntryIfNoneExist(Bundle.BundleEntryComponent theEntry) {
115 		return theEntry.getRequest().getIfNoneExist();
116 	}
117 
118 	@Override
119 	public String getEntryRequestUrl(Bundle.BundleEntryComponent theEntry) {
120 		return theEntry.getRequest().getUrl();
121 	}
122 
123 	@Override
124 	public void setResponseLocation(Bundle.BundleEntryComponent theEntry, String theResponseLocation) {
125 		theEntry.getResponse().setLocation(theResponseLocation);
126 	}
127 
128 	@Override
129 	public void setResponseETag(Bundle.BundleEntryComponent theEntry, String theEtag) {
130 		theEntry.getResponse().setEtag(theEtag);
131 	}
132 
133 	@Override
134 	public String getEntryRequestIfMatch(Bundle.BundleEntryComponent theEntry) {
135 		return theEntry.getRequest().getIfMatch();
136 	}
137 
138 	@Override
139 	public String getEntryRequestIfNoneExist(Bundle.BundleEntryComponent theEntry) {
140 		return theEntry.getRequest().getIfNoneExist();
141 	}
142 
143 	@Override
144 	public String getEntryRequestIfNoneMatch(Bundle.BundleEntryComponent theEntry) {
145 		return theEntry.getRequest().getIfNoneMatch();
146 	}
147 
148 	@Override
149 	public void setResponseOutcome(Bundle.BundleEntryComponent theEntry, IBaseOperationOutcome theOperationOutcome) {
150 		theEntry.getResponse().setOutcome((Resource) theOperationOutcome);
151 	}
152 
153 	@Override
154 	public void setRequestVerb(Bundle.BundleEntryComponent theEntry, String theVerb) {
155 		theEntry.getRequest().setMethod(Bundle.HTTPVerb.fromCode(theVerb));
156 	}
157 
158 	@Override
159 	public void setRequestUrl(Bundle.BundleEntryComponent theEntry, String theUrl) {
160 		theEntry.getRequest().setUrl(theUrl);
161 	}
162 
163 }