/// <summary> /// Maps the specified act to an adverse event /// </summary> protected override AdverseEvent MapToFhir(Act model, WebOperationContext webOperationContext) { var retVal = DataTypeConverter.CreateResource <AdverseEvent>(model); retVal.Identifier = DataTypeConverter.ToFhirIdentifier <Act>(model.Identifiers.FirstOrDefault()); retVal.Category = AdverseEventCategory.AdverseEvent; retVal.Type = DataTypeConverter.ToFhirCodeableConcept(model.LoadProperty <Concept>("TypeConcept")); var recordTarget = model.LoadCollection <ActParticipation>("Participations").FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.RecordTarget); if (recordTarget != null) { retVal.Subject = DataTypeConverter.CreateReference <Patient>(recordTarget.LoadProperty <Entity>("PlayerEntity"), webOperationContext); } // 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 act does not appear to be an adverse event"); } retVal.Date = subject.ActTime.DateTime; // Reactions = HasManifestation var reactions = subject.LoadCollection <ActRelationship>("Relationships").Where(o => o.RelationshipTypeKey == ActRelationshipTypeKeys.HasManifestation); retVal.Reaction = reactions.Select(o => DataTypeConverter.CreateReference <Condition>(o.LoadProperty <Act>("TargetAct"), webOperationContext)).ToList(); var location = model.LoadCollection <ActParticipation>("Participations").FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.Location); if (location != null) { retVal.Location = DataTypeConverter.CreateReference <Location>(location.LoadProperty <Entity>("PlayerEntity"), webOperationContext); } // Severity var severity = subject.LoadCollection <ActRelationship>("Relationships").First(o => o.RelationshipTypeKey == ActRelationshipTypeKeys.HasComponent && o.LoadProperty <Act>("TargetAct").TypeConceptKey == ObservationTypeKeys.Severity); if (severity != null) { retVal.Seriousness = DataTypeConverter.ToFhirCodeableConcept(severity.LoadProperty <CodedObservation>("TargetAct").Value, "http://hl7.org/fhir/adverse-event-seriousness"); } // 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 MARC.HI.EHRS.SVC.Messaging.FHIR.DataTypes.FhirCodeableConcept(new Uri("http://hl7.org/fhir/adverse-event-outcome"), "fatal"); } else if (model.StatusConceptKey == StatusKeys.Active) { retVal.Outcome = new MARC.HI.EHRS.SVC.Messaging.FHIR.DataTypes.FhirCodeableConcept(new Uri("http://hl7.org/fhir/adverse-event-outcome"), "ongoing"); } else if (model.StatusConceptKey == StatusKeys.Completed) { retVal.Outcome = new MARC.HI.EHRS.SVC.Messaging.FHIR.DataTypes.FhirCodeableConcept(new Uri("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.CreatePlainReference <Practitioner>(author.LoadProperty <Entity>("PlayerEntity"), webOperationContext); } // Suspect entities var refersTo = model.LoadCollection <ActRelationship>("Relationships").Where(o => o.RelationshipTypeKey == ActRelationshipTypeKeys.RefersTo); if (refersTo.Count() > 0) { 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 AdverseEventSuspectEntity() { Instance = DataTypeConverter.CreatePlainReference <Substance>(product, webOperationContext) }); } else { return new AdverseEventSuspectEntity() { Instance = DataTypeConverter.CreatePlainReference <Medication>(consumable, webOperationContext) } }; }).ToList(); } return(retVal); }
/// <summary> /// Map to FHIR /// </summary> protected override Condition MapToFhir(CodedObservation model, WebOperationContext webOperationContext) { var retVal = DataTypeConverter.CreateResource <Condition>(model); retVal.Identifier = model.LoadCollection <ActIdentifier>("Identifiers").Select(o => DataTypeConverter.ToFhirIdentifier <Act>(o)).ToList(); // Clinical status of the condition if (model.StatusConceptKey == StatusKeys.Active) { retVal.ClinicalStatus = ConditionClinicalStatus.Active; } else if (model.StatusConceptKey == StatusKeys.Completed) { retVal.ClinicalStatus = ConditionClinicalStatus.Resolved; } else if (model.StatusConceptKey == StatusKeys.Nullified) { retVal.VerificationStatus = ConditionVerificationStatus.EnteredInError; } else if (model.StatusConceptKey == StatusKeys.Obsolete) { retVal.ClinicalStatus = ConditionClinicalStatus.Inactive; } // Category retVal.Category.Add(new MARC.HI.EHRS.SVC.Messaging.FHIR.DataTypes.FhirCodeableConcept(new Uri("http://hl7.org/fhir/condition-category"), "encounter-diagnosis")); // Severity? var actRelationshipService = ApplicationContext.Current.GetService <IDataPersistenceService <ActRelationship> >(); var severity = actRelationshipService.Query(o => o.SourceEntityKey == model.Key && o.RelationshipTypeKey == ActRelationshipTypeKeys.HasComponent && o.TargetAct.TypeConceptKey == ObservationTypeKeys.Severity, AuthenticationContext.Current.Principal); if (severity == null) // Perhaps we should get from neighbor if this is in an encounter { var contextAct = actRelationshipService.Query(o => o.TargetActKey == model.Key, AuthenticationContext.Current.Principal).FirstOrDefault(); if (contextAct != null) { severity = actRelationshipService.Query(o => o.SourceEntityKey == contextAct.SourceEntityKey && o.RelationshipTypeKey == ActRelationshipTypeKeys.HasComponent && o.TargetAct.TypeConceptKey == ObservationTypeKeys.Severity, AuthenticationContext.Current.Principal); } } // Severity if (severity != null) { retVal.Severity = DataTypeConverter.ToFhirCodeableConcept((severity as CodedObservation).LoadProperty <Concept>("Value")); } retVal.Code = DataTypeConverter.ToFhirCodeableConcept(model.LoadProperty <Concept>("Value")); // body sites? var sites = actRelationshipService.Query(o => o.SourceEntityKey == model.Key && o.RelationshipTypeKey == ActRelationshipTypeKeys.HasComponent && o.TargetAct.TypeConceptKey == ObservationTypeKeys.FindingSite, AuthenticationContext.Current.Principal); retVal.BodySite = sites.Select(o => DataTypeConverter.ToFhirCodeableConcept(o.LoadProperty <CodedObservation>("TargetAct").LoadProperty <Concept>("Value"))).ToList(); // Subject var recordTarget = model.LoadCollection <ActParticipation>("Participations").FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.RecordTarget); if (recordTarget != null) { this.traceSource.TraceInformation("RCT: {0}", recordTarget.PlayerEntityKey); retVal.Subject = DataTypeConverter.CreateReference <Patient>(recordTarget.LoadProperty <Entity>("PlayerEntity"), webOperationContext); } // Onset if (model.StartTime.HasValue || model.StopTime.HasValue) { retVal.Onset = new FhirPeriod() { Start = model.StartTime?.DateTime, Stop = model.StopTime?.DateTime } } ; else { retVal.Onset = new FhirDateTime(model.ActTime.DateTime); } retVal.AssertionDate = model.CreationTime.LocalDateTime; var author = model.LoadCollection <ActParticipation>("Participations").FirstOrDefault(o => o.ParticipationRoleKey == ActParticipationKey.Authororiginator); if (author != null) { retVal.Asserter = DataTypeConverter.CreatePlainReference <Practitioner>(author.LoadProperty <Entity>("PlayerEntity"), webOperationContext); } return(retVal); }