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 - 2016 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 ResourceMetadataKeyEnum.UPDATED.put(BaseResource.this, new InstantDt(theHeaderDateValue)); 261 return this; 262 } 263 264 @Override 265 public IBaseMetaType setVersionId(String theVersionId) { 266 setId(getId().withVersion(theVersionId)); 267 return this; 268 } 269 }; 270 } 271 272 @Override 273 public ResourceMetadataMap getResourceMetadata() { 274 if (myResourceMetadata == null) { 275 myResourceMetadata = new ResourceMetadataMap(); 276 } 277 return myResourceMetadata; 278 } 279 280 @Override 281 public NarrativeDt getText() { 282 if (myText == null) { 283 myText = new NarrativeDt(); 284 } 285 return myText; 286 } 287 288 /** 289 * Intended to be called by extending classes {@link #isEmpty()} implementations, returns <code>true</code> if all 290 * content in this superclass instance is empty per the semantics of {@link #isEmpty()}. 291 */ 292 @Override 293 protected boolean isBaseEmpty() { 294 return super.isBaseEmpty() && ElementUtil.isEmpty(myLanguage, myText, myId); 295 } 296 297 public void setContained(ContainedDt theContained) { 298 myContained = theContained; 299 } 300 301 public void setId(IdDt theId) { 302 myId = theId; 303 } 304 305 public BaseResource setId(IIdType theId) { 306 if (theId instanceof IdDt) { 307 myId = (IdDt) theId; 308 } else if (theId != null) { 309 myId = new IdDt(theId.getValue()); 310 } else { 311 myId = null; 312 } 313 return this; 314 } 315 316 public BaseResource setId(String theId) { 317 if (theId == null) { 318 myId = null; 319 } else { 320 myId = new IdDt(theId); 321 } 322 return this; 323 } 324 325 @Override 326 public void setLanguage(CodeDt theLanguage) { 327 myLanguage = theLanguage; 328 } 329 330 @Override 331 public void setResourceMetadata(ResourceMetadataMap theMap) { 332 Validate.notNull(theMap, "The Map must not be null"); 333 myResourceMetadata = theMap; 334 } 335 336 public void setText(NarrativeDt theText) { 337 myText = theText; 338 } 339 340 @Override 341 public String toString() { 342 ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); 343 b.append("id", getId().toUnqualified()); 344 return b.toString(); 345 } 346 347}