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