001package ca.uhn.fhir.rest.api;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2019 University Health Network
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.util.CoverageIgnore;
024import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
025import org.hl7.fhir.instance.model.api.IBaseResource;
026import org.hl7.fhir.instance.model.api.IIdType;
027
028import java.util.List;
029import java.util.Map;
030
031public class MethodOutcome {
032
033        private Boolean myCreated;
034        private IIdType myId;
035        private IBaseOperationOutcome myOperationOutcome;
036        private IBaseResource myResource;
037        private Map<String, List<String>> myResponseHeaders;
038
039        /**
040         * Constructor
041         */
042        public MethodOutcome() {
043                super();
044        }
045
046        /**
047         * Constructor
048         *
049         * @param theId      The ID of the created/updated resource
050         * @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
051         *                   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.
052         */
053        @CoverageIgnore
054        public MethodOutcome(IIdType theId, Boolean theCreated) {
055                myId = theId;
056                myCreated = theCreated;
057        }
058
059        /**
060         * Constructor
061         *
062         * @param theId                   The ID of the created/updated resource
063         * @param theBaseOperationOutcome The operation outcome to return with the response (or null for none)
064         */
065        public MethodOutcome(IIdType theId, IBaseOperationOutcome theBaseOperationOutcome) {
066                myId = theId;
067                myOperationOutcome = theBaseOperationOutcome;
068        }
069
070        /**
071         * Constructor
072         *
073         * @param theId                   The ID of the created/updated resource
074         * @param theBaseOperationOutcome The operation outcome to return with the response (or null for none)
075         * @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
076         *                                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.
077         */
078        public MethodOutcome(IIdType theId, IBaseOperationOutcome theBaseOperationOutcome, Boolean theCreated) {
079                myId = theId;
080                myOperationOutcome = theBaseOperationOutcome;
081                myCreated = theCreated;
082        }
083
084        /**
085         * Constructor
086         *
087         * @param theId The ID of the created/updated resource
088         */
089        public MethodOutcome(IIdType theId) {
090                myId = theId;
091        }
092
093        /**
094         * Constructor
095         *
096         * @param theOperationOutcome The operation outcome resource to return
097         */
098        public MethodOutcome(IBaseOperationOutcome theOperationOutcome) {
099                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}