001package ca.uhn.fhir.model.dstu2.resource;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.Date;
006import java.util.List;
007
008/*
009 * #%L
010 * HAPI FHIR Structures - DSTU2 (FHIR v1.0.0)
011 * %%
012 * Copyright (C) 2014 - 2018 University Health Network
013 * %%
014 * Licensed under the Apache License, Version 2.0 (the "License");
015 * you may not use this file except in compliance with the License.
016 * You may obtain a copy of the License at
017 * 
018 *      http://www.apache.org/licenses/LICENSE-2.0
019 * 
020 * Unless required by applicable law or agreed to in writing, software
021 * distributed under the License is distributed on an "AS IS" BASIS,
022 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
023 * See the License for the specific language governing permissions and
024 * limitations under the License.
025 * #L%
026 */
027
028import org.apache.commons.lang3.Validate;
029import org.apache.commons.lang3.builder.ToStringBuilder;
030import org.apache.commons.lang3.builder.ToStringStyle;
031import org.hl7.fhir.instance.model.api.IBaseCoding;
032import org.hl7.fhir.instance.model.api.IBaseMetaType;
033import org.hl7.fhir.instance.model.api.IIdType;
034import org.hl7.fhir.instance.model.api.IPrimitiveType;
035
036import ca.uhn.fhir.model.api.BaseElement;
037import ca.uhn.fhir.model.api.IResource;
038import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
039import ca.uhn.fhir.model.api.Tag;
040import ca.uhn.fhir.model.api.TagList;
041import ca.uhn.fhir.model.api.annotation.Child;
042import ca.uhn.fhir.model.api.annotation.SearchParamDefinition;
043import ca.uhn.fhir.model.base.composite.BaseCodingDt;
044import ca.uhn.fhir.model.base.resource.ResourceMetadataMap;
045import ca.uhn.fhir.model.dstu2.composite.CodingDt;
046import ca.uhn.fhir.model.dstu2.composite.ContainedDt;
047import ca.uhn.fhir.model.dstu2.composite.NarrativeDt;
048import ca.uhn.fhir.model.primitive.CodeDt;
049import ca.uhn.fhir.model.primitive.IdDt;
050import ca.uhn.fhir.model.primitive.InstantDt;
051import ca.uhn.fhir.rest.gclient.StringClientParam;
052import ca.uhn.fhir.util.ElementUtil;
053
054public abstract class BaseResource extends BaseElement implements IResource {
055
056        /**
057         * <b>Fluent Client</b> search parameter constant for <b>_id</b>
058         * <p>
059         * Description: <b>the _id of a resource</b><br>
060         * Type: <b>string</b><br>
061         * Path: <b>Resource._id</b><br>
062         * </p>
063         */
064        public static final StringClientParam RES_ID = new StringClientParam(BaseResource.SP_RES_ID);
065        
066        /**
067         * Search parameter constant for <b>_id</b>
068         */
069        @SearchParamDefinition(name="_id", path="", description="The ID of the resource", type="string"  )
070        public static final String SP_RES_ID = "_id";
071
072
073        /**
074         * Search parameter constant for <b>_language</b>
075         */
076        @SearchParamDefinition(name="_language", path="", description="The language of the resource", type="string"  )
077        public static final String SP_RES_LANGUAGE = "_language";
078
079        @Child(name = "contained", order = 2, min = 0, max = 1)
080        private ContainedDt myContained;
081
082
083        private IdDt myId;
084
085        @Child(name = "language", order = 0, min = 0, max = 1)
086        private CodeDt myLanguage;
087
088        private ResourceMetadataMap myResourceMetadata;
089
090        @Child(name = "text", order = 1, min = 0, max = 1)
091        private NarrativeDt myText;
092
093        @Override
094        public ContainedDt getContained() {
095                if (myContained == null) {
096                        myContained = new ContainedDt();
097                }
098                return myContained;
099        }
100
101        public IdDt getId() {
102                if (myId == null) {
103                        myId = new IdDt();
104                }
105                return myId;
106        }
107
108        @Override
109        public IIdType getIdElement() {
110                return getId();
111        }
112
113        @Override
114        public CodeDt getLanguage() {
115                if (myLanguage == null) {
116                        myLanguage = new CodeDt();
117                }
118                return myLanguage;
119        }
120
121        @Override
122        public IBaseMetaType getMeta() {
123                return new IBaseMetaType() {
124                        
125                        private static final long serialVersionUID = 1L;
126
127                        @Override
128                        public IBaseMetaType addProfile(String theProfile) {
129                                ArrayList<IdDt> newTagList = new ArrayList<IdDt>();
130                                List<IdDt> existingTagList = ResourceMetadataKeyEnum.PROFILES.get(BaseResource.this);
131                                if (existingTagList != null) {
132                                        newTagList.addAll(existingTagList);
133                                }
134                                ResourceMetadataKeyEnum.PROFILES.put(BaseResource.this, newTagList);
135                                
136                                IdDt tag = new IdDt(theProfile);
137                                newTagList.add(tag);
138                                return this;
139                        }
140                        
141                        @Override
142                        public IBaseCoding addSecurity() {
143                                List<BaseCodingDt> tagList = ResourceMetadataKeyEnum.SECURITY_LABELS.get(BaseResource.this);
144                                if (tagList == null) {
145                                        tagList = new ArrayList<BaseCodingDt>();
146                                        ResourceMetadataKeyEnum.SECURITY_LABELS.put(BaseResource.this, tagList);
147                                }
148                                CodingDt tag = new CodingDt();
149                                tagList.add(tag);
150                                return tag;
151                        }
152                        
153                        @Override
154                        public IBaseCoding addTag() {
155                                TagList tagList = ResourceMetadataKeyEnum.TAG_LIST.get(BaseResource.this);
156                                if (tagList == null) {
157                                        tagList = new TagList();
158                                        ResourceMetadataKeyEnum.TAG_LIST.put(BaseResource.this, tagList);
159                                }
160                                Tag tag = new Tag();
161                                tagList.add(tag);
162                                return tag;
163                        }
164                        
165                        @Override
166                        public List<String> getFormatCommentsPost() {
167                                return Collections.emptyList();
168                        }
169                        
170                        @Override
171                        public List<String> getFormatCommentsPre() {
172                                return Collections.emptyList();
173                        }
174                        
175                        @Override
176                        public Date getLastUpdated() {
177                                InstantDt lu = ResourceMetadataKeyEnum.UPDATED.get(BaseResource.this);
178                                if (lu != null) {
179                                        return lu.getValue();
180                                }
181                                return null;
182                        }
183                        
184                        @Override
185                        public List<? extends IPrimitiveType<String>> getProfile() {
186                                ArrayList<IPrimitiveType<String>> retVal = new ArrayList<IPrimitiveType<String>>();
187                                List<IdDt> profilesList = ResourceMetadataKeyEnum.PROFILES.get(BaseResource.this);
188                                if (profilesList == null) {
189                                        return Collections.emptyList();
190                                }
191                                for (IdDt next : profilesList) {
192                                        retVal.add(next);
193                                }
194                                return Collections.unmodifiableList(retVal);
195                        }
196                        
197                        @Override
198                        public List<? extends IBaseCoding> getSecurity() {
199                                ArrayList<CodingDt> retVal = new ArrayList<CodingDt>();
200                                List<BaseCodingDt> labelsList = ResourceMetadataKeyEnum.SECURITY_LABELS.get(BaseResource.this);
201                                if (labelsList == null) {
202                                        return Collections.emptyList();
203                                }
204                                for (BaseCodingDt next : labelsList) {
205                                        retVal.add(new CodingDt(next.getSystemElement().getValue(), next.getCodeElement().getValue()).setDisplay(next.getDisplayElement().getValue()));
206                                }
207                                return Collections.unmodifiableList(retVal);
208                        }
209                        
210                        @Override
211                        public IBaseCoding getSecurity(String theSystem, String theCode) {
212                                for (IBaseCoding next : getSecurity()) {
213                                        if (theSystem.equals(next.getSystem()) && theCode.equals(next.getCode())) {
214                                                return next;
215                                        }
216                                }
217                                return null;
218                        }
219                        
220                        @Override
221                        public List<? extends IBaseCoding> getTag() {
222                                ArrayList<IBaseCoding> retVal = new ArrayList<IBaseCoding>();
223                                TagList tagList = ResourceMetadataKeyEnum.TAG_LIST.get(BaseResource.this);
224                                if (tagList == null) {
225                                        return Collections.emptyList();
226                                }
227                                for (Tag next : tagList) {
228                                        retVal.add(next);
229                                }
230                                return Collections.unmodifiableList(retVal);
231                        }
232                        
233                        @Override
234                        public IBaseCoding getTag(String theSystem, String theCode) {
235                                for (IBaseCoding next : getTag()) {
236                                        if (next.getSystem().equals(theSystem) && next.getCode().equals(theCode)) {
237                                                return next;
238                                        }
239                                }
240                                return null;
241                        }
242                        
243                        @Override
244                        public String getVersionId() {
245                                return getId().getVersionIdPart();
246                        }
247                        
248                        @Override
249                        public boolean hasFormatComment() {
250                                return false;
251                        }
252                        
253                        @Override
254                        public boolean isEmpty() {
255                                return getResourceMetadata().isEmpty();
256                        }
257                        
258                        @Override
259                        public IBaseMetaType setLastUpdated(Date theHeaderDateValue) {
260                                if (theHeaderDateValue == null) {
261                                        getResourceMetadata().remove(ResourceMetadataKeyEnum.UPDATED);
262                                } else {
263                                        ResourceMetadataKeyEnum.UPDATED.put(BaseResource.this, new InstantDt(theHeaderDateValue));
264                                }
265                                return this;
266                        }
267                        
268                        @Override
269                        public IBaseMetaType setVersionId(String theVersionId) {
270                                setId(getId().withVersion(theVersionId));
271                                return this;
272                        }
273                };
274        }
275
276        @Override
277        public ResourceMetadataMap getResourceMetadata() {
278                if (myResourceMetadata == null) {
279                        myResourceMetadata = new ResourceMetadataMap();
280                }
281                return myResourceMetadata;
282        }
283
284        @Override
285        public NarrativeDt getText() {
286                if (myText == null) {
287                        myText = new NarrativeDt();
288                }
289                return myText;
290        }
291
292        /**
293         * Intended to be called by extending classes {@link #isEmpty()} implementations, returns <code>true</code> if all
294         * content in this superclass instance is empty per the semantics of {@link #isEmpty()}.
295         */
296        @Override
297        protected boolean isBaseEmpty() {
298                return super.isBaseEmpty() && ElementUtil.isEmpty(myLanguage, myText, myId);
299        }
300
301        public void setContained(ContainedDt theContained) {
302                myContained = theContained;
303        }
304        
305        public void setId(IdDt theId) {
306                myId = theId;
307        }
308
309        public BaseResource setId(IIdType theId) {
310                if (theId instanceof IdDt) {
311                        myId = (IdDt) theId;
312                } else if (theId != null) {
313                        myId = new IdDt(theId.getValue());
314                } else {
315                        myId = null;
316                }
317                return this;
318        }
319
320        public BaseResource setId(String theId) {
321                if (theId == null) {
322                        myId = null;
323                } else {
324                        myId = new IdDt(theId);
325                }
326                return this;
327        }
328
329        @Override
330        public void setLanguage(CodeDt theLanguage) {
331                myLanguage = theLanguage;
332        }
333
334        @Override
335        public void setResourceMetadata(ResourceMetadataMap theMap) {
336                Validate.notNull(theMap, "The Map must not be null");
337                myResourceMetadata = theMap;
338        }
339
340        public void setText(NarrativeDt theText) {
341                myText = theText;
342        }
343
344        @Override
345        public String toString() {
346                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
347                b.append("id", getId().toUnqualified());
348                return b.toString();
349        }
350
351}