/// <summary>
        /// Maps the outbound resource to FHIR.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>Returns the mapped FHIR resource.</returns>
        protected override ImmunizationRecommendation MapToFhir(SubstanceAdministration model)
        {
            ImmunizationRecommendation retVal = new ImmunizationRecommendation();

            retVal.Id          = model.Key.ToString();
            retVal.DateElement = new FhirDateTime(DateTimeOffset.Now);
            retVal.Identifier  = model.Identifiers.Select(o => DataTypeConverter.ToFhirIdentifier(o)).ToList();

            var rct = model.LoadCollection <ActParticipation>(nameof(Act.Participations)).FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.RecordTarget)?.LoadProperty <Entity>(nameof(ActParticipation.PlayerEntity));

            if (rct != null)
            {
                retVal.Patient = DataTypeConverter.CreateNonVersionedReference <Patient>(rct);
            }

            var mat = model.Participations.FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.Product).PlayerEntity;

            // Recommend
            string status         = (model.StopTime ?? model.ActTime) < DateTimeOffset.Now ? "overdue" : "due";
            var    recommendation = new ImmunizationRecommendation.RecommendationComponent()
            {
                DoseNumber  = new PositiveInt(model.SequenceId),
                VaccineCode = new List <CodeableConcept>()
                {
                    DataTypeConverter.ToFhirCodeableConcept(mat?.TypeConceptKey)
                },
                ForecastStatus = new CodeableConcept("http://hl7.org/fhir/conceptset/immunization-recommendation-status", status),
                DateCriterion  = new List <ImmunizationRecommendation.DateCriterionComponent>()
                {
                    new ImmunizationRecommendation.DateCriterionComponent()
                    {
                        Code         = new CodeableConcept("http://hl7.org/fhir/conceptset/immunization-recommendation-date-criterion", "recommended"),
                        ValueElement = new FhirDateTime(model.ActTime)
                    }
                }
            };

            if (model.StartTime.HasValue)
            {
                recommendation.DateCriterion.Add(new ImmunizationRecommendation.DateCriterionComponent()
                {
                    Code         = new CodeableConcept("http://hl7.org/fhir/conceptset/immunization-recommendation-date-criterion", "earliest"),
                    ValueElement = new FhirDateTime(model.StartTime.Value)
                });
            }
            if (model.StopTime.HasValue)
            {
                recommendation.DateCriterion.Add(new ImmunizationRecommendation.DateCriterionComponent()
                {
                    Code         = new CodeableConcept("http://hl7.org/fhir/conceptset/immunization-recommendation-date-criterion", "overdue"),
                    ValueElement = new FhirDateTime(model.StopTime.Value)
                });
            }

            retVal.Recommendation = new List <ImmunizationRecommendation.RecommendationComponent>()
            {
                recommendation
            };
            return(retVal);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Map to FHIR
        /// </summary>
        protected override Hl7.Fhir.Model.Organization MapToFhir(Core.Model.Entities.Organization model)
        {
            var retVal = DataTypeConverter.CreateResource <Hl7.Fhir.Model.Organization>(model);

            retVal.Identifier = model.LoadCollection(o => o.Identifiers).Select(o => DataTypeConverter.ToFhirIdentifier(o)).ToList();
            retVal.Active     = StatusKeys.ActiveStates.Contains(model.StatusConceptKey.Value);
            retVal.Telecom    = model.LoadCollection(o => o.Telecoms).Select(DataTypeConverter.ToFhirTelecom).ToList();
            retVal.Address    = model.LoadCollection(o => o.Addresses).Select(DataTypeConverter.ToFhirAddress).ToList();
            retVal.Name       = model.LoadCollection(o => o.Names).FirstOrDefault(o => o.NameUseKey == NameUseKeys.OfficialRecord)?.ToDisplay();
            retVal.Alias      = model.LoadCollection(o => o.Names).Where(o => o.NameUseKey == NameUseKeys.Pseudonym).Select(o => o.ToDisplay());

            var parent = model.LoadCollection(o => o.Relationships).FirstOrDefault(o => o.RelationshipTypeKey == EntityRelationshipTypeKeys.Parent);

            if (parent != null)
            {
                retVal.PartOf = DataTypeConverter.CreateNonVersionedReference <Hl7.Fhir.Model.Organization>(parent.TargetEntityKey);
            }

            return(retVal);
        }
        /// <inheritdoc/>
        protected override AdverseEvent MapToFhir(Act model)
        {
            var retVal = DataTypeConverter.CreateResource <AdverseEvent>(model);

            retVal.Identifier = DataTypeConverter.ToFhirIdentifier(model.Identifiers.FirstOrDefault());
            retVal.Category   = new List <CodeableConcept>
            {
                DataTypeConverter.ToFhirCodeableConcept(model.TypeConceptKey)
            };

            var recordTarget = model.LoadCollection <ActParticipation>("Participations").FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.RecordTarget);

            if (recordTarget != null)
            {
                retVal.Subject = DataTypeConverter.CreateVersionedReference <Patient>(recordTarget.LoadProperty <Entity>("PlayerEntity"));
            }

            // Main topic of the concern
            var subject = model.LoadCollection <ActRelationship>("Relationships").FirstOrDefault(o => o.RelationshipTypeKey == ActRelationshipTypeKeys.HasSubject)?.LoadProperty <Act>("TargetAct");

            if (subject == null)
            {
                throw new InvalidOperationException(this.m_localizationService.GetString("error.messaging.fhir.adverseEvent.act"));
            }

            retVal.DateElement = new FhirDateTime(subject.ActTime);

            // Reactions = HasManifestation
            var reactions = subject.LoadCollection <ActRelationship>("Relationships").Where(o => o.RelationshipTypeKey == ActRelationshipTypeKeys.HasManifestation).FirstOrDefault();

            if (reactions != null)
            {
                retVal.Event = DataTypeConverter.ToFhirCodeableConcept(reactions.LoadProperty <CodedObservation>("TargetAct").ValueKey);
            }

            var location = model.LoadCollection <ActParticipation>("Participations").FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.Location);

            if (location != null)
            {
                retVal.Location = DataTypeConverter.CreateVersionedReference <Location>(location.LoadProperty <Entity>("PlayerEntity"));
            }

            // Severity
            var severity = subject.LoadCollection <ActRelationship>("Relationships").First(o => o.RelationshipTypeKey == ActRelationshipTypeKeys.HasComponent && o.LoadProperty <Act>("TargetAct").TypeConceptKey == ObservationTypeKeys.Severity);

            if (severity != null)
            {
                retVal.Severity = DataTypeConverter.ToFhirCodeableConcept(severity.LoadProperty <CodedObservation>("TargetAct").ValueKey, "http://terminology.hl7.org/CodeSystem/adverse-event-severity");
            }

            // Did the patient die?
            var causeOfDeath = model.LoadCollection <ActRelationship>("Relationships").FirstOrDefault(o => o.RelationshipTypeKey == ActRelationshipTypeKeys.IsCauseOf && o.LoadProperty <Act>("TargetAct").TypeConceptKey == ObservationTypeKeys.ClinicalState && (o.TargetAct as CodedObservation)?.ValueKey == Guid.Parse("6df3720b-857f-4ba2-826f-b7f1d3c3adbb"));

            if (causeOfDeath != null)
            {
                retVal.Outcome = new CodeableConcept("http://hl7.org/fhir/adverse-event-outcome", "fatal");
            }
            else if (model.StatusConceptKey == StatusKeys.Active)
            {
                retVal.Outcome = new CodeableConcept("http://hl7.org/fhir/adverse-event-outcome", "ongoing");
            }
            else if (model.StatusConceptKey == StatusKeys.Completed)
            {
                retVal.Outcome = new CodeableConcept("http://hl7.org/fhir/adverse-event-outcome", "resolved");
            }

            var author = model.LoadCollection <ActParticipation>("Participations").FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.Authororiginator);

            if (author != null)
            {
                retVal.Recorder = DataTypeConverter.CreateNonVersionedReference <Practitioner>(author.LoadProperty <Entity>("PlayerEntity"));
            }

            // Suspect entities
            var refersTo = model.LoadCollection <ActRelationship>("Relationships").Where(o => o.RelationshipTypeKey == ActRelationshipTypeKeys.RefersTo).ToArray();

            if (refersTo.Any())
            {
                retVal.SuspectEntity = refersTo.Select(o => o.LoadProperty <Act>("TargetAct")).OfType <SubstanceAdministration>().Select(o =>
                {
                    var consumable = o.LoadCollection <ActParticipation>("Participations").FirstOrDefault(x => x.ParticipationRoleKey == ActParticipationKey.Consumable)?.LoadProperty <ManufacturedMaterial>("PlayerEntity");
                    if (consumable == null)
                    {
                        var product = o.LoadCollection <ActParticipation>("Participations").FirstOrDefault(x => x.ParticipationRoleKey == ActParticipationKey.Product)?.LoadProperty <Material>("PlayerEntity");

                        return(new AdverseEvent.SuspectEntityComponent
                        {
                            Instance = DataTypeConverter.CreateNonVersionedReference <Substance>(product)
                        });
                    }

                    return(new AdverseEvent.SuspectEntityComponent
                    {
                        Instance = DataTypeConverter.CreateNonVersionedReference <Medication>(consumable)
                    });
                }).ToList();
            }

            return(retVal);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Map a patient object to FHIR.
        /// </summary>
        /// <param name="model">The patient to map to FHIR</param>
        /// <returns>Returns the mapped FHIR resource.</returns>
        protected override Patient MapToFhir(Core.Model.Roles.Patient model)
        {
            // If the model is being constructed as part of a bundle, then the caller
            // should have included the bundle so we can add any related resources
            var partOfBundle = model.GetAnnotations <Bundle>().FirstOrDefault();

            var retVal = DataTypeConverter.CreateResource <Patient>(model);

            retVal.Active  = StatusKeys.ActiveStates.Contains(model.StatusConceptKey.Value);
            retVal.Address = model.GetAddresses().Select(DataTypeConverter.ToFhirAddress).ToList();

            if (model.DateOfBirth.HasValue)
            {
                switch (model.DateOfBirthPrecision.GetValueOrDefault())
                {
                case DatePrecision.Day:
                    retVal.BirthDate = model.DateOfBirth.Value.ToString("yyyy-MM-dd");
                    break;

                case DatePrecision.Month:
                    retVal.BirthDate = model.DateOfBirth.Value.ToString("yyyy-MM");
                    break;

                case DatePrecision.Year:
                    retVal.BirthDate = model.DateOfBirth.Value.ToString("yyyy");
                    break;
                }
            }

            // Deceased precision
            if (model.DeceasedDate.HasValue)
            {
                if (model.DeceasedDate == DateTime.MinValue)
                {
                    retVal.Deceased = new FhirBoolean(true);
                }
                else
                {
                    switch (model.DeceasedDatePrecision)
                    {
                    case DatePrecision.Day:
                        retVal.Deceased = new FhirDateTime(model.DeceasedDate.Value.Year, model.DeceasedDate.Value.Month, model.DeceasedDate.Value.Day);
                        break;

                    case DatePrecision.Month:
                        retVal.Deceased = new FhirDateTime(model.DeceasedDate.Value.Year, model.DeceasedDate.Value.Month);
                        break;

                    case DatePrecision.Year:
                        retVal.Deceased = new FhirDateTime(model.DeceasedDate.Value.Year);
                        break;

                    default:
                        retVal.Deceased = DataTypeConverter.ToFhirDateTime(model.DeceasedDate);
                        break;
                    }
                }
            }

            if (model.GenderConceptKey.HasValue)
            {
                retVal.Gender = DataTypeConverter.ToFhirEnumeration <AdministrativeGender>(model.GenderConceptKey, "http://hl7.org/fhir/administrative-gender", true);
            }

            retVal.Identifier    = model.Identifiers?.Select(DataTypeConverter.ToFhirIdentifier).ToList();
            retVal.MultipleBirth = model.MultipleBirthOrder == 0 ? (DataType) new FhirBoolean(true) : model.MultipleBirthOrder.HasValue ? new Integer(model.MultipleBirthOrder.Value) : null;
            retVal.Name          = model.GetNames().Select(DataTypeConverter.ToFhirHumanName).ToList();
            retVal.Telecom       = model.GetTelecoms().Select(DataTypeConverter.ToFhirTelecom).ToList();
            retVal.Communication = model.GetPersonLanguages().Select(DataTypeConverter.ToFhirCommunicationComponent).ToList();

            foreach (var rel in model.GetRelationships().Where(o => !o.InversionIndicator))
            {
                if (rel.RelationshipTypeKey == EntityRelationshipTypeKeys.Contact)
                {
                    var relEntity = rel.LoadProperty(o => o.TargetEntity);

                    if (relEntity is Core.Model.Entities.Person person)
                    {
                        var contact = new Patient.ContactComponent()
                        {
                            ElementId    = $"{person.Key}",
                            Address      = DataTypeConverter.ToFhirAddress(person.GetAddresses().FirstOrDefault()),
                            Relationship = new List <CodeableConcept>()
                            {
                                DataTypeConverter.ToFhirCodeableConcept(rel.RelationshipRoleKey, "http://terminology.hl7.org/CodeSystem/v2-0131"),
                                DataTypeConverter.ToFhirCodeableConcept(rel.RelationshipTypeKey, "http://terminology.hl7.org/CodeSystem/v2-0131")
                            }.OfType <CodeableConcept>().ToList(),
                            Name = DataTypeConverter.ToFhirHumanName(person.GetNames().FirstOrDefault()),
                            // TODO: Gender
                            Gender  = DataTypeConverter.ToFhirEnumeration <AdministrativeGender>(person.GenderConceptKey, "http://hl7.org/fhir/administrative-gender"),
                            Telecom = person.GetTelecoms().Select(t => DataTypeConverter.ToFhirTelecom(t)).ToList()
                        };

                        var scoper = person.LoadCollection(o => o.Relationships).FirstOrDefault(o => o.RelationshipTypeKey == EntityRelationshipTypeKeys.Scoper);
                        if (scoper != null)
                        {
                            contact.Organization = DataTypeConverter.CreateNonVersionedReference <Hl7.Fhir.Model.Organization>(scoper.LoadProperty(o => o.TargetEntity));
                        }
                        DataTypeConverter.AddExtensions(person, contact);
                        retVal.Contact.Add(contact);
                    }
                    else if (relEntity is Core.Model.Entities.Organization org) // it *IS* an organization
                    {
                        var contact = new Patient.ContactComponent()
                        {
                            ElementId    = $"{org.Key}",
                            Relationship = new List <CodeableConcept>()
                            {
                                DataTypeConverter.ToFhirCodeableConcept(rel.RelationshipRoleKey, "http://terminology.hl7.org/CodeSystem/v2-0131"),
                                DataTypeConverter.ToFhirCodeableConcept(rel.RelationshipTypeKey, "http://terminology.hl7.org/CodeSystem/v2-0131")
                            }.OfType <CodeableConcept>().ToList(),
                            Organization = DataTypeConverter.CreateNonVersionedReference <Hl7.Fhir.Model.Organization>(org)
                        };
                        retVal.Contact.Add(contact);
                    }
                }
                else if (rel.RelationshipTypeKey == EntityRelationshipTypeKeys.Scoper)
                {
                    var scoper = rel.LoadProperty(o => o.TargetEntity);

                    retVal.ManagingOrganization = DataTypeConverter.CreateNonVersionedReference <Hl7.Fhir.Model.Organization>(scoper);

                    // If this is part of a bundle, include it
                    partOfBundle?.Entry.Add(new Bundle.EntryComponent
                    {
                        FullUrl  = $"{MessageUtil.GetBaseUri()}/Organization/{scoper.Key}",
                        Resource = FhirResourceHandlerUtil.GetMapperForInstance(scoper).MapToFhir(scoper)
                    });
                }
                else if (rel.RelationshipTypeKey == EntityRelationshipTypeKeys.HealthcareProvider)
                {
                    var practitioner = rel.LoadProperty(o => o.TargetEntity);

                    retVal.GeneralPractitioner.Add(DataTypeConverter.CreateVersionedReference <Practitioner>(practitioner));

                    // If this is part of a bundle, include it
                    partOfBundle?.Entry.Add(new Bundle.EntryComponent
                    {
                        FullUrl  = $"{MessageUtil.GetBaseUri()}/Practitioner/{practitioner.Key}",
                        Resource = FhirResourceHandlerUtil.GetMapperForInstance(practitioner).MapToFhir(practitioner)
                    });
                }
                else if (rel.RelationshipTypeKey == EntityRelationshipTypeKeys.Replaces)
                {
                    retVal.Link.Add(this.CreateLink <Patient>(rel.TargetEntityKey.Value, Patient.LinkType.Replaces));
                }
                else if (rel.RelationshipTypeKey == EntityRelationshipTypeKeys.Duplicate)
                {
                    retVal.Link.Add(this.CreateLink <Patient>(rel.TargetEntityKey.Value, Patient.LinkType.Seealso));
                }
                else if (rel.RelationshipTypeKey == MDM_MASTER_LINK) // HACK: MDM Master Record
                {
                    if (rel.SourceEntityKey.HasValue && rel.SourceEntityKey != model.Key)
                    {
                        retVal.Link.Add(this.CreateLink <Patient>(rel.SourceEntityKey.Value, Patient.LinkType.Seealso));
                    }
                    else // Is a local
                    {
                        retVal.Link.Add(this.CreateLink <Patient>(rel.TargetEntityKey.Value, Patient.LinkType.Refer));
                    }
                }
                else if (rel.ClassificationKey == EntityRelationshipTypeKeys.EquivalentEntity)
                {
                    retVal.Link.Add(this.CreateLink <Patient>(rel.TargetEntityKey.Value, Patient.LinkType.Refer));
                }
                else if (partOfBundle != null) // This is part of a bundle and we need to include it
                {
                    // HACK: This piece of code is used to add any RelatedPersons to the container bundle if it is part of a bundle
                    if (this.GetRelatedPersonConceptUuids().Contains(rel.RelationshipTypeKey.Value))
                    {
                        var relative = FhirResourceHandlerUtil.GetMapperForInstance(rel).MapToFhir(rel);
                        partOfBundle.Entry.Add(new Bundle.EntryComponent()
                        {
                            FullUrl  = $"{MessageUtil.GetBaseUri()}/RelatedPerson/{rel.Key}",
                            Resource = relative
                        });
                    }
                }
            }

            // Reverse relationships of family member?
            var uuids = model.Relationships.Where(r => r.RelationshipTypeKey == MDM_MASTER_LINK).Select(r => r.SourceEntityKey).Union(new Guid?[] { model.Key }).ToArray();
            var familyMemberConcepts = this.GetFamilyMemberUuids();
            var reverseRelationships = this.m_erRepository.Find(o => uuids.Contains(o.TargetEntityKey) && familyMemberConcepts.Contains(o.RelationshipTypeKey.Value) && o.ObsoleteVersionSequenceId == null);

            foreach (var rrv in reverseRelationships)
            {
                retVal.Link.Add(new Patient.LinkComponent
                {
                    Type  = Patient.LinkType.Seealso,
                    Other = DataTypeConverter.CreateNonVersionedReference <RelatedPerson>(rrv)
                });

                // If this is part of a bundle, include it
                partOfBundle?.Entry.Add(new Bundle.EntryComponent
                {
                    FullUrl  = $"{MessageUtil.GetBaseUri()}/RelatedPerson/{rrv.Key}",
                    Resource = FhirResourceHandlerUtil.GetMappersFor(ResourceType.RelatedPerson).First().MapToFhir(rrv)
                });
            }

            // Was this record replaced?
            if (!retVal.Active.GetValueOrDefault())
            {
                var replacedRelationships = this.m_erRepository.Find(o => uuids.Contains(o.TargetEntityKey) && o.RelationshipTypeKey == EntityRelationshipTypeKeys.Replaces && o.ObsoleteVersionSequenceId == null);

                foreach (var repl in replacedRelationships)
                {
                    retVal.Link.Add(new Patient.LinkComponent()
                    {
                        Type  = Patient.LinkType.ReplacedBy,
                        Other = DataTypeConverter.CreateNonVersionedReference <Patient>(repl.LoadProperty(o => o.SourceEntity)),
                    });
                }
            }

            var photo = model.LoadCollection(o => o.Extensions).FirstOrDefault(o => o.ExtensionTypeKey == ExtensionTypeKeys.JpegPhotoExtension);

            if (photo != null)
            {
                retVal.Photo = new List <Attachment>
                {
                    new Attachment
                    {
                        ContentType = "image/jpg",
                        Data        = photo.ExtensionValueXml
                    }
                };
            }

            return(retVal);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Create patient link
 /// </summary>
 private Patient.LinkComponent CreateLink <TLink>(Guid targetEntityKey, Patient.LinkType type) where TLink : DomainResource, new() => new Patient.LinkComponent()
 {
     Type  = type,
     Other = DataTypeConverter.CreateNonVersionedReference <TLink>(targetEntityKey)
 };
Ejemplo n.º 6
0
        /// <summary>
        /// Map the specified patient encounter to a FHIR based encounter
        /// </summary>
        protected override Encounter MapToFhir(PatientEncounter model)
        {
            var retVal = DataTypeConverter.CreateResource <Encounter>(model);

            // Map the identifier
            retVal.Identifier = model.LoadCollection <ActIdentifier>("Identifiers").Select(o => DataTypeConverter.ToFhirIdentifier(o)).ToList();

            // Map status keys
            switch (model.StatusConceptKey.ToString().ToUpper())
            {
            case StatusKeyStrings.Active:
            case StatusKeyStrings.New:
                switch (model.MoodConceptKey.ToString().ToUpper())
                {
                case MoodConceptKeyStrings.Eventoccurrence:
                case MoodConceptKeyStrings.Request:
                    retVal.Status = Encounter.EncounterStatus.InProgress;
                    break;

                case MoodConceptKeyStrings.Intent:
                case MoodConceptKeyStrings.Promise:
                    retVal.Status = Encounter.EncounterStatus.Planned;
                    break;
                }

                break;

            case StatusKeyStrings.Cancelled:
                retVal.Status = Encounter.EncounterStatus.Cancelled;
                break;

            case StatusKeyStrings.Nullified:
                retVal.Status = Encounter.EncounterStatus.EnteredInError;
                break;

            case StatusKeyStrings.Obsolete:
                retVal.Status = Encounter.EncounterStatus.Unknown;
                break;

            case StatusKeyStrings.Completed:
                retVal.Status = Encounter.EncounterStatus.Finished;
                break;
            }

            if (model.StartTime.HasValue || model.StopTime.HasValue)
            {
                retVal.Period = DataTypeConverter.ToPeriod(model.StartTime, model.StopTime);
            }
            else
            {
                retVal.Period = DataTypeConverter.ToPeriod(model.ActTime, model.ActTime);
            }

            retVal.ReasonCode = new List <CodeableConcept>
            {
                DataTypeConverter.ToFhirCodeableConcept(model.ReasonConceptKey)
            };

            retVal.Class = DataTypeConverter.ToFhirCodeableConcept(model.TypeConceptKey).GetCoding();

            // Map associated
            var associated = model.LoadCollection <ActParticipation>("Participations").ToArray();

            // Subject of encounter
            retVal.Subject = DataTypeConverter.CreateNonVersionedReference <Patient>(associated.FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.RecordTarget)?.LoadProperty <Entity>("PlayerEntity"));

            // Locations
            retVal.Location = associated.Where(o => o.LoadProperty <Entity>("PlayerEntity") is Place).Select(o => new Encounter.LocationComponent
            {
                Period   = DataTypeConverter.ToPeriod(model.CreationTime, null),
                Location = DataTypeConverter.CreateVersionedReference <Location>(o.PlayerEntity)
            }).ToList();

            // Service provider
            var cst = associated.FirstOrDefault(o => o.LoadProperty <Entity>("PlayerEntity") is Organization && o.ParticipationRoleKey == ActParticipationKey.Custodian);

            if (cst != null)
            {
                retVal.ServiceProvider = DataTypeConverter.CreateVersionedReference <Hl7.Fhir.Model.Organization>(cst.PlayerEntity);
            }

            // Participants
            retVal.Participant = associated.Where(o => o.LoadProperty <Entity>("PlayerEntity") is Provider || o.LoadProperty <Entity>("PlayerEntity") is UserEntity).Select(o => new Encounter.ParticipantComponent
            {
                Type = new List <CodeableConcept>
                {
                    DataTypeConverter.ToFhirCodeableConcept(o.ParticipationRoleKey)
                },
                Individual = DataTypeConverter.CreateVersionedReference <Practitioner>(o.PlayerEntity)
            }).ToList();

            return(retVal);
        }
        /// <summary>
        /// Maps the object to model to fhir
        /// </summary>
        protected override MedicationAdministration MapToFhir(SubstanceAdministration model)
        {
            var retVal = DataTypeConverter.CreateResource <MedicationAdministration>(model);

            retVal.Identifier   = model.LoadCollection <ActIdentifier>(nameof(Act.Identifiers)).Select(DataTypeConverter.ToFhirIdentifier).ToList();
            retVal.StatusReason = new List <CodeableConcept> {
                DataTypeConverter.ToFhirCodeableConcept(model.ReasonConceptKey)
            };

            switch (model.StatusConceptKey.ToString().ToUpper())
            {
            case StatusKeyStrings.Active:
                retVal.Status = MedicationAdministration.MedicationAdministrationStatusCodes.InProgress;
                break;

            case StatusKeyStrings.Cancelled:
                retVal.Status = MedicationAdministration.MedicationAdministrationStatusCodes.Stopped;
                break;

            case StatusKeyStrings.Nullified:
                retVal.Status = MedicationAdministration.MedicationAdministrationStatusCodes.EnteredInError;
                break;

            case StatusKeyStrings.Completed:
                retVal.Status = MedicationAdministration.MedicationAdministrationStatusCodes.Completed;
                break;

            case StatusKeyStrings.Obsolete:
                retVal.Status = MedicationAdministration.MedicationAdministrationStatusCodes.Unknown;
                break;
            }

            if (model.IsNegated)
            {
                retVal.Status = MedicationAdministration.MedicationAdministrationStatusCodes.NotDone;
            }

            retVal.Category = DataTypeConverter.ToFhirCodeableConcept(model.TypeConceptKey, "http://hl7.org/fhir/medication-admin-category");

            var consumableRelationship = model.LoadCollection <ActParticipation>(nameof(Act.Participations)).FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.Consumable);
            var productRelationship    = model.LoadCollection <ActParticipation>(nameof(Act.Participations)).FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.Product);

            if (consumableRelationship != null)
            {
                retVal.Medication = DataTypeConverter.CreateVersionedReference <Medication>(consumableRelationship.LoadProperty <ManufacturedMaterial>("PlayerEntity"));
            }
            else if (productRelationship != null)
            {
                retVal.Medication = DataTypeConverter.CreateVersionedReference <Substance>(productRelationship.LoadProperty <Material>("PlayerEntity"));
                //retVal.Medication = DataTypeConverter.ToFhirCodeableConcept(productRelationship.LoadProperty<Material>("PlayerEntity").LoadProperty<Concept>("TypeConcept"));
            }

            var rct = model.LoadCollection <ActParticipation>(nameof(Act.Participations)).FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.RecordTarget);

            if (rct != null)
            {
                retVal.Subject = DataTypeConverter.CreateVersionedReference <Patient>(rct.LoadProperty <Entity>("PlayerEntity"));
            }

            // Encounter
            var erService = ApplicationServiceContext.Current.GetService <IDataPersistenceService <EntityRelationship> >();
            var tr        = 0;
            var enc       = erService.Query(o => o.TargetEntityKey == model.Key && o.RelationshipTypeKey == ActRelationshipTypeKeys.HasComponent && o.ObsoleteVersionSequenceId == null, 0, 10, out tr, AuthenticationContext.Current.Principal);

            if (enc != null)
            {
                retVal.EventHistory = enc.Select(o => DataTypeConverter.CreateNonVersionedReference <Encounter>(o.TargetEntityKey)).ToList();
                // TODO: Encounter
            }

            // Effective time
            retVal.Effective = DataTypeConverter.ToPeriod(model.StartTime ?? model.ActTime, model.StopTime);

            // performer
            var performer = model.LoadCollection <ActParticipation>(nameof(Act.Participations)).Where(o => o.ParticipationRoleKey == ActParticipationKey.Performer || o.ParticipationRoleKey == ActParticipationKey.Authororiginator);

            retVal.Performer = performer.Select(o => new MedicationAdministration.PerformerComponent
            {
                Actor = DataTypeConverter.CreateVersionedReference <Practitioner>(o.LoadProperty <Entity>(nameof(ActParticipation.PlayerEntity)))
            }).ToList();


            retVal.Dosage = new MedicationAdministration.DosageComponent
            {
                Site  = DataTypeConverter.ToFhirCodeableConcept(model.SiteKey),
                Route = DataTypeConverter.ToFhirCodeableConcept(model.RouteKey),
                Dose  = new Quantity
                {
                    Value = model.DoseQuantity,
                    Unit  = DataTypeConverter.ToFhirCodeableConcept(model.DoseUnitKey, "http://hl7.org/fhir/sid/ucum").GetCoding()?.Code
                }
            };

            return(retVal);
        }
        /// <summary>
        /// Map to FHIR
        /// </summary>
        protected override Observation MapToFhir(Core.Model.Acts.Observation model)
        {
            var retVal = DataTypeConverter.CreateResource <Observation>(model);

            retVal.Identifier = model.LoadCollection <ActIdentifier>(nameof(Act.Identifiers)).Select(o => DataTypeConverter.ToFhirIdentifier(o)).ToList();

            switch (model.StatusConceptKey.ToString().ToUpper())
            {
            case StatusKeyStrings.New:
            case StatusKeyStrings.Active:
                retVal.Status = ObservationStatus.Preliminary;
                break;

            case StatusKeyStrings.Cancelled:
                retVal.Status = ObservationStatus.Cancelled;
                break;

            case StatusKeyStrings.Nullified:
                retVal.Status = ObservationStatus.EnteredInError;
                break;

            case StatusKeyStrings.Completed:
                if (model.LoadCollection <ActRelationship>(nameof(Act.Relationships)).Any(o => o.RelationshipTypeKey == ActRelationshipTypeKeys.Replaces))    // was amended
                {
                    retVal.Status = ObservationStatus.Amended;
                }
                else
                {
                    retVal.Status = ObservationStatus.Final;
                }

                break;

            case StatusKeyStrings.Obsolete:
                retVal.Status = ObservationStatus.Unknown;
                break;
            }

            if (model.StartTime.HasValue || model.StopTime.HasValue)
            {
                retVal.Effective = DataTypeConverter.ToPeriod(model.StartTime, model.StopTime);
            }
            else
            {
                retVal.Effective = DataTypeConverter.ToFhirDateTime(model.ActTime);
            }

            retVal.Code = DataTypeConverter.ToFhirCodeableConcept(model.TypeConceptKey);

            // RCT
            var rct = model.LoadCollection <ActParticipation>(nameof(Act.Participations)).FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.RecordTarget);

            if (rct != null)
            {
                retVal.Subject = DataTypeConverter.CreateNonVersionedReference <Patient>(rct.LoadProperty <Entity>(nameof(ActParticipation.PlayerEntity)));
            }

            // Performer
            retVal.Performer = model.Participations.Where(o => o.ParticipationRoleKey == ActParticipationKey.Performer)
                               .Select(o => DataTypeConverter.CreateNonVersionedReference <Practitioner>(o.LoadProperty <Entity>(nameof(ActParticipation.PlayerEntity))))
                               .ToList();

            retVal.Issued = model.CreationTime;

            // Value
            switch (model.ValueType)
            {
            case "CD":
                retVal.Value = DataTypeConverter.ToFhirCodeableConcept((model as CodedObservation).ValueKey);
                break;

            case "PQ":
                var qty = model as QuantityObservation;
                retVal.Value = DataTypeConverter.ToQuantity(qty.Value, qty.UnitOfMeasureKey);
                break;

            case "ED":
            case "ST":
                retVal.Value = new FhirString((model as TextObservation).Value);
                break;
            }

            if (model.InterpretationConceptKey.HasValue)
            {
                retVal.Interpretation = new List <CodeableConcept>
                {
                    DataTypeConverter.ToFhirCodeableConcept(model.InterpretationConceptKey)
                };
            }

            return(retVal);
        }