
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 - 2021 Smile CDR, Inc. 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 @Override 102 public IdDt getId() { 103 if (myId == null) { 104 myId = new IdDt(); 105 } 106 return myId; 107 } 108 109 @Override 110 public IIdType getIdElement() { 111 return getId(); 112 } 113 114 @Override 115 public CodeDt getLanguage() { 116 if (myLanguage == null) { 117 myLanguage = new CodeDt(); 118 } 119 return myLanguage; 120 } 121 122 @Override 123 public IBaseMetaType getMeta() { 124 return new IBaseMetaType() { 125 126 private static final long serialVersionUID = 1L; 127 128 @Override 129 public IBaseMetaType addProfile(String theProfile) { 130 ArrayList<IdDt> newTagList = new ArrayList<IdDt>(); 131 List<IdDt> existingTagList = ResourceMetadataKeyEnum.PROFILES.get(BaseResource.this); 132 if (existingTagList != null) { 133 newTagList.addAll(existingTagList); 134 } 135 ResourceMetadataKeyEnum.PROFILES.put(BaseResource.this, newTagList); 136 137 IdDt tag = new IdDt(theProfile); 138 newTagList.add(tag); 139 return this; 140 } 141 142 @Override 143 public IBaseCoding addSecurity() { 144 List<BaseCodingDt> tagList = ResourceMetadataKeyEnum.SECURITY_LABELS.get(BaseResource.this); 145 if (tagList == null) { 146 tagList = new ArrayList<BaseCodingDt>(); 147 ResourceMetadataKeyEnum.SECURITY_LABELS.put(BaseResource.this, tagList); 148 } 149 CodingDt tag = new CodingDt(); 150 tagList.add(tag); 151 return tag; 152 } 153 154 @Override 155 public IBaseCoding addTag() { 156 TagList tagList = ResourceMetadataKeyEnum.TAG_LIST.get(BaseResource.this); 157 if (tagList == null) { 158 tagList = new TagList(); 159 ResourceMetadataKeyEnum.TAG_LIST.put(BaseResource.this, tagList); 160 } 161 Tag tag = new Tag(); 162 tagList.add(tag); 163 return tag; 164 } 165 166 @Override 167 public List<String> getFormatCommentsPost() { 168 return Collections.emptyList(); 169 } 170 171 @Override 172 public Object getUserData(String theName) { 173 throw new UnsupportedOperationException(); 174 } 175 176 @Override 177 public void setUserData(String theName, Object theValue) { 178 throw new UnsupportedOperationException(); 179 } 180 181 @Override 182 public List<String> getFormatCommentsPre() { 183 return Collections.emptyList(); 184 } 185 186 @Override 187 public Date getLastUpdated() { 188 InstantDt lu = ResourceMetadataKeyEnum.UPDATED.get(BaseResource.this); 189 if (lu != null) { 190 return lu.getValue(); 191 } 192 return null; 193 } 194 195 @Override 196 public List<? extends IPrimitiveType<String>> getProfile() { 197 ArrayList<IPrimitiveType<String>> retVal = new ArrayList<IPrimitiveType<String>>(); 198 List<IdDt> profilesList = ResourceMetadataKeyEnum.PROFILES.get(BaseResource.this); 199 if (profilesList == null) { 200 return Collections.emptyList(); 201 } 202 for (IdDt next : profilesList) { 203 retVal.add(next); 204 } 205 return Collections.unmodifiableList(retVal); 206 } 207 208 @Override 209 public List<? extends IBaseCoding> getSecurity() { 210 ArrayList<CodingDt> retVal = new ArrayList<CodingDt>(); 211 List<BaseCodingDt> labelsList = ResourceMetadataKeyEnum.SECURITY_LABELS.get(BaseResource.this); 212 if (labelsList == null) { 213 return Collections.emptyList(); 214 } 215 for (BaseCodingDt next : labelsList) { 216 retVal.add(new CodingDt(next.getSystemElement().getValue(), next.getCodeElement().getValue()).setDisplay(next.getDisplayElement().getValue())); 217 } 218 return Collections.unmodifiableList(retVal); 219 } 220 221 @Override 222 public IBaseCoding getSecurity(String theSystem, String theCode) { 223 for (IBaseCoding next : getSecurity()) { 224 if (theSystem.equals(next.getSystem()) && theCode.equals(next.getCode())) { 225 return next; 226 } 227 } 228 return null; 229 } 230 231 @Override 232 public List<? extends IBaseCoding> getTag() { 233 ArrayList<IBaseCoding> retVal = new ArrayList<IBaseCoding>(); 234 TagList tagList = ResourceMetadataKeyEnum.TAG_LIST.get(BaseResource.this); 235 if (tagList == null) { 236 return Collections.emptyList(); 237 } 238 for (Tag next : tagList) { 239 retVal.add(next); 240 } 241 return Collections.unmodifiableList(retVal); 242 } 243 244 @Override 245 public IBaseCoding getTag(String theSystem, String theCode) { 246 for (IBaseCoding next : getTag()) { 247 if (next.getSystem().equals(theSystem) && next.getCode().equals(theCode)) { 248 return next; 249 } 250 } 251 return null; 252 } 253 254 @Override 255 public String getVersionId() { 256 return getId().getVersionIdPart(); 257 } 258 259 @Override 260 public boolean hasFormatComment() { 261 return false; 262 } 263 264 @Override 265 public boolean isEmpty() { 266 return getResourceMetadata().isEmpty(); 267 } 268 269 @Override 270 public IBaseMetaType setLastUpdated(Date theHeaderDateValue) { 271 if (theHeaderDateValue == null) { 272 getResourceMetadata().remove(ResourceMetadataKeyEnum.UPDATED); 273 } else { 274 ResourceMetadataKeyEnum.UPDATED.put(BaseResource.this, new InstantDt(theHeaderDateValue)); 275 } 276 return this; 277 } 278 279 @Override 280 public IBaseMetaType setVersionId(String theVersionId) { 281 setId(getId().withVersion(theVersionId)); 282 return this; 283 } 284 }; 285 } 286 287 @Override 288 public ResourceMetadataMap getResourceMetadata() { 289 if (myResourceMetadata == null) { 290 myResourceMetadata = new ResourceMetadataMap(); 291 } 292 return myResourceMetadata; 293 } 294 295 @Override 296 public NarrativeDt getText() { 297 if (myText == null) { 298 myText = new NarrativeDt(); 299 } 300 return myText; 301 } 302 303 /** 304 * Intended to be called by extending classes {@link #isEmpty()} implementations, returns <code>true</code> if all 305 * content in this superclass instance is empty per the semantics of {@link #isEmpty()}. 306 */ 307 @Override 308 protected boolean isBaseEmpty() { 309 return super.isBaseEmpty() && ElementUtil.isEmpty(myLanguage, myText, myId); 310 } 311 312 public void setContained(ContainedDt theContained) { 313 myContained = theContained; 314 } 315 316 @Override 317 public void setId(IdDt theId) { 318 myId = theId; 319 } 320 321 @Override 322 public BaseResource setId(IIdType theId) { 323 if (theId instanceof IdDt) { 324 myId = (IdDt) theId; 325 } else if (theId != null) { 326 myId = new IdDt(theId.getValue()); 327 } else { 328 myId = null; 329 } 330 return this; 331 } 332 333 @Override 334 public BaseResource setId(String theId) { 335 if (theId == null) { 336 myId = null; 337 } else { 338 myId = new IdDt(theId); 339 } 340 return this; 341 } 342 343 @Override 344 public void setLanguage(CodeDt theLanguage) { 345 myLanguage = theLanguage; 346 } 347 348 @Override 349 public void setResourceMetadata(ResourceMetadataMap theMap) { 350 Validate.notNull(theMap, "The Map must not be null"); 351 myResourceMetadata = theMap; 352 } 353 354 public void setText(NarrativeDt theText) { 355 myText = theText; 356 } 357 358 @Override 359 public String toString() { 360 ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); 361 b.append("id", getId().toUnqualified()); 362 return b.toString(); 363 } 364 365}