
001/*- 002 * #%L 003 * HAPI FHIR - Core Library 004 * %% 005 * Copyright (C) 2014 - 2025 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.util; 021 022import ca.uhn.fhir.context.BaseRuntimeChildDefinition; 023import ca.uhn.fhir.context.BaseRuntimeElementDefinition; 024import ca.uhn.fhir.context.FhirContext; 025import ca.uhn.fhir.context.FhirVersionEnum; 026import ca.uhn.fhir.context.RuntimeResourceDefinition; 027import ca.uhn.fhir.model.primitive.IdDt; 028import jakarta.annotation.Nonnull; 029import jakarta.annotation.Nullable; 030import org.apache.commons.lang3.StringUtils; 031import org.apache.commons.lang3.Validate; 032import org.hl7.fhir.instance.model.api.IBase; 033import org.hl7.fhir.instance.model.api.IBaseBackboneElement; 034import org.hl7.fhir.instance.model.api.IBaseBundle; 035import org.hl7.fhir.instance.model.api.IBaseParameters; 036import org.hl7.fhir.instance.model.api.IBaseResource; 037import org.hl7.fhir.instance.model.api.IIdType; 038import org.hl7.fhir.instance.model.api.IPrimitiveType; 039 040import java.util.Date; 041import java.util.Objects; 042import java.util.Optional; 043 044/** 045 * This class can be used to build a Bundle resource to be used as a FHIR transaction. Convenience methods provide 046 * support for setting various bundle fields and working with bundle parts such as metadata and entry 047 * (method and search). 048 * 049 * <p> 050 * <p> 051 * This is not yet complete, and doesn't support all FHIR features. <b>USE WITH CAUTION</b> as the API 052 * may change. 053 * 054 * @since 5.1.0 055 */ 056public class BundleBuilder { 057 058 private final FhirContext myContext; 059 private final IBaseBundle myBundle; 060 private final RuntimeResourceDefinition myBundleDef; 061 private final BaseRuntimeChildDefinition myEntryChild; 062 private final BaseRuntimeChildDefinition myMetaChild; 063 private final BaseRuntimeChildDefinition mySearchChild; 064 private final BaseRuntimeElementDefinition<?> myEntryDef; 065 private final BaseRuntimeElementDefinition<?> myMetaDef; 066 private final BaseRuntimeElementDefinition mySearchDef; 067 private final BaseRuntimeChildDefinition myEntryResourceChild; 068 private final BaseRuntimeChildDefinition myEntryFullUrlChild; 069 private final BaseRuntimeChildDefinition myEntryRequestChild; 070 private final BaseRuntimeElementDefinition<?> myEntryRequestDef; 071 private final BaseRuntimeChildDefinition myEntryRequestUrlChild; 072 private final BaseRuntimeChildDefinition myEntryRequestMethodChild; 073 private final BaseRuntimeElementDefinition<?> myEntryRequestMethodDef; 074 private final BaseRuntimeChildDefinition myEntryRequestIfNoneExistChild; 075 076 /** 077 * Constructor 078 */ 079 public BundleBuilder(FhirContext theContext) { 080 myContext = theContext; 081 082 myBundleDef = myContext.getResourceDefinition("Bundle"); 083 myBundle = (IBaseBundle) myBundleDef.newInstance(); 084 085 myEntryChild = myBundleDef.getChildByName("entry"); 086 myEntryDef = myEntryChild.getChildByName("entry"); 087 088 mySearchChild = myEntryDef.getChildByName("search"); 089 mySearchDef = mySearchChild.getChildByName("search"); 090 091 if (myContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.DSTU3)) { 092 myMetaChild = myBundleDef.getChildByName("meta"); 093 myMetaDef = myMetaChild.getChildByName("meta"); 094 } else { 095 myMetaChild = null; 096 myMetaDef = null; 097 } 098 099 myEntryResourceChild = myEntryDef.getChildByName("resource"); 100 myEntryFullUrlChild = myEntryDef.getChildByName("fullUrl"); 101 102 myEntryRequestChild = myEntryDef.getChildByName("request"); 103 myEntryRequestDef = myEntryRequestChild.getChildByName("request"); 104 105 myEntryRequestUrlChild = myEntryRequestDef.getChildByName("url"); 106 107 myEntryRequestMethodChild = myEntryRequestDef.getChildByName("method"); 108 myEntryRequestMethodDef = myEntryRequestMethodChild.getChildByName("method"); 109 110 myEntryRequestIfNoneExistChild = myEntryRequestDef.getChildByName("ifNoneExist"); 111 } 112 113 /** 114 * Sets the specified primitive field on the bundle with the value provided. 115 * 116 * @param theFieldName Name of the primitive field. 117 * @param theFieldValue Value of the field to be set. 118 */ 119 public BundleBuilder setBundleField(String theFieldName, String theFieldValue) { 120 BaseRuntimeChildDefinition typeChild = myBundleDef.getChildByName(theFieldName); 121 Validate.notNull(typeChild, "Unable to find field %s", theFieldName); 122 123 IPrimitiveType<?> type = (IPrimitiveType<?>) 124 typeChild.getChildByName(theFieldName).newInstance(typeChild.getInstanceConstructorArguments()); 125 type.setValueAsString(theFieldValue); 126 typeChild.getMutator().setValue(myBundle, type); 127 return this; 128 } 129 130 private void setBundleFieldIfNotAlreadySet(String theFieldName, String theFieldValue) { 131 BaseRuntimeChildDefinition typeChild = myBundleDef.getChildByName(theFieldName); 132 Validate.notNull(typeChild, "Unable to find field %s", theFieldName); 133 Optional<IBase> firstValue = typeChild.getAccessor().getFirstValueOrNull(myBundle); 134 if (firstValue.isPresent()) { 135 IPrimitiveType<?> value = (IPrimitiveType<?>) firstValue.get(); 136 if (!value.isEmpty()) { 137 return; 138 } 139 } 140 141 setBundleField(theFieldName, theFieldValue); 142 } 143 144 /** 145 * Sets the specified primitive field on the search entry with the value provided. 146 * 147 * @param theSearch Search part of the entry 148 * @param theFieldName Name of the primitive field. 149 * @param theFieldValue Value of the field to be set. 150 */ 151 public BundleBuilder setSearchField(IBase theSearch, String theFieldName, String theFieldValue) { 152 BaseRuntimeChildDefinition typeChild = mySearchDef.getChildByName(theFieldName); 153 Validate.notNull(typeChild, "Unable to find field %s", theFieldName); 154 155 IPrimitiveType<?> type = (IPrimitiveType<?>) 156 typeChild.getChildByName(theFieldName).newInstance(typeChild.getInstanceConstructorArguments()); 157 type.setValueAsString(theFieldValue); 158 typeChild.getMutator().setValue(theSearch, type); 159 return this; 160 } 161 162 public BundleBuilder setSearchField(IBase theSearch, String theFieldName, IPrimitiveType<?> theFieldValue) { 163 BaseRuntimeChildDefinition typeChild = mySearchDef.getChildByName(theFieldName); 164 Validate.notNull(typeChild, "Unable to find field %s", theFieldName); 165 166 typeChild.getMutator().setValue(theSearch, theFieldValue); 167 return this; 168 } 169 170 /** 171 * Adds a FHIRPatch patch bundle to the transaction 172 * 173 * @param theTarget The target resource ID to patch 174 * @param thePatch The FHIRPath Parameters resource 175 * @since 6.3.0 176 */ 177 public PatchBuilder addTransactionFhirPatchEntry(IIdType theTarget, IBaseParameters thePatch) { 178 Validate.notNull(theTarget, "theTarget must not be null"); 179 Validate.notBlank(theTarget.getResourceType(), "theTarget must contain a resource type"); 180 Validate.notBlank(theTarget.getIdPart(), "theTarget must contain an ID"); 181 182 IPrimitiveType<?> url = addAndPopulateTransactionBundleEntryRequest( 183 thePatch, 184 theTarget.getValue(), 185 theTarget.toUnqualifiedVersionless().getValue(), 186 "PATCH"); 187 188 return new PatchBuilder(url); 189 } 190 191 /** 192 * Adds a FHIRPatch patch bundle to the transaction. This method is intended for conditional PATCH operations. If you 193 * know the ID of the resource you wish to patch, use {@link #addTransactionFhirPatchEntry(IIdType, IBaseParameters)} 194 * instead. 195 * 196 * @param thePatch The FHIRPath Parameters resource 197 * @see #addTransactionFhirPatchEntry(IIdType, IBaseParameters) 198 * @since 6.3.0 199 */ 200 public PatchBuilder addTransactionFhirPatchEntry(IBaseParameters thePatch) { 201 IPrimitiveType<?> url = addAndPopulateTransactionBundleEntryRequest(thePatch, null, null, "PATCH"); 202 203 return new PatchBuilder(url); 204 } 205 206 /** 207 * Adds an entry containing an update (PUT) request. 208 * Also sets the Bundle.type value to "transaction" if it is not already set. 209 * 210 * @param theResource The resource to update 211 */ 212 public UpdateBuilder addTransactionUpdateEntry(IBaseResource theResource) { 213 return addTransactionUpdateEntry(theResource, null); 214 } 215 216 /** 217 * Adds an entry containing an update (PUT) request. 218 * Also sets the Bundle.type value to "transaction" if it is not already set. 219 * 220 * @param theResource The resource to update 221 * @param theRequestUrl The url to attach to the Bundle.entry.request.url. If null, will default to the resource ID. 222 */ 223 public UpdateBuilder addTransactionUpdateEntry(IBaseResource theResource, String theRequestUrl) { 224 Validate.notNull(theResource, "theResource must not be null"); 225 226 IIdType id = getIdTypeForUpdate(theResource); 227 228 String fullUrl = id.getValue(); 229 String verb = "PUT"; 230 String requestUrl = StringUtils.isBlank(theRequestUrl) 231 ? id.toUnqualifiedVersionless().getValue() 232 : theRequestUrl; 233 234 IPrimitiveType<?> url = addAndPopulateTransactionBundleEntryRequest(theResource, fullUrl, requestUrl, verb); 235 236 return new UpdateBuilder(url); 237 } 238 239 @Nonnull 240 private IPrimitiveType<?> addAndPopulateTransactionBundleEntryRequest( 241 IBaseResource theResource, String theFullUrl, String theRequestUrl, String theHttpVerb) { 242 setBundleFieldIfNotAlreadySet("type", "transaction"); 243 244 IBase request = addEntryAndReturnRequest(theResource, theFullUrl); 245 246 // Bundle.entry.request.url 247 IPrimitiveType<?> url = addRequestUrl(request, theRequestUrl); 248 249 // Bundle.entry.request.method 250 addRequestMethod(request, theHttpVerb); 251 return url; 252 } 253 254 /** 255 * Adds an entry containing an update (UPDATE) request without the body of the resource. 256 * Also sets the Bundle.type value to "transaction" if it is not already set. 257 * 258 * @param theResource The resource to update. 259 */ 260 public void addTransactionUpdateIdOnlyEntry(IBaseResource theResource) { 261 setBundleFieldIfNotAlreadySet("type", "transaction"); 262 263 Validate.notNull(theResource, "theResource must not be null"); 264 265 IIdType id = getIdTypeForUpdate(theResource); 266 String requestUrl = id.toUnqualifiedVersionless().getValue(); 267 String fullUrl = id.getValue(); 268 String httpMethod = "PUT"; 269 270 addIdOnlyEntry(requestUrl, httpMethod, fullUrl); 271 } 272 273 /** 274 * Adds an entry containing an create (POST) request. 275 * Also sets the Bundle.type value to "transaction" if it is not already set. 276 * 277 * @param theResource The resource to create 278 */ 279 public CreateBuilder addTransactionCreateEntry(IBaseResource theResource) { 280 return addTransactionCreateEntry(theResource, null); 281 } 282 283 /** 284 * Adds an entry containing an create (POST) request. 285 * Also sets the Bundle.type value to "transaction" if it is not already set. 286 * 287 * @param theResource The resource to create 288 * @param theFullUrl The fullUrl to attach to the entry. If null, will default to the resource ID. 289 */ 290 public CreateBuilder addTransactionCreateEntry(IBaseResource theResource, @Nullable String theFullUrl) { 291 setBundleFieldIfNotAlreadySet("type", "transaction"); 292 293 IBase request = addEntryAndReturnRequest( 294 theResource, 295 theFullUrl != null ? theFullUrl : theResource.getIdElement().getValue()); 296 297 String resourceType = myContext.getResourceType(theResource); 298 299 // Bundle.entry.request.url 300 addRequestUrl(request, resourceType); 301 302 // Bundle.entry.request.method 303 addRequestMethod(request, "POST"); 304 305 return new CreateBuilder(request); 306 } 307 308 /** 309 * Adds an entry containing a create (POST) request without the body of the resource. 310 * Also sets the Bundle.type value to "transaction" if it is not already set. 311 * 312 * @param theResource The resource to create 313 */ 314 public void addTransactionCreateEntryIdOnly(IBaseResource theResource) { 315 setBundleFieldIfNotAlreadySet("type", "transaction"); 316 317 String requestUrl = myContext.getResourceType(theResource); 318 String fullUrl = theResource.getIdElement().getValue(); 319 String httpMethod = "POST"; 320 321 addIdOnlyEntry(requestUrl, httpMethod, fullUrl); 322 } 323 324 private void addIdOnlyEntry(String theRequestUrl, String theHttpMethod, String theFullUrl) { 325 IBase entry = addEntry(); 326 327 // Bundle.entry.request 328 IBase request = myEntryRequestDef.newInstance(); 329 myEntryRequestChild.getMutator().setValue(entry, request); 330 331 // Bundle.entry.request.url 332 addRequestUrl(request, theRequestUrl); 333 334 // Bundle.entry.request.method 335 addRequestMethod(request, theHttpMethod); 336 337 // Bundle.entry.fullUrl 338 addFullUrl(entry, theFullUrl); 339 } 340 341 /** 342 * Adds an entry containing a delete (DELETE) request. 343 * Also sets the Bundle.type value to "transaction" if it is not already set. 344 * <p> 345 * Note that the resource is only used to extract its ID and type, and the body of the resource is not included in the entry, 346 * 347 * @param theCondition The conditional URL, e.g. "Patient?identifier=foo|bar" 348 * @since 6.8.0 349 */ 350 public DeleteBuilder addTransactionDeleteConditionalEntry(String theCondition) { 351 Validate.notBlank(theCondition, "theCondition must not be blank"); 352 353 setBundleFieldIfNotAlreadySet("type", "transaction"); 354 return addDeleteEntry(theCondition); 355 } 356 357 /** 358 * Adds an entry containing a delete (DELETE) request. 359 * Also sets the Bundle.type value to "transaction" if it is not already set. 360 * <p> 361 * Note that the resource is only used to extract its ID and type, and the body of the resource is not included in the entry, 362 * 363 * @param theResource The resource to delete. 364 */ 365 public DeleteBuilder addTransactionDeleteEntry(IBaseResource theResource) { 366 String resourceType = myContext.getResourceType(theResource); 367 String idPart = theResource.getIdElement().toUnqualifiedVersionless().getIdPart(); 368 return addTransactionDeleteEntry(resourceType, idPart); 369 } 370 371 /** 372 * Adds an entry containing a delete (DELETE) request. 373 * Also sets the Bundle.type value to "transaction" if it is not already set. 374 * <p> 375 * Note that the resource is only used to extract its ID and type, and the body of the resource is not included in the entry, 376 * 377 * @param theResourceId The resource ID to delete. 378 * @return 379 */ 380 public DeleteBuilder addTransactionDeleteEntry(IIdType theResourceId) { 381 String resourceType = theResourceId.getResourceType(); 382 String idPart = theResourceId.getIdPart(); 383 return addTransactionDeleteEntry(resourceType, idPart); 384 } 385 386 /** 387 * Adds an entry containing a delete (DELETE) request. 388 * Also sets the Bundle.type value to "transaction" if it is not already set. 389 * 390 * @param theResourceType The type resource to delete. 391 * @param theIdPart the ID of the resource to delete. 392 */ 393 public DeleteBuilder addTransactionDeleteEntry(String theResourceType, String theIdPart) { 394 setBundleFieldIfNotAlreadySet("type", "transaction"); 395 IdDt idDt = new IdDt(theIdPart); 396 397 String deleteUrl = idDt.toUnqualifiedVersionless() 398 .withResourceType(theResourceType) 399 .getValue(); 400 401 return addDeleteEntry(deleteUrl); 402 } 403 404 /** 405 * Adds an entry containing a delete (DELETE) request. 406 * Also sets the Bundle.type value to "transaction" if it is not already set. 407 * 408 * @param theMatchUrl The match URL, e.g. <code>Patient?identifier=http://foo|123</code> 409 * @since 6.3.0 410 */ 411 public BaseOperationBuilder addTransactionDeleteEntryConditional(String theMatchUrl) { 412 Validate.notBlank(theMatchUrl, "theMatchUrl must not be null or blank"); 413 return addDeleteEntry(theMatchUrl); 414 } 415 416 @Nonnull 417 private DeleteBuilder addDeleteEntry(String theDeleteUrl) { 418 IBase request = addEntryAndReturnRequest(); 419 420 // Bundle.entry.request.url 421 addRequestUrl(request, theDeleteUrl); 422 423 // Bundle.entry.request.method 424 addRequestMethod(request, "DELETE"); 425 426 return new DeleteBuilder(); 427 } 428 429 private IIdType getIdTypeForUpdate(IBaseResource theResource) { 430 IIdType id = theResource.getIdElement(); 431 if (id.hasIdPart() && !id.hasResourceType()) { 432 String resourceType = myContext.getResourceType(theResource); 433 id = id.withResourceType(resourceType); 434 } 435 return id; 436 } 437 438 public void addFullUrl(IBase theEntry, String theFullUrl) { 439 IPrimitiveType<?> fullUrl = 440 (IPrimitiveType<?>) myContext.getElementDefinition("uri").newInstance(); 441 fullUrl.setValueAsString(theFullUrl); 442 myEntryFullUrlChild.getMutator().setValue(theEntry, fullUrl); 443 } 444 445 private IPrimitiveType<?> addRequestUrl(IBase request, String theRequestUrl) { 446 IPrimitiveType<?> url = 447 (IPrimitiveType<?>) myContext.getElementDefinition("uri").newInstance(); 448 url.setValueAsString(theRequestUrl); 449 myEntryRequestUrlChild.getMutator().setValue(request, url); 450 return url; 451 } 452 453 private void addRequestMethod(IBase theRequest, String theMethod) { 454 IPrimitiveType<?> method = (IPrimitiveType<?>) 455 myEntryRequestMethodDef.newInstance(myEntryRequestMethodChild.getInstanceConstructorArguments()); 456 method.setValueAsString(theMethod); 457 myEntryRequestMethodChild.getMutator().setValue(theRequest, method); 458 } 459 460 /** 461 * Adds an entry for a Collection bundle type 462 */ 463 public void addCollectionEntry(IBaseResource theResource) { 464 setType("collection"); 465 addEntryAndReturnRequest(theResource); 466 } 467 468 /** 469 * Adds an entry for a Document bundle type 470 */ 471 public void addDocumentEntry(IBaseResource theResource) { 472 setType("document"); 473 addEntryAndReturnRequest(theResource); 474 } 475 476 public void addDocumentEntry(IBaseResource theResource, String theFullUrl) { 477 setType("document"); 478 addEntryAndReturnRequest(theResource, theFullUrl); 479 } 480 481 /** 482 * Adds an entry for a Message bundle type 483 * 484 * @since 8.4.0 485 */ 486 public void addMessageEntry(IBaseResource theResource) { 487 setType("message"); 488 addEntryAndReturnRequest(theResource); 489 } 490 491 /** 492 * Creates new entry and adds it to the bundle 493 * 494 * @return Returns the new entry. 495 */ 496 public IBase addEntry() { 497 IBase entry = myEntryDef.newInstance(); 498 addEntry(entry); 499 return entry; 500 } 501 502 public IBase addEntry(IBase theEntry) { 503 myEntryChild.getMutator().addValue(myBundle, theEntry); 504 return theEntry; 505 } 506 507 /** 508 * Creates new search instance for the specified entry. 509 * Note that this method does not work for DSTU2 model classes, it will only work 510 * on DSTU3+. 511 * 512 * @param entry Entry to create search instance for 513 * @return Returns the search instance 514 */ 515 public IBaseBackboneElement addSearch(IBase entry) { 516 Validate.isTrue( 517 myContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.DSTU3), 518 "This method may only be called for FHIR version DSTU3 and above"); 519 520 IBase searchInstance = mySearchDef.newInstance(); 521 mySearchChild.getMutator().setValue(entry, searchInstance); 522 return (IBaseBackboneElement) searchInstance; 523 } 524 525 private IBase addEntryAndReturnRequest(IBaseResource theResource) { 526 IIdType id = theResource.getIdElement(); 527 if (id.hasVersionIdPart()) { 528 id = id.toVersionless(); 529 } 530 return addEntryAndReturnRequest(theResource, id.getValue()); 531 } 532 533 private IBase addEntryAndReturnRequest(IBaseResource theResource, String theFullUrl) { 534 Validate.notNull(theResource, "theResource must not be null"); 535 536 IBase entry = addEntry(); 537 538 // Bundle.entry.fullUrl 539 addFullUrl(entry, theFullUrl); 540 541 // Bundle.entry.resource 542 myEntryResourceChild.getMutator().setValue(entry, theResource); 543 544 // Bundle.entry.request 545 IBase request = myEntryRequestDef.newInstance(); 546 myEntryRequestChild.getMutator().setValue(entry, request); 547 return request; 548 } 549 550 public IBase addEntryAndReturnRequest() { 551 IBase entry = addEntry(); 552 553 // Bundle.entry.request 554 IBase request = myEntryRequestDef.newInstance(); 555 myEntryRequestChild.getMutator().setValue(entry, request); 556 return request; 557 } 558 559 public IBaseBundle getBundle() { 560 return myBundle; 561 } 562 563 /** 564 * Convenience method which auto-casts the results of {@link #getBundle()} 565 * 566 * @since 6.3.0 567 */ 568 public <T extends IBaseBundle> T getBundleTyped() { 569 return (T) myBundle; 570 } 571 572 /** 573 * Note that this method does not work for DSTU2 model classes, it will only work 574 * on DSTU3+. 575 */ 576 public BundleBuilder setMetaField(String theFieldName, IBase theFieldValue) { 577 Validate.isTrue( 578 myContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.DSTU3), 579 "This method may only be called for FHIR version DSTU3 and above"); 580 581 BaseRuntimeChildDefinition.IMutator mutator = 582 myMetaDef.getChildByName(theFieldName).getMutator(); 583 mutator.setValue(myBundle.getMeta(), theFieldValue); 584 return this; 585 } 586 587 /** 588 * Sets the specified entry field. 589 * 590 * @param theEntry The entry instance to set values on 591 * @param theEntryChildName The child field name of the entry instance to be set 592 * @param theValue The field value to set 593 */ 594 public void addToEntry(IBase theEntry, String theEntryChildName, IBase theValue) { 595 addToBase(theEntry, theEntryChildName, theValue, myEntryDef); 596 } 597 598 /** 599 * Sets the specified search field. 600 * 601 * @param theSearch The search instance to set values on 602 * @param theSearchFieldName The child field name of the search instance to be set 603 * @param theSearchFieldValue The field value to set 604 */ 605 public void addToSearch(IBase theSearch, String theSearchFieldName, IBase theSearchFieldValue) { 606 addToBase(theSearch, theSearchFieldName, theSearchFieldValue, mySearchDef); 607 } 608 609 private void addToBase( 610 IBase theBase, String theSearchChildName, IBase theValue, BaseRuntimeElementDefinition mySearchDef) { 611 BaseRuntimeChildDefinition defn = mySearchDef.getChildByName(theSearchChildName); 612 Validate.notNull(defn, "Unable to get child definition %s from %s", theSearchChildName, theBase); 613 defn.getMutator().addValue(theBase, theValue); 614 } 615 616 /** 617 * Creates a new primitive. 618 * 619 * @param theTypeName The element type for the primitive 620 * @param <T> Actual type of the parameterized primitive type interface 621 * @return Returns the new empty instance of the element definition. 622 */ 623 public <T> IPrimitiveType<T> newPrimitive(String theTypeName) { 624 BaseRuntimeElementDefinition primitiveDefinition = myContext.getElementDefinition(theTypeName); 625 Validate.notNull(primitiveDefinition, "Unable to find definition for %s", theTypeName); 626 return (IPrimitiveType<T>) primitiveDefinition.newInstance(); 627 } 628 629 /** 630 * Creates a new primitive instance of the specified element type. 631 * 632 * @param theTypeName Element type to create 633 * @param theInitialValue Initial value to be set on the new instance 634 * @param <T> Actual type of the parameterized primitive type interface 635 * @return Returns the newly created instance 636 */ 637 public <T> IPrimitiveType<T> newPrimitive(String theTypeName, T theInitialValue) { 638 IPrimitiveType<T> retVal = newPrimitive(theTypeName); 639 retVal.setValue(theInitialValue); 640 return retVal; 641 } 642 643 /** 644 * Sets a value for <code>Bundle.type</code>. That this is a coded field so {@literal theType} 645 * must be an actual valid value for this field or a {@link ca.uhn.fhir.parser.DataFormatException} 646 * will be thrown. 647 */ 648 public void setType(String theType) { 649 setBundleField("type", theType); 650 } 651 652 /** 653 * Adds an identifier to <code>Bundle.identifier</code> 654 * 655 * @param theSystem The system 656 * @param theValue The value 657 * @since 6.4.0 658 */ 659 public void setIdentifier(@Nullable String theSystem, @Nullable String theValue) { 660 FhirTerser terser = myContext.newTerser(); 661 IBase identifier = terser.addElement(myBundle, "identifier"); 662 terser.setElement(identifier, "system", theSystem); 663 terser.setElement(identifier, "value", theValue); 664 } 665 666 /** 667 * Sets the timestamp in <code>Bundle.timestamp</code> 668 * 669 * @since 6.4.0 670 */ 671 public void setTimestamp(@Nonnull IPrimitiveType<Date> theTimestamp) { 672 FhirTerser terser = myContext.newTerser(); 673 terser.setElement(myBundle, "Bundle.timestamp", theTimestamp.getValueAsString()); 674 } 675 676 /** 677 * Adds a profile URL to <code>Bundle.meta.profile</code> 678 * 679 * @since 7.4.0 680 */ 681 public void addProfile(String theProfile) { 682 FhirTerser terser = myContext.newTerser(); 683 terser.addElement(myBundle, "Bundle.meta.profile", theProfile); 684 } 685 686 public IBase addSearchMatchEntry(IBaseResource theResource) { 687 setType("searchset"); 688 689 IBase entry = addEntry(); 690 // Bundle.entry.resource 691 myEntryResourceChild.getMutator().setValue(entry, theResource); 692 // Bundle.entry.search 693 IBase search = addSearch(entry); 694 setSearchField(search, "mode", "match"); 695 696 return entry; 697 } 698 699 public class DeleteBuilder extends BaseOperationBuilder { 700 701 // nothing yet 702 703 } 704 705 public class PatchBuilder extends BaseOperationBuilderWithConditionalUrl<PatchBuilder> { 706 707 PatchBuilder(IPrimitiveType<?> theUrl) { 708 super(theUrl); 709 } 710 } 711 712 public class UpdateBuilder extends BaseOperationBuilderWithConditionalUrl<UpdateBuilder> { 713 UpdateBuilder(IPrimitiveType<?> theUrl) { 714 super(theUrl); 715 } 716 } 717 718 public class CreateBuilder extends BaseOperationBuilder { 719 private final IBase myRequest; 720 721 CreateBuilder(IBase theRequest) { 722 myRequest = theRequest; 723 } 724 725 /** 726 * Make this create a Conditional Create 727 */ 728 public CreateBuilder conditional(String theConditionalUrl) { 729 BaseRuntimeElementDefinition<?> stringDefinition = 730 Objects.requireNonNull(myContext.getElementDefinition("string")); 731 IPrimitiveType<?> ifNoneExist = (IPrimitiveType<?>) stringDefinition.newInstance(); 732 ifNoneExist.setValueAsString(theConditionalUrl); 733 734 myEntryRequestIfNoneExistChild.getMutator().setValue(myRequest, ifNoneExist); 735 736 return this; 737 } 738 } 739 740 public abstract class BaseOperationBuilder { 741 742 /** 743 * Returns a reference to the BundleBuilder instance. 744 * <p> 745 * Calling this method has no effect at all, it is only 746 * provided for easy method chaning if you want to build 747 * your bundle as a single fluent call. 748 * 749 * @since 6.3.0 750 */ 751 public BundleBuilder andThen() { 752 return BundleBuilder.this; 753 } 754 } 755 756 public abstract class BaseOperationBuilderWithConditionalUrl<T extends BaseOperationBuilder> 757 extends BaseOperationBuilder { 758 759 private final IPrimitiveType<?> myUrl; 760 761 BaseOperationBuilderWithConditionalUrl(IPrimitiveType<?> theUrl) { 762 myUrl = theUrl; 763 } 764 765 /** 766 * Make this update a Conditional Update 767 */ 768 @SuppressWarnings("unchecked") 769 public T conditional(String theConditionalUrl) { 770 myUrl.setValueAsString(theConditionalUrl); 771 return (T) this; 772 } 773 } 774}