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