001package org.hl7.fhir.instance.model;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009  
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030*/
031
032// Generated on Wed, Jul 13, 2016 05:32+1000 for FHIR v1.0.2
033import java.util.*;
034
035import org.hl7.fhir.exceptions.FHIRException;
036import org.hl7.fhir.instance.model.api.IBaseBackboneElement;
037import org.hl7.fhir.utilities.Utilities;
038
039import ca.uhn.fhir.model.api.annotation.*;
040/**
041 * Indicates that a medication product is to be or has been dispensed for a named person/patient.  This includes a description of the medication product (supply) provided and the instructions for administering the medication.  The medication dispense is the result of a pharmacy system responding to a medication order.
042 */
043@ResourceDef(name="MedicationDispense", profile="http://hl7.org/fhir/Profile/MedicationDispense")
044public class MedicationDispense extends DomainResource {
045
046    public enum MedicationDispenseStatus {
047        /**
048         * The dispense has started but has not yet completed.
049         */
050        INPROGRESS, 
051        /**
052         * Actions implied by the administration have been temporarily halted, but are expected to continue later. May also be called "suspended"
053         */
054        ONHOLD, 
055        /**
056         * All actions that are implied by the dispense have occurred.
057         */
058        COMPLETED, 
059        /**
060         * The dispense was entered in error and therefore nullified.
061         */
062        ENTEREDINERROR, 
063        /**
064         * Actions implied by the dispense have been permanently halted, before all of them occurred.
065         */
066        STOPPED, 
067        /**
068         * added to help the parsers
069         */
070        NULL;
071        public static MedicationDispenseStatus fromCode(String codeString) throws FHIRException {
072            if (codeString == null || "".equals(codeString))
073                return null;
074        if ("in-progress".equals(codeString))
075          return INPROGRESS;
076        if ("on-hold".equals(codeString))
077          return ONHOLD;
078        if ("completed".equals(codeString))
079          return COMPLETED;
080        if ("entered-in-error".equals(codeString))
081          return ENTEREDINERROR;
082        if ("stopped".equals(codeString))
083          return STOPPED;
084        throw new FHIRException("Unknown MedicationDispenseStatus code '"+codeString+"'");
085        }
086        public String toCode() {
087          switch (this) {
088            case INPROGRESS: return "in-progress";
089            case ONHOLD: return "on-hold";
090            case COMPLETED: return "completed";
091            case ENTEREDINERROR: return "entered-in-error";
092            case STOPPED: return "stopped";
093            default: return "?";
094          }
095        }
096        public String getSystem() {
097          switch (this) {
098            case INPROGRESS: return "http://hl7.org/fhir/medication-dispense-status";
099            case ONHOLD: return "http://hl7.org/fhir/medication-dispense-status";
100            case COMPLETED: return "http://hl7.org/fhir/medication-dispense-status";
101            case ENTEREDINERROR: return "http://hl7.org/fhir/medication-dispense-status";
102            case STOPPED: return "http://hl7.org/fhir/medication-dispense-status";
103            default: return "?";
104          }
105        }
106        public String getDefinition() {
107          switch (this) {
108            case INPROGRESS: return "The dispense has started but has not yet completed.";
109            case ONHOLD: return "Actions implied by the administration have been temporarily halted, but are expected to continue later. May also be called \"suspended\"";
110            case COMPLETED: return "All actions that are implied by the dispense have occurred.";
111            case ENTEREDINERROR: return "The dispense was entered in error and therefore nullified.";
112            case STOPPED: return "Actions implied by the dispense have been permanently halted, before all of them occurred.";
113            default: return "?";
114          }
115        }
116        public String getDisplay() {
117          switch (this) {
118            case INPROGRESS: return "In Progress";
119            case ONHOLD: return "On Hold";
120            case COMPLETED: return "Completed";
121            case ENTEREDINERROR: return "Entered in-Error";
122            case STOPPED: return "Stopped";
123            default: return "?";
124          }
125        }
126    }
127
128  public static class MedicationDispenseStatusEnumFactory implements EnumFactory<MedicationDispenseStatus> {
129    public MedicationDispenseStatus fromCode(String codeString) throws IllegalArgumentException {
130      if (codeString == null || "".equals(codeString))
131            if (codeString == null || "".equals(codeString))
132                return null;
133        if ("in-progress".equals(codeString))
134          return MedicationDispenseStatus.INPROGRESS;
135        if ("on-hold".equals(codeString))
136          return MedicationDispenseStatus.ONHOLD;
137        if ("completed".equals(codeString))
138          return MedicationDispenseStatus.COMPLETED;
139        if ("entered-in-error".equals(codeString))
140          return MedicationDispenseStatus.ENTEREDINERROR;
141        if ("stopped".equals(codeString))
142          return MedicationDispenseStatus.STOPPED;
143        throw new IllegalArgumentException("Unknown MedicationDispenseStatus code '"+codeString+"'");
144        }
145        public Enumeration<MedicationDispenseStatus> fromType(Base code) throws FHIRException {
146          if (code == null || code.isEmpty())
147            return null;
148          String codeString = ((PrimitiveType) code).asStringValue();
149          if (codeString == null || "".equals(codeString))
150            return null;
151        if ("in-progress".equals(codeString))
152          return new Enumeration<MedicationDispenseStatus>(this, MedicationDispenseStatus.INPROGRESS);
153        if ("on-hold".equals(codeString))
154          return new Enumeration<MedicationDispenseStatus>(this, MedicationDispenseStatus.ONHOLD);
155        if ("completed".equals(codeString))
156          return new Enumeration<MedicationDispenseStatus>(this, MedicationDispenseStatus.COMPLETED);
157        if ("entered-in-error".equals(codeString))
158          return new Enumeration<MedicationDispenseStatus>(this, MedicationDispenseStatus.ENTEREDINERROR);
159        if ("stopped".equals(codeString))
160          return new Enumeration<MedicationDispenseStatus>(this, MedicationDispenseStatus.STOPPED);
161        throw new FHIRException("Unknown MedicationDispenseStatus code '"+codeString+"'");
162        }
163    public String toCode(MedicationDispenseStatus code) {
164      if (code == MedicationDispenseStatus.INPROGRESS)
165        return "in-progress";
166      if (code == MedicationDispenseStatus.ONHOLD)
167        return "on-hold";
168      if (code == MedicationDispenseStatus.COMPLETED)
169        return "completed";
170      if (code == MedicationDispenseStatus.ENTEREDINERROR)
171        return "entered-in-error";
172      if (code == MedicationDispenseStatus.STOPPED)
173        return "stopped";
174      return "?";
175      }
176    }
177
178    @Block()
179    public static class MedicationDispenseDosageInstructionComponent extends BackboneElement implements IBaseBackboneElement {
180        /**
181         * Free text dosage instructions can be used for cases where the instructions are too complex to code. When coded instructions are present, the free text instructions may still be present for display to humans taking or administering the medication.
182         */
183        @Child(name = "text", type = {StringType.class}, order=1, min=0, max=1, modifier=false, summary=true)
184        @Description(shortDefinition="Dosage Instructions", formalDefinition="Free text dosage instructions can be used for cases where the instructions are too complex to code. When coded instructions are present, the free text instructions may still be present for display to humans taking or administering the medication." )
185        protected StringType text;
186
187        /**
188         * Additional instructions such as "Swallow with plenty of water" which may or may not be coded.
189         */
190        @Child(name = "additionalInstructions", type = {CodeableConcept.class}, order=2, min=0, max=1, modifier=false, summary=true)
191        @Description(shortDefinition="E.g. \"Take with food\"", formalDefinition="Additional instructions such as \"Swallow with plenty of water\" which may or may not be coded." )
192        protected CodeableConcept additionalInstructions;
193
194        /**
195         * The timing schedule for giving the medication to the patient.  The Schedule data type allows many different expressions.  For example, "Every  8 hours"; "Three times a day"; "1/2 an hour before breakfast for 10 days from 23-Dec 2011:";  "15 Oct 2013, 17 Oct 2013 and 1 Nov 2013".
196         */
197        @Child(name = "timing", type = {Timing.class}, order=3, min=0, max=1, modifier=false, summary=true)
198        @Description(shortDefinition="When medication should be administered", formalDefinition="The timing schedule for giving the medication to the patient.  The Schedule data type allows many different expressions.  For example, \"Every  8 hours\"; \"Three times a day\"; \"1/2 an hour before breakfast for 10 days from 23-Dec 2011:\";  \"15 Oct 2013, 17 Oct 2013 and 1 Nov 2013\"." )
199        protected Timing timing;
200
201        /**
202         * Indicates whether the Medication is only taken when needed within a specific dosing schedule (Boolean option), or it indicates the precondition for taking the Medication (CodeableConcept).  
203
204Specifically if 'boolean' datatype is selected, then the following logic applies:  If set to True, this indicates that the medication is only taken when needed, within the specified schedule.
205         */
206        @Child(name = "asNeeded", type = {BooleanType.class, CodeableConcept.class}, order=4, min=0, max=1, modifier=false, summary=true)
207        @Description(shortDefinition="Take \"as needed\" f(or x)", formalDefinition="Indicates whether the Medication is only taken when needed within a specific dosing schedule (Boolean option), or it indicates the precondition for taking the Medication (CodeableConcept).  \n\nSpecifically if 'boolean' datatype is selected, then the following logic applies:  If set to True, this indicates that the medication is only taken when needed, within the specified schedule." )
208        protected Type asNeeded;
209
210        /**
211         * A coded specification of the anatomic site where the medication first enters the body.
212         */
213        @Child(name = "site", type = {CodeableConcept.class, BodySite.class}, order=5, min=0, max=1, modifier=false, summary=true)
214        @Description(shortDefinition="Body site to administer to", formalDefinition="A coded specification of the anatomic site where the medication first enters the body." )
215        protected Type site;
216
217        /**
218         * A code specifying the route or physiological path of administration of a therapeutic agent into or onto a subject.
219         */
220        @Child(name = "route", type = {CodeableConcept.class}, order=6, min=0, max=1, modifier=false, summary=true)
221        @Description(shortDefinition="How drug should enter body", formalDefinition="A code specifying the route or physiological path of administration of a therapeutic agent into or onto a subject." )
222        protected CodeableConcept route;
223
224        /**
225         * A coded value indicating the method by which the medication is intended to be or was introduced into or on the body.
226         */
227        @Child(name = "method", type = {CodeableConcept.class}, order=7, min=0, max=1, modifier=false, summary=true)
228        @Description(shortDefinition="Technique for administering medication", formalDefinition="A coded value indicating the method by which the medication is intended to be or was introduced into or on the body." )
229        protected CodeableConcept method;
230
231        /**
232         * The amount of therapeutic or other substance given at one administration event.
233         */
234        @Child(name = "dose", type = {Range.class, SimpleQuantity.class}, order=8, min=0, max=1, modifier=false, summary=true)
235        @Description(shortDefinition="Amount of medication per dose", formalDefinition="The amount of therapeutic or other substance given at one administration event." )
236        protected Type dose;
237
238        /**
239         * Identifies the speed with which the medication was or will be introduced into the patient. Typically the rate for an infusion e.g. 100 ml per 1 hour or 100 ml/hr.  May also be expressed as a rate per unit of time e.g. 500 ml per 2 hours.   Currently we do not specify a default of '1' in the denominator, but this is being discussed. Other examples: 200 mcg/min or 200 mcg/1 minute; 1 liter/8 hours.
240         */
241        @Child(name = "rate", type = {Ratio.class, Range.class}, order=9, min=0, max=1, modifier=false, summary=true)
242        @Description(shortDefinition="Amount of medication per unit of time", formalDefinition="Identifies the speed with which the medication was or will be introduced into the patient. Typically the rate for an infusion e.g. 100 ml per 1 hour or 100 ml/hr.  May also be expressed as a rate per unit of time e.g. 500 ml per 2 hours.   Currently we do not specify a default of '1' in the denominator, but this is being discussed. Other examples: 200 mcg/min or 200 mcg/1 minute; 1 liter/8 hours." )
243        protected Type rate;
244
245        /**
246         * The maximum total quantity of a therapeutic substance that may be administered to a subject over the period of time,  e.g. 1000mg in 24 hours.
247         */
248        @Child(name = "maxDosePerPeriod", type = {Ratio.class}, order=10, min=0, max=1, modifier=false, summary=true)
249        @Description(shortDefinition="Upper limit on medication per unit of time", formalDefinition="The maximum total quantity of a therapeutic substance that may be administered to a subject over the period of time,  e.g. 1000mg in 24 hours." )
250        protected Ratio maxDosePerPeriod;
251
252        private static final long serialVersionUID = -1470136646L;
253
254    /*
255     * Constructor
256     */
257      public MedicationDispenseDosageInstructionComponent() {
258        super();
259      }
260
261        /**
262         * @return {@link #text} (Free text dosage instructions can be used for cases where the instructions are too complex to code. When coded instructions are present, the free text instructions may still be present for display to humans taking or administering the medication.). This is the underlying object with id, value and extensions. The accessor "getText" gives direct access to the value
263         */
264        public StringType getTextElement() { 
265          if (this.text == null)
266            if (Configuration.errorOnAutoCreate())
267              throw new Error("Attempt to auto-create MedicationDispenseDosageInstructionComponent.text");
268            else if (Configuration.doAutoCreate())
269              this.text = new StringType(); // bb
270          return this.text;
271        }
272
273        public boolean hasTextElement() { 
274          return this.text != null && !this.text.isEmpty();
275        }
276
277        public boolean hasText() { 
278          return this.text != null && !this.text.isEmpty();
279        }
280
281        /**
282         * @param value {@link #text} (Free text dosage instructions can be used for cases where the instructions are too complex to code. When coded instructions are present, the free text instructions may still be present for display to humans taking or administering the medication.). This is the underlying object with id, value and extensions. The accessor "getText" gives direct access to the value
283         */
284        public MedicationDispenseDosageInstructionComponent setTextElement(StringType value) { 
285          this.text = value;
286          return this;
287        }
288
289        /**
290         * @return Free text dosage instructions can be used for cases where the instructions are too complex to code. When coded instructions are present, the free text instructions may still be present for display to humans taking or administering the medication.
291         */
292        public String getText() { 
293          return this.text == null ? null : this.text.getValue();
294        }
295
296        /**
297         * @param value Free text dosage instructions can be used for cases where the instructions are too complex to code. When coded instructions are present, the free text instructions may still be present for display to humans taking or administering the medication.
298         */
299        public MedicationDispenseDosageInstructionComponent setText(String value) { 
300          if (Utilities.noString(value))
301            this.text = null;
302          else {
303            if (this.text == null)
304              this.text = new StringType();
305            this.text.setValue(value);
306          }
307          return this;
308        }
309
310        /**
311         * @return {@link #additionalInstructions} (Additional instructions such as "Swallow with plenty of water" which may or may not be coded.)
312         */
313        public CodeableConcept getAdditionalInstructions() { 
314          if (this.additionalInstructions == null)
315            if (Configuration.errorOnAutoCreate())
316              throw new Error("Attempt to auto-create MedicationDispenseDosageInstructionComponent.additionalInstructions");
317            else if (Configuration.doAutoCreate())
318              this.additionalInstructions = new CodeableConcept(); // cc
319          return this.additionalInstructions;
320        }
321
322        public boolean hasAdditionalInstructions() { 
323          return this.additionalInstructions != null && !this.additionalInstructions.isEmpty();
324        }
325
326        /**
327         * @param value {@link #additionalInstructions} (Additional instructions such as "Swallow with plenty of water" which may or may not be coded.)
328         */
329        public MedicationDispenseDosageInstructionComponent setAdditionalInstructions(CodeableConcept value) { 
330          this.additionalInstructions = value;
331          return this;
332        }
333
334        /**
335         * @return {@link #timing} (The timing schedule for giving the medication to the patient.  The Schedule data type allows many different expressions.  For example, "Every  8 hours"; "Three times a day"; "1/2 an hour before breakfast for 10 days from 23-Dec 2011:";  "15 Oct 2013, 17 Oct 2013 and 1 Nov 2013".)
336         */
337        public Timing getTiming() { 
338          if (this.timing == null)
339            if (Configuration.errorOnAutoCreate())
340              throw new Error("Attempt to auto-create MedicationDispenseDosageInstructionComponent.timing");
341            else if (Configuration.doAutoCreate())
342              this.timing = new Timing(); // cc
343          return this.timing;
344        }
345
346        public boolean hasTiming() { 
347          return this.timing != null && !this.timing.isEmpty();
348        }
349
350        /**
351         * @param value {@link #timing} (The timing schedule for giving the medication to the patient.  The Schedule data type allows many different expressions.  For example, "Every  8 hours"; "Three times a day"; "1/2 an hour before breakfast for 10 days from 23-Dec 2011:";  "15 Oct 2013, 17 Oct 2013 and 1 Nov 2013".)
352         */
353        public MedicationDispenseDosageInstructionComponent setTiming(Timing value) { 
354          this.timing = value;
355          return this;
356        }
357
358        /**
359         * @return {@link #asNeeded} (Indicates whether the Medication is only taken when needed within a specific dosing schedule (Boolean option), or it indicates the precondition for taking the Medication (CodeableConcept).  
360
361Specifically if 'boolean' datatype is selected, then the following logic applies:  If set to True, this indicates that the medication is only taken when needed, within the specified schedule.)
362         */
363        public Type getAsNeeded() { 
364          return this.asNeeded;
365        }
366
367        /**
368         * @return {@link #asNeeded} (Indicates whether the Medication is only taken when needed within a specific dosing schedule (Boolean option), or it indicates the precondition for taking the Medication (CodeableConcept).  
369
370Specifically if 'boolean' datatype is selected, then the following logic applies:  If set to True, this indicates that the medication is only taken when needed, within the specified schedule.)
371         */
372        public BooleanType getAsNeededBooleanType() throws FHIRException { 
373          if (!(this.asNeeded instanceof BooleanType))
374            throw new FHIRException("Type mismatch: the type BooleanType was expected, but "+this.asNeeded.getClass().getName()+" was encountered");
375          return (BooleanType) this.asNeeded;
376        }
377
378        public boolean hasAsNeededBooleanType() { 
379          return this.asNeeded instanceof BooleanType;
380        }
381
382        /**
383         * @return {@link #asNeeded} (Indicates whether the Medication is only taken when needed within a specific dosing schedule (Boolean option), or it indicates the precondition for taking the Medication (CodeableConcept).  
384
385Specifically if 'boolean' datatype is selected, then the following logic applies:  If set to True, this indicates that the medication is only taken when needed, within the specified schedule.)
386         */
387        public CodeableConcept getAsNeededCodeableConcept() throws FHIRException { 
388          if (!(this.asNeeded instanceof CodeableConcept))
389            throw new FHIRException("Type mismatch: the type CodeableConcept was expected, but "+this.asNeeded.getClass().getName()+" was encountered");
390          return (CodeableConcept) this.asNeeded;
391        }
392
393        public boolean hasAsNeededCodeableConcept() { 
394          return this.asNeeded instanceof CodeableConcept;
395        }
396
397        public boolean hasAsNeeded() { 
398          return this.asNeeded != null && !this.asNeeded.isEmpty();
399        }
400
401        /**
402         * @param value {@link #asNeeded} (Indicates whether the Medication is only taken when needed within a specific dosing schedule (Boolean option), or it indicates the precondition for taking the Medication (CodeableConcept).  
403
404Specifically if 'boolean' datatype is selected, then the following logic applies:  If set to True, this indicates that the medication is only taken when needed, within the specified schedule.)
405         */
406        public MedicationDispenseDosageInstructionComponent setAsNeeded(Type value) { 
407          this.asNeeded = value;
408          return this;
409        }
410
411        /**
412         * @return {@link #site} (A coded specification of the anatomic site where the medication first enters the body.)
413         */
414        public Type getSite() { 
415          return this.site;
416        }
417
418        /**
419         * @return {@link #site} (A coded specification of the anatomic site where the medication first enters the body.)
420         */
421        public CodeableConcept getSiteCodeableConcept() throws FHIRException { 
422          if (!(this.site instanceof CodeableConcept))
423            throw new FHIRException("Type mismatch: the type CodeableConcept was expected, but "+this.site.getClass().getName()+" was encountered");
424          return (CodeableConcept) this.site;
425        }
426
427        public boolean hasSiteCodeableConcept() { 
428          return this.site instanceof CodeableConcept;
429        }
430
431        /**
432         * @return {@link #site} (A coded specification of the anatomic site where the medication first enters the body.)
433         */
434        public Reference getSiteReference() throws FHIRException { 
435          if (!(this.site instanceof Reference))
436            throw new FHIRException("Type mismatch: the type Reference was expected, but "+this.site.getClass().getName()+" was encountered");
437          return (Reference) this.site;
438        }
439
440        public boolean hasSiteReference() { 
441          return this.site instanceof Reference;
442        }
443
444        public boolean hasSite() { 
445          return this.site != null && !this.site.isEmpty();
446        }
447
448        /**
449         * @param value {@link #site} (A coded specification of the anatomic site where the medication first enters the body.)
450         */
451        public MedicationDispenseDosageInstructionComponent setSite(Type value) { 
452          this.site = value;
453          return this;
454        }
455
456        /**
457         * @return {@link #route} (A code specifying the route or physiological path of administration of a therapeutic agent into or onto a subject.)
458         */
459        public CodeableConcept getRoute() { 
460          if (this.route == null)
461            if (Configuration.errorOnAutoCreate())
462              throw new Error("Attempt to auto-create MedicationDispenseDosageInstructionComponent.route");
463            else if (Configuration.doAutoCreate())
464              this.route = new CodeableConcept(); // cc
465          return this.route;
466        }
467
468        public boolean hasRoute() { 
469          return this.route != null && !this.route.isEmpty();
470        }
471
472        /**
473         * @param value {@link #route} (A code specifying the route or physiological path of administration of a therapeutic agent into or onto a subject.)
474         */
475        public MedicationDispenseDosageInstructionComponent setRoute(CodeableConcept value) { 
476          this.route = value;
477          return this;
478        }
479
480        /**
481         * @return {@link #method} (A coded value indicating the method by which the medication is intended to be or was introduced into or on the body.)
482         */
483        public CodeableConcept getMethod() { 
484          if (this.method == null)
485            if (Configuration.errorOnAutoCreate())
486              throw new Error("Attempt to auto-create MedicationDispenseDosageInstructionComponent.method");
487            else if (Configuration.doAutoCreate())
488              this.method = new CodeableConcept(); // cc
489          return this.method;
490        }
491
492        public boolean hasMethod() { 
493          return this.method != null && !this.method.isEmpty();
494        }
495
496        /**
497         * @param value {@link #method} (A coded value indicating the method by which the medication is intended to be or was introduced into or on the body.)
498         */
499        public MedicationDispenseDosageInstructionComponent setMethod(CodeableConcept value) { 
500          this.method = value;
501          return this;
502        }
503
504        /**
505         * @return {@link #dose} (The amount of therapeutic or other substance given at one administration event.)
506         */
507        public Type getDose() { 
508          return this.dose;
509        }
510
511        /**
512         * @return {@link #dose} (The amount of therapeutic or other substance given at one administration event.)
513         */
514        public Range getDoseRange() throws FHIRException { 
515          if (!(this.dose instanceof Range))
516            throw new FHIRException("Type mismatch: the type Range was expected, but "+this.dose.getClass().getName()+" was encountered");
517          return (Range) this.dose;
518        }
519
520        public boolean hasDoseRange() { 
521          return this.dose instanceof Range;
522        }
523
524        /**
525         * @return {@link #dose} (The amount of therapeutic or other substance given at one administration event.)
526         */
527        public SimpleQuantity getDoseSimpleQuantity() throws FHIRException { 
528          if (!(this.dose instanceof SimpleQuantity))
529            throw new FHIRException("Type mismatch: the type SimpleQuantity was expected, but "+this.dose.getClass().getName()+" was encountered");
530          return (SimpleQuantity) this.dose;
531        }
532
533        public boolean hasDoseSimpleQuantity() { 
534          return this.dose instanceof SimpleQuantity;
535        }
536
537        public boolean hasDose() { 
538          return this.dose != null && !this.dose.isEmpty();
539        }
540
541        /**
542         * @param value {@link #dose} (The amount of therapeutic or other substance given at one administration event.)
543         */
544        public MedicationDispenseDosageInstructionComponent setDose(Type value) { 
545          this.dose = value;
546          return this;
547        }
548
549        /**
550         * @return {@link #rate} (Identifies the speed with which the medication was or will be introduced into the patient. Typically the rate for an infusion e.g. 100 ml per 1 hour or 100 ml/hr.  May also be expressed as a rate per unit of time e.g. 500 ml per 2 hours.   Currently we do not specify a default of '1' in the denominator, but this is being discussed. Other examples: 200 mcg/min or 200 mcg/1 minute; 1 liter/8 hours.)
551         */
552        public Type getRate() { 
553          return this.rate;
554        }
555
556        /**
557         * @return {@link #rate} (Identifies the speed with which the medication was or will be introduced into the patient. Typically the rate for an infusion e.g. 100 ml per 1 hour or 100 ml/hr.  May also be expressed as a rate per unit of time e.g. 500 ml per 2 hours.   Currently we do not specify a default of '1' in the denominator, but this is being discussed. Other examples: 200 mcg/min or 200 mcg/1 minute; 1 liter/8 hours.)
558         */
559        public Ratio getRateRatio() throws FHIRException { 
560          if (!(this.rate instanceof Ratio))
561            throw new FHIRException("Type mismatch: the type Ratio was expected, but "+this.rate.getClass().getName()+" was encountered");
562          return (Ratio) this.rate;
563        }
564
565        public boolean hasRateRatio() { 
566          return this.rate instanceof Ratio;
567        }
568
569        /**
570         * @return {@link #rate} (Identifies the speed with which the medication was or will be introduced into the patient. Typically the rate for an infusion e.g. 100 ml per 1 hour or 100 ml/hr.  May also be expressed as a rate per unit of time e.g. 500 ml per 2 hours.   Currently we do not specify a default of '1' in the denominator, but this is being discussed. Other examples: 200 mcg/min or 200 mcg/1 minute; 1 liter/8 hours.)
571         */
572        public Range getRateRange() throws FHIRException { 
573          if (!(this.rate instanceof Range))
574            throw new FHIRException("Type mismatch: the type Range was expected, but "+this.rate.getClass().getName()+" was encountered");
575          return (Range) this.rate;
576        }
577
578        public boolean hasRateRange() { 
579          return this.rate instanceof Range;
580        }
581
582        public boolean hasRate() { 
583          return this.rate != null && !this.rate.isEmpty();
584        }
585
586        /**
587         * @param value {@link #rate} (Identifies the speed with which the medication was or will be introduced into the patient. Typically the rate for an infusion e.g. 100 ml per 1 hour or 100 ml/hr.  May also be expressed as a rate per unit of time e.g. 500 ml per 2 hours.   Currently we do not specify a default of '1' in the denominator, but this is being discussed. Other examples: 200 mcg/min or 200 mcg/1 minute; 1 liter/8 hours.)
588         */
589        public MedicationDispenseDosageInstructionComponent setRate(Type value) { 
590          this.rate = value;
591          return this;
592        }
593
594        /**
595         * @return {@link #maxDosePerPeriod} (The maximum total quantity of a therapeutic substance that may be administered to a subject over the period of time,  e.g. 1000mg in 24 hours.)
596         */
597        public Ratio getMaxDosePerPeriod() { 
598          if (this.maxDosePerPeriod == null)
599            if (Configuration.errorOnAutoCreate())
600              throw new Error("Attempt to auto-create MedicationDispenseDosageInstructionComponent.maxDosePerPeriod");
601            else if (Configuration.doAutoCreate())
602              this.maxDosePerPeriod = new Ratio(); // cc
603          return this.maxDosePerPeriod;
604        }
605
606        public boolean hasMaxDosePerPeriod() { 
607          return this.maxDosePerPeriod != null && !this.maxDosePerPeriod.isEmpty();
608        }
609
610        /**
611         * @param value {@link #maxDosePerPeriod} (The maximum total quantity of a therapeutic substance that may be administered to a subject over the period of time,  e.g. 1000mg in 24 hours.)
612         */
613        public MedicationDispenseDosageInstructionComponent setMaxDosePerPeriod(Ratio value) { 
614          this.maxDosePerPeriod = value;
615          return this;
616        }
617
618        protected void listChildren(List<Property> childrenList) {
619          super.listChildren(childrenList);
620          childrenList.add(new Property("text", "string", "Free text dosage instructions can be used for cases where the instructions are too complex to code. When coded instructions are present, the free text instructions may still be present for display to humans taking or administering the medication.", 0, java.lang.Integer.MAX_VALUE, text));
621          childrenList.add(new Property("additionalInstructions", "CodeableConcept", "Additional instructions such as \"Swallow with plenty of water\" which may or may not be coded.", 0, java.lang.Integer.MAX_VALUE, additionalInstructions));
622          childrenList.add(new Property("timing", "Timing", "The timing schedule for giving the medication to the patient.  The Schedule data type allows many different expressions.  For example, \"Every  8 hours\"; \"Three times a day\"; \"1/2 an hour before breakfast for 10 days from 23-Dec 2011:\";  \"15 Oct 2013, 17 Oct 2013 and 1 Nov 2013\".", 0, java.lang.Integer.MAX_VALUE, timing));
623          childrenList.add(new Property("asNeeded[x]", "boolean|CodeableConcept", "Indicates whether the Medication is only taken when needed within a specific dosing schedule (Boolean option), or it indicates the precondition for taking the Medication (CodeableConcept).  \n\nSpecifically if 'boolean' datatype is selected, then the following logic applies:  If set to True, this indicates that the medication is only taken when needed, within the specified schedule.", 0, java.lang.Integer.MAX_VALUE, asNeeded));
624          childrenList.add(new Property("site[x]", "CodeableConcept|Reference(BodySite)", "A coded specification of the anatomic site where the medication first enters the body.", 0, java.lang.Integer.MAX_VALUE, site));
625          childrenList.add(new Property("route", "CodeableConcept", "A code specifying the route or physiological path of administration of a therapeutic agent into or onto a subject.", 0, java.lang.Integer.MAX_VALUE, route));
626          childrenList.add(new Property("method", "CodeableConcept", "A coded value indicating the method by which the medication is intended to be or was introduced into or on the body.", 0, java.lang.Integer.MAX_VALUE, method));
627          childrenList.add(new Property("dose[x]", "Range|SimpleQuantity", "The amount of therapeutic or other substance given at one administration event.", 0, java.lang.Integer.MAX_VALUE, dose));
628          childrenList.add(new Property("rate[x]", "Ratio|Range", "Identifies the speed with which the medication was or will be introduced into the patient. Typically the rate for an infusion e.g. 100 ml per 1 hour or 100 ml/hr.  May also be expressed as a rate per unit of time e.g. 500 ml per 2 hours.   Currently we do not specify a default of '1' in the denominator, but this is being discussed. Other examples: 200 mcg/min or 200 mcg/1 minute; 1 liter/8 hours.", 0, java.lang.Integer.MAX_VALUE, rate));
629          childrenList.add(new Property("maxDosePerPeriod", "Ratio", "The maximum total quantity of a therapeutic substance that may be administered to a subject over the period of time,  e.g. 1000mg in 24 hours.", 0, java.lang.Integer.MAX_VALUE, maxDosePerPeriod));
630        }
631
632      @Override
633      public void setProperty(String name, Base value) throws FHIRException {
634        if (name.equals("text"))
635          this.text = castToString(value); // StringType
636        else if (name.equals("additionalInstructions"))
637          this.additionalInstructions = castToCodeableConcept(value); // CodeableConcept
638        else if (name.equals("timing"))
639          this.timing = castToTiming(value); // Timing
640        else if (name.equals("asNeeded[x]"))
641          this.asNeeded = (Type) value; // Type
642        else if (name.equals("site[x]"))
643          this.site = (Type) value; // Type
644        else if (name.equals("route"))
645          this.route = castToCodeableConcept(value); // CodeableConcept
646        else if (name.equals("method"))
647          this.method = castToCodeableConcept(value); // CodeableConcept
648        else if (name.equals("dose[x]"))
649          this.dose = (Type) value; // Type
650        else if (name.equals("rate[x]"))
651          this.rate = (Type) value; // Type
652        else if (name.equals("maxDosePerPeriod"))
653          this.maxDosePerPeriod = castToRatio(value); // Ratio
654        else
655          super.setProperty(name, value);
656      }
657
658      @Override
659      public Base addChild(String name) throws FHIRException {
660        if (name.equals("text")) {
661          throw new FHIRException("Cannot call addChild on a primitive type MedicationDispense.text");
662        }
663        else if (name.equals("additionalInstructions")) {
664          this.additionalInstructions = new CodeableConcept();
665          return this.additionalInstructions;
666        }
667        else if (name.equals("timing")) {
668          this.timing = new Timing();
669          return this.timing;
670        }
671        else if (name.equals("asNeededBoolean")) {
672          this.asNeeded = new BooleanType();
673          return this.asNeeded;
674        }
675        else if (name.equals("asNeededCodeableConcept")) {
676          this.asNeeded = new CodeableConcept();
677          return this.asNeeded;
678        }
679        else if (name.equals("siteCodeableConcept")) {
680          this.site = new CodeableConcept();
681          return this.site;
682        }
683        else if (name.equals("siteReference")) {
684          this.site = new Reference();
685          return this.site;
686        }
687        else if (name.equals("route")) {
688          this.route = new CodeableConcept();
689          return this.route;
690        }
691        else if (name.equals("method")) {
692          this.method = new CodeableConcept();
693          return this.method;
694        }
695        else if (name.equals("doseRange")) {
696          this.dose = new Range();
697          return this.dose;
698        }
699        else if (name.equals("doseSimpleQuantity")) {
700          this.dose = new SimpleQuantity();
701          return this.dose;
702        }
703        else if (name.equals("rateRatio")) {
704          this.rate = new Ratio();
705          return this.rate;
706        }
707        else if (name.equals("rateRange")) {
708          this.rate = new Range();
709          return this.rate;
710        }
711        else if (name.equals("maxDosePerPeriod")) {
712          this.maxDosePerPeriod = new Ratio();
713          return this.maxDosePerPeriod;
714        }
715        else
716          return super.addChild(name);
717      }
718
719      public MedicationDispenseDosageInstructionComponent copy() {
720        MedicationDispenseDosageInstructionComponent dst = new MedicationDispenseDosageInstructionComponent();
721        copyValues(dst);
722        dst.text = text == null ? null : text.copy();
723        dst.additionalInstructions = additionalInstructions == null ? null : additionalInstructions.copy();
724        dst.timing = timing == null ? null : timing.copy();
725        dst.asNeeded = asNeeded == null ? null : asNeeded.copy();
726        dst.site = site == null ? null : site.copy();
727        dst.route = route == null ? null : route.copy();
728        dst.method = method == null ? null : method.copy();
729        dst.dose = dose == null ? null : dose.copy();
730        dst.rate = rate == null ? null : rate.copy();
731        dst.maxDosePerPeriod = maxDosePerPeriod == null ? null : maxDosePerPeriod.copy();
732        return dst;
733      }
734
735      @Override
736      public boolean equalsDeep(Base other) {
737        if (!super.equalsDeep(other))
738          return false;
739        if (!(other instanceof MedicationDispenseDosageInstructionComponent))
740          return false;
741        MedicationDispenseDosageInstructionComponent o = (MedicationDispenseDosageInstructionComponent) other;
742        return compareDeep(text, o.text, true) && compareDeep(additionalInstructions, o.additionalInstructions, true)
743           && compareDeep(timing, o.timing, true) && compareDeep(asNeeded, o.asNeeded, true) && compareDeep(site, o.site, true)
744           && compareDeep(route, o.route, true) && compareDeep(method, o.method, true) && compareDeep(dose, o.dose, true)
745           && compareDeep(rate, o.rate, true) && compareDeep(maxDosePerPeriod, o.maxDosePerPeriod, true);
746      }
747
748      @Override
749      public boolean equalsShallow(Base other) {
750        if (!super.equalsShallow(other))
751          return false;
752        if (!(other instanceof MedicationDispenseDosageInstructionComponent))
753          return false;
754        MedicationDispenseDosageInstructionComponent o = (MedicationDispenseDosageInstructionComponent) other;
755        return compareValues(text, o.text, true);
756      }
757
758      public boolean isEmpty() {
759        return super.isEmpty() && (text == null || text.isEmpty()) && (additionalInstructions == null || additionalInstructions.isEmpty())
760           && (timing == null || timing.isEmpty()) && (asNeeded == null || asNeeded.isEmpty()) && (site == null || site.isEmpty())
761           && (route == null || route.isEmpty()) && (method == null || method.isEmpty()) && (dose == null || dose.isEmpty())
762           && (rate == null || rate.isEmpty()) && (maxDosePerPeriod == null || maxDosePerPeriod.isEmpty())
763          ;
764      }
765
766  public String fhirType() {
767    return "MedicationDispense.dosageInstruction";
768
769  }
770
771  }
772
773    @Block()
774    public static class MedicationDispenseSubstitutionComponent extends BackboneElement implements IBaseBackboneElement {
775        /**
776         * A code signifying whether a different drug was dispensed from what was prescribed.
777         */
778        @Child(name = "type", type = {CodeableConcept.class}, order=1, min=1, max=1, modifier=false, summary=true)
779        @Description(shortDefinition="Type of substitution", formalDefinition="A code signifying whether a different drug was dispensed from what was prescribed." )
780        protected CodeableConcept type;
781
782        /**
783         * Indicates the reason for the substitution of (or lack of substitution) from what was prescribed.
784         */
785        @Child(name = "reason", type = {CodeableConcept.class}, order=2, min=0, max=Child.MAX_UNLIMITED, modifier=false, summary=true)
786        @Description(shortDefinition="Why was substitution made", formalDefinition="Indicates the reason for the substitution of (or lack of substitution) from what was prescribed." )
787        protected List<CodeableConcept> reason;
788
789        /**
790         * The person or organization that has primary responsibility for the substitution.
791         */
792        @Child(name = "responsibleParty", type = {Practitioner.class}, order=3, min=0, max=Child.MAX_UNLIMITED, modifier=false, summary=true)
793        @Description(shortDefinition="Who is responsible for the substitution", formalDefinition="The person or organization that has primary responsibility for the substitution." )
794        protected List<Reference> responsibleParty;
795        /**
796         * The actual objects that are the target of the reference (The person or organization that has primary responsibility for the substitution.)
797         */
798        protected List<Practitioner> responsiblePartyTarget;
799
800
801        private static final long serialVersionUID = 1218245830L;
802
803    /*
804     * Constructor
805     */
806      public MedicationDispenseSubstitutionComponent() {
807        super();
808      }
809
810    /*
811     * Constructor
812     */
813      public MedicationDispenseSubstitutionComponent(CodeableConcept type) {
814        super();
815        this.type = type;
816      }
817
818        /**
819         * @return {@link #type} (A code signifying whether a different drug was dispensed from what was prescribed.)
820         */
821        public CodeableConcept getType() { 
822          if (this.type == null)
823            if (Configuration.errorOnAutoCreate())
824              throw new Error("Attempt to auto-create MedicationDispenseSubstitutionComponent.type");
825            else if (Configuration.doAutoCreate())
826              this.type = new CodeableConcept(); // cc
827          return this.type;
828        }
829
830        public boolean hasType() { 
831          return this.type != null && !this.type.isEmpty();
832        }
833
834        /**
835         * @param value {@link #type} (A code signifying whether a different drug was dispensed from what was prescribed.)
836         */
837        public MedicationDispenseSubstitutionComponent setType(CodeableConcept value) { 
838          this.type = value;
839          return this;
840        }
841
842        /**
843         * @return {@link #reason} (Indicates the reason for the substitution of (or lack of substitution) from what was prescribed.)
844         */
845        public List<CodeableConcept> getReason() { 
846          if (this.reason == null)
847            this.reason = new ArrayList<CodeableConcept>();
848          return this.reason;
849        }
850
851        public boolean hasReason() { 
852          if (this.reason == null)
853            return false;
854          for (CodeableConcept item : this.reason)
855            if (!item.isEmpty())
856              return true;
857          return false;
858        }
859
860        /**
861         * @return {@link #reason} (Indicates the reason for the substitution of (or lack of substitution) from what was prescribed.)
862         */
863    // syntactic sugar
864        public CodeableConcept addReason() { //3
865          CodeableConcept t = new CodeableConcept();
866          if (this.reason == null)
867            this.reason = new ArrayList<CodeableConcept>();
868          this.reason.add(t);
869          return t;
870        }
871
872    // syntactic sugar
873        public MedicationDispenseSubstitutionComponent addReason(CodeableConcept t) { //3
874          if (t == null)
875            return this;
876          if (this.reason == null)
877            this.reason = new ArrayList<CodeableConcept>();
878          this.reason.add(t);
879          return this;
880        }
881
882        /**
883         * @return {@link #responsibleParty} (The person or organization that has primary responsibility for the substitution.)
884         */
885        public List<Reference> getResponsibleParty() { 
886          if (this.responsibleParty == null)
887            this.responsibleParty = new ArrayList<Reference>();
888          return this.responsibleParty;
889        }
890
891        public boolean hasResponsibleParty() { 
892          if (this.responsibleParty == null)
893            return false;
894          for (Reference item : this.responsibleParty)
895            if (!item.isEmpty())
896              return true;
897          return false;
898        }
899
900        /**
901         * @return {@link #responsibleParty} (The person or organization that has primary responsibility for the substitution.)
902         */
903    // syntactic sugar
904        public Reference addResponsibleParty() { //3
905          Reference t = new Reference();
906          if (this.responsibleParty == null)
907            this.responsibleParty = new ArrayList<Reference>();
908          this.responsibleParty.add(t);
909          return t;
910        }
911
912    // syntactic sugar
913        public MedicationDispenseSubstitutionComponent addResponsibleParty(Reference t) { //3
914          if (t == null)
915            return this;
916          if (this.responsibleParty == null)
917            this.responsibleParty = new ArrayList<Reference>();
918          this.responsibleParty.add(t);
919          return this;
920        }
921
922        /**
923         * @return {@link #responsibleParty} (The actual objects that are the target of the reference. The reference library doesn't populate this, but you can use this to hold the resources if you resolvethemt. The person or organization that has primary responsibility for the substitution.)
924         */
925        public List<Practitioner> getResponsiblePartyTarget() { 
926          if (this.responsiblePartyTarget == null)
927            this.responsiblePartyTarget = new ArrayList<Practitioner>();
928          return this.responsiblePartyTarget;
929        }
930
931    // syntactic sugar
932        /**
933         * @return {@link #responsibleParty} (Add an actual object that is the target of the reference. The reference library doesn't use these, but you can use this to hold the resources if you resolvethemt. The person or organization that has primary responsibility for the substitution.)
934         */
935        public Practitioner addResponsiblePartyTarget() { 
936          Practitioner r = new Practitioner();
937          if (this.responsiblePartyTarget == null)
938            this.responsiblePartyTarget = new ArrayList<Practitioner>();
939          this.responsiblePartyTarget.add(r);
940          return r;
941        }
942
943        protected void listChildren(List<Property> childrenList) {
944          super.listChildren(childrenList);
945          childrenList.add(new Property("type", "CodeableConcept", "A code signifying whether a different drug was dispensed from what was prescribed.", 0, java.lang.Integer.MAX_VALUE, type));
946          childrenList.add(new Property("reason", "CodeableConcept", "Indicates the reason for the substitution of (or lack of substitution) from what was prescribed.", 0, java.lang.Integer.MAX_VALUE, reason));
947          childrenList.add(new Property("responsibleParty", "Reference(Practitioner)", "The person or organization that has primary responsibility for the substitution.", 0, java.lang.Integer.MAX_VALUE, responsibleParty));
948        }
949
950      @Override
951      public void setProperty(String name, Base value) throws FHIRException {
952        if (name.equals("type"))
953          this.type = castToCodeableConcept(value); // CodeableConcept
954        else if (name.equals("reason"))
955          this.getReason().add(castToCodeableConcept(value));
956        else if (name.equals("responsibleParty"))
957          this.getResponsibleParty().add(castToReference(value));
958        else
959          super.setProperty(name, value);
960      }
961
962      @Override
963      public Base addChild(String name) throws FHIRException {
964        if (name.equals("type")) {
965          this.type = new CodeableConcept();
966          return this.type;
967        }
968        else if (name.equals("reason")) {
969          return addReason();
970        }
971        else if (name.equals("responsibleParty")) {
972          return addResponsibleParty();
973        }
974        else
975          return super.addChild(name);
976      }
977
978      public MedicationDispenseSubstitutionComponent copy() {
979        MedicationDispenseSubstitutionComponent dst = new MedicationDispenseSubstitutionComponent();
980        copyValues(dst);
981        dst.type = type == null ? null : type.copy();
982        if (reason != null) {
983          dst.reason = new ArrayList<CodeableConcept>();
984          for (CodeableConcept i : reason)
985            dst.reason.add(i.copy());
986        };
987        if (responsibleParty != null) {
988          dst.responsibleParty = new ArrayList<Reference>();
989          for (Reference i : responsibleParty)
990            dst.responsibleParty.add(i.copy());
991        };
992        return dst;
993      }
994
995      @Override
996      public boolean equalsDeep(Base other) {
997        if (!super.equalsDeep(other))
998          return false;
999        if (!(other instanceof MedicationDispenseSubstitutionComponent))
1000          return false;
1001        MedicationDispenseSubstitutionComponent o = (MedicationDispenseSubstitutionComponent) other;
1002        return compareDeep(type, o.type, true) && compareDeep(reason, o.reason, true) && compareDeep(responsibleParty, o.responsibleParty, true)
1003          ;
1004      }
1005
1006      @Override
1007      public boolean equalsShallow(Base other) {
1008        if (!super.equalsShallow(other))
1009          return false;
1010        if (!(other instanceof MedicationDispenseSubstitutionComponent))
1011          return false;
1012        MedicationDispenseSubstitutionComponent o = (MedicationDispenseSubstitutionComponent) other;
1013        return true;
1014      }
1015
1016      public boolean isEmpty() {
1017        return super.isEmpty() && (type == null || type.isEmpty()) && (reason == null || reason.isEmpty())
1018           && (responsibleParty == null || responsibleParty.isEmpty());
1019      }
1020
1021  public String fhirType() {
1022    return "MedicationDispense.substitution";
1023
1024  }
1025
1026  }
1027
1028    /**
1029     * Identifier assigned by the dispensing facility - this is an identifier assigned outside FHIR.
1030     */
1031    @Child(name = "identifier", type = {Identifier.class}, order=0, min=0, max=1, modifier=false, summary=true)
1032    @Description(shortDefinition="External identifier", formalDefinition="Identifier assigned by the dispensing facility - this is an identifier assigned outside FHIR." )
1033    protected Identifier identifier;
1034
1035    /**
1036     * A code specifying the state of the set of dispense events.
1037     */
1038    @Child(name = "status", type = {CodeType.class}, order=1, min=0, max=1, modifier=true, summary=true)
1039    @Description(shortDefinition="in-progress | on-hold | completed | entered-in-error | stopped", formalDefinition="A code specifying the state of the set of dispense events." )
1040    protected Enumeration<MedicationDispenseStatus> status;
1041
1042    /**
1043     * A link to a resource representing the person to whom the medication will be given.
1044     */
1045    @Child(name = "patient", type = {Patient.class}, order=2, min=0, max=1, modifier=false, summary=true)
1046    @Description(shortDefinition="Who the dispense is for", formalDefinition="A link to a resource representing the person to whom the medication will be given." )
1047    protected Reference patient;
1048
1049    /**
1050     * The actual object that is the target of the reference (A link to a resource representing the person to whom the medication will be given.)
1051     */
1052    protected Patient patientTarget;
1053
1054    /**
1055     * The individual responsible for dispensing the medication.
1056     */
1057    @Child(name = "dispenser", type = {Practitioner.class}, order=3, min=0, max=1, modifier=false, summary=true)
1058    @Description(shortDefinition="Practitioner responsible for dispensing medication", formalDefinition="The individual responsible for dispensing the medication." )
1059    protected Reference dispenser;
1060
1061    /**
1062     * The actual object that is the target of the reference (The individual responsible for dispensing the medication.)
1063     */
1064    protected Practitioner dispenserTarget;
1065
1066    /**
1067     * Indicates the medication order that is being dispensed against.
1068     */
1069    @Child(name = "authorizingPrescription", type = {MedicationOrder.class}, order=4, min=0, max=Child.MAX_UNLIMITED, modifier=false, summary=true)
1070    @Description(shortDefinition="Medication order that authorizes the dispense", formalDefinition="Indicates the medication order that is being dispensed against." )
1071    protected List<Reference> authorizingPrescription;
1072    /**
1073     * The actual objects that are the target of the reference (Indicates the medication order that is being dispensed against.)
1074     */
1075    protected List<MedicationOrder> authorizingPrescriptionTarget;
1076
1077
1078    /**
1079     * Indicates the type of dispensing event that is performed. For example, Trial Fill, Completion of Trial, Partial Fill, Emergency Fill, Samples, etc.
1080     */
1081    @Child(name = "type", type = {CodeableConcept.class}, order=5, min=0, max=1, modifier=false, summary=true)
1082    @Description(shortDefinition="Trial fill, partial fill, emergency fill, etc.", formalDefinition="Indicates the type of dispensing event that is performed. For example, Trial Fill, Completion of Trial, Partial Fill, Emergency Fill, Samples, etc." )
1083    protected CodeableConcept type;
1084
1085    /**
1086     * The amount of medication that has been dispensed. Includes unit of measure.
1087     */
1088    @Child(name = "quantity", type = {SimpleQuantity.class}, order=6, min=0, max=1, modifier=false, summary=true)
1089    @Description(shortDefinition="Amount dispensed", formalDefinition="The amount of medication that has been dispensed. Includes unit of measure." )
1090    protected SimpleQuantity quantity;
1091
1092    /**
1093     * The amount of medication expressed as a timing amount.
1094     */
1095    @Child(name = "daysSupply", type = {SimpleQuantity.class}, order=7, min=0, max=1, modifier=false, summary=true)
1096    @Description(shortDefinition="Days Supply", formalDefinition="The amount of medication expressed as a timing amount." )
1097    protected SimpleQuantity daysSupply;
1098
1099    /**
1100     * Identifies the medication being administered. This is either a link to a resource representing the details of the medication or a simple attribute carrying a code that identifies the medication from a known list of medications.
1101     */
1102    @Child(name = "medication", type = {CodeableConcept.class, Medication.class}, order=8, min=1, max=1, modifier=false, summary=true)
1103    @Description(shortDefinition="What medication was supplied", formalDefinition="Identifies the medication being administered. This is either a link to a resource representing the details of the medication or a simple attribute carrying a code that identifies the medication from a known list of medications." )
1104    protected Type medication;
1105
1106    /**
1107     * The time when the dispensed product was packaged and reviewed.
1108     */
1109    @Child(name = "whenPrepared", type = {DateTimeType.class}, order=9, min=0, max=1, modifier=false, summary=true)
1110    @Description(shortDefinition="Dispense processing time", formalDefinition="The time when the dispensed product was packaged and reviewed." )
1111    protected DateTimeType whenPrepared;
1112
1113    /**
1114     * The time the dispensed product was provided to the patient or their representative.
1115     */
1116    @Child(name = "whenHandedOver", type = {DateTimeType.class}, order=10, min=0, max=1, modifier=false, summary=true)
1117    @Description(shortDefinition="When product was given out", formalDefinition="The time the dispensed product was provided to the patient or their representative." )
1118    protected DateTimeType whenHandedOver;
1119
1120    /**
1121     * Identification of the facility/location where the medication was shipped to, as part of the dispense event.
1122     */
1123    @Child(name = "destination", type = {Location.class}, order=11, min=0, max=1, modifier=false, summary=true)
1124    @Description(shortDefinition="Where the medication was sent", formalDefinition="Identification of the facility/location where the medication was shipped to, as part of the dispense event." )
1125    protected Reference destination;
1126
1127    /**
1128     * The actual object that is the target of the reference (Identification of the facility/location where the medication was shipped to, as part of the dispense event.)
1129     */
1130    protected Location destinationTarget;
1131
1132    /**
1133     * Identifies the person who picked up the medication.  This will usually be a patient or their caregiver, but some cases exist where it can be a healthcare professional.
1134     */
1135    @Child(name = "receiver", type = {Patient.class, Practitioner.class}, order=12, min=0, max=Child.MAX_UNLIMITED, modifier=false, summary=true)
1136    @Description(shortDefinition="Who collected the medication", formalDefinition="Identifies the person who picked up the medication.  This will usually be a patient or their caregiver, but some cases exist where it can be a healthcare professional." )
1137    protected List<Reference> receiver;
1138    /**
1139     * The actual objects that are the target of the reference (Identifies the person who picked up the medication.  This will usually be a patient or their caregiver, but some cases exist where it can be a healthcare professional.)
1140     */
1141    protected List<Resource> receiverTarget;
1142
1143
1144    /**
1145     * Extra information about the dispense that could not be conveyed in the other attributes.
1146     */
1147    @Child(name = "note", type = {StringType.class}, order=13, min=0, max=1, modifier=false, summary=true)
1148    @Description(shortDefinition="Information about the dispense", formalDefinition="Extra information about the dispense that could not be conveyed in the other attributes." )
1149    protected StringType note;
1150
1151    /**
1152     * Indicates how the medication is to be used by the patient.
1153     */
1154    @Child(name = "dosageInstruction", type = {}, order=14, min=0, max=Child.MAX_UNLIMITED, modifier=false, summary=true)
1155    @Description(shortDefinition="Medicine administration instructions to the patient/caregiver", formalDefinition="Indicates how the medication is to be used by the patient." )
1156    protected List<MedicationDispenseDosageInstructionComponent> dosageInstruction;
1157
1158    /**
1159     * Indicates whether or not substitution was made as part of the dispense.  In some cases substitution will be expected but does not happen, in other cases substitution is not expected but does happen.  This block explains what substitution did or did not happen and why.
1160     */
1161    @Child(name = "substitution", type = {}, order=15, min=0, max=1, modifier=false, summary=true)
1162    @Description(shortDefinition="Deals with substitution of one medicine for another", formalDefinition="Indicates whether or not substitution was made as part of the dispense.  In some cases substitution will be expected but does not happen, in other cases substitution is not expected but does happen.  This block explains what substitution did or did not happen and why." )
1163    protected MedicationDispenseSubstitutionComponent substitution;
1164
1165    private static final long serialVersionUID = -2071218407L;
1166
1167  /*
1168   * Constructor
1169   */
1170    public MedicationDispense() {
1171      super();
1172    }
1173
1174  /*
1175   * Constructor
1176   */
1177    public MedicationDispense(Type medication) {
1178      super();
1179      this.medication = medication;
1180    }
1181
1182    /**
1183     * @return {@link #identifier} (Identifier assigned by the dispensing facility - this is an identifier assigned outside FHIR.)
1184     */
1185    public Identifier getIdentifier() { 
1186      if (this.identifier == null)
1187        if (Configuration.errorOnAutoCreate())
1188          throw new Error("Attempt to auto-create MedicationDispense.identifier");
1189        else if (Configuration.doAutoCreate())
1190          this.identifier = new Identifier(); // cc
1191      return this.identifier;
1192    }
1193
1194    public boolean hasIdentifier() { 
1195      return this.identifier != null && !this.identifier.isEmpty();
1196    }
1197
1198    /**
1199     * @param value {@link #identifier} (Identifier assigned by the dispensing facility - this is an identifier assigned outside FHIR.)
1200     */
1201    public MedicationDispense setIdentifier(Identifier value) { 
1202      this.identifier = value;
1203      return this;
1204    }
1205
1206    /**
1207     * @return {@link #status} (A code specifying the state of the set of dispense events.). This is the underlying object with id, value and extensions. The accessor "getStatus" gives direct access to the value
1208     */
1209    public Enumeration<MedicationDispenseStatus> getStatusElement() { 
1210      if (this.status == null)
1211        if (Configuration.errorOnAutoCreate())
1212          throw new Error("Attempt to auto-create MedicationDispense.status");
1213        else if (Configuration.doAutoCreate())
1214          this.status = new Enumeration<MedicationDispenseStatus>(new MedicationDispenseStatusEnumFactory()); // bb
1215      return this.status;
1216    }
1217
1218    public boolean hasStatusElement() { 
1219      return this.status != null && !this.status.isEmpty();
1220    }
1221
1222    public boolean hasStatus() { 
1223      return this.status != null && !this.status.isEmpty();
1224    }
1225
1226    /**
1227     * @param value {@link #status} (A code specifying the state of the set of dispense events.). This is the underlying object with id, value and extensions. The accessor "getStatus" gives direct access to the value
1228     */
1229    public MedicationDispense setStatusElement(Enumeration<MedicationDispenseStatus> value) { 
1230      this.status = value;
1231      return this;
1232    }
1233
1234    /**
1235     * @return A code specifying the state of the set of dispense events.
1236     */
1237    public MedicationDispenseStatus getStatus() { 
1238      return this.status == null ? null : this.status.getValue();
1239    }
1240
1241    /**
1242     * @param value A code specifying the state of the set of dispense events.
1243     */
1244    public MedicationDispense setStatus(MedicationDispenseStatus value) { 
1245      if (value == null)
1246        this.status = null;
1247      else {
1248        if (this.status == null)
1249          this.status = new Enumeration<MedicationDispenseStatus>(new MedicationDispenseStatusEnumFactory());
1250        this.status.setValue(value);
1251      }
1252      return this;
1253    }
1254
1255    /**
1256     * @return {@link #patient} (A link to a resource representing the person to whom the medication will be given.)
1257     */
1258    public Reference getPatient() { 
1259      if (this.patient == null)
1260        if (Configuration.errorOnAutoCreate())
1261          throw new Error("Attempt to auto-create MedicationDispense.patient");
1262        else if (Configuration.doAutoCreate())
1263          this.patient = new Reference(); // cc
1264      return this.patient;
1265    }
1266
1267    public boolean hasPatient() { 
1268      return this.patient != null && !this.patient.isEmpty();
1269    }
1270
1271    /**
1272     * @param value {@link #patient} (A link to a resource representing the person to whom the medication will be given.)
1273     */
1274    public MedicationDispense setPatient(Reference value) { 
1275      this.patient = value;
1276      return this;
1277    }
1278
1279    /**
1280     * @return {@link #patient} The actual object that is the target of the reference. The reference library doesn't populate this, but you can use it to hold the resource if you resolve it. (A link to a resource representing the person to whom the medication will be given.)
1281     */
1282    public Patient getPatientTarget() { 
1283      if (this.patientTarget == null)
1284        if (Configuration.errorOnAutoCreate())
1285          throw new Error("Attempt to auto-create MedicationDispense.patient");
1286        else if (Configuration.doAutoCreate())
1287          this.patientTarget = new Patient(); // aa
1288      return this.patientTarget;
1289    }
1290
1291    /**
1292     * @param value {@link #patient} The actual object that is the target of the reference. The reference library doesn't use these, but you can use it to hold the resource if you resolve it. (A link to a resource representing the person to whom the medication will be given.)
1293     */
1294    public MedicationDispense setPatientTarget(Patient value) { 
1295      this.patientTarget = value;
1296      return this;
1297    }
1298
1299    /**
1300     * @return {@link #dispenser} (The individual responsible for dispensing the medication.)
1301     */
1302    public Reference getDispenser() { 
1303      if (this.dispenser == null)
1304        if (Configuration.errorOnAutoCreate())
1305          throw new Error("Attempt to auto-create MedicationDispense.dispenser");
1306        else if (Configuration.doAutoCreate())
1307          this.dispenser = new Reference(); // cc
1308      return this.dispenser;
1309    }
1310
1311    public boolean hasDispenser() { 
1312      return this.dispenser != null && !this.dispenser.isEmpty();
1313    }
1314
1315    /**
1316     * @param value {@link #dispenser} (The individual responsible for dispensing the medication.)
1317     */
1318    public MedicationDispense setDispenser(Reference value) { 
1319      this.dispenser = value;
1320      return this;
1321    }
1322
1323    /**
1324     * @return {@link #dispenser} The actual object that is the target of the reference. The reference library doesn't populate this, but you can use it to hold the resource if you resolve it. (The individual responsible for dispensing the medication.)
1325     */
1326    public Practitioner getDispenserTarget() { 
1327      if (this.dispenserTarget == null)
1328        if (Configuration.errorOnAutoCreate())
1329          throw new Error("Attempt to auto-create MedicationDispense.dispenser");
1330        else if (Configuration.doAutoCreate())
1331          this.dispenserTarget = new Practitioner(); // aa
1332      return this.dispenserTarget;
1333    }
1334
1335    /**
1336     * @param value {@link #dispenser} The actual object that is the target of the reference. The reference library doesn't use these, but you can use it to hold the resource if you resolve it. (The individual responsible for dispensing the medication.)
1337     */
1338    public MedicationDispense setDispenserTarget(Practitioner value) { 
1339      this.dispenserTarget = value;
1340      return this;
1341    }
1342
1343    /**
1344     * @return {@link #authorizingPrescription} (Indicates the medication order that is being dispensed against.)
1345     */
1346    public List<Reference> getAuthorizingPrescription() { 
1347      if (this.authorizingPrescription == null)
1348        this.authorizingPrescription = new ArrayList<Reference>();
1349      return this.authorizingPrescription;
1350    }
1351
1352    public boolean hasAuthorizingPrescription() { 
1353      if (this.authorizingPrescription == null)
1354        return false;
1355      for (Reference item : this.authorizingPrescription)
1356        if (!item.isEmpty())
1357          return true;
1358      return false;
1359    }
1360
1361    /**
1362     * @return {@link #authorizingPrescription} (Indicates the medication order that is being dispensed against.)
1363     */
1364    // syntactic sugar
1365    public Reference addAuthorizingPrescription() { //3
1366      Reference t = new Reference();
1367      if (this.authorizingPrescription == null)
1368        this.authorizingPrescription = new ArrayList<Reference>();
1369      this.authorizingPrescription.add(t);
1370      return t;
1371    }
1372
1373    // syntactic sugar
1374    public MedicationDispense addAuthorizingPrescription(Reference t) { //3
1375      if (t == null)
1376        return this;
1377      if (this.authorizingPrescription == null)
1378        this.authorizingPrescription = new ArrayList<Reference>();
1379      this.authorizingPrescription.add(t);
1380      return this;
1381    }
1382
1383    /**
1384     * @return {@link #authorizingPrescription} (The actual objects that are the target of the reference. The reference library doesn't populate this, but you can use this to hold the resources if you resolvethemt. Indicates the medication order that is being dispensed against.)
1385     */
1386    public List<MedicationOrder> getAuthorizingPrescriptionTarget() { 
1387      if (this.authorizingPrescriptionTarget == null)
1388        this.authorizingPrescriptionTarget = new ArrayList<MedicationOrder>();
1389      return this.authorizingPrescriptionTarget;
1390    }
1391
1392    // syntactic sugar
1393    /**
1394     * @return {@link #authorizingPrescription} (Add an actual object that is the target of the reference. The reference library doesn't use these, but you can use this to hold the resources if you resolvethemt. Indicates the medication order that is being dispensed against.)
1395     */
1396    public MedicationOrder addAuthorizingPrescriptionTarget() { 
1397      MedicationOrder r = new MedicationOrder();
1398      if (this.authorizingPrescriptionTarget == null)
1399        this.authorizingPrescriptionTarget = new ArrayList<MedicationOrder>();
1400      this.authorizingPrescriptionTarget.add(r);
1401      return r;
1402    }
1403
1404    /**
1405     * @return {@link #type} (Indicates the type of dispensing event that is performed. For example, Trial Fill, Completion of Trial, Partial Fill, Emergency Fill, Samples, etc.)
1406     */
1407    public CodeableConcept getType() { 
1408      if (this.type == null)
1409        if (Configuration.errorOnAutoCreate())
1410          throw new Error("Attempt to auto-create MedicationDispense.type");
1411        else if (Configuration.doAutoCreate())
1412          this.type = new CodeableConcept(); // cc
1413      return this.type;
1414    }
1415
1416    public boolean hasType() { 
1417      return this.type != null && !this.type.isEmpty();
1418    }
1419
1420    /**
1421     * @param value {@link #type} (Indicates the type of dispensing event that is performed. For example, Trial Fill, Completion of Trial, Partial Fill, Emergency Fill, Samples, etc.)
1422     */
1423    public MedicationDispense setType(CodeableConcept value) { 
1424      this.type = value;
1425      return this;
1426    }
1427
1428    /**
1429     * @return {@link #quantity} (The amount of medication that has been dispensed. Includes unit of measure.)
1430     */
1431    public SimpleQuantity getQuantity() { 
1432      if (this.quantity == null)
1433        if (Configuration.errorOnAutoCreate())
1434          throw new Error("Attempt to auto-create MedicationDispense.quantity");
1435        else if (Configuration.doAutoCreate())
1436          this.quantity = new SimpleQuantity(); // cc
1437      return this.quantity;
1438    }
1439
1440    public boolean hasQuantity() { 
1441      return this.quantity != null && !this.quantity.isEmpty();
1442    }
1443
1444    /**
1445     * @param value {@link #quantity} (The amount of medication that has been dispensed. Includes unit of measure.)
1446     */
1447    public MedicationDispense setQuantity(SimpleQuantity value) { 
1448      this.quantity = value;
1449      return this;
1450    }
1451
1452    /**
1453     * @return {@link #daysSupply} (The amount of medication expressed as a timing amount.)
1454     */
1455    public SimpleQuantity getDaysSupply() { 
1456      if (this.daysSupply == null)
1457        if (Configuration.errorOnAutoCreate())
1458          throw new Error("Attempt to auto-create MedicationDispense.daysSupply");
1459        else if (Configuration.doAutoCreate())
1460          this.daysSupply = new SimpleQuantity(); // cc
1461      return this.daysSupply;
1462    }
1463
1464    public boolean hasDaysSupply() { 
1465      return this.daysSupply != null && !this.daysSupply.isEmpty();
1466    }
1467
1468    /**
1469     * @param value {@link #daysSupply} (The amount of medication expressed as a timing amount.)
1470     */
1471    public MedicationDispense setDaysSupply(SimpleQuantity value) { 
1472      this.daysSupply = value;
1473      return this;
1474    }
1475
1476    /**
1477     * @return {@link #medication} (Identifies the medication being administered. This is either a link to a resource representing the details of the medication or a simple attribute carrying a code that identifies the medication from a known list of medications.)
1478     */
1479    public Type getMedication() { 
1480      return this.medication;
1481    }
1482
1483    /**
1484     * @return {@link #medication} (Identifies the medication being administered. This is either a link to a resource representing the details of the medication or a simple attribute carrying a code that identifies the medication from a known list of medications.)
1485     */
1486    public CodeableConcept getMedicationCodeableConcept() throws FHIRException { 
1487      if (!(this.medication instanceof CodeableConcept))
1488        throw new FHIRException("Type mismatch: the type CodeableConcept was expected, but "+this.medication.getClass().getName()+" was encountered");
1489      return (CodeableConcept) this.medication;
1490    }
1491
1492    public boolean hasMedicationCodeableConcept() { 
1493      return this.medication instanceof CodeableConcept;
1494    }
1495
1496    /**
1497     * @return {@link #medication} (Identifies the medication being administered. This is either a link to a resource representing the details of the medication or a simple attribute carrying a code that identifies the medication from a known list of medications.)
1498     */
1499    public Reference getMedicationReference() throws FHIRException { 
1500      if (!(this.medication instanceof Reference))
1501        throw new FHIRException("Type mismatch: the type Reference was expected, but "+this.medication.getClass().getName()+" was encountered");
1502      return (Reference) this.medication;
1503    }
1504
1505    public boolean hasMedicationReference() { 
1506      return this.medication instanceof Reference;
1507    }
1508
1509    public boolean hasMedication() { 
1510      return this.medication != null && !this.medication.isEmpty();
1511    }
1512
1513    /**
1514     * @param value {@link #medication} (Identifies the medication being administered. This is either a link to a resource representing the details of the medication or a simple attribute carrying a code that identifies the medication from a known list of medications.)
1515     */
1516    public MedicationDispense setMedication(Type value) { 
1517      this.medication = value;
1518      return this;
1519    }
1520
1521    /**
1522     * @return {@link #whenPrepared} (The time when the dispensed product was packaged and reviewed.). This is the underlying object with id, value and extensions. The accessor "getWhenPrepared" gives direct access to the value
1523     */
1524    public DateTimeType getWhenPreparedElement() { 
1525      if (this.whenPrepared == null)
1526        if (Configuration.errorOnAutoCreate())
1527          throw new Error("Attempt to auto-create MedicationDispense.whenPrepared");
1528        else if (Configuration.doAutoCreate())
1529          this.whenPrepared = new DateTimeType(); // bb
1530      return this.whenPrepared;
1531    }
1532
1533    public boolean hasWhenPreparedElement() { 
1534      return this.whenPrepared != null && !this.whenPrepared.isEmpty();
1535    }
1536
1537    public boolean hasWhenPrepared() { 
1538      return this.whenPrepared != null && !this.whenPrepared.isEmpty();
1539    }
1540
1541    /**
1542     * @param value {@link #whenPrepared} (The time when the dispensed product was packaged and reviewed.). This is the underlying object with id, value and extensions. The accessor "getWhenPrepared" gives direct access to the value
1543     */
1544    public MedicationDispense setWhenPreparedElement(DateTimeType value) { 
1545      this.whenPrepared = value;
1546      return this;
1547    }
1548
1549    /**
1550     * @return The time when the dispensed product was packaged and reviewed.
1551     */
1552    public Date getWhenPrepared() { 
1553      return this.whenPrepared == null ? null : this.whenPrepared.getValue();
1554    }
1555
1556    /**
1557     * @param value The time when the dispensed product was packaged and reviewed.
1558     */
1559    public MedicationDispense setWhenPrepared(Date value) { 
1560      if (value == null)
1561        this.whenPrepared = null;
1562      else {
1563        if (this.whenPrepared == null)
1564          this.whenPrepared = new DateTimeType();
1565        this.whenPrepared.setValue(value);
1566      }
1567      return this;
1568    }
1569
1570    /**
1571     * @return {@link #whenHandedOver} (The time the dispensed product was provided to the patient or their representative.). This is the underlying object with id, value and extensions. The accessor "getWhenHandedOver" gives direct access to the value
1572     */
1573    public DateTimeType getWhenHandedOverElement() { 
1574      if (this.whenHandedOver == null)
1575        if (Configuration.errorOnAutoCreate())
1576          throw new Error("Attempt to auto-create MedicationDispense.whenHandedOver");
1577        else if (Configuration.doAutoCreate())
1578          this.whenHandedOver = new DateTimeType(); // bb
1579      return this.whenHandedOver;
1580    }
1581
1582    public boolean hasWhenHandedOverElement() { 
1583      return this.whenHandedOver != null && !this.whenHandedOver.isEmpty();
1584    }
1585
1586    public boolean hasWhenHandedOver() { 
1587      return this.whenHandedOver != null && !this.whenHandedOver.isEmpty();
1588    }
1589
1590    /**
1591     * @param value {@link #whenHandedOver} (The time the dispensed product was provided to the patient or their representative.). This is the underlying object with id, value and extensions. The accessor "getWhenHandedOver" gives direct access to the value
1592     */
1593    public MedicationDispense setWhenHandedOverElement(DateTimeType value) { 
1594      this.whenHandedOver = value;
1595      return this;
1596    }
1597
1598    /**
1599     * @return The time the dispensed product was provided to the patient or their representative.
1600     */
1601    public Date getWhenHandedOver() { 
1602      return this.whenHandedOver == null ? null : this.whenHandedOver.getValue();
1603    }
1604
1605    /**
1606     * @param value The time the dispensed product was provided to the patient or their representative.
1607     */
1608    public MedicationDispense setWhenHandedOver(Date value) { 
1609      if (value == null)
1610        this.whenHandedOver = null;
1611      else {
1612        if (this.whenHandedOver == null)
1613          this.whenHandedOver = new DateTimeType();
1614        this.whenHandedOver.setValue(value);
1615      }
1616      return this;
1617    }
1618
1619    /**
1620     * @return {@link #destination} (Identification of the facility/location where the medication was shipped to, as part of the dispense event.)
1621     */
1622    public Reference getDestination() { 
1623      if (this.destination == null)
1624        if (Configuration.errorOnAutoCreate())
1625          throw new Error("Attempt to auto-create MedicationDispense.destination");
1626        else if (Configuration.doAutoCreate())
1627          this.destination = new Reference(); // cc
1628      return this.destination;
1629    }
1630
1631    public boolean hasDestination() { 
1632      return this.destination != null && !this.destination.isEmpty();
1633    }
1634
1635    /**
1636     * @param value {@link #destination} (Identification of the facility/location where the medication was shipped to, as part of the dispense event.)
1637     */
1638    public MedicationDispense setDestination(Reference value) { 
1639      this.destination = value;
1640      return this;
1641    }
1642
1643    /**
1644     * @return {@link #destination} The actual object that is the target of the reference. The reference library doesn't populate this, but you can use it to hold the resource if you resolve it. (Identification of the facility/location where the medication was shipped to, as part of the dispense event.)
1645     */
1646    public Location getDestinationTarget() { 
1647      if (this.destinationTarget == null)
1648        if (Configuration.errorOnAutoCreate())
1649          throw new Error("Attempt to auto-create MedicationDispense.destination");
1650        else if (Configuration.doAutoCreate())
1651          this.destinationTarget = new Location(); // aa
1652      return this.destinationTarget;
1653    }
1654
1655    /**
1656     * @param value {@link #destination} The actual object that is the target of the reference. The reference library doesn't use these, but you can use it to hold the resource if you resolve it. (Identification of the facility/location where the medication was shipped to, as part of the dispense event.)
1657     */
1658    public MedicationDispense setDestinationTarget(Location value) { 
1659      this.destinationTarget = value;
1660      return this;
1661    }
1662
1663    /**
1664     * @return {@link #receiver} (Identifies the person who picked up the medication.  This will usually be a patient or their caregiver, but some cases exist where it can be a healthcare professional.)
1665     */
1666    public List<Reference> getReceiver() { 
1667      if (this.receiver == null)
1668        this.receiver = new ArrayList<Reference>();
1669      return this.receiver;
1670    }
1671
1672    public boolean hasReceiver() { 
1673      if (this.receiver == null)
1674        return false;
1675      for (Reference item : this.receiver)
1676        if (!item.isEmpty())
1677          return true;
1678      return false;
1679    }
1680
1681    /**
1682     * @return {@link #receiver} (Identifies the person who picked up the medication.  This will usually be a patient or their caregiver, but some cases exist where it can be a healthcare professional.)
1683     */
1684    // syntactic sugar
1685    public Reference addReceiver() { //3
1686      Reference t = new Reference();
1687      if (this.receiver == null)
1688        this.receiver = new ArrayList<Reference>();
1689      this.receiver.add(t);
1690      return t;
1691    }
1692
1693    // syntactic sugar
1694    public MedicationDispense addReceiver(Reference t) { //3
1695      if (t == null)
1696        return this;
1697      if (this.receiver == null)
1698        this.receiver = new ArrayList<Reference>();
1699      this.receiver.add(t);
1700      return this;
1701    }
1702
1703    /**
1704     * @return {@link #receiver} (The actual objects that are the target of the reference. The reference library doesn't populate this, but you can use this to hold the resources if you resolvethemt. Identifies the person who picked up the medication.  This will usually be a patient or their caregiver, but some cases exist where it can be a healthcare professional.)
1705     */
1706    public List<Resource> getReceiverTarget() { 
1707      if (this.receiverTarget == null)
1708        this.receiverTarget = new ArrayList<Resource>();
1709      return this.receiverTarget;
1710    }
1711
1712    /**
1713     * @return {@link #note} (Extra information about the dispense that could not be conveyed in the other attributes.). This is the underlying object with id, value and extensions. The accessor "getNote" gives direct access to the value
1714     */
1715    public StringType getNoteElement() { 
1716      if (this.note == null)
1717        if (Configuration.errorOnAutoCreate())
1718          throw new Error("Attempt to auto-create MedicationDispense.note");
1719        else if (Configuration.doAutoCreate())
1720          this.note = new StringType(); // bb
1721      return this.note;
1722    }
1723
1724    public boolean hasNoteElement() { 
1725      return this.note != null && !this.note.isEmpty();
1726    }
1727
1728    public boolean hasNote() { 
1729      return this.note != null && !this.note.isEmpty();
1730    }
1731
1732    /**
1733     * @param value {@link #note} (Extra information about the dispense that could not be conveyed in the other attributes.). This is the underlying object with id, value and extensions. The accessor "getNote" gives direct access to the value
1734     */
1735    public MedicationDispense setNoteElement(StringType value) { 
1736      this.note = value;
1737      return this;
1738    }
1739
1740    /**
1741     * @return Extra information about the dispense that could not be conveyed in the other attributes.
1742     */
1743    public String getNote() { 
1744      return this.note == null ? null : this.note.getValue();
1745    }
1746
1747    /**
1748     * @param value Extra information about the dispense that could not be conveyed in the other attributes.
1749     */
1750    public MedicationDispense setNote(String value) { 
1751      if (Utilities.noString(value))
1752        this.note = null;
1753      else {
1754        if (this.note == null)
1755          this.note = new StringType();
1756        this.note.setValue(value);
1757      }
1758      return this;
1759    }
1760
1761    /**
1762     * @return {@link #dosageInstruction} (Indicates how the medication is to be used by the patient.)
1763     */
1764    public List<MedicationDispenseDosageInstructionComponent> getDosageInstruction() { 
1765      if (this.dosageInstruction == null)
1766        this.dosageInstruction = new ArrayList<MedicationDispenseDosageInstructionComponent>();
1767      return this.dosageInstruction;
1768    }
1769
1770    public boolean hasDosageInstruction() { 
1771      if (this.dosageInstruction == null)
1772        return false;
1773      for (MedicationDispenseDosageInstructionComponent item : this.dosageInstruction)
1774        if (!item.isEmpty())
1775          return true;
1776      return false;
1777    }
1778
1779    /**
1780     * @return {@link #dosageInstruction} (Indicates how the medication is to be used by the patient.)
1781     */
1782    // syntactic sugar
1783    public MedicationDispenseDosageInstructionComponent addDosageInstruction() { //3
1784      MedicationDispenseDosageInstructionComponent t = new MedicationDispenseDosageInstructionComponent();
1785      if (this.dosageInstruction == null)
1786        this.dosageInstruction = new ArrayList<MedicationDispenseDosageInstructionComponent>();
1787      this.dosageInstruction.add(t);
1788      return t;
1789    }
1790
1791    // syntactic sugar
1792    public MedicationDispense addDosageInstruction(MedicationDispenseDosageInstructionComponent t) { //3
1793      if (t == null)
1794        return this;
1795      if (this.dosageInstruction == null)
1796        this.dosageInstruction = new ArrayList<MedicationDispenseDosageInstructionComponent>();
1797      this.dosageInstruction.add(t);
1798      return this;
1799    }
1800
1801    /**
1802     * @return {@link #substitution} (Indicates whether or not substitution was made as part of the dispense.  In some cases substitution will be expected but does not happen, in other cases substitution is not expected but does happen.  This block explains what substitution did or did not happen and why.)
1803     */
1804    public MedicationDispenseSubstitutionComponent getSubstitution() { 
1805      if (this.substitution == null)
1806        if (Configuration.errorOnAutoCreate())
1807          throw new Error("Attempt to auto-create MedicationDispense.substitution");
1808        else if (Configuration.doAutoCreate())
1809          this.substitution = new MedicationDispenseSubstitutionComponent(); // cc
1810      return this.substitution;
1811    }
1812
1813    public boolean hasSubstitution() { 
1814      return this.substitution != null && !this.substitution.isEmpty();
1815    }
1816
1817    /**
1818     * @param value {@link #substitution} (Indicates whether or not substitution was made as part of the dispense.  In some cases substitution will be expected but does not happen, in other cases substitution is not expected but does happen.  This block explains what substitution did or did not happen and why.)
1819     */
1820    public MedicationDispense setSubstitution(MedicationDispenseSubstitutionComponent value) { 
1821      this.substitution = value;
1822      return this;
1823    }
1824
1825      protected void listChildren(List<Property> childrenList) {
1826        super.listChildren(childrenList);
1827        childrenList.add(new Property("identifier", "Identifier", "Identifier assigned by the dispensing facility - this is an identifier assigned outside FHIR.", 0, java.lang.Integer.MAX_VALUE, identifier));
1828        childrenList.add(new Property("status", "code", "A code specifying the state of the set of dispense events.", 0, java.lang.Integer.MAX_VALUE, status));
1829        childrenList.add(new Property("patient", "Reference(Patient)", "A link to a resource representing the person to whom the medication will be given.", 0, java.lang.Integer.MAX_VALUE, patient));
1830        childrenList.add(new Property("dispenser", "Reference(Practitioner)", "The individual responsible for dispensing the medication.", 0, java.lang.Integer.MAX_VALUE, dispenser));
1831        childrenList.add(new Property("authorizingPrescription", "Reference(MedicationOrder)", "Indicates the medication order that is being dispensed against.", 0, java.lang.Integer.MAX_VALUE, authorizingPrescription));
1832        childrenList.add(new Property("type", "CodeableConcept", "Indicates the type of dispensing event that is performed. For example, Trial Fill, Completion of Trial, Partial Fill, Emergency Fill, Samples, etc.", 0, java.lang.Integer.MAX_VALUE, type));
1833        childrenList.add(new Property("quantity", "SimpleQuantity", "The amount of medication that has been dispensed. Includes unit of measure.", 0, java.lang.Integer.MAX_VALUE, quantity));
1834        childrenList.add(new Property("daysSupply", "SimpleQuantity", "The amount of medication expressed as a timing amount.", 0, java.lang.Integer.MAX_VALUE, daysSupply));
1835        childrenList.add(new Property("medication[x]", "CodeableConcept|Reference(Medication)", "Identifies the medication being administered. This is either a link to a resource representing the details of the medication or a simple attribute carrying a code that identifies the medication from a known list of medications.", 0, java.lang.Integer.MAX_VALUE, medication));
1836        childrenList.add(new Property("whenPrepared", "dateTime", "The time when the dispensed product was packaged and reviewed.", 0, java.lang.Integer.MAX_VALUE, whenPrepared));
1837        childrenList.add(new Property("whenHandedOver", "dateTime", "The time the dispensed product was provided to the patient or their representative.", 0, java.lang.Integer.MAX_VALUE, whenHandedOver));
1838        childrenList.add(new Property("destination", "Reference(Location)", "Identification of the facility/location where the medication was shipped to, as part of the dispense event.", 0, java.lang.Integer.MAX_VALUE, destination));
1839        childrenList.add(new Property("receiver", "Reference(Patient|Practitioner)", "Identifies the person who picked up the medication.  This will usually be a patient or their caregiver, but some cases exist where it can be a healthcare professional.", 0, java.lang.Integer.MAX_VALUE, receiver));
1840        childrenList.add(new Property("note", "string", "Extra information about the dispense that could not be conveyed in the other attributes.", 0, java.lang.Integer.MAX_VALUE, note));
1841        childrenList.add(new Property("dosageInstruction", "", "Indicates how the medication is to be used by the patient.", 0, java.lang.Integer.MAX_VALUE, dosageInstruction));
1842        childrenList.add(new Property("substitution", "", "Indicates whether or not substitution was made as part of the dispense.  In some cases substitution will be expected but does not happen, in other cases substitution is not expected but does happen.  This block explains what substitution did or did not happen and why.", 0, java.lang.Integer.MAX_VALUE, substitution));
1843      }
1844
1845      @Override
1846      public void setProperty(String name, Base value) throws FHIRException {
1847        if (name.equals("identifier"))
1848          this.identifier = castToIdentifier(value); // Identifier
1849        else if (name.equals("status"))
1850          this.status = new MedicationDispenseStatusEnumFactory().fromType(value); // Enumeration<MedicationDispenseStatus>
1851        else if (name.equals("patient"))
1852          this.patient = castToReference(value); // Reference
1853        else if (name.equals("dispenser"))
1854          this.dispenser = castToReference(value); // Reference
1855        else if (name.equals("authorizingPrescription"))
1856          this.getAuthorizingPrescription().add(castToReference(value));
1857        else if (name.equals("type"))
1858          this.type = castToCodeableConcept(value); // CodeableConcept
1859        else if (name.equals("quantity"))
1860          this.quantity = castToSimpleQuantity(value); // SimpleQuantity
1861        else if (name.equals("daysSupply"))
1862          this.daysSupply = castToSimpleQuantity(value); // SimpleQuantity
1863        else if (name.equals("medication[x]"))
1864          this.medication = (Type) value; // Type
1865        else if (name.equals("whenPrepared"))
1866          this.whenPrepared = castToDateTime(value); // DateTimeType
1867        else if (name.equals("whenHandedOver"))
1868          this.whenHandedOver = castToDateTime(value); // DateTimeType
1869        else if (name.equals("destination"))
1870          this.destination = castToReference(value); // Reference
1871        else if (name.equals("receiver"))
1872          this.getReceiver().add(castToReference(value));
1873        else if (name.equals("note"))
1874          this.note = castToString(value); // StringType
1875        else if (name.equals("dosageInstruction"))
1876          this.getDosageInstruction().add((MedicationDispenseDosageInstructionComponent) value);
1877        else if (name.equals("substitution"))
1878          this.substitution = (MedicationDispenseSubstitutionComponent) value; // MedicationDispenseSubstitutionComponent
1879        else
1880          super.setProperty(name, value);
1881      }
1882
1883      @Override
1884      public Base addChild(String name) throws FHIRException {
1885        if (name.equals("identifier")) {
1886          this.identifier = new Identifier();
1887          return this.identifier;
1888        }
1889        else if (name.equals("status")) {
1890          throw new FHIRException("Cannot call addChild on a primitive type MedicationDispense.status");
1891        }
1892        else if (name.equals("patient")) {
1893          this.patient = new Reference();
1894          return this.patient;
1895        }
1896        else if (name.equals("dispenser")) {
1897          this.dispenser = new Reference();
1898          return this.dispenser;
1899        }
1900        else if (name.equals("authorizingPrescription")) {
1901          return addAuthorizingPrescription();
1902        }
1903        else if (name.equals("type")) {
1904          this.type = new CodeableConcept();
1905          return this.type;
1906        }
1907        else if (name.equals("quantity")) {
1908          this.quantity = new SimpleQuantity();
1909          return this.quantity;
1910        }
1911        else if (name.equals("daysSupply")) {
1912          this.daysSupply = new SimpleQuantity();
1913          return this.daysSupply;
1914        }
1915        else if (name.equals("medicationCodeableConcept")) {
1916          this.medication = new CodeableConcept();
1917          return this.medication;
1918        }
1919        else if (name.equals("medicationReference")) {
1920          this.medication = new Reference();
1921          return this.medication;
1922        }
1923        else if (name.equals("whenPrepared")) {
1924          throw new FHIRException("Cannot call addChild on a primitive type MedicationDispense.whenPrepared");
1925        }
1926        else if (name.equals("whenHandedOver")) {
1927          throw new FHIRException("Cannot call addChild on a primitive type MedicationDispense.whenHandedOver");
1928        }
1929        else if (name.equals("destination")) {
1930          this.destination = new Reference();
1931          return this.destination;
1932        }
1933        else if (name.equals("receiver")) {
1934          return addReceiver();
1935        }
1936        else if (name.equals("note")) {
1937          throw new FHIRException("Cannot call addChild on a primitive type MedicationDispense.note");
1938        }
1939        else if (name.equals("dosageInstruction")) {
1940          return addDosageInstruction();
1941        }
1942        else if (name.equals("substitution")) {
1943          this.substitution = new MedicationDispenseSubstitutionComponent();
1944          return this.substitution;
1945        }
1946        else
1947          return super.addChild(name);
1948      }
1949
1950  public String fhirType() {
1951    return "MedicationDispense";
1952
1953  }
1954
1955      public MedicationDispense copy() {
1956        MedicationDispense dst = new MedicationDispense();
1957        copyValues(dst);
1958        dst.identifier = identifier == null ? null : identifier.copy();
1959        dst.status = status == null ? null : status.copy();
1960        dst.patient = patient == null ? null : patient.copy();
1961        dst.dispenser = dispenser == null ? null : dispenser.copy();
1962        if (authorizingPrescription != null) {
1963          dst.authorizingPrescription = new ArrayList<Reference>();
1964          for (Reference i : authorizingPrescription)
1965            dst.authorizingPrescription.add(i.copy());
1966        };
1967        dst.type = type == null ? null : type.copy();
1968        dst.quantity = quantity == null ? null : quantity.copy();
1969        dst.daysSupply = daysSupply == null ? null : daysSupply.copy();
1970        dst.medication = medication == null ? null : medication.copy();
1971        dst.whenPrepared = whenPrepared == null ? null : whenPrepared.copy();
1972        dst.whenHandedOver = whenHandedOver == null ? null : whenHandedOver.copy();
1973        dst.destination = destination == null ? null : destination.copy();
1974        if (receiver != null) {
1975          dst.receiver = new ArrayList<Reference>();
1976          for (Reference i : receiver)
1977            dst.receiver.add(i.copy());
1978        };
1979        dst.note = note == null ? null : note.copy();
1980        if (dosageInstruction != null) {
1981          dst.dosageInstruction = new ArrayList<MedicationDispenseDosageInstructionComponent>();
1982          for (MedicationDispenseDosageInstructionComponent i : dosageInstruction)
1983            dst.dosageInstruction.add(i.copy());
1984        };
1985        dst.substitution = substitution == null ? null : substitution.copy();
1986        return dst;
1987      }
1988
1989      protected MedicationDispense typedCopy() {
1990        return copy();
1991      }
1992
1993      @Override
1994      public boolean equalsDeep(Base other) {
1995        if (!super.equalsDeep(other))
1996          return false;
1997        if (!(other instanceof MedicationDispense))
1998          return false;
1999        MedicationDispense o = (MedicationDispense) other;
2000        return compareDeep(identifier, o.identifier, true) && compareDeep(status, o.status, true) && compareDeep(patient, o.patient, true)
2001           && compareDeep(dispenser, o.dispenser, true) && compareDeep(authorizingPrescription, o.authorizingPrescription, true)
2002           && compareDeep(type, o.type, true) && compareDeep(quantity, o.quantity, true) && compareDeep(daysSupply, o.daysSupply, true)
2003           && compareDeep(medication, o.medication, true) && compareDeep(whenPrepared, o.whenPrepared, true)
2004           && compareDeep(whenHandedOver, o.whenHandedOver, true) && compareDeep(destination, o.destination, true)
2005           && compareDeep(receiver, o.receiver, true) && compareDeep(note, o.note, true) && compareDeep(dosageInstruction, o.dosageInstruction, true)
2006           && compareDeep(substitution, o.substitution, true);
2007      }
2008
2009      @Override
2010      public boolean equalsShallow(Base other) {
2011        if (!super.equalsShallow(other))
2012          return false;
2013        if (!(other instanceof MedicationDispense))
2014          return false;
2015        MedicationDispense o = (MedicationDispense) other;
2016        return compareValues(status, o.status, true) && compareValues(whenPrepared, o.whenPrepared, true) && compareValues(whenHandedOver, o.whenHandedOver, true)
2017           && compareValues(note, o.note, true);
2018      }
2019
2020      public boolean isEmpty() {
2021        return super.isEmpty() && (identifier == null || identifier.isEmpty()) && (status == null || status.isEmpty())
2022           && (patient == null || patient.isEmpty()) && (dispenser == null || dispenser.isEmpty()) && (authorizingPrescription == null || authorizingPrescription.isEmpty())
2023           && (type == null || type.isEmpty()) && (quantity == null || quantity.isEmpty()) && (daysSupply == null || daysSupply.isEmpty())
2024           && (medication == null || medication.isEmpty()) && (whenPrepared == null || whenPrepared.isEmpty())
2025           && (whenHandedOver == null || whenHandedOver.isEmpty()) && (destination == null || destination.isEmpty())
2026           && (receiver == null || receiver.isEmpty()) && (note == null || note.isEmpty()) && (dosageInstruction == null || dosageInstruction.isEmpty())
2027           && (substitution == null || substitution.isEmpty());
2028      }
2029
2030  @Override
2031  public ResourceType getResourceType() {
2032    return ResourceType.MedicationDispense;
2033   }
2034
2035  @SearchParamDefinition(name="identifier", path="MedicationDispense.identifier", description="Return dispenses with this external identifier", type="token" )
2036  public static final String SP_IDENTIFIER = "identifier";
2037  @SearchParamDefinition(name="code", path="MedicationDispense.medicationCodeableConcept", description="Return dispenses of this medicine code", type="token" )
2038  public static final String SP_CODE = "code";
2039  @SearchParamDefinition(name="receiver", path="MedicationDispense.receiver", description="Who collected the medication", type="reference" )
2040  public static final String SP_RECEIVER = "receiver";
2041  @SearchParamDefinition(name="destination", path="MedicationDispense.destination", description="Return dispenses that should be sent to a specific destination", type="reference" )
2042  public static final String SP_DESTINATION = "destination";
2043  @SearchParamDefinition(name="medication", path="MedicationDispense.medicationReference", description="Return dispenses of this medicine resource", type="reference" )
2044  public static final String SP_MEDICATION = "medication";
2045  @SearchParamDefinition(name="responsibleparty", path="MedicationDispense.substitution.responsibleParty", description="Return all dispenses with the specified responsible party", type="reference" )
2046  public static final String SP_RESPONSIBLEPARTY = "responsibleparty";
2047  @SearchParamDefinition(name="type", path="MedicationDispense.type", description="Return all dispenses of a specific type", type="token" )
2048  public static final String SP_TYPE = "type";
2049  @SearchParamDefinition(name="whenhandedover", path="MedicationDispense.whenHandedOver", description="Date when medication handed over to patient (outpatient setting), or supplied to ward or clinic (inpatient setting)", type="date" )
2050  public static final String SP_WHENHANDEDOVER = "whenhandedover";
2051  @SearchParamDefinition(name="whenprepared", path="MedicationDispense.whenPrepared", description="Date when medication prepared", type="date" )
2052  public static final String SP_WHENPREPARED = "whenprepared";
2053  @SearchParamDefinition(name="dispenser", path="MedicationDispense.dispenser", description="Return all dispenses performed by a specific individual", type="reference" )
2054  public static final String SP_DISPENSER = "dispenser";
2055  @SearchParamDefinition(name="prescription", path="MedicationDispense.authorizingPrescription", description="The identity of a prescription to list dispenses from", type="reference" )
2056  public static final String SP_PRESCRIPTION = "prescription";
2057  @SearchParamDefinition(name="patient", path="MedicationDispense.patient", description="The identity of a patient to list dispenses  for", type="reference" )
2058  public static final String SP_PATIENT = "patient";
2059  @SearchParamDefinition(name="status", path="MedicationDispense.status", description="Status of the dispense", type="token" )
2060  public static final String SP_STATUS = "status";
2061
2062}
2063