/// <summary> /// Map a patient object to FHIR. /// </summary> /// <param name="model">The patient to map to FHIR</param> /// <param name="restOperationContext">The current REST operation context</param> /// <returns>Returns the mapped FHIR resource.</returns> protected override Patient MapToFhir(Core.Model.Roles.Patient model, RestOperationContext restOperationContext) { var retVal = DataTypeConverter.CreateResource <Patient>(model); retVal.Active = model.StatusConceptKey == StatusKeys.Active; retVal.Address = model.LoadCollection <EntityAddress>("Addresses").Select(o => DataTypeConverter.ToFhirAddress(o)).ToList(); retVal.BirthDate = model.DateOfBirth; retVal.Deceased = model.DeceasedDate == DateTime.MinValue ? (object)new FhirBoolean(true) : model.DeceasedDate != null ? new FhirDate(model.DeceasedDate.Value) : null; retVal.Gender = DataTypeConverter.ToFhirCodeableConcept(model.LoadProperty <Concept>("GenderConcept"), "http://hl7.org/fhir/administrative-gender")?.GetPrimaryCode()?.Code; retVal.Identifier = model.Identifiers?.Select(o => DataTypeConverter.ToFhirIdentifier(o)).ToList(); retVal.MultipleBirth = model.MultipleBirthOrder == 0 ? (FhirElement) new FhirBoolean(true) : model.MultipleBirthOrder.HasValue ? new FhirInt(model.MultipleBirthOrder.Value) : null; retVal.Name = model.LoadCollection <EntityName>("Names").Select(o => DataTypeConverter.ToFhirHumanName(o)).ToList(); retVal.Timestamp = model.ModifiedOn.DateTime; retVal.Telecom = model.LoadCollection <EntityTelecomAddress>("Telecoms").Select(o => DataTypeConverter.ToFhirTelecom(o)).ToList(); // TODO: Relationships foreach (var rel in model.LoadCollection <EntityRelationship>("Relationships").Where(o => !o.InversionIndicator)) { // Family member if (rel.LoadProperty <Concept>(nameof(EntityRelationship.RelationshipType)).ConceptSetsXml.Contains(ConceptSetKeys.FamilyMember)) { // Create the relative object var relative = DataTypeConverter.CreateResource <RelatedPerson>(rel.LoadProperty <Person>(nameof(EntityRelationship.TargetEntity))); relative.Relationship = DataTypeConverter.ToFhirCodeableConcept(rel.LoadProperty <Concept>(nameof(EntityRelationship.RelationshipType))); relative.Address = DataTypeConverter.ToFhirAddress(rel.TargetEntity.Addresses.FirstOrDefault()); relative.Gender = DataTypeConverter.ToFhirCodeableConcept((rel.TargetEntity as Core.Model.Roles.Patient)?.LoadProperty <Concept>(nameof(Core.Model.Roles.Patient.GenderConcept))); relative.Identifier = rel.TargetEntity.LoadCollection <EntityIdentifier>(nameof(Entity.Identifiers)).Select(o => DataTypeConverter.ToFhirIdentifier(o)).ToList(); relative.Name = DataTypeConverter.ToFhirHumanName(rel.TargetEntity.LoadCollection <EntityName>(nameof(Entity.Names)).FirstOrDefault()); if (rel.TargetEntity is Core.Model.Roles.Patient) { relative.Patient = DataTypeConverter.CreateReference <Patient>(rel.TargetEntity, restOperationContext); } relative.Telecom = rel.TargetEntity.LoadCollection <EntityTelecomAddress>(nameof(Entity.Telecoms)).Select(o => DataTypeConverter.ToFhirTelecom(o)).ToList(); retVal.Contained.Add(new ContainedResource() { Item = relative }); } else if (rel.RelationshipTypeKey == EntityRelationshipTypeKeys.HealthcareProvider) { retVal.Provider = DataTypeConverter.CreateReference <Practitioner>(rel.LoadProperty <Entity>(nameof(EntityRelationship.TargetEntity)), restOperationContext); } else if (rel.RelationshipTypeKey == EntityRelationshipTypeKeys.Replaces) { retVal.Link.Add(new PatientLink() { Type = PatientLinkType.Replace, Other = Reference.CreateLocalResourceReference(rel.LoadProperty <Patient>(nameof(EntityRelationship.TargetEntity))) }); } else if (rel.RelationshipTypeKey == EntityRelationshipTypeKeys.Duplicate) { retVal.Link.Add(new PatientLink() { Type = PatientLinkType.SeeAlso, Other = Reference.CreateLocalResourceReference(rel.LoadProperty <Patient>(nameof(EntityRelationship.TargetEntity))) }); } else if (rel.RelationshipTypeKey?.ToString() == "97730a52-7e30-4dcd-94cd-fd532d111578") // MDM Master Record { if (rel.SourceEntityKey != model.Key) { retVal.Link.Add(new PatientLink() // Is a master { Type = PatientLinkType.SeeAlso, Other = Reference.CreateLocalResourceReference(rel.LoadProperty <Patient>(nameof(EntityRelationship.SourceEntity))) }); } else // Is a local { retVal.Link.Add(new PatientLink() { Type = PatientLinkType.Refer, Other = Reference.CreateLocalResourceReference(rel.LoadProperty <Patient>(nameof(EntityRelationship.TargetEntity))) }); } } } var photo = model.LoadCollection <EntityExtension>("Extensions").FirstOrDefault(o => o.ExtensionTypeKey == ExtensionTypeKeys.JpegPhotoExtension); if (photo != null) { retVal.Photo = new List <Attachment>() { new Attachment() { ContentType = "image/jpg", Data = photo.ExtensionValueXml } } } ; // TODO: Links return(retVal); }