View Javadoc
1   package ca.uhn.fhir.rest.api;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
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.util.CoverageIgnore;
24  import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
25  import org.hl7.fhir.instance.model.api.IBaseResource;
26  import org.hl7.fhir.instance.model.api.IIdType;
27  
28  import java.util.List;
29  import java.util.Map;
30  
31  public class MethodOutcome {
32  
33  	private Boolean myCreated;
34  	private IIdType myId;
35  	private IBaseOperationOutcome myOperationOutcome;
36  	private IBaseResource myResource;
37  	private Map<String, List<String>> myResponseHeaders;
38  
39  	/**
40  	 * Constructor
41  	 */
42  	public MethodOutcome() {
43  		super();
44  	}
45  
46  	/**
47  	 * Constructor
48  	 *
49  	 * @param theId      The ID of the created/updated resource
50  	 * @param theCreated If not null, indicates whether the resource was created (as opposed to being updated). This is generally not needed, since the server can assume based on the method being called
51  	 *                   whether the result was a creation or an update. However, it can be useful if you are implementing an update method that does a create if the ID doesn't already exist.
52  	 */
53  	@CoverageIgnore
54  	public MethodOutcome(IIdType theId, Boolean theCreated) {
55  		myId = theId;
56  		myCreated = theCreated;
57  	}
58  
59  	/**
60  	 * Constructor
61  	 *
62  	 * @param theId                   The ID of the created/updated resource
63  	 * @param theBaseOperationOutcome The operation outcome to return with the response (or null for none)
64  	 */
65  	public MethodOutcome(IIdType theId, IBaseOperationOutcome theBaseOperationOutcome) {
66  		myId = theId;
67  		myOperationOutcome = theBaseOperationOutcome;
68  	}
69  
70  	/**
71  	 * Constructor
72  	 *
73  	 * @param theId                   The ID of the created/updated resource
74  	 * @param theBaseOperationOutcome The operation outcome to return with the response (or null for none)
75  	 * @param theCreated              If not null, indicates whether the resource was created (as opposed to being updated). This is generally not needed, since the server can assume based on the method being called
76  	 *                                whether the result was a creation or an update. However, it can be useful if you are implementing an update method that does a create if the ID doesn't already exist.
77  	 */
78  	public MethodOutcome(IIdType theId, IBaseOperationOutcome theBaseOperationOutcome, Boolean theCreated) {
79  		myId = theId;
80  		myOperationOutcome = theBaseOperationOutcome;
81  		myCreated = theCreated;
82  	}
83  
84  	/**
85  	 * Constructor
86  	 *
87  	 * @param theId The ID of the created/updated resource
88  	 */
89  	public MethodOutcome(IIdType theId) {
90  		myId = theId;
91  	}
92  
93  	/**
94  	 * Constructor
95  	 *
96  	 * @param theOperationOutcome The operation outcome resource to return
97  	 */
98  	public MethodOutcome(IBaseOperationOutcome theOperationOutcome) {
99  		myOperationOutcome = theOperationOutcome;
100 	}
101 
102 	/**
103 	 * This will be set to {@link Boolean#TRUE} for instance of MethodOutcome which are
104 	 * returned to client instances, if the server has responded with an HTTP 201 Created.
105 	 */
106 	public Boolean getCreated() {
107 		return myCreated;
108 	}
109 
110 	/**
111 	 * If not null, indicates whether the resource was created (as opposed to being updated). This is generally not needed, since the server can assume based on the method being called whether the
112 	 * result was a creation or an update. However, it can be useful if you are implementing an update method that does a create if the ID doesn't already exist.
113 	 * <p>
114 	 * Users of HAPI should only interact with this method in Server applications
115 	 * </p>
116 	 *
117 	 * @param theCreated If not null, indicates whether the resource was created (as opposed to being updated). This is generally not needed, since the server can assume based on the method being called
118 	 *                   whether the result was a creation or an update. However, it can be useful if you are implementing an update method that does a create if the ID doesn't already exist.
119 	 * @return Returns a reference to <code>this</code> for easy method chaining
120 	 */
121 	public MethodOutcome setCreated(Boolean theCreated) {
122 		myCreated = theCreated;
123 		return this;
124 	}
125 
126 	public IIdType getId() {
127 		return myId;
128 	}
129 
130 	/**
131 	 * @param theId The ID of the created/updated resource
132 	 * @return Returns a reference to <code>this</code> for easy method chaining
133 	 */
134 	public MethodOutcome setId(IIdType theId) {
135 		myId = theId;
136 		return this;
137 	}
138 
139 	/**
140 	 * Returns the {@link IBaseOperationOutcome} resource to return to the client or <code>null</code> if none.
141 	 *
142 	 * @return This method <b>will return null</b>, unlike many methods in the API.
143 	 */
144 	public IBaseOperationOutcome getOperationOutcome() {
145 		return myOperationOutcome;
146 	}
147 
148 	/**
149 	 * Sets the {@link IBaseOperationOutcome} resource to return to the client. Set to <code>null</code> (which is the default) if none.
150 	 *
151 	 * @return Returns a reference to <code>this</code> for easy method chaining
152 	 */
153 	public MethodOutcome setOperationOutcome(IBaseOperationOutcome theBaseOperationOutcome) {
154 		myOperationOutcome = theBaseOperationOutcome;
155 		return this;
156 	}
157 
158 	/**
159 	 * <b>From a client response:</b> If the method returned an actual resource body (e.g. a create/update with
160 	 * "Prefer: return=representation") this field will be populated with the
161 	 * resource itself.
162 	 */
163 	public IBaseResource getResource() {
164 		return myResource;
165 	}
166 
167 	/**
168 	 * <b>In a server response</b>: This field may be populated in server code with the final resource for operations
169 	 * where a resource body is being created/updated. E.g. for an update method, this field could be populated with
170 	 * the resource after the update is applied, with the new version ID, lastUpdate time, etc.
171 	 * <p>
172 	 * This field is optional, but if it is populated the server will return the resource body if requested to
173 	 * do so via the HTTP Prefer header.
174 	 * </p>
175 	 *
176 	 * @return Returns a reference to <code>this</code> for easy method chaining
177 	 */
178 	public MethodOutcome setResource(IBaseResource theResource) {
179 		myResource = theResource;
180 		return this;
181 	}
182 
183 	/**
184 	 * Gets the headers for the HTTP response
185 	 */
186 	public Map<String, List<String>> getResponseHeaders() {
187 		return myResponseHeaders;
188 	}
189 
190 	/**
191 	 * Sets the headers for the HTTP response
192 	 */
193 	public void setResponseHeaders(Map<String, List<String>> theResponseHeaders) {
194 		myResponseHeaders = theResponseHeaders;
195 	}
196 
197 	public void setCreatedUsingStatusCode(int theResponseStatusCode) {
198 		if (theResponseStatusCode == Constants.STATUS_HTTP_201_CREATED) {
199 			setCreated(true);
200 		}
201 	}
202 }