001package ca.uhn.fhir.rest.api;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2017 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 org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
024import org.hl7.fhir.instance.model.api.IBaseResource;
025import org.hl7.fhir.instance.model.api.IIdType;
026
027import ca.uhn.fhir.util.CoverageIgnore;
028
029public class MethodOutcome {
030
031        private Boolean myCreated;
032        private IIdType myId;
033        private IBaseOperationOutcome myOperationOutcome;
034        private IBaseResource myResource;
035
036        /**
037         * Constructor
038         */
039        public MethodOutcome() {
040                super();
041        }
042
043        /**
044         * Constructor
045         * 
046         * @param theId
047         *            The ID of the created/updated resource
048         * 
049         * @param theCreated
050         *            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
063         *            The ID of the created/updated resource
064         * 
065         * @param theBaseOperationOutcome
066         *            The operation outcome to return with the response (or null for none)
067         */
068        public MethodOutcome(IIdType theId, IBaseOperationOutcome theBaseOperationOutcome) {
069                myId = theId;
070                myOperationOutcome = theBaseOperationOutcome;
071        }
072
073        /**
074         * Constructor
075         * 
076         * @param theId
077         *            The ID of the created/updated resource
078         * 
079         * @param theBaseOperationOutcome
080         *            The operation outcome to return with the response (or null for none)
081         * 
082         * @param theCreated
083         *            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
084         *            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.
085         */
086        public MethodOutcome(IIdType theId, IBaseOperationOutcome theBaseOperationOutcome, Boolean theCreated) {
087                myId = theId;
088                myOperationOutcome = theBaseOperationOutcome;
089                myCreated = theCreated;
090        }
091
092        /**
093         * Constructor
094         * 
095         * @param theId
096         *            The ID of the created/updated resource
097         */
098        public MethodOutcome(IIdType theId) {
099                myId = theId;
100        }
101
102        /**
103         * Constructor
104         * 
105         * @param theOperationOutcome
106         *            The operation outcome resource to return
107         */
108        public MethodOutcome(IBaseOperationOutcome theOperationOutcome) {
109                myOperationOutcome = theOperationOutcome;
110        }
111
112        /**
113         * This will be set to {@link Boolean#TRUE} for instance of MethodOutcome which are
114         * returned to client instances, if the server has responded with an HTTP 201 Created.
115         */
116        public Boolean getCreated() {
117                return myCreated;
118        }
119
120        public IIdType getId() {
121                return myId;
122        }
123
124        /**
125         * Returns the {@link IBaseOperationOutcome} resource to return to the client or <code>null</code> if none.
126         * 
127         * @return This method <b>will return null</b>, unlike many methods in the API.
128         */
129        public IBaseOperationOutcome getOperationOutcome() {
130                return myOperationOutcome;
131        }
132
133        /**
134         * <b>From a client response:</b> If the method returned an actual resource body (e.g. a create/update with
135         * "Prefer: return=representation") this field will be populated with the
136         * resource itself.
137         */
138        public IBaseResource getResource() {
139                return myResource;
140        }
141
142        /**
143         * 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
144         * 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.
145         * <p>
146         * Users of HAPI should only interact with this method in Server applications
147         * </p>
148         * 
149         * @param theCreated
150         *            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
151         *            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.
152         * @return Returns a reference to <code>this</code> for easy method chaining
153         */
154        public MethodOutcome setCreated(Boolean theCreated) {
155                myCreated = theCreated;
156                return this;
157        }
158
159        /**
160         * @param theId
161         *            The ID of the created/updated resource
162         * @return Returns a reference to <code>this</code> for easy method chaining
163         */
164        public MethodOutcome setId(IIdType theId) {
165                myId = theId;
166                return this;
167        }
168
169        /**
170         * Sets the {@link IBaseOperationOutcome} resource to return to the client. Set to <code>null</code> (which is the default) if none.
171         * @return Returns a reference to <code>this</code> for easy method chaining
172         */
173        public MethodOutcome setOperationOutcome(IBaseOperationOutcome theBaseOperationOutcome) {
174                myOperationOutcome = theBaseOperationOutcome;
175                return this;
176        }
177
178        /**
179         * <b>In a server response</b>: This field may be populated in server code with the final resource for operations
180         * where a resource body is being created/updated. E.g. for an update method, this field could be populated with
181         * the resource after the update is applied, with the new version ID, lastUpdate time, etc. 
182         * <p>
183         * This field is optional, but if it is populated the server will return the resource body if requested to
184         * do so via the HTTP Prefer header.
185         * </p> 
186         * @return Returns a reference to <code>this</code> for easy method chaining
187         */
188        public MethodOutcome setResource(IBaseResource theResource) {
189                myResource = theResource;
190                return this;
191        }
192
193}