001package org.hl7.fhir.dstu3.model; 002 003import java.io.IOException; 004import java.io.Serializable; 005import java.util.ArrayList; 006import java.util.HashMap; 007import java.util.List; 008import java.util.Map; 009 010import org.hl7.fhir.dstu3.elementmodel.Element; 011import org.hl7.fhir.dstu3.elementmodel.ObjectConverter; 012import org.hl7.fhir.exceptions.FHIRException; 013import org.hl7.fhir.instance.model.api.IBase; 014import org.hl7.fhir.utilities.Utilities; 015import org.hl7.fhir.utilities.xhtml.XhtmlNode; 016import org.hl7.fhir.utilities.xhtml.XhtmlParser; 017 018import ca.uhn.fhir.model.api.IElement; 019//Add comment to test SVN 020public abstract class Base implements Serializable, IBase, IElement { 021 022 /** 023 * User appended data items - allow users to add extra information to the class 024 */ 025private Map<String, Object> userData; 026 027 /** 028 * Round tracking xml comments for testing convenience 029 */ 030 private List<String> formatCommentsPre; 031 032 /** 033 * Round tracking xml comments for testing convenience 034 */ 035 private List<String> formatCommentsPost; 036 037 038 public Object getUserData(String name) { 039 if (userData == null) 040 return null; 041 return userData.get(name); 042 } 043 044 public void setUserData(String name, Object value) { 045 if (userData == null) 046 userData = new HashMap<String, Object>(); 047 userData.put(name, value); 048 } 049 050 public void clearUserData(String name) { 051 if (userData != null) 052 userData.remove(name); 053 } 054 055 public void setUserDataINN(String name, Object value) { 056 if (value == null) 057 return; 058 059 if (userData == null) 060 userData = new HashMap<String, Object>(); 061 userData.put(name, value); 062 } 063 064 public boolean hasUserData(String name) { 065 if (userData == null) 066 return false; 067 else 068 return userData.containsKey(name); 069 } 070 071 public String getUserString(String name) { 072 Object ud = getUserData(name); 073 if (ud == null) 074 return null; 075 if (ud instanceof String) 076 return (String) ud; 077 return ud.toString(); 078 } 079 080 public int getUserInt(String name) { 081 if (!hasUserData(name)) 082 return 0; 083 return (Integer) getUserData(name); 084 } 085 086 public boolean hasFormatComment() { 087 return (formatCommentsPre != null && !formatCommentsPre.isEmpty()) || (formatCommentsPost != null && !formatCommentsPost.isEmpty()); 088 } 089 090 public List<String> getFormatCommentsPre() { 091 if (formatCommentsPre == null) 092 formatCommentsPre = new ArrayList<String>(); 093 return formatCommentsPre; 094 } 095 096 public List<String> getFormatCommentsPost() { 097 if (formatCommentsPost == null) 098 formatCommentsPost = new ArrayList<String>(); 099 return formatCommentsPost; 100 } 101 102 // these 3 allow evaluation engines to get access to primitive values 103 public boolean isPrimitive() { 104 return false; 105 } 106 107 public boolean isBooleanPrimitive() { 108 return false; 109 } 110 111 public boolean hasPrimitiveValue() { 112 return isPrimitive(); 113 } 114 115 public String primitiveValue() { 116 return null; 117 } 118 119 public abstract String fhirType() ; 120 121 public boolean hasType(String... name) { 122 String t = fhirType(); 123 for (String n : name) 124 if (n.equalsIgnoreCase(t)) 125 return true; 126 return false; 127 } 128 129 protected abstract void listChildren(List<Property> result) ; 130 131 public Base setProperty(String name, Base value) throws FHIRException { 132 throw new FHIRException("Attempt to set unknown property "+name); 133 } 134 135 public Base addChild(String name) throws FHIRException { 136 throw new FHIRException("Attempt to add child with unknown name "+name); 137 } 138 139 /** 140 * Supports iterating the children elements in some generic processor or browser 141 * All defined children will be listed, even if they have no value on this instance 142 * 143 * Note that the actual content of primitive or xhtml elements is not iterated explicitly. 144 * To find these, the processing code must recognise the element as a primitive, typecast 145 * the value to a {@link Type}, and examine the value 146 * 147 * @return a list of all the children defined for this element 148 */ 149 public List<Property> children() { 150 List<Property> result = new ArrayList<Property>(); 151 listChildren(result); 152 return result; 153 } 154 155 public Property getChildByName(String name) { 156 List<Property> children = new ArrayList<Property>(); 157 listChildren(children); 158 for (Property c : children) 159 if (c.getName().equals(name)) 160 return c; 161 return null; 162 } 163 164 public List<Base> listChildrenByName(String name) throws FHIRException { 165 List<Base> result = new ArrayList<Base>(); 166 for (Base b : listChildrenByName(name, true)) 167 if (b != null) 168 result.add(b); 169 return result; 170 } 171 172 public Base[] listChildrenByName(String name, boolean checkValid) throws FHIRException { 173 if (name.equals("*")) { 174 List<Property> children = new ArrayList<Property>(); 175 listChildren(children); 176 List<Base> result = new ArrayList<Base>(); 177 for (Property c : children) 178 result.addAll(c.getValues()); 179 return result.toArray(new Base[result.size()]); 180 } 181 else 182 return getProperty(name.hashCode(), name, checkValid); 183 } 184 185 public boolean isEmpty() { 186 return true; // userData does not count 187 } 188 189 public boolean equalsDeep(Base other) { 190 return other != null; 191 } 192 193 public boolean equalsShallow(Base other) { 194 return other != null; 195 } 196 197 public static boolean compareDeep(List<? extends Base> e1, List<? extends Base> e2, boolean allowNull) { 198 if (noList(e1) && noList(e2) && allowNull) 199 return true; 200 if (noList(e1) || noList(e2)) 201 return false; 202 if (e1.size() != e2.size()) 203 return false; 204 for (int i = 0; i < e1.size(); i++) { 205 if (!compareDeep(e1.get(i), e2.get(i), allowNull)) 206 return false; 207 } 208 return true; 209 } 210 211 private static boolean noList(List<? extends Base> list) { 212 return list == null || list.isEmpty(); 213 } 214 215 public static boolean compareDeep(Base e1, Base e2, boolean allowNull) { 216 if (allowNull) { 217 boolean noLeft = e1 == null || e1.isEmpty(); 218 boolean noRight = e2 == null || e2.isEmpty(); 219 if (noLeft && noRight) { 220 return true; 221 } 222 } 223 if (e1 == null || e2 == null) 224 return false; 225 if (e2.isMetadataBased() && !e1.isMetadataBased()) // respect existing order for debugging consistency; outcome must be the same either way 226 return e2.equalsDeep(e1); 227 else 228 return e1.equalsDeep(e2); 229 } 230 231 public static boolean compareDeep(XhtmlNode div1, XhtmlNode div2, boolean allowNull) { 232 if (div1 == null && div2 == null && allowNull) 233 return true; 234 if (div1 == null || div2 == null) 235 return false; 236 return div1.equalsDeep(div2); 237 } 238 239 240 public static boolean compareValues(List<? extends PrimitiveType> e1, List<? extends PrimitiveType> e2, boolean allowNull) { 241 if (e1 == null && e2 == null && allowNull) 242 return true; 243 if (e1 == null || e2 == null) 244 return false; 245 if (e1.size() != e2.size()) 246 return false; 247 for (int i = 0; i < e1.size(); i++) { 248 if (!compareValues(e1.get(i), e2.get(i), allowNull)) 249 return false; 250 } 251 return true; 252 } 253 254 public static boolean compareValues(PrimitiveType e1, PrimitiveType e2, boolean allowNull) { 255 boolean noLeft = e1 == null || e1.isEmpty(); 256 boolean noRight = e2 == null || e2.isEmpty(); 257 if (noLeft && noRight && allowNull) { 258 return true; 259 } 260 if (noLeft != noRight) 261 return false; 262 return e1.equalsShallow(e2); 263 } 264 265 // -- converters for property setters 266 267 public Type castToType(Base b) throws FHIRException { 268 if (b instanceof Type) 269 return (Type) b; 270 else if (b.isMetadataBased()) 271 return ((org.hl7.fhir.dstu3.elementmodel.Element) b).asType(); 272 else 273 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Reference"); 274 } 275 276 277 public BooleanType castToBoolean(Base b) throws FHIRException { 278 if (b instanceof BooleanType) 279 return (BooleanType) b; 280 else 281 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Boolean"); 282 } 283 284 public IntegerType castToInteger(Base b) throws FHIRException { 285 if (b instanceof IntegerType) 286 return (IntegerType) b; 287 else 288 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Integer"); 289 } 290 291 public DecimalType castToDecimal(Base b) throws FHIRException { 292 if (b instanceof DecimalType) 293 return (DecimalType) b; 294 else 295 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Decimal"); 296 } 297 298 public Base64BinaryType castToBase64Binary(Base b) throws FHIRException { 299 if (b instanceof Base64BinaryType) 300 return (Base64BinaryType) b; 301 else 302 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Base64Binary"); 303 } 304 305 public InstantType castToInstant(Base b) throws FHIRException { 306 if (b instanceof InstantType) 307 return (InstantType) b; 308 else 309 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Instant"); 310 } 311 312 public StringType castToString(Base b) throws FHIRException { 313 if (b instanceof StringType) 314 return (StringType) b; 315 else if (b.hasPrimitiveValue()) 316 return new StringType(b.primitiveValue()); 317 else 318 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a String"); 319 } 320 321 public UriType castToUri(Base b) throws FHIRException { 322 if (b instanceof UriType) 323 return (UriType) b; 324 else if (b.hasPrimitiveValue()) 325 return new UriType(b.primitiveValue()); 326 else 327 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Uri"); 328 } 329 330 public DateType castToDate(Base b) throws FHIRException { 331 if (b instanceof DateType) 332 return (DateType) b; 333 else if (b.hasPrimitiveValue()) 334 return new DateType(b.primitiveValue()); 335 else 336 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Date"); 337 } 338 339 public DateTimeType castToDateTime(Base b) throws FHIRException { 340 if (b instanceof DateTimeType) 341 return (DateTimeType) b; 342 else if (b.fhirType().equals("dateTime")) 343 return new DateTimeType(b.primitiveValue()); 344 else 345 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a DateTime"); 346 } 347 348 public TimeType castToTime(Base b) throws FHIRException { 349 if (b instanceof TimeType) 350 return (TimeType) b; 351 else 352 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Time"); 353 } 354 355 public CodeType castToCode(Base b) throws FHIRException { 356 if (b instanceof CodeType) 357 return (CodeType) b; 358 else if (b.isPrimitive()) 359 return new CodeType(b.primitiveValue()); 360 else 361 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Code"); 362 } 363 364 public OidType castToOid(Base b) throws FHIRException { 365 if (b instanceof OidType) 366 return (OidType) b; 367 else 368 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Oid"); 369 } 370 371 public IdType castToId(Base b) throws FHIRException { 372 if (b instanceof IdType) 373 return (IdType) b; 374 else 375 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Id"); 376 } 377 378 public UnsignedIntType castToUnsignedInt(Base b) throws FHIRException { 379 if (b instanceof UnsignedIntType) 380 return (UnsignedIntType) b; 381 else 382 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a UnsignedInt"); 383 } 384 385 public PositiveIntType castToPositiveInt(Base b) throws FHIRException { 386 if (b instanceof PositiveIntType) 387 return (PositiveIntType) b; 388 else 389 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a PositiveInt"); 390 } 391 392 public MarkdownType castToMarkdown(Base b) throws FHIRException { 393 if (b instanceof MarkdownType) 394 return (MarkdownType) b; 395 else 396 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Markdown"); 397 } 398 399 public Annotation castToAnnotation(Base b) throws FHIRException { 400 if (b instanceof Annotation) 401 return (Annotation) b; 402 else 403 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Annotation"); 404 } 405 406 public Dosage castToDosage(Base b) throws FHIRException { 407 if (b instanceof Dosage) 408 return (Dosage) b; 409 else throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an DosageInstruction"); 410 } 411 412 413 public Attachment castToAttachment(Base b) throws FHIRException { 414 if (b instanceof Attachment) 415 return (Attachment) b; 416 else 417 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Attachment"); 418 } 419 420 public Identifier castToIdentifier(Base b) throws FHIRException { 421 if (b instanceof Identifier) 422 return (Identifier) b; 423 else 424 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Identifier"); 425 } 426 427 public CodeableConcept castToCodeableConcept(Base b) throws FHIRException { 428 if (b instanceof CodeableConcept) 429 return (CodeableConcept) b; 430 else if (b instanceof Element) { 431 return ObjectConverter.readAsCodeableConcept((Element) b); 432 } else if (b instanceof CodeType) { 433 CodeableConcept cc = new CodeableConcept(); 434 cc.addCoding().setCode(((CodeType) b).asStringValue()); 435 return cc; 436 } else 437 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a CodeableConcept"); 438 } 439 440 public Coding castToCoding(Base b) throws FHIRException { 441 if (b instanceof Coding) 442 return (Coding) b; 443 else if (b instanceof Element) { 444 return ObjectConverter.readAsCoding((Element) b); 445 } else 446 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Coding"); 447 } 448 449 public Quantity castToQuantity(Base b) throws FHIRException { 450 if (b instanceof Quantity) 451 return (Quantity) b; 452 else 453 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Quantity"); 454 } 455 456 public Money castToMoney(Base b) throws FHIRException { 457 if (b instanceof Money) 458 return (Money) b; 459 else 460 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Money"); 461 } 462 463 public Duration castToDuration(Base b) throws FHIRException { 464 if (b instanceof Duration) 465 return (Duration) b; 466 else 467 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Duration"); 468 } 469 470 public SimpleQuantity castToSimpleQuantity(Base b) throws FHIRException { 471 if (b instanceof SimpleQuantity) 472 return (SimpleQuantity) b; 473 else if (b instanceof Quantity) { 474 Quantity q = (Quantity) b; 475 SimpleQuantity sq = new SimpleQuantity(); 476 sq.setValueElement(q.getValueElement()); 477 sq.setComparatorElement(q.getComparatorElement()); 478 sq.setUnitElement(q.getUnitElement()); 479 sq.setSystemElement(q.getSystemElement()); 480 sq.setCodeElement(q.getCodeElement()); 481 return sq; 482 } else 483 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an SimpleQuantity"); 484 } 485 486 public Range castToRange(Base b) throws FHIRException { 487 if (b instanceof Range) 488 return (Range) b; 489 else 490 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Range"); 491 } 492 493 public Period castToPeriod(Base b) throws FHIRException { 494 if (b instanceof Period) 495 return (Period) b; 496 else 497 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Period"); 498 } 499 500 public Ratio castToRatio(Base b) throws FHIRException { 501 if (b instanceof Ratio) 502 return (Ratio) b; 503 else 504 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Ratio"); 505 } 506 507 public SampledData castToSampledData(Base b) throws FHIRException { 508 if (b instanceof SampledData) 509 return (SampledData) b; 510 else 511 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a SampledData"); 512 } 513 514 public Signature castToSignature(Base b) throws FHIRException { 515 if (b instanceof Signature) 516 return (Signature) b; 517 else 518 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Signature"); 519 } 520 521 public HumanName castToHumanName(Base b) throws FHIRException { 522 if (b instanceof HumanName) 523 return (HumanName) b; 524 else 525 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a HumanName"); 526 } 527 528 public Address castToAddress(Base b) throws FHIRException { 529 if (b instanceof Address) 530 return (Address) b; 531 else 532 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Address"); 533 } 534 535 public ContactDetail castToContactDetail(Base b) throws FHIRException { 536 if (b instanceof ContactDetail) 537 return (ContactDetail) b; 538 else 539 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ContactDetail"); 540 } 541 542 public Contributor castToContributor(Base b) throws FHIRException { 543 if (b instanceof Contributor) 544 return (Contributor) b; 545 else 546 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Contributor"); 547 } 548 549 public UsageContext castToUsageContext(Base b) throws FHIRException { 550 if (b instanceof UsageContext) 551 return (UsageContext) b; 552 else 553 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a UsageContext"); 554 } 555 556 public RelatedArtifact castToRelatedArtifact(Base b) throws FHIRException { 557 if (b instanceof RelatedArtifact) 558 return (RelatedArtifact) b; 559 else 560 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a RelatedArtifact"); 561 } 562 563 public ContactPoint castToContactPoint(Base b) throws FHIRException { 564 if (b instanceof ContactPoint) 565 return (ContactPoint) b; 566 else 567 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ContactPoint"); 568 } 569 570 public Timing castToTiming(Base b) throws FHIRException { 571 if (b instanceof Timing) 572 return (Timing) b; 573 else 574 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Timing"); 575 } 576 577 public Reference castToReference(Base b) throws FHIRException { 578 if (b instanceof Reference) 579 return (Reference) b; 580 else if (b.isPrimitive() && Utilities.isURL(b.primitiveValue())) 581 return new Reference().setReference(b.primitiveValue()); 582 else if (b instanceof org.hl7.fhir.dstu3.elementmodel.Element && b.fhirType().equals("Reference")) { 583 org.hl7.fhir.dstu3.elementmodel.Element e = (org.hl7.fhir.dstu3.elementmodel.Element) b; 584 return new Reference().setReference(e.getChildValue("reference")).setDisplay(e.getChildValue("display")); 585 } else 586 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Reference"); 587 } 588 589 public Meta castToMeta(Base b) throws FHIRException { 590 if (b instanceof Meta) 591 return (Meta) b; 592 else 593 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Meta"); 594 } 595 596 public Extension castToExtension(Base b) throws FHIRException { 597 if (b instanceof Extension) 598 return (Extension) b; 599 else 600 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Extension"); 601 } 602 603 public Resource castToResource(Base b) throws FHIRException { 604 if (b instanceof Resource) 605 return (Resource) b; 606 else 607 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Resource"); 608 } 609 610 public Narrative castToNarrative(Base b) throws FHIRException { 611 if (b instanceof Narrative) 612 return (Narrative) b; 613 else 614 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Narrative"); 615 } 616 617 618 public ElementDefinition castToElementDefinition(Base b) throws FHIRException { 619 if (b instanceof ElementDefinition) 620 return (ElementDefinition) b; 621 else 622 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ElementDefinition"); 623 } 624 625 public DataRequirement castToDataRequirement(Base b) throws FHIRException { 626 if (b instanceof DataRequirement) 627 return (DataRequirement) b; 628 else 629 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a DataRequirement"); 630 } 631 632 public ParameterDefinition castToParameterDefinition(Base b) throws FHIRException { 633 if (b instanceof ParameterDefinition) 634 return (ParameterDefinition) b; 635 else 636 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ParameterDefinition"); 637 } 638 639 public TriggerDefinition castToTriggerDefinition(Base b) throws FHIRException { 640 if (b instanceof TriggerDefinition) 641 return (TriggerDefinition) b; 642 else 643 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a TriggerDefinition"); 644 } 645 646 public XhtmlNode castToXhtml(Base b) throws FHIRException { 647 if (b instanceof Element) { 648 return ((Element) b).getXhtml(); 649 } else if (b instanceof StringType) { 650 try { 651 return new XhtmlParser().parseFragment(((StringType) b).asStringValue()); 652 } catch (IOException e) { 653 throw new FHIRException(e); 654 } 655 } else 656 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to XHtml"); 657 } 658 659 public String castToXhtmlString(Base b) throws FHIRException { 660 if (b instanceof Element) { 661 return ((Element) b).getValue(); 662 } else if (b instanceof StringType) { 663 return ((StringType) b).asStringValue(); 664 } else 665 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to XHtml string"); 666 } 667 668 protected boolean isMetadataBased() { 669 return false; 670 } 671 672 public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException { 673 if (checkValid) 674 throw new FHIRException("Attempt to read invalid property '"+name+"' on type "+fhirType()); 675 return null; 676 } 677 678 public Base setProperty(int hash, String name, Base value) throws FHIRException { 679 throw new FHIRException("Attempt to write to invalid property '"+name+"' on type "+fhirType()); 680 } 681 682 public Base makeProperty(int hash, String name) throws FHIRException { 683 throw new FHIRException("Attempt to make an invalid property '"+name+"' on type "+fhirType()); 684 } 685 686 public String[] getTypesForProperty(int hash, String name) throws FHIRException { 687 throw new FHIRException("Attempt to get types for an invalid property '"+name+"' on type "+fhirType()); 688 } 689 690 public static boolean equals(String v1, String v2) { 691 if (v1 == null && v2 == null) 692 return true; 693 else if (v1 == null || v2 == null) 694 return false; 695 else 696 return v1.equals(v2); 697 } 698 699 public boolean isResource() { 700 return false; 701 } 702 703 704 public abstract String getIdBase(); 705 public abstract void setIdBase(String value); 706 707 public Property getNamedProperty(String _name) throws FHIRException { 708 return getNamedProperty(_name.hashCode(), _name, false); 709 } 710 public Property getNamedProperty(int _hash, String _name, boolean _checkValid) throws FHIRException { 711 if (_checkValid) 712 throw new FHIRException("Attempt to read invalid property '"+_name+"' on type "+fhirType()); 713 return null; 714 } 715 716}