/// <summary> /// Persists data to the DEMOGRAPHICS table. /// </summary> /// <param name="demographics"></param> /// <returns></returns> public int SaveDemographics(Demographics demographics) { var db = new CSET_Context(); // Convert Size and AssetValue from their keys to the strings they are stored as string assetValue = db.DEMOGRAPHICS_ASSET_VALUES.Where(dav => dav.DemographicsAssetId == demographics.AssetValue).FirstOrDefault()?.AssetValue; string assetSize = db.DEMOGRAPHICS_SIZE.Where(dav => dav.DemographicId == demographics.Size).FirstOrDefault()?.Size; // If the user selected nothing for sector or industry, store a null - 0 violates foreign key if (demographics.SectorId == 0) { demographics.SectorId = null; } if (demographics.IndustryId == 0) { demographics.IndustryId = null; } // Add or update the DEMOGRAPHICS record var dbDemographics = new DEMOGRAPHICS() { Assessment_Id = demographics.AssessmentId, IndustryId = demographics.IndustryId, SectorId = demographics.SectorId, Size = assetSize, AssetValue = assetValue, Facilitator = demographics.Facilitator == 0 ? null : demographics.Facilitator, PointOfContact = demographics.PointOfContact == 0 ? null : demographics.PointOfContact, IsScoped = demographics.IsScoped, Agency = demographics.Agency, OrganizationType = demographics.OrganizationType == 0 ? null : demographics.OrganizationType, OrganizationName = demographics.OrganizationName }; db.DEMOGRAPHICS.AddOrUpdate(dbDemographics, x => x.Assessment_Id); db.SaveChanges(); demographics.AssessmentId = dbDemographics.Assessment_Id; AssessmentUtil.TouchAssessment(dbDemographics.Assessment_Id); return(demographics.AssessmentId); }
private DEMOGRAPHICS buildDemographic(Patient iPatient) { DEMOGRAPHICS result = new DEMOGRAPHICS(); if (iPatient.Identifier.Count > 0) { // PERSON_ID result.PERSON_ID = iPatient.Id; // MRN foreach (Identifier i in iPatient.Identifier) { if (i.Type != null) { foreach (var code in i.Type.Coding) { if (code.Code == "MR") { result.MRN = iPatient.Identifier.FirstOrDefault().Value; } } } } // BIRTH_DATE if (iPatient.BirthDate != null) { result.BIRTH_DATE = DateTime.ParseExact(iPatient.BirthDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); // GENDER if (iPatient.Gender != null) { if (iPatient.Gender == AdministrativeGender.Female) { result.GENDER = "F"; } else if (iPatient.Gender == AdministrativeGender.Male) { result.GENDER = "M"; } else if (iPatient.Gender == AdministrativeGender.Other) { result.GENDER = "O"; } else if (iPatient.Gender == AdministrativeGender.Unknown) { result.GENDER = "U"; } else { result.GENDER = "U"; } // PRIMARY_LANGUAGE and NEEDS_INTERPRETER if (iPatient.Communication.Count > 0) { string lang = iPatient.Communication.FirstOrDefault().Language.Coding.FirstOrDefault().Code; int end = lang.IndexOf('-'); if (end < 0) { end = lang.Length < 4 ? lang.Length : 3; } result.PRIMARY_LANGUAGE = lang.Substring(0, end); if (iPatient.Communication.FirstOrDefault().Preferred != null) { result.NEEDS_INTERPRETER = "S"; } else { result.NEEDS_INTERPRETER = ""; } } else { result.PRIMARY_LANGUAGE = ""; result.NEEDS_INTERPRETER = ""; } // race result.RACE1 = "UN"; result.RACE2 = "UN"; result.RACE3 = "UN"; result.RACE4 = "UN"; result.RACE5 = "UN"; result.HISPANIC = "U"; result.SEXUAL_ORIENTATION = null; result.GENDER_IDENTITY = null; if (iPatient.Extension.Count() > 0) { int race_count = 0; foreach (Extension e in iPatient.Extension) { if (e.TypeName == "CodeableConcept") { CodeableConcept c = (CodeableConcept)e.Value; if (c.Text == "race") { if (race_count == 0) { result.RACE1 = RaceAbbreviationGivenConceptCode(c.Coding.FirstOrDefault().Code); race_count++; } else if (race_count == 1) { result.RACE2 = RaceAbbreviationGivenConceptCode(c.Coding.FirstOrDefault().Code); race_count++; } else if (race_count == 2) { result.RACE3 = RaceAbbreviationGivenConceptCode(c.Coding.FirstOrDefault().Code); race_count++; } else if (race_count == 3) { result.RACE4 = RaceAbbreviationGivenConceptCode(c.Coding.FirstOrDefault().Code); race_count++; } else if (race_count == 4) { result.RACE5 = RaceAbbreviationGivenConceptCode(c.Coding.FirstOrDefault().Code); race_count++; } } if (c.Text == "ethnicity") { result.HISPANIC = EthnicityAbbreviationGivenConceptCode(c.Coding.FirstOrDefault().Code); } } } // SEXUAL_ORIENTATION result.SEXUAL_ORIENTATION = null; // GENDER_IDENTITY result.GENDER_IDENTITY = null; } } else { // missing gender log.Info("Error: Missing gender for " + iPatient.Id); result = null; } } else { // Invalid Birth Date log.Info("Error: Missing Birthdate for " + iPatient.Id); result = null; } } return(result); }
public List <FHIRPatientSummary> LoadVDW() { List <FHIRPatientSummary> results = new List <FHIRPatientSummary>(); log.Info("Start Importing Patients"); using (var context = new VDW_3_1_Entities()) { try { Bundle bundles = m_FhirClient.Search <Patient>(new string[] { "_count=100" }); // get all the patients from all the pages List <Patient> patients = new List <Patient>(); while (bundles != null) { foreach (Bundle.EntryComponent item in bundles.Entry) { Patient p = (Patient)item.Resource; patients.Add(p); } bundles = m_FhirClient.Continue(bundles); } m_ProgressBar.Maximum = patients.Count; m_ProgressBar.Step = 1; m_ProgressBar.Value = 0; int total = patients.Count; int cur = 1; foreach (var p in patients) { FHIRPatientSummary sum = new FHIRPatientSummary(); sum.PERSON_ID = p.Id; // build and save DEMOGRAPHIC record for patient DEMOGRAPHICS demo = buildDemographic(p); if (demo != null) { try { context.DEMOGRAPHICS.Add(demo); context.SaveChanges(); // build and save CENSUS_LOCATION records List <CENSUS_LOCATION> locs = buildGeocode(p); foreach (var loc in locs) { try { context.CENSUS_LOCATION.Add(loc); context.SaveChanges(); } catch (Exception ex) { log.Info("Error Census Location: " + ex.Message); } } // build and save Encounter records for patient try { Bundle encounters = m_FhirClient.Search <Encounter>(new string[] { "patient=" + p.Id }); foreach (var e_item in encounters.Entry) { Encounter e_temp = (Encounter)e_item.Resource; ENCOUNTERS tENCOUNTERS = buildEncounter(e_temp, p); if (tENCOUNTERS != null) { try { context.ENCOUNTERS.Add(tENCOUNTERS); context.SaveChanges(); } catch (Exception ex) { log.Info("Error Encounter: " + ex.Message); } } else { log.Info("Error Encounter: bad encounter record for patient " + p.Id); } } } catch (Exception ex) { log.Info("Error Encounter: " + ex.Message); } // build and save Diagnose records for patient try { Bundle diagnosis = m_FhirClient.Search <DiagnosticReport>(new string[] { "patient=" + p.Id }); foreach (var d_item in diagnosis.Entry) { DiagnosticReport tDiagnoses = null; ; } } catch (Exception ex) { log.Info("Error Diagnosis: " + ex.Message); } // build and save VITAL_SIGN records for patient try { Bundle o = m_FhirClient.Search <Observation>(new string[] { "patient=" + p.Id }); foreach (var tempD in o.Entry) { Observation obs = (Observation)tempD.Resource; foreach (CodeableConcept cc in obs.Category) { foreach (var code in cc.Coding) { if (code.Code == "vital-signs") { sum.VitalSignTotalCount++; } } } } } catch (Exception ex) { log.Info("Error Vital Sign: " + ex.Message); } } catch (Exception ex) { log.Info("Error: " + ex.Message); } } else { log.Info("Error in patient: " + p.Id); } // Location Count sum.LocationTotalCount = p.Address.Count; // Encounter Count Bundle en = m_FhirClient.Search <Encounter>(new string[] { "patient=" + p.Id }); sum.EncounterTotalCount = en.Entry.Count; // Diagnoses Count Bundle d = m_FhirClient.Search <DiagnosticReport>(new string[] { "patient=" + p.Id }); sum.DiagnosesTotalCount = d.Entry.Count; results.Add(sum); cur++; m_ProgressBar.Value = m_ProgressBar.Value + 1; } log.Info("Number of Patients " + results.Count.ToString()); log.Info("End Importing Patients"); } catch (Exception ex) { log.Info("Error: " + ex.Message); } } return(results); }