internal static MedicationStatement ToFhirInternal(HVMedication hvMedication, MedicationStatement medicationStatement)
        {
            var embeddedMedication = new FhirMedication();

            embeddedMedication.Id = "med" + Guid.NewGuid();

            medicationStatement.Contained.Add(ToFhirInternal(hvMedication, embeddedMedication));
            medicationStatement.Medication = embeddedMedication.GetContainerReference();

            medicationStatement.SetStatusAsActive();
            medicationStatement.SetTakenAsNotApplicable();

            medicationStatement.Dosage = AddDosage(hvMedication.Dose, hvMedication.Frequency, hvMedication.Route);

            if (hvMedication.Prescription != null)
            {
                var embeddedMedicationRequest = new MedicationRequest();
                embeddedMedicationRequest.Id = "medReq" + Guid.NewGuid();

                Practitioner prescribedBy = hvMedication.Prescription.PrescribedBy.ToFhir();
                prescribedBy.Id = "prac" + Guid.NewGuid();
                medicationStatement.Contained.Add(prescribedBy);

                MedicationRequest request = ToFhirInternal(hvMedication.Prescription, embeddedMedicationRequest);
                request.Medication = embeddedMedication.GetContainerReference();
                request.Requester  = new MedicationRequest.RequesterComponent
                {
                    Agent = prescribedBy.GetContainerReference()
                };
                medicationStatement.Contained.Add(request);
                medicationStatement.BasedOn.Add(embeddedMedicationRequest.GetContainerReference());
            }

            return(medicationStatement);
        }
Example #2
0
        public void WhenMedicationTransformedToHealthVault_ThenGenericNameIsCopiedFromExtension()
        {
            const string ingredientName = "Capecitabine (substance)";
            var          fhirMedication = new FhirMedication()
            {
                Code = new CodeableConcept()
                {
                    Text = "Capecitabine 500mg oral tablet (Xeloda)"
                },
                Extension = new System.Collections.Generic.List <Extension>
                {
                    new Extension
                    {
                        Url       = HealthVaultExtensions.Medication,
                        Extension = new System.Collections.Generic.List <Extension>
                        {
                            new Extension
                            {
                                Url   = HealthVaultExtensions.MedicationGenericName,
                                Value = new CodeableConcept(system: "http://snomed.info/sct",
                                                            code: "386906001",
                                                            display: null,
                                                            text: ingredientName)
                            }
                        }
                    }
                }
            };

            var hvMedication = fhirMedication.ToHealthVault() as HVMedication;

            Assert.AreEqual(ingredientName, hvMedication.GenericName?.Text);
        }
        internal static FhirMedication ToFhirInternal(HVMedication hvMedication, FhirMedication fhirMedication)
        {
            fhirMedication.Code = hvMedication.Name.ToFhir();

            if (hvMedication.GenericName != null || hvMedication.Strength != null)
            {
                var medicationExtension = new Extension {
                    Url = HealthVaultExtensions.Medication
                };

                if (hvMedication.GenericName != null)
                {
                    var genericName = hvMedication.GenericName?.ToFhir();

                    medicationExtension.AddExtension(HealthVaultExtensions.MedicationGenericName,
                                                     genericName);
                }

                if (hvMedication.Strength != null)
                {
                    var strengthExtension = new Extension {
                        Url = HealthVaultExtensions.MedicationStrength
                    };

                    strengthExtension.AddExtension(HealthVaultExtensions.MedicationStrengthDisplay,
                                                   new FhirString(hvMedication.Strength.Display));

                    foreach (var structuredMeasurement in hvMedication.Strength.Structured)
                    {
                        var simpleQuantity = new SimpleQuantity()
                        {
                            Value = new decimal(structuredMeasurement.Value),
                            Unit  = structuredMeasurement.Units.Text
                        };

                        if (structuredMeasurement.Units.Any())
                        {
                            CodedValue measurementUnit = structuredMeasurement.Units.First();

                            simpleQuantity.Code   = measurementUnit.Value;
                            simpleQuantity.System = HealthVaultVocabularies.GenerateSystemUrl(measurementUnit.VocabularyName, measurementUnit.Family);
                        }

                        strengthExtension.AddExtension(HealthVaultExtensions.MedicationStrengthQuantity, simpleQuantity);
                    }

                    medicationExtension.Extension.Add(strengthExtension);
                }

                fhirMedication.Extension.Add(medicationExtension);
            }

            return(fhirMedication);
        }
