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