001package ca.uhn.fhir.util; 002 003/*- 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 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.FhirContext; 024import ca.uhn.fhir.context.RuntimeResourceDefinition; 025import org.hl7.fhir.instance.model.api.IBase; 026import org.hl7.fhir.instance.model.api.IBaseCoding; 027import org.hl7.fhir.instance.model.api.IBaseReference; 028import org.hl7.fhir.instance.model.api.IBaseResource; 029import org.hl7.fhir.instance.model.api.IIdType; 030import org.hl7.fhir.instance.model.api.IPrimitiveType; 031 032import javax.annotation.Nonnull; 033import java.util.Date; 034 035/** 036 * This class can be used to generate <code>Composition</code> resources in 037 * a version independent way. 038 * 039 * @since 6.4.0 040 */ 041public class CompositionBuilder { 042 043 private final FhirContext myCtx; 044 private final IBaseResource myComposition; 045 private final RuntimeResourceDefinition myCompositionDef; 046 private final FhirTerser myTerser; 047 048 public CompositionBuilder(@Nonnull FhirContext theFhirContext) { 049 myCtx = theFhirContext; 050 myCompositionDef = myCtx.getResourceDefinition("Composition"); 051 myTerser = myCtx.newTerser(); 052 myComposition = myCompositionDef.newInstance(); 053 } 054 055 @SuppressWarnings("unchecked") 056 public <T extends IBaseResource> T getComposition() { 057 return (T) myComposition; 058 } 059 060 /** 061 * Add a value to <code>Composition.author</code> 062 */ 063 public void addAuthor(IIdType theAuthorId) { 064 IBaseReference reference = myTerser.addElement(myComposition, "Composition.author"); 065 reference.setReference(theAuthorId.getValue()); 066 } 067 068 /** 069 * Set a value in <code>Composition.status</code> 070 */ 071 public void setStatus(String theStatusCode) { 072 myTerser.setElement(myComposition, "Composition.status", theStatusCode); 073 } 074 075 076 /** 077 * Set a value in <code>Composition.subject</code> 078 */ 079 public void setSubject(IIdType theSubject) { 080 myTerser.setElement(myComposition, "Composition.subject.reference", theSubject.getValue()); 081 } 082 083 /** 084 * Add a Coding to <code>Composition.type.coding</code> 085 */ 086 public void addTypeCoding(String theSystem, String theCode, String theDisplay) { 087 IBaseCoding coding = myTerser.addElement(myComposition, "Composition.type.coding"); 088 coding.setCode(theCode); 089 coding.setSystem(theSystem); 090 coding.setDisplay(theDisplay); 091 } 092 093 /** 094 * Set a value in <code>Composition.date</code> 095 */ 096 public void setDate(IPrimitiveType<Date> theDate) { 097 myTerser.setElement(myComposition, "Composition.date", theDate.getValueAsString()); 098 } 099 100 /** 101 * Set a value in <code>Composition.title</code> 102 */ 103 public void setTitle(String theTitle) { 104 myTerser.setElement(myComposition, "Composition.title", theTitle); 105 } 106 107 /** 108 * Set a value in <code>Composition.confidentiality</code> 109 */ 110 public void setConfidentiality(String theConfidentiality) { 111 myTerser.setElement(myComposition, "Composition.confidentiality", theConfidentiality); 112 } 113 114 /** 115 * Set a value in <code>Composition.id</code> 116 */ 117 public void setId(IIdType theId) { 118 myComposition.setId(theId.getValue()); 119 } 120 121 public SectionBuilder addSection() { 122 IBase section = myTerser.addElement(myComposition, "Composition.section"); 123 return new SectionBuilder(section); 124 } 125 126 public class SectionBuilder { 127 128 private final IBase mySection; 129 130 /** 131 * Constructor 132 */ 133 private SectionBuilder(IBase theSection) { 134 mySection = theSection; 135 } 136 137 /** 138 * Sets the section title 139 */ 140 public void setTitle(String theTitle) { 141 myTerser.setElement(mySection, "title", theTitle); 142 } 143 144 /** 145 * Add a coding to section.code 146 */ 147 public void addCodeCoding(String theSystem, String theCode, String theDisplay) { 148 IBaseCoding coding = myTerser.addElement(mySection, "code.coding"); 149 coding.setCode(theCode); 150 coding.setSystem(theSystem); 151 coding.setDisplay(theDisplay); 152 } 153 154 /** 155 * Adds a reference to entry.reference 156 */ 157 public void addEntry(IIdType theReference) { 158 IBaseReference entry = myTerser.addElement(mySection, "entry"); 159 entry.setReference(theReference.getValue()); 160 } 161 162 /** 163 * Adds narrative text to the section 164 */ 165 public void setText(String theStatus, String theDivHtml) { 166 IBase text = myTerser.addElement(mySection, "text"); 167 myTerser.setElement(text, "status", theStatus); 168 myTerser.setElement(text, "div", theDivHtml); 169 } 170 } 171 172 173} 174