001package ca.uhn.fhir.model.api;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 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.apache.commons.lang3.Validate;
024import org.apache.commons.lang3.builder.ToStringBuilder;
025import org.apache.commons.lang3.builder.ToStringStyle;
026
027import ca.uhn.fhir.model.primitive.BoundCodeDt;
028import ca.uhn.fhir.model.primitive.CodeDt;
029import ca.uhn.fhir.model.primitive.DecimalDt;
030import ca.uhn.fhir.model.primitive.IdDt;
031import ca.uhn.fhir.model.primitive.InstantDt;
032import ca.uhn.fhir.model.primitive.StringDt;
033import ca.uhn.fhir.model.primitive.XhtmlDt;
034import ca.uhn.fhir.model.valueset.BundleEntrySearchModeEnum;
035import ca.uhn.fhir.model.valueset.BundleEntryTransactionMethodEnum;
036import ca.uhn.fhir.util.ElementUtil;
037
038public class BundleEntry extends BaseBundle {
039
040        //@formatter:off
041        /* ****************************************************
042         * NB: add any new fields to the isEmpty() method!!!
043         *****************************************************/
044        //@formatter:on
045        private TagList myCategories;
046        private InstantDt myDeletedAt;
047        private StringDt myDeletedByEmail;
048        private StringDt myDeletedByName;
049        private StringDt myDeletedComment;
050        private IdDt myDeletedResourceId;
051        private CodeDt myDeletedResourceType;
052        private StringDt myDeletedResourceVersion;
053        private StringDt myLinkAlternate;
054        private StringDt myLinkBase;
055        private StringDt myLinkSearch;
056        private StringDt myLinkSelf;
057        private InstantDt myPublished;
058        private IResource myResource;
059        private DecimalDt myScore;
060        private BoundCodeDt<BundleEntrySearchModeEnum> mySearchMode;
061        private BoundCodeDt<BundleEntryTransactionMethodEnum> myTransactionOperation;
062        private XhtmlDt mySummary;
063        private StringDt myTitle;
064        private InstantDt myUpdated;
065
066        /**
067         * @deprecated Tags wil become immutable in a future release of HAPI, so
068         *             {@link #addCategory(String, String, String)} should be used instead
069         */
070        @Deprecated
071        public Tag addCategory() {
072                Tag retVal = new Tag();
073                getCategories().add(retVal);
074                return retVal;
075        }
076
077        public void addCategory(String theScheme, String theTerm, String theLabel) {
078                getCategories().add(new Tag(theScheme, theTerm, theLabel));
079        }
080
081        public void addCategory(Tag theTag) {
082                getCategories().add(theTag);
083        }
084
085        public TagList getCategories() {
086                if (myCategories == null) {
087                        myCategories = new TagList();
088                }
089                return myCategories;
090        }
091
092        /**
093         * Gets the date/time that thius entry was deleted.
094         */
095        public InstantDt getDeletedAt() {
096                if (myDeletedAt == null) {
097                        myDeletedAt = new InstantDt();
098                }
099                return myDeletedAt;
100        }
101
102        public StringDt getDeletedByEmail() {
103                if (myDeletedByEmail == null) {
104                        myDeletedByEmail = new StringDt();
105                }
106                return myDeletedByEmail;
107        }
108
109        public StringDt getDeletedByName() {
110                if (myDeletedByName == null) {
111                        myDeletedByName = new StringDt();
112                }
113                return myDeletedByName;
114        }
115
116        public StringDt getDeletedComment() {
117                if (myDeletedComment == null) {
118                        myDeletedComment = new StringDt();
119                }
120                return myDeletedComment;
121        }
122
123        public IdDt getDeletedResourceId() {
124                if (myDeletedResourceId == null) {
125                        myDeletedResourceId = new IdDt();
126                }
127                return myDeletedResourceId;
128        }
129
130        public CodeDt getDeletedResourceType() {
131                if (myDeletedResourceType == null) {
132                        myDeletedResourceType = new CodeDt();
133                }
134                return myDeletedResourceType;
135        }
136
137        public StringDt getDeletedResourceVersion() {
138                if (myDeletedResourceVersion == null) {
139                        myDeletedResourceVersion = new StringDt();
140                }
141                return myDeletedResourceVersion;
142        }
143
144        /**
145         * @deprecated Setting IDs on bundle entries is redundant since resources already have an ID field. Instead of
146         *             providing an ID using this method, set the ID on the resource using {@link IResource#setId(IdDt)} or
147         *             if this entry represents a deleted resource, use {@link #setDeletedResourceId(IdDt)}.
148         */
149        @Override
150        @Deprecated
151        public IdDt getId() {
152                return super.getId();
153        }
154
155        public StringDt getLinkAlternate() {
156                if (myLinkAlternate == null) {
157                        myLinkAlternate = new StringDt();
158                }
159                return myLinkAlternate;
160        }
161
162        /**
163         * @deprecated Use resource ID to determine base URL
164         */
165        @Deprecated
166        public StringDt getLinkBase() {
167                if (myLinkBase == null) {
168                        myLinkBase = new StringDt();
169                }
170                return myLinkBase;
171        }
172
173        public StringDt getLinkSearch() {
174                if (myLinkSearch == null) {
175                        myLinkSearch = new StringDt();
176                }
177                return myLinkSearch;
178        }
179
180        public StringDt getLinkSelf() {
181                if (myLinkSelf == null) {
182                        myLinkSelf = new StringDt();
183                }
184                return myLinkSelf;
185        }
186
187        public InstantDt getPublished() {
188                if (myPublished == null) {
189                        myPublished = new InstantDt();
190                }
191                return myPublished;
192        }
193
194        public IResource getResource() {
195                return myResource;
196        }
197
198        public DecimalDt getScore() {
199                if (myScore == null) {
200                        myScore = new DecimalDt();
201                }
202                return myScore;
203        }
204
205
206        public XhtmlDt getSummary() {
207                if (mySummary == null) {
208                        mySummary = new XhtmlDt();
209                }
210                return mySummary;
211        }
212
213        public StringDt getTitle() {
214                if (myTitle == null) {
215                        myTitle = new StringDt();
216                }
217                return myTitle;
218        }
219
220        /**
221         * @deprecated <b>DSTU2 Note:</b> As of DSTU2, bundle entries no longer have an updated time (this bit of metadata
222         *             has been moved to the resource &lt;meta/&gt; element so it is redundant here). In preparation for
223         *             DSTU2, it is recommended that you migrate code away from using this method and over to using resource
224         *             metadata instead.
225         */
226        @Deprecated
227        public InstantDt getUpdated() {
228                if (myUpdated == null) {
229                        myUpdated = new InstantDt();
230                }
231                if (myUpdated.isEmpty() && myResource != null) {
232                        InstantDt resourceUpdated = ResourceMetadataKeyEnum.UPDATED.get(myResource);
233                        if (resourceUpdated != null && !resourceUpdated.isEmpty()) {
234                                return resourceUpdated;
235                        }
236                }
237                return myUpdated;
238        }
239
240        @Override
241        public boolean isEmpty() {
242                //@formatter:off
243                return super.isEmpty() && 
244                                ElementUtil.isEmpty(
245                                                myDeletedResourceId, myDeletedResourceType, myDeletedResourceVersion, myDeletedAt, 
246                                                myScore, mySearchMode, myTransactionOperation, myCategories, 
247                                                myLinkAlternate, myLinkSelf, myPublished, myResource, mySummary, 
248                                                myTitle, myUpdated, myDeletedByEmail, myDeletedByName, myDeletedComment);
249                //@formatter:on
250        }
251
252        /**
253         * Sets the date/time that this entry was deleted.
254         */
255        public void setDeleted(InstantDt theDeletedAt) {
256                myDeletedAt = theDeletedAt;
257        }
258
259        public void setDeletedByEmail(StringDt theDeletedByEmail) {
260                myDeletedByEmail = theDeletedByEmail;
261        }
262
263        public void setDeletedByName(StringDt theDeletedByName) {
264                if (myDeletedByName == null) {
265                        myDeletedByName = new StringDt();
266                }
267                myDeletedByName = theDeletedByName;
268        }
269
270        public void setDeletedComment(StringDt theDeletedComment) {
271                myDeletedComment = theDeletedComment;
272        }
273
274        public void setDeletedResourceId(IdDt theDeletedResourceId) {
275                myDeletedResourceId = theDeletedResourceId;
276        }
277
278        public void setDeletedResourceType(CodeDt theDeletedResourceType) {
279                myDeletedResourceType = theDeletedResourceType;
280        }
281
282        public void setDeletedResourceVersion(StringDt theDeletedResourceVersion) {
283                myDeletedResourceVersion = theDeletedResourceVersion;
284        }
285
286        /**
287         * @deprecated Bundle entries no longer have an ID in DSTU2, as ID is explicitly stated in the resource itself.
288         */
289        @Override
290        @Deprecated
291        public void setId(IdDt theId) {
292                super.setId(theId);
293        }
294
295        public void setLinkAlternate(StringDt theLinkAlternate) {
296                myLinkAlternate = theLinkAlternate;
297        }
298
299        /**
300         * @deprecated Use resource ID to determine base URL
301         */
302        @Deprecated
303        public void setLinkBase(StringDt theLinkBase) {
304                myLinkBase = theLinkBase;
305        }
306
307        public void setLinkSearch(StringDt theLinkSearch) {
308                myLinkSearch = theLinkSearch;
309        }
310
311        public void setLinkSelf(StringDt theLinkSelf) {
312                if (myLinkSelf == null) {
313                        myLinkSelf = new StringDt();
314                }
315                myLinkSelf = theLinkSelf;
316        }
317
318        public void setPublished(InstantDt thePublished) {
319                Validate.notNull(thePublished, "Published may not be null");
320                myPublished = thePublished;
321        }
322
323        public void setResource(IResource theResource) {
324                myResource = theResource;
325        }
326
327        public void setScore(DecimalDt theScore) {
328                myScore = theScore;
329        }
330
331
332        /**
333         * @deprecated <b>DSTU2 Note:</b> As of DSTU2, bundle entries no longer have an updated time (this bit of metadata
334         *             has been moved to the resource &lt;meta/&gt; element so it is redundant here). In preparation for
335         *             DSTU2, it is recommended that you migrate code away from using this method and over to using resource
336         *             metadata instead.
337         */
338        @Deprecated
339        public void setUpdated(InstantDt theUpdated) {
340                Validate.notNull(theUpdated, "Updated may not be null");
341                myUpdated = theUpdated;
342        }
343
344        @Override
345        public String toString() {
346                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
347                if (getResource() != null) {
348                        b.append("type", getResource().getClass().getSimpleName());
349                } else {
350                        b.append("No resource");
351                }
352                b.append("id", getId());
353                return b.toString();
354        }
355
356        public BoundCodeDt<BundleEntrySearchModeEnum> getSearchMode() {
357                if (mySearchMode == null) {
358                        mySearchMode = new BoundCodeDt<BundleEntrySearchModeEnum>(BundleEntrySearchModeEnum.VALUESET_BINDER);
359                }
360                return mySearchMode;
361        }
362
363        public void setSearchMode(BoundCodeDt<BundleEntrySearchModeEnum> theSearchMode) {
364                mySearchMode = theSearchMode;
365        }
366
367        public BoundCodeDt<BundleEntryTransactionMethodEnum> getTransactionMethod() {
368                if (myTransactionOperation == null) {
369                        myTransactionOperation = new BoundCodeDt<BundleEntryTransactionMethodEnum>(BundleEntryTransactionMethodEnum.VALUESET_BINDER);
370                }
371                return myTransactionOperation;
372        }
373
374        public void setTransactionMethod(BoundCodeDt<BundleEntryTransactionMethodEnum> theTransactionOperation) {
375                myTransactionOperation = theTransactionOperation;
376        }
377
378        
379        
380        
381}