Example #4
0
        public void WhenMedicationTransformedToHealthVault_ThenNameIsCopiedFromCode()
        {
            const string medicationName = "Amoxicillin 250mg/5ml Suspension";
            var          fhirMedication = new FhirMedication()
            {
                Code = new CodeableConcept()
                {
                    Text = medicationName
                }
            };

            var hvMedication = fhirMedication.ToHealthVault() as HVMedication;

            Assert.AreEqual(medicationName, hvMedication.Name?.Text);
        }
Example #5
0
        public static ThingBase ToHealthVault(this FhirMedication fhirMedication)
        {
            var hvMedication = new HVMedication();

            var name = fhirMedication.Code.ToCodableValue();

            if (name == null)
            {
                throw new NotSupportedException($"{nameof(FhirMedication)} should" +
                                                $" have {nameof(fhirMedication.Code)}");
            }
            hvMedication.Name = name;

            var medicationExtension = fhirMedication.GetExtension(HealthVaultExtensions.Medication);

            if (medicationExtension != null)
            {
                var genericName = medicationExtension
                                  .GetExtensionValue <CodeableConcept>(HealthVaultExtensions.MedicationGenericName);
                hvMedication.GenericName = genericName?.ToCodableValue();

                var strengthExtension = medicationExtension.GetExtension(HealthVaultExtensions.MedicationStrength);
                if (strengthExtension != null)
                {
                    string display  = strengthExtension.GetStringExtension(HealthVaultExtensions.MedicationStrengthDisplay);
                    var    strength = new GeneralMeasurement(display);
                    foreach (var quantityExtension
                             in strengthExtension.GetExtensions(HealthVaultExtensions.MedicationStrengthQuantity))
                    {
                        var quantity = quantityExtension.Value as Quantity;
                        if (quantity == null)
                        {
                            continue;
                        }
                        strength.Structured.Add(new StructuredMeasurement
                        {
                            Value = (double)quantity.Value,
                            Units = CodeToHealthVaultHelper.CreateCodableValueFromQuantityValues(
                                quantity.System, quantity.Code, quantity.Unit)
                        });
                    }
                    hvMedication.Strength = strength;
                }
            }

            return(hvMedication);
        }
Example #6
0
        public void WhenMedicationTransformedToHealthVault_ThenStrengthIsCopiedFromExtension()
        {
            const int    ingredientAmount  = 500;
            const string ingredientDisplay = "500mg";
            var          fhirMedication    = new FhirMedication()
            {
                Code = new CodeableConcept()
                {
                    Text = "Capecitabine 500mg oral tablet (Xeloda)"
                },
                Extension = new System.Collections.Generic.List <Extension>
                {
                    new Extension
                    {
                        Url       = HealthVaultExtensions.Medication,
                        Extension = new System.Collections.Generic.List <Extension>
                        {
                            new Extension
                            {
                                Url       = HealthVaultExtensions.MedicationStrength,
                                Extension = new System.Collections.Generic.List <Extension>
                                {
                                    new Extension
                                    {
                                        Url   = HealthVaultExtensions.MedicationStrengthDisplay,
                                        Value = new FhirString(ingredientDisplay)
                                    },
                                    new Extension
                                    {
                                        Url   = HealthVaultExtensions.MedicationStrengthQuantity,
                                        Value = new Quantity(ingredientAmount, "mg")
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var hvMedication = fhirMedication.ToHealthVault() as HVMedication;

            Assert.AreEqual(ingredientDisplay, hvMedication.Strength?.Display);
            Assert.AreEqual(ingredientAmount, hvMedication.Strength?.Structured?.First()?.Value);
        }
Example #7
0
        public override ErrorList Validate()
        {
            var result = new ErrorList();

            result.AddRange(base.Validate());

            if (Identifier != null)
            {
                Identifier.ForEach(elem => result.AddRange(elem.Validate()));
            }
            if (Patient != null)
            {
                result.AddRange(Patient.Validate());
            }
            if (WasNotGivenElement != null)
            {
                result.AddRange(WasNotGivenElement.Validate());
            }
            if (ReasonNotGiven != null)
            {
                ReasonNotGiven.ForEach(elem => result.AddRange(elem.Validate()));
            }
            if (WhenGiven != null)
            {
                result.AddRange(WhenGiven.Validate());
            }
            if (Medication != null)
            {
                result.AddRange(Medication.Validate());
            }
            if (AdministrationDevice != null)
            {
                AdministrationDevice.ForEach(elem => result.AddRange(elem.Validate()));
            }
            if (Dosage != null)
            {
                Dosage.ForEach(elem => result.AddRange(elem.Validate()));
            }

            return(result);
        }
Example #8
0
        public override IDeepCopyable CopyTo(IDeepCopyable other)
        {
            var dest = other as MedicationOrder;

            if (dest != null)
            {
                base.CopyTo(dest);
                if (Identifier != null)
                {
                    dest.Identifier = new List <Hl7.Fhir.Model.Identifier>(Identifier.DeepCopy());
                }
                if (DateWrittenElement != null)
                {
                    dest.DateWrittenElement = (Hl7.Fhir.Model.FhirDateTime)DateWrittenElement.DeepCopy();
                }
                if (StatusElement != null)
                {
                    dest.StatusElement = (Code <Hl7.Fhir.Model.MedicationOrder.MedicationOrderStatus>)StatusElement.DeepCopy();
                }
                if (DateEndedElement != null)
                {
                    dest.DateEndedElement = (Hl7.Fhir.Model.FhirDateTime)DateEndedElement.DeepCopy();
                }
                if (ReasonEnded != null)
                {
                    dest.ReasonEnded = (Hl7.Fhir.Model.CodeableConcept)ReasonEnded.DeepCopy();
                }
                if (Patient != null)
                {
                    dest.Patient = (Hl7.Fhir.Model.ResourceReference)Patient.DeepCopy();
                }
                if (Prescriber != null)
                {
                    dest.Prescriber = (Hl7.Fhir.Model.ResourceReference)Prescriber.DeepCopy();
                }
                if (Encounter != null)
                {
                    dest.Encounter = (Hl7.Fhir.Model.ResourceReference)Encounter.DeepCopy();
                }
                if (Reason != null)
                {
                    dest.Reason = (Hl7.Fhir.Model.Element)Reason.DeepCopy();
                }
                if (NoteElement != null)
                {
                    dest.NoteElement = (Hl7.Fhir.Model.FhirString)NoteElement.DeepCopy();
                }
                if (Medication != null)
                {
                    dest.Medication = (Hl7.Fhir.Model.Element)Medication.DeepCopy();
                }
                if (DosageInstruction != null)
                {
                    dest.DosageInstruction = new List <Hl7.Fhir.Model.MedicationOrder.MedicationOrderDosageInstructionComponent>(DosageInstruction.DeepCopy());
                }
                if (DispenseRequest != null)
                {
                    dest.DispenseRequest = (Hl7.Fhir.Model.MedicationOrder.MedicationOrderDispenseRequestComponent)DispenseRequest.DeepCopy();
                }
                if (Substitution != null)
                {
                    dest.Substitution = (Hl7.Fhir.Model.MedicationOrder.MedicationOrderSubstitutionComponent)Substitution.DeepCopy();
                }
                if (PriorPrescription != null)
                {
                    dest.PriorPrescription = (Hl7.Fhir.Model.ResourceReference)PriorPrescription.DeepCopy();
                }
                return(dest);
            }
            else
            {
                throw new ArgumentException("Can only copy to an object of the same type", "other");
            }
        }
        public override IDeepCopyable CopyTo(IDeepCopyable other)
        {
            var dest = other as MedicationAdministration;

            if (dest != null)
            {
                base.CopyTo(dest);
                if (Identifier != null)
                {
                    dest.Identifier = new List <Hl7.Fhir.Model.Identifier>(Identifier.DeepCopy());
                }
                if (StatusElement != null)
                {
                    dest.StatusElement = (Code <Hl7.Fhir.Model.MedicationAdministrationStatus>)StatusElement.DeepCopy();
                }
                if (Patient != null)
                {
                    dest.Patient = (Hl7.Fhir.Model.ResourceReference)Patient.DeepCopy();
                }
                if (Practitioner != null)
                {
                    dest.Practitioner = (Hl7.Fhir.Model.ResourceReference)Practitioner.DeepCopy();
                }
                if (Encounter != null)
                {
                    dest.Encounter = (Hl7.Fhir.Model.ResourceReference)Encounter.DeepCopy();
                }
                if (Prescription != null)
                {
                    dest.Prescription = (Hl7.Fhir.Model.ResourceReference)Prescription.DeepCopy();
                }
                if (WasNotGivenElement != null)
                {
                    dest.WasNotGivenElement = (Hl7.Fhir.Model.FhirBoolean)WasNotGivenElement.DeepCopy();
                }
                if (ReasonNotGiven != null)
                {
                    dest.ReasonNotGiven = new List <Hl7.Fhir.Model.CodeableConcept>(ReasonNotGiven.DeepCopy());
                }
                if (ReasonGiven != null)
                {
                    dest.ReasonGiven = new List <Hl7.Fhir.Model.CodeableConcept>(ReasonGiven.DeepCopy());
                }
                if (EffectiveTime != null)
                {
                    dest.EffectiveTime = (Hl7.Fhir.Model.Element)EffectiveTime.DeepCopy();
                }
                if (Medication != null)
                {
                    dest.Medication = (Hl7.Fhir.Model.Element)Medication.DeepCopy();
                }
                if (Device != null)
                {
                    dest.Device = new List <Hl7.Fhir.Model.ResourceReference>(Device.DeepCopy());
                }
                if (NoteElement != null)
                {
                    dest.NoteElement = (Hl7.Fhir.Model.FhirString)NoteElement.DeepCopy();
                }
                if (Dosage != null)
                {
                    dest.Dosage = (Hl7.Fhir.Model.MedicationAdministration.DosageComponent)Dosage.DeepCopy();
                }
                return(dest);
            }
            else
            {
                throw new ArgumentException("Can only copy to an object of the same type", "other");
            }
        }
        public override IDeepCopyable CopyTo(IDeepCopyable other)
        {
            var dest = other as MedicationDispense;

            if (dest != null)
            {
                base.CopyTo(dest);
                if (Identifier != null)
                {
                    dest.Identifier = (Hl7.Fhir.Model.Identifier)Identifier.DeepCopy();
                }
                if (StatusElement != null)
                {
                    dest.StatusElement = (Code <Hl7.Fhir.Model.MedicationDispense.MedicationDispenseStatus>)StatusElement.DeepCopy();
                }
                if (Patient != null)
                {
                    dest.Patient = (Hl7.Fhir.Model.ResourceReference)Patient.DeepCopy();
                }
                if (Dispenser != null)
                {
                    dest.Dispenser = (Hl7.Fhir.Model.ResourceReference)Dispenser.DeepCopy();
                }
                if (AuthorizingPrescription != null)
                {
                    dest.AuthorizingPrescription = new List <Hl7.Fhir.Model.ResourceReference>(AuthorizingPrescription.DeepCopy());
                }
                if (Type != null)
                {
                    dest.Type = (Hl7.Fhir.Model.CodeableConcept)Type.DeepCopy();
                }
                if (Quantity != null)
                {
                    dest.Quantity = (Hl7.Fhir.Model.Quantity)Quantity.DeepCopy();
                }
                if (DaysSupply != null)
                {
                    dest.DaysSupply = (Hl7.Fhir.Model.Quantity)DaysSupply.DeepCopy();
                }
                if (Medication != null)
                {
                    dest.Medication = (Hl7.Fhir.Model.ResourceReference)Medication.DeepCopy();
                }
                if (WhenPreparedElement != null)
                {
                    dest.WhenPreparedElement = (Hl7.Fhir.Model.FhirDateTime)WhenPreparedElement.DeepCopy();
                }
                if (WhenHandedOverElement != null)
                {
                    dest.WhenHandedOverElement = (Hl7.Fhir.Model.FhirDateTime)WhenHandedOverElement.DeepCopy();
                }
                if (Destination != null)
                {
                    dest.Destination = (Hl7.Fhir.Model.ResourceReference)Destination.DeepCopy();
                }
                if (Receiver != null)
                {
                    dest.Receiver = new List <Hl7.Fhir.Model.ResourceReference>(Receiver.DeepCopy());
                }
                if (NoteElement != null)
                {
                    dest.NoteElement = (Hl7.Fhir.Model.FhirString)NoteElement.DeepCopy();
                }
                if (DosageInstruction != null)
                {
                    dest.DosageInstruction = new List <Hl7.Fhir.Model.MedicationDispense.MedicationDispenseDosageInstructionComponent>(DosageInstruction.DeepCopy());
                }
                if (Substitution != null)
                {
                    dest.Substitution = (Hl7.Fhir.Model.MedicationDispense.MedicationDispenseSubstitutionComponent)Substitution.DeepCopy();
                }
                return(dest);
            }
            else
            {
                throw new ArgumentException("Can only copy to an object of the same type", "other");
            }
        }
        public override IDeepCopyable CopyTo(IDeepCopyable other)
        {
            var dest = other as MedicationStatement;

            if (dest != null)
            {
                base.CopyTo(dest);
                if (Identifier != null)
                {
                    dest.Identifier = new List <Hl7.Fhir.Model.Identifier>(Identifier.DeepCopy());
                }
                if (Patient != null)
                {
                    dest.Patient = (Hl7.Fhir.Model.ResourceReference)Patient.DeepCopy();
                }
                if (InformationSource != null)
                {
                    dest.InformationSource = (Hl7.Fhir.Model.ResourceReference)InformationSource.DeepCopy();
                }
                if (DateAssertedElement != null)
                {
                    dest.DateAssertedElement = (Hl7.Fhir.Model.FhirDateTime)DateAssertedElement.DeepCopy();
                }
                if (StatusElement != null)
                {
                    dest.StatusElement = (Code <Hl7.Fhir.Model.MedicationStatement.MedicationStatementStatus>)StatusElement.DeepCopy();
                }
                if (WasNotTakenElement != null)
                {
                    dest.WasNotTakenElement = (Hl7.Fhir.Model.FhirBoolean)WasNotTakenElement.DeepCopy();
                }
                if (ReasonNotTaken != null)
                {
                    dest.ReasonNotTaken = new List <Hl7.Fhir.Model.CodeableConcept>(ReasonNotTaken.DeepCopy());
                }
                if (ReasonForUse != null)
                {
                    dest.ReasonForUse = (Hl7.Fhir.Model.Element)ReasonForUse.DeepCopy();
                }
                if (Effective != null)
                {
                    dest.Effective = (Hl7.Fhir.Model.Element)Effective.DeepCopy();
                }
                if (NoteElement != null)
                {
                    dest.NoteElement = (Hl7.Fhir.Model.FhirString)NoteElement.DeepCopy();
                }
                if (SupportingInformation != null)
                {
                    dest.SupportingInformation = new List <Hl7.Fhir.Model.ResourceReference>(SupportingInformation.DeepCopy());
                }
                if (Medication != null)
                {
                    dest.Medication = (Hl7.Fhir.Model.Element)Medication.DeepCopy();
                }
                if (Dosage != null)
                {
                    dest.Dosage = new List <Hl7.Fhir.Model.MedicationStatement.DosageComponent>(Dosage.DeepCopy());
                }
                return(dest);
            }
            else
            {
                throw new ArgumentException("Can only copy to an object of the same type", "other");
            }
        }
        public override IDeepCopyable CopyTo(IDeepCopyable other)
        {
            var dest = other as MedicationStatement;

            if (dest == null)
            {
                throw new ArgumentException("Can only copy to an object of the same type", "other");
            }

            base.CopyTo(dest);
            if (Identifier != null)
            {
                dest.Identifier = new List <Hl7.Fhir.Model.Identifier>(Identifier.DeepCopy());
            }
            if (BasedOn != null)
            {
                dest.BasedOn = new List <Hl7.Fhir.Model.ResourceReference>(BasedOn.DeepCopy());
            }
            if (PartOf != null)
            {
                dest.PartOf = new List <Hl7.Fhir.Model.ResourceReference>(PartOf.DeepCopy());
            }
            if (StatusElement != null)
            {
                dest.StatusElement = (Code <Hl7.Fhir.Model.MedicationStatement.MedicationStatusCodes>)StatusElement.DeepCopy();
            }
            if (StatusReason != null)
            {
                dest.StatusReason = new List <Hl7.Fhir.Model.CodeableConcept>(StatusReason.DeepCopy());
            }
            if (Category != null)
            {
                dest.Category = (Hl7.Fhir.Model.CodeableConcept)Category.DeepCopy();
            }
            if (Medication != null)
            {
                dest.Medication = (Hl7.Fhir.Model.Element)Medication.DeepCopy();
            }
            if (Subject != null)
            {
                dest.Subject = (Hl7.Fhir.Model.ResourceReference)Subject.DeepCopy();
            }
            if (Context != null)
            {
                dest.Context = (Hl7.Fhir.Model.ResourceReference)Context.DeepCopy();
            }
            if (Effective != null)
            {
                dest.Effective = (Hl7.Fhir.Model.Element)Effective.DeepCopy();
            }
            if (DateAssertedElement != null)
            {
                dest.DateAssertedElement = (Hl7.Fhir.Model.FhirDateTime)DateAssertedElement.DeepCopy();
            }
            if (InformationSource != null)
            {
                dest.InformationSource = (Hl7.Fhir.Model.ResourceReference)InformationSource.DeepCopy();
            }
            if (DerivedFrom != null)
            {
                dest.DerivedFrom = new List <Hl7.Fhir.Model.ResourceReference>(DerivedFrom.DeepCopy());
            }
            if (ReasonCode != null)
            {
                dest.ReasonCode = new List <Hl7.Fhir.Model.CodeableConcept>(ReasonCode.DeepCopy());
            }
            if (ReasonReference != null)
            {
                dest.ReasonReference = new List <Hl7.Fhir.Model.ResourceReference>(ReasonReference.DeepCopy());
            }
            if (Note != null)
            {
                dest.Note = new List <Hl7.Fhir.Model.Annotation>(Note.DeepCopy());
            }
            if (Dosage != null)
            {
                dest.Dosage = new List <Hl7.Fhir.Model.Dosage>(Dosage.DeepCopy());
            }
            return(dest);
        }
Example #13
0
        public static void AddMedicationOrder(string id, string medicationName)
        {
            using (var dbcontext = new ApplicationDbContext())
            {
                var user = dbcontext.Users.FirstOrDefault(a => a.Id == id);
                //todo: I'm not sure about the web portion with regards with what the view should return, I leave
                //      leave that to you, but I left logic to return the medication order ID if you desire. To
                //      show whether the post was successful

                //todo: I do not know where the medicationName will be pulled from, so change harcoded "medicationName"
                //      to the parameter name you expect to use.

                //Full list of Parameters you may also decide to pass in:
                //patientID (done), medicationName, system, and display

                //First let us create the FHIR client
                var fhirClient = new FhirClient(Constants.HapiFhirServerBase);

                //First we need to create our medication
                var medication = new Medication
                {
                    Code = new CodeableConcept("ICD-10", medicationName)
                };

                //Now we need to push this to the server and grab the ID
                var medicationResource = fhirClient.Create<Hl7.Fhir.Model.Medication>(medication);
                var medicationResourceID = medicationResource.Id;

                //Create an empty medication order resource and then assign attributes
                var fhirMedicationOrder = new Hl7.Fhir.Model.MedicationOrder();

                //There is no API for "Reference" in MedicationOrder model, unlike Patient model.
                //You must initialize ResourceReference inline.
                fhirMedicationOrder.Medication = new ResourceReference()
                {
                    Reference = fhirClient.Endpoint.OriginalString + "Medication/" + medicationResourceID,
                    Display = "EhrgoHealth"
                };

                //Now associate Medication Order to a Patient
                fhirMedicationOrder.Patient = new ResourceReference();
                fhirMedicationOrder.Patient.Reference = "Patient/" + user.FhirPatientId;

                //Push the local patient resource to the FHIR Server and expect a newly assigned ID
                var medicationOrderResource = fhirClient.Create<Hl7.Fhir.Model.MedicationOrder>(fhirMedicationOrder);
            }
        }
        public static void SerializeMedication(Hl7.Fhir.Model.Medication value, IFhirWriter writer, bool summary)
        {
            writer.WriteStartRootObject("Medication");
            writer.WriteStartComplexContent();

            // Serialize element _id
            if (value.LocalIdElement != null)
            {
                writer.WritePrimitiveContents("_id", value.LocalIdElement, XmlSerializationHint.Attribute);
            }

            // Serialize element extension
            if (value.Extension != null && !summary && value.Extension.Count > 0)
            {
                writer.WriteStartArrayElement("extension");
                foreach (var item in value.Extension)
                {
                    writer.WriteStartArrayMember("extension");
                    ExtensionSerializer.SerializeExtension(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }

            // Serialize element language
            if (value.LanguageElement != null && !summary)
            {
                writer.WriteStartElement("language");
                CodeSerializer.SerializeCode(value.LanguageElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element text
            if (value.Text != null && !summary)
            {
                writer.WriteStartElement("text");
                NarrativeSerializer.SerializeNarrative(value.Text, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element contained
            if (value.Contained != null && !summary && value.Contained.Count > 0)
            {
                writer.WriteStartArrayElement("contained");
                foreach (var item in value.Contained)
                {
                    writer.WriteStartArrayMember("contained");
                    FhirSerializer.SerializeResource(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }

            // Serialize element name
            if (value.NameElement != null)
            {
                writer.WriteStartElement("name");
                FhirStringSerializer.SerializeFhirString(value.NameElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element code
            if (value.Code != null)
            {
                writer.WriteStartElement("code");
                CodeableConceptSerializer.SerializeCodeableConcept(value.Code, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element isBrand
            if (value.IsBrandElement != null)
            {
                writer.WriteStartElement("isBrand");
                FhirBooleanSerializer.SerializeFhirBoolean(value.IsBrandElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element manufacturer
            if (value.Manufacturer != null)
            {
                writer.WriteStartElement("manufacturer");
                ResourceReferenceSerializer.SerializeResourceReference(value.Manufacturer, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element kind
            if (value.KindElement != null)
            {
                writer.WriteStartElement("kind");
                CodeSerializer.SerializeCode <Hl7.Fhir.Model.Medication.MedicationKind>(value.KindElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element product
            if (value.Product != null && !summary)
            {
                writer.WriteStartElement("product");
                MedicationSerializer.SerializeMedicationProductComponent(value.Product, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element package
            if (value.Package != null && !summary)
            {
                writer.WriteStartElement("package");
                MedicationSerializer.SerializeMedicationPackageComponent(value.Package, writer, summary);
                writer.WriteEndElement();
            }


            writer.WriteEndComplexContent();
            writer.WriteEndRootObject();
        }
        public void WhenMedicationStatementTransformedToHealthVault_ThenDosageIsCopiedToDoseFrequencyAndRoute()
        {
            var embeddedMedication = new FhirMedication()
            {
                Code = new CodeableConcept()
                {
                    Text = "Amoxicillin 250mg/5ml Suspension"
                }
            };
            string embeddedMedicationId = "med" + Guid.NewGuid();

            embeddedMedication.Id = embeddedMedicationId;

            const int    dosage              = 1;
            const int    period              = 8;
            const string routeCode           = "po";
            var          medicationStatement = new MedicationStatement()
            {
                Medication = new ResourceReference(embeddedMedicationId),
                Dosage     = new System.Collections.Generic.List <Dosage>
                {
                    new Dosage
                    {
                        Dose = new SimpleQuantity()
                        {
                            Value  = dosage,
                            Unit   = "Tablets",
                            System = HealthVaultVocabularies.GenerateSystemUrl
                                         (HealthVaultVocabularies.MedicationDoseUnits,
                                         HealthVaultVocabularies.Wc),
                            Code = "tablet"
                        },
                        Timing = new Timing
                        {
                            Repeat = new Timing.RepeatComponent
                            {
                                Period     = period,
                                PeriodUnit = Timing.UnitsOfTime.H
                            }
                        },
                        Route = new CodeableConcept
                        {
                            Text   = "By mouth",
                            Coding = new System.Collections.Generic.List <Coding>
                            {
                                new Coding
                                {
                                    Code   = routeCode,
                                    System = HealthVaultVocabularies.GenerateSystemUrl
                                                 (HealthVaultVocabularies.MedicationRoutes,
                                                 HealthVaultVocabularies.Wc),
                                    Version = "2"
                                }
                            }
                        }
                    }
                }
            };

            medicationStatement.Contained.Add(embeddedMedication);
            medicationStatement.Medication = embeddedMedication.GetContainerReference();

            var hvMedication = medicationStatement.ToHealthVault() as HVMedication;

            Assert.AreEqual(dosage, hvMedication?.Dose?.Structured?.First()?.Value);
            Assert.AreEqual(period, hvMedication?.Frequency?.Structured?.First()?.Value);
            Assert.AreEqual(routeCode, hvMedication?.Route?.First()?.Value);
        }
Example #16
0
        /// <summary>
        /// Parse Medication
        /// </summary>
        public static Hl7.Fhir.Model.Medication ParseMedication(IFhirReader reader, ErrorList errors, Hl7.Fhir.Model.Medication existingInstance = null)
        {
            Hl7.Fhir.Model.Medication result = existingInstance != null ? existingInstance : new Hl7.Fhir.Model.Medication();
            string currentElementName        = reader.CurrentElementName;

            reader.EnterElement();

            while (reader.HasMoreElements())
            {
                var atName = reader.CurrentElementName;
                // Parse element extension
                if (atName == "extension")
                {
                    result.Extension = new List <Hl7.Fhir.Model.Extension>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "extension"))
                    {
                        result.Extension.Add(ExtensionParser.ParseExtension(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element language
                else if (atName == "language")
                {
                    result.LanguageElement = CodeParser.ParseCode(reader, errors);
                }

                // Parse element text
                else if (atName == "text")
                {
                    result.Text = NarrativeParser.ParseNarrative(reader, errors);
                }

                // Parse element contained
                else if (atName == "contained")
                {
                    result.Contained = new List <Hl7.Fhir.Model.Resource>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "contained"))
                    {
                        result.Contained.Add(ParserUtils.ParseContainedResource(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element _id
                else if (atName == "_id")
                {
                    result.LocalIdElement = Id.Parse(reader.ReadPrimitiveContents(typeof(Id)));
                }

                // Parse element name
                else if (atName == "name")
                {
                    result.NameElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element code
                else if (atName == "code")
                {
                    result.Code = CodeableConceptParser.ParseCodeableConcept(reader, errors);
                }

                // Parse element isBrand
                else if (atName == "isBrand")
                {
                    result.IsBrandElement = FhirBooleanParser.ParseFhirBoolean(reader, errors);
                }

                // Parse element manufacturer
                else if (atName == "manufacturer")
                {
                    result.Manufacturer = ResourceReferenceParser.ParseResourceReference(reader, errors);
                }

                // Parse element kind
                else if (atName == "kind")
                {
                    result.KindElement = CodeParser.ParseCode <Hl7.Fhir.Model.Medication.MedicationKind>(reader, errors);
                }

                // Parse element product
                else if (atName == "product")
                {
                    result.Product = MedicationParser.ParseMedicationProductComponent(reader, errors);
                }

                // Parse element package
                else if (atName == "package")
                {
                    result.Package = MedicationParser.ParseMedicationPackageComponent(reader, errors);
                }

                else
                {
                    errors.Add(String.Format("Encountered unknown element {0} while parsing {1}", reader.CurrentElementName, currentElementName), reader);
                    reader.SkipSubElementsFor(currentElementName);
                    result = null;
                }
            }

            reader.LeaveElement();
            return(result);
        }