001package ca.uhn.fhir.util; 002 003/*- 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2020 University Health Network 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import ca.uhn.fhir.context.BaseRuntimeChildDefinition; 024import ca.uhn.fhir.context.BaseRuntimeElementDefinition; 025import ca.uhn.fhir.context.FhirContext; 026import ca.uhn.fhir.context.RuntimeResourceDefinition; 027import org.hl7.fhir.instance.model.api.IBase; 028import org.hl7.fhir.instance.model.api.IBaseBundle; 029import org.hl7.fhir.instance.model.api.IBaseResource; 030import org.hl7.fhir.instance.model.api.IPrimitiveType; 031import org.thymeleaf.util.Validate; 032 033/** 034 * This class can be used to build a Bundle resource to be used as a FHIR transaction. 035 * 036 * This is not yet complete, and doesn't support all FHIR features. <b>USE WITH CAUTION</b> as the API 037 * may change. 038 * 039 * @since 5.1.0 040 */ 041public class TransactionBuilder { 042 043 private final FhirContext myContext; 044 private final IBaseBundle myBundle; 045 private final RuntimeResourceDefinition myBundleDef; 046 private final BaseRuntimeChildDefinition myEntryChild; 047 private final BaseRuntimeElementDefinition<?> myEntryDef; 048 private final BaseRuntimeChildDefinition myEntryResourceChild; 049 private final BaseRuntimeChildDefinition myEntryFullUrlChild; 050 private final BaseRuntimeChildDefinition myEntryRequestChild; 051 private final BaseRuntimeElementDefinition<?> myEntryRequestDef; 052 private final BaseRuntimeChildDefinition myEntryRequestUrlChild; 053 private final BaseRuntimeChildDefinition myEntryRequestMethodChild; 054 private final BaseRuntimeElementDefinition<?> myEntryRequestMethodDef; 055 056 /** 057 * Constructor 058 */ 059 public TransactionBuilder(FhirContext theContext) { 060 myContext = theContext; 061 062 myBundleDef = myContext.getResourceDefinition("Bundle"); 063 myBundle = (IBaseBundle) myBundleDef.newInstance(); 064 065 BaseRuntimeChildDefinition typeChild = myBundleDef.getChildByName("type"); 066 IPrimitiveType<?> type = (IPrimitiveType<?>) typeChild.getChildByName("type").newInstance(typeChild.getInstanceConstructorArguments()); 067 type.setValueAsString("transaction"); 068 typeChild.getMutator().setValue(myBundle, type); 069 070 myEntryChild = myBundleDef.getChildByName("entry"); 071 myEntryDef = myEntryChild.getChildByName("entry"); 072 073 myEntryResourceChild = myEntryDef.getChildByName("resource"); 074 myEntryFullUrlChild = myEntryDef.getChildByName("fullUrl"); 075 076 myEntryRequestChild = myEntryDef.getChildByName("request"); 077 myEntryRequestDef = myEntryRequestChild.getChildByName("request"); 078 079 myEntryRequestUrlChild = myEntryRequestDef.getChildByName("url"); 080 081 myEntryRequestMethodChild = myEntryRequestDef.getChildByName("method"); 082 myEntryRequestMethodDef = myEntryRequestMethodChild.getChildByName("method"); 083 084 085 } 086 087 /** 088 * Adds an entry containing an update (PUT) request 089 * 090 * @param theResource The resource to update 091 */ 092 public TransactionBuilder addUpdateEntry(IBaseResource theResource) { 093 Validate.notNull(theResource, "theResource must not be null"); 094 Validate.notEmpty(theResource.getIdElement().getValue(), "theResource must have an ID"); 095 096 IBase entry = myEntryDef.newInstance(); 097 myEntryChild.getMutator().addValue(myBundle, entry); 098 099 // Bundle.entry.fullUrl 100 IPrimitiveType<?> fullUrl = (IPrimitiveType<?>) myContext.getElementDefinition("uri").newInstance(); 101 fullUrl.setValueAsString(theResource.getIdElement().getValue()); 102 myEntryFullUrlChild.getMutator().setValue(entry, fullUrl); 103 104 // Bundle.entry.resource 105 myEntryResourceChild.getMutator().setValue(entry, theResource); 106 107 // Bundle.entry.request 108 IBase request = myEntryRequestDef.newInstance(); 109 myEntryRequestChild.getMutator().setValue(entry, request); 110 111 // Bundle.entry.request.url 112 IPrimitiveType<?> url = (IPrimitiveType<?>) myContext.getElementDefinition("uri").newInstance(); 113 url.setValueAsString(theResource.getIdElement().toUnqualifiedVersionless().getValue()); 114 myEntryRequestUrlChild.getMutator().setValue(request, url); 115 116 // Bundle.entry.request.url 117 IPrimitiveType<?> method = (IPrimitiveType<?>) myEntryRequestMethodDef.newInstance(myEntryRequestMethodChild.getInstanceConstructorArguments()); 118 method.setValueAsString("PUT"); 119 myEntryRequestMethodChild.getMutator().setValue(request, method); 120 121 return this; 122 } 123 124 125 126 public IBaseBundle getBundle() { 127 return myBundle; 128 } 129 130}