public override Guid WriteToCDS(OrganizationServiceProxy _serviceProxy)
        {
            Guid profileId = Guid.Empty;

            try
            {
                HealthCDM.Contact addContact = new HealthCDM.Contact();

                addContact.GenderCode               = new OptionSetValue(GenderCode);
                addContact.FirstName                = FirstName;
                addContact.LastName                 = LastName;
                addContact.Address1_Line1           = Address1Line1;
                addContact.Address1_City            = Address1City;
                addContact.Address1_StateOrProvince = Address1StateOrProvince;
                addContact.Address1_PostalCode      = Address1PostalCode;
                addContact.Telephone1               = Telephone1;
                addContact.MobilePhone              = MobilePhone;
                addContact.Telephone2               = Telephone2;
                addContact.Address1_Country         = Address1Country;
                addContact.EMailAddress1            = FirstName + "." + LastName + "@" + EmailAddressDomain;
                addContact.Address1_Country         = Address1Country;
                addContact.Salutation               = Salutation;
                addContact.BirthDate                = BirthDate;

                // set the primary language
                addContact.msemr_Communication1Language = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, PrimaryLanguageCode, (int)HealthCDMEnums.CodeableConcept_Type.Language));

                addContact.msemr_ContactType = new OptionSetValue((int)HealthCDMEnums.Contact_Contacttype.RelatedPerson);

                try
                {
                    profileId = _serviceProxy.Create(addContact);

                    if (profileId != Guid.Empty)
                    {
                        ContactId = profileId.ToString();
                    }
                    else
                    {
                        throw new Exception("Contact Id == null");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return(profileId);
        }
        public override Guid WriteToCDS(string cdsUrl, string cdsUserName, string cdsPassword)
        {
            Guid profileId = Guid.Empty;

            try
            {
                #region Get our Login Information

                // setup the variables
                OrganizationServiceProxy _serviceProxy;

                // homeRealmUri will stay null for now
                Uri homeRealmUri = null;

                // setup credentials from whatever is in the app.config
                ClientCredentials credentials;

                // same for organizationuri comes from app.config
                Uri organizationUri;

                // set the organization uri from what was in the app.config
                organizationUri = new Uri(cdsUrl);

                credentials = new ClientCredentials();
                credentials.UserName.UserName = cdsUserName;
                credentials.UserName.Password = cdsPassword;

                #endregion

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                using (_serviceProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null))
                {
                    // To impersonate set the GUID of CRM user here (which I merely took from CRM itself
                    // would need not to use this caller id in the future (as it will change per instance of CRM)
                    //_serviceProxy.CallerId = new Guid("14D40CB7-81D5-E311-93F5-00155D00330C");
                    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

                    //enable using proxy types
                    _serviceProxy.EnableProxyTypes();

                    HealthCDM.Contact addContact = new HealthCDM.Contact();

                    addContact.GenderCode               = new OptionSetValue(GenderCode);
                    addContact.FirstName                = FirstName;
                    addContact.LastName                 = LastName;
                    addContact.Address1_Line1           = Address1Line1;
                    addContact.Address1_City            = Address1City;
                    addContact.Address1_StateOrProvince = Address1StateOrProvince;
                    addContact.Address1_PostalCode      = Address1PostalCode;
                    addContact.Telephone1               = Telephone1;
                    addContact.MobilePhone              = MobilePhone;
                    addContact.Telephone2               = Telephone2;
                    addContact.Address1_Country         = Address1Country;
                    addContact.EMailAddress1            = FirstName + "." + LastName + "@" + EmailAddressDomain;
                    addContact.Address1_Country         = Address1Country;
                    addContact.Salutation               = Salutation;
                    addContact.BirthDate                = BirthDate;
                    // set the primary language
                    addContact.msemr_Communication1Language = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, PrimaryLanguageCode, (int)HealthCDMEnums.CodeableConcept_Type.Language));


                    try
                    {
                        profileId = _serviceProxy.Create(addContact);

                        if (profileId != Guid.Empty)
                        {
                            ContactId = profileId.ToString();
                        }
                        else
                        {
                            throw new Exception("Contact Id == null");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return(profileId);
        }
Example #3
0
        public override Guid WriteToCDS(OrganizationServiceProxy _serviceProxy)
        {
            Guid profileId = Guid.Empty;

            try
            {
                HealthCDM.Contact addContact = new HealthCDM.Contact();

                //Set standard atttributes (this could be done via reflection)
                // but for now this is all we are setting
                addContact.GenderCode               = new OptionSetValue(GenderCode);
                addContact.FirstName                = FirstName;
                addContact.LastName                 = LastName;
                addContact.Address1_Line1           = Address1Line1;
                addContact.Address1_City            = Address1City;
                addContact.Address1_StateOrProvince = Address1StateOrProvince;
                addContact.Address1_PostalCode      = Address1PostalCode;
                addContact.Telephone1               = Telephone1;
                addContact.MobilePhone              = MobilePhone;
                addContact.Telephone2               = Telephone2;
                addContact.Address1_Country         = Address1Country;
                addContact.EMailAddress1            = FirstName + "." + LastName + "@" + EmailAddressDomain;
                addContact.Address1_Country         = Address1Country;
                addContact.Salutation               = Salutation;
                addContact.BirthDate                = BirthDate;
                // set the primary language
                addContact.msemr_Communication1Language = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, PrimaryLanguageCode, (int)HealthCDMEnums.CodeableConcept_Type.Language));

                addContact.msemr_ContactType = new OptionSetValue((int)HealthCDMEnums.Contact_Contacttype.Patient);

                // Set the Primary Practitioner, Emergency Contact & Primary Contacts
                if (!string.IsNullOrEmpty(PrimaryPractitionerId))
                {
                    addContact.msemr_GeneralPractioner = new EntityReference(HealthCDM.Contact.EntityLogicalName, Guid.Parse(PrimaryPractitionerId));
                }

                if (!string.IsNullOrEmpty(EmergencyContactId))
                {
                    addContact.msemr_Contact1             = new EntityReference(HealthCDM.Contact.EntityLogicalName, Guid.Parse(EmergencyContactId));
                    addContact.msemr_Contact1Relationship = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, EmergencyContactRelationshipTypeId, (int)HealthCDMEnums.CodeableConcept_Type.PatientRelationshipType));
                }

                if (!string.IsNullOrEmpty(PrimaryContactId))
                {
                    addContact.msemr_Contact2             = new EntityReference(HealthCDM.Contact.EntityLogicalName, Guid.Parse(PrimaryContactId));
                    addContact.msemr_Contact2Relationship = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, PrimaryContactRelationshipTypeId, (int)HealthCDMEnums.CodeableConcept_Type.PatientRelationshipType));
                }

                addContact.msemr_MedicalRecordNumber = PatientMedicalNumber;

                try
                {
                    profileId = _serviceProxy.Create(addContact);

                    if (profileId != Guid.Empty)
                    {
                        ContactId = profileId.ToString();

                        #region remap section due to dependencies

                        // Add alergies to patient
                        if (AllergyIntolerances.Count > 0)
                        {
                            foreach (AllergyIntolerance ai in AllergyIntolerances)
                            {
                                ai.PatientId = ContactId;
                                ai.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (Devices.Count > 0)
                        {
                            foreach (Device device in Devices)
                            {
                                Location location = Locations[SampleDataCache.SelectRandomItem.Next(0, Locations.Count - 1)];

                                device.PatientId  = ContactId;
                                device.LocationId = location.LocationId;
                                device.WriteToCDS(_serviceProxy);
                            }
                        }


                        if (Appointments.Count > 0)
                        {
                            foreach (Appointment appointment in Appointments)
                            {
                                Location location = Locations[SampleDataCache.SelectRandomItem.Next(0, Locations.Count - 1)];

                                appointment.Description   = FullName + " - Appointment";
                                appointment.PatientId     = ContactId;
                                appointment.LocationId    = location.LocationId;
                                appointment.AppointmentId = appointment.WriteToCDS(_serviceProxy).ToString();
                            }
                        }

                        if (Encounters.Count > 0)
                        {
                            foreach (Encounter encounter in Encounters)
                            {
                                Appointment appointment = Appointments[SampleDataCache.SelectRandomItem.Next(0, Appointments.Count - 1)];

                                encounter.Name             = FullName + " - Patient Encounter";
                                encounter.SubjectPatientId = ContactId;
                                encounter.AppointmentEmrId = appointment.AppointmentId;
                                encounter.EncounterId      = encounter.WriteToCDS(_serviceProxy).ToString();
                            }
                        }

                        if (ReferralRequests.Count > 0)
                        {
                            foreach (ReferralRequest request in ReferralRequests)
                            {
                                Encounter   encounter   = Encounters[SampleDataCache.SelectRandomItem.Next(0, Encounters.Count - 1)];
                                Appointment appointment = Appointments[SampleDataCache.SelectRandomItem.Next(0, Appointments.Count - 1)];

                                request.PatientId      = ContactId;
                                request.PractitionerId = primaryPractitionerId;
                                request.EncounterId    = encounter.EncounterId;
                                request.AppointmentId  = appointment.AppointmentId;
                                request.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (NutritionOrders.Count > 0)
                        {
                            foreach (NutritionOrder no in NutritionOrders)
                            {
                                Encounter encounter = Encounters[SampleDataCache.SelectRandomItem.Next(0, Encounters.Count - 1)];

                                no.PatientId      = ContactId;
                                no.PractitionerId = PrimaryPractitionerId;
                                no.EncounterId    = encounter.EncounterId;
                                no.WriteToCDS(_serviceProxy);
                            }
                        }

                        //if (Products.Count > 0)
                        //{
                        //    // we need the Uomid/groupid first
                        //    Uom.WriteToCDS(_serviceProxy);

                        //    foreach (Medication product in Products)
                        //    {
                        //        product.UomId = uom.UomId;
                        //        product.UnitGroupId = uom.GroupId;
                        //        product.Subject = "Sample Product " + GenerateRandomNumber();
                        //        product.WriteToCDS(_serviceProxy);
                        //    }

                        //    PriceList.Products = products;
                        //    PriceList.GroupId = uom.GroupId;
                        //    PriceList.UomId = uom.UomId;

                        //    PriceList.WriteToCDS(_serviceProxy);
                        //}

                        if (MedicationRequests.Count > 0)
                        {
                            foreach (MedicationRequest request in MedicationRequests)
                            {
                                Encounter  encounter = Encounters[SampleDataCache.SelectRandomItem.Next(0, Encounters.Count - 1)];
                                Medication product   = Products[SampleDataCache.SelectRandomItem.Next(0, Products.Count - 1)];

                                request.PatientId                = ContactId;
                                request.PractitionerId           = primaryPractitionerId;
                                request.MedicationTypePreference = string.Empty; // This is the PRODUCT reference which we don't have yet
                                request.EncounterId              = encounter.EncounterId;
                                request.MedicationTypePreference = product.MedicationId;
                                request.WriteToCDS(_serviceProxy);

                                MedicationAdministration admin = new DataModel.MedicationAdministration();

                                admin.PatientId   = ContactId;
                                admin.EncounterId = encounter.EncounterId;
                                admin.Name        = FullName + " - medication  administration";
                                admin.MedicationAdministrationId = admin.WriteToCDS(_serviceProxy).ToString();

                                medicationAdminitrations.Add(admin);
                            }
                        }

                        if (EpisodesOfCare.Count > 0)
                        {
                            foreach (EpisodeOfCare request in EpisodesOfCare)
                            {
                                Encounter encounter = Encounters[SampleDataCache.SelectRandomItem.Next(0, Encounters.Count - 1)];

                                request.PatientId       = ContactId;
                                request.PractitionerId  = primaryPractitionerId;
                                request.Description     = FullName + " - Episode of Care";
                                request.EncounterId     = encounter.EncounterId;
                                request.EpisodeOfCareId = request.WriteToCDS(_serviceProxy).ToString();

                                // now we will add in the Observations
                                Device   device   = Devices[SampleDataCache.SelectRandomItem.Next(0, Devices.Count - 1)];
                                Location location = Locations[SampleDataCache.SelectRandomItem.Next(0, Locations.Count - 1)];

                                Observation observation = new Observation();

                                observation.PatientId       = ContactId;
                                observation.Description     = FullName + " - Observation";
                                observation.EpisodeOfCareId = request.EpisodeOfCareId;
                                observation.LocationId      = location.LocationId;
                                observation.DeviceId        = device.DeviceId;

                                observation.WriteToCDS(_serviceProxy);

                                Observations.Add(observation);
                            }
                        }

                        if (Careplans.Count > 0)
                        {
                            foreach (CarePlan plan in Careplans)
                            {
                                Encounter encounter = Encounters[SampleDataCache.SelectRandomItem.Next(0, Encounters.Count - 1)];

                                plan.PatientId   = ContactId;
                                plan.EncounterId = encounter.EncounterId;
                                plan.Description = FullName + " - Description";
                                plan.Title       = FullName + " - Care Plan;";
                                plan.CarePlanId  = plan.WriteToCDS(_serviceProxy).ToString();
                            }
                        }

                        if (Conditions.Count > 0)
                        {
                            foreach (Condition condition in Conditions)
                            {
                                Appointment appointment = Appointments[SampleDataCache.SelectRandomItem.Next(0, Appointments.Count - 1)];
                                CarePlan    careplan    = Careplans[SampleDataCache.SelectRandomItem.Next(0, Careplans.Count - 1)];

                                condition.PatientId      = ContactId;
                                condition.PractitionerId = PrimaryPractitionerId;
                                condition.AsserterId     = ContactId;
                                condition.AppointmentId  = appointment.AppointmentId;
                                condition.CarePlanId     = careplan.CarePlanId;
                                condition.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (RiskAssessments.Count > 0)
                        {
                            foreach (RiskAssessment assessment in RiskAssessments)
                            {
                                Encounter encounter = Encounters[SampleDataCache.SelectRandomItem.Next(0, Encounters.Count - 1)];
                                Condition condition = Conditions[SampleDataCache.SelectRandomItem.Next(0, Conditions.Count - 1)];

                                assessment.PatientId      = ContactId;
                                assessment.PractitionerId = PrimaryPractitionerId;
                                assessment.ConditionId    = condition.ConditionId;
                                assessment.EncounterId    = encounter.EncounterId;
                                assessment.Name           = FullName + " - Risk Assessment";
                                assessment.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (Procedures.Count > 0)
                        {
                            foreach (Procedure procedure in Procedures)
                            {
                                Encounter encounter = Encounters[SampleDataCache.SelectRandomItem.Next(0, Encounters.Count - 1)];

                                procedure.EncounterId = encounter.EncounterId;
                                procedure.PatientId   = ContactId;
                                procedure.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (Careteams.Count > 0)
                        {
                            foreach (CareTeam team in Careteams)
                            {
                                team.PatientId  = ContactId;
                                team.Name       = addContact.FullName + " - Care Team;";
                                team.CareTeamId = team.WriteToCDS(_serviceProxy).ToString();

                                // for each care team we will add the participants
                                CareTeamParticipant patient       = new CareTeamParticipant();
                                CareTeamParticipant relatedperson = new CareTeamParticipant();
                                CareTeamParticipant practitioner  = new CareTeamParticipant();

                                patient.CareTeamId       = team.CareTeamId;
                                relatedperson.CareTeamId = team.CareTeamId;
                                practitioner.CareTeamId  = team.CareTeamId;

                                patient.PatientId = ContactId;

                                relatedperson.RelatedPersonId = PrimaryContactId;
                                relatedperson.PatientId       = ContactId;

                                practitioner.PractitionerId = PrimaryPractitionerId;
                                practitioner.PatientId      = ContactId;

                                patient.MemberType       = (int)HealthCDMEnums.CareTeamParticipant_Membertype.Patient;
                                relatedperson.MemberType = (int)HealthCDMEnums.CareTeamParticipant_Membertype.RelationPerson;
                                practitioner.MemberType  = (int)HealthCDMEnums.CareTeamParticipant_Membertype.Practitioner;

                                patient.WriteToCDS(_serviceProxy);
                                relatedperson.WriteToCDS(_serviceProxy);
                                practitioner.WriteToCDS(_serviceProxy);

                                CareTeamParticipants.Add(patient);
                                CareTeamParticipants.Add(relatedperson);
                                CareTeamParticipants.Add(practitioner);

                                //patient.RoleId = SampleDataCache.CodeableConcepts[HealthCDMEnums.CodeableConcept_Type.ParticipantRole.ToString()]
                                //    .Values.ElementAt(SampleDataCache.SelectRandomItem.Next(0, SampleDataCache.CodeableConcepts[HealthCDMEnums.CodeableConcept_Type.ParticipantRole.ToString()]
                                //    .Values.Count - 1)).Key;
                            }
                        }

                        if (Specimens.Count > 0)
                        {
                            foreach (Specimen specimen in Specimens)
                            {
                                Device device = Devices[SampleDataCache.SelectRandomItem.Next(0, Devices.Count - 1)];

                                specimen.PatientId      = ContactId;
                                specimen.PractitionerId = PrimaryPractitionerId;
                                specimen.DeviceId       = device.DeviceId;
                                specimen.Name           = FullName + " - Specimen";
                                specimen.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (Tasks.Count > 0)
                        {
                            foreach (PatientTask task in Tasks)
                            {
                                Encounter encounter = Encounters[SampleDataCache.SelectRandomItem.Next(0, Encounters.Count - 1)];

                                task.OwnerPatientId           = ContactId;
                                task.RequestingPatientId      = ContactId;
                                task.Subject                  = FullName + " - Task";
                                task.OwnerPractitionerId      = PrimaryPractitionerId;
                                task.RequestingPractitionerId = PrimaryPractitionerId;
                                task.EncounterId              = encounter.EncounterId;
                                task.WriteToCDS(_serviceProxy);
                            }
                        }

                        #endregion
                    }
                    else
                    {
                        throw new Exception("Contact Id == null");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return(profileId);
        }
Example #4
0
        public override Guid WriteToCDS(OrganizationServiceProxy _serviceProxy)
        {
            Guid profileId = Guid.Empty;

            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                HealthCDM.Contact addContact = new HealthCDM.Contact();

                //Set standard atttributes (this could be done via reflection)
                // but for now this is all we are setting
                addContact.GenderCode               = new OptionSetValue(GenderCode);
                addContact.FirstName                = FirstName;
                addContact.LastName                 = LastName;
                addContact.Address1_Line1           = Address1Line1;
                addContact.Address1_City            = Address1City;
                addContact.Address1_StateOrProvince = Address1StateOrProvince;
                addContact.Address1_PostalCode      = Address1PostalCode;
                addContact.Telephone1               = Telephone1;
                addContact.MobilePhone              = MobilePhone;
                addContact.Telephone2               = Telephone2;
                addContact.Address1_Country         = Address1Country;
                addContact.EMailAddress1            = FirstName + "." + LastName + "@" + EmailAddressDomain;
                addContact.Address1_Country         = Address1Country;
                addContact.Salutation               = Salutation;
                addContact.BirthDate                = BirthDate;
                // set the primary language
                addContact.msemr_Communication1Language = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, PrimaryLanguageCode, (int)HealthCDMEnums.CodeableConcept_Type.Language));

                addContact.msemr_ContactType = new OptionSetValue((int)HealthCDMEnums.Contact_Contacttype.Practitioner);

                try
                {
                    profileId = _serviceProxy.Create(addContact);

                    if (profileId != Guid.Empty)
                    {
                        ContactId = profileId.ToString();

                        // now that we have the ContactId for this Practitioner we need
                        // to create the roles we have and the qualifications

                        foreach (PractitionerRole role in Roles)
                        {
                            role.PractitionerId = ContactId;

                            try
                            {
                                role.WriteToCDS(_serviceProxy);
                            }
                            catch (Exception rex)
                            {
                                Console.WriteLine(rex.ToString());
                            }
                        }

                        foreach (PractitionerQualification qualification in Qualifications)
                        {
                            qualification.PractitionerId = ContactId;

                            try
                            {
                                qualification.WriteToCDS(_serviceProxy);
                            }
                            catch (Exception qex)
                            {
                                Console.WriteLine(qex.ToString());
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Contact Id == null");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return(profileId);
        }
Example #5
0
        public override Guid WriteToCDS(OrganizationServiceProxy _serviceProxy)
        {
            Guid profileId = Guid.Empty;

            try
            {
                HealthCDM.Contact addContact = new HealthCDM.Contact();

                //Set standard atttributes (this could be done via reflection)
                // but for now this is all we are setting
                addContact.GenderCode               = new OptionSetValue(GenderCode);
                addContact.FirstName                = FirstName;
                addContact.LastName                 = LastName;
                addContact.Address1_Line1           = Address1Line1;
                addContact.Address1_City            = Address1City;
                addContact.Address1_StateOrProvince = Address1StateOrProvince;
                addContact.Address1_PostalCode      = Address1PostalCode;
                addContact.Telephone1               = Telephone1;
                addContact.MobilePhone              = MobilePhone;
                addContact.Telephone2               = Telephone2;
                addContact.Address1_Country         = Address1Country;
                addContact.EMailAddress1            = FirstName + "." + LastName + "@" + EmailAddressDomain;
                addContact.Address1_Country         = Address1Country;
                addContact.Salutation               = Salutation;
                addContact.BirthDate                = BirthDate;
                // set the primary language
                addContact.msemr_Communication1Language = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, PrimaryLanguageCode, (int)HealthCDMEnums.CodeableConcept_Type.Language));

                addContact.msemr_ContactType = new OptionSetValue((int)HealthCDMEnums.Contact_Contacttype.Patient);

                // Set the Primary Practitioner, Emergency Contact & Primary Contacts
                if (!string.IsNullOrEmpty(PrimaryPractitionerId))
                {
                    addContact.msemr_GeneralPractioner = new EntityReference(HealthCDM.Contact.EntityLogicalName, Guid.Parse(PrimaryPractitionerId));
                }

                if (!string.IsNullOrEmpty(EmergencyContactId))
                {
                    addContact.msemr_Contact1             = new EntityReference(HealthCDM.Contact.EntityLogicalName, Guid.Parse(EmergencyContactId));
                    addContact.msemr_Contact1Relationship = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, EmergencyContactRelationshipTypeId, (int)HealthCDMEnums.CodeableConcept_Type.PatientRelationshipType));
                }

                if (!string.IsNullOrEmpty(PrimaryContactId))
                {
                    addContact.msemr_Contact2             = new EntityReference(HealthCDM.Contact.EntityLogicalName, Guid.Parse(PrimaryContactId));
                    addContact.msemr_Contact2Relationship = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, PrimaryContactRelationshipTypeId, (int)HealthCDMEnums.CodeableConcept_Type.PatientRelationshipType));
                }

                addContact.msemr_MedicalRecordNumber = PatientMedicalNumber;

                try
                {
                    profileId = _serviceProxy.Create(addContact);

                    if (profileId != Guid.Empty)
                    {
                        ContactId = profileId.ToString();

                        // Add alergies to patient
                        if (AllergyIntolerances.Count > 0)
                        {
                            foreach (AllergyIntolerance ai in AllergyIntolerances)
                            {
                                ai.PatientId = ContactId;
                                ai.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (NutritionOrders.Count > 0)
                        {
                            foreach (NutritionOrder no in NutritionOrders)
                            {
                                no.Patient      = ContactId;
                                no.Practitioner = PrimaryPractitionerId;
                                no.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (Conditions.Count > 0)
                        {
                            foreach (Condition condition in Conditions)
                            {
                                condition.PatientId      = ContactId;
                                condition.PractitionerId = PrimaryPractitionerId;
                                condition.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (Devices.Count > 0)
                        {
                            foreach (Device device in Devices)
                            {
                                device.PatientId = ContactId;
                                device.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (Procedures.Count > 0)
                        {
                            foreach (Procedure procedure in Procedures)
                            {
                                procedure.PatientId = ContactId;
                                procedure.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (ReferralRequests.Count > 0)
                        {
                            foreach (ReferralRequest request in ReferralRequests)
                            {
                                request.PatientId      = ContactId;
                                request.PractitionerId = primaryPractitionerId;
                                request.WriteToCDS(_serviceProxy);
                            }
                        }

                        if (MedicationRequests.Count > 0)
                        {
                            foreach (MedicationRequest request in MedicationRequests)
                            {
                                request.PatientId      = ContactId;
                                request.PractitionerId = primaryPractitionerId;
                                request.WriteToCDS(_serviceProxy);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Contact Id == null");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return(profileId);
        }
Example #6
0
        public override Guid WriteToCDS(string cdsUrl, string cdsUserName, string cdsPassword)
        {
            Guid profileId = Guid.Empty;

            try
            {
                #region Get our Login Information

                // setup the variables
                OrganizationServiceProxy _serviceProxy;

                // homeRealmUri will stay null for now
                Uri homeRealmUri = null;

                // setup credentials from whatever is in the app.config
                ClientCredentials credentials;

                // same for organizationuri comes from app.config
                Uri organizationUri;

                // set the organization uri from what was in the app.config
                organizationUri = new Uri(cdsUrl);

                credentials = new ClientCredentials();
                credentials.UserName.UserName = cdsUserName;
                credentials.UserName.Password = cdsPassword;

                #endregion

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                using (_serviceProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null))
                {
                    // To impersonate set the GUID of CRM user here (which I merely took from CRM itself
                    // would need not to use this caller id in the future (as it will change per instance of CRM)
                    //_serviceProxy.CallerId = new Guid("14D40CB7-81D5-E311-93F5-00155D00330C");
                    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

                    //enable using proxy types
                    _serviceProxy.EnableProxyTypes();

                    HealthCDM.Contact addContact = new HealthCDM.Contact();

                    //Set standard atttributes (this could be done via reflection)
                    // but for now this is all we are setting
                    addContact.GenderCode               = new OptionSetValue(GenderCode);
                    addContact.FirstName                = FirstName;
                    addContact.LastName                 = LastName;
                    addContact.Address1_Line1           = Address1Line1;
                    addContact.Address1_City            = Address1City;
                    addContact.Address1_StateOrProvince = Address1StateOrProvince;
                    addContact.Address1_PostalCode      = Address1PostalCode;
                    addContact.Telephone1               = Telephone1;
                    addContact.MobilePhone              = MobilePhone;
                    addContact.Telephone2               = Telephone2;
                    addContact.Address1_Country         = Address1Country;
                    addContact.EMailAddress1            = FirstName + "." + LastName + "@" + EmailAddressDomain;
                    addContact.Address1_Country         = Address1Country;
                    addContact.Salutation               = Salutation;
                    addContact.BirthDate                = BirthDate;
                    // set the primary language
                    addContact.msemr_Communication1Language = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, PrimaryLanguageCode, (int)HealthCDMEnums.CodeableConcept_Type.Language));

                    addContact.msemr_ContactType = new OptionSetValue((int)HealthCDMEnums.Contact_Contacttype.Patient);

                    // Set the Primary Practitioner, Emergency Contact & Primary Contacts
                    if (!string.IsNullOrEmpty(PrimaryPractitionerId))
                    {
                        addContact.msemr_GeneralPractioner = new EntityReference(HealthCDM.Contact.EntityLogicalName, Guid.Parse(PrimaryPractitionerId));
                    }

                    if (!string.IsNullOrEmpty(EmergencyContactId))
                    {
                        addContact.msemr_Contact1             = new EntityReference(HealthCDM.Contact.EntityLogicalName, Guid.Parse(EmergencyContactId));
                        addContact.msemr_Contact1Relationship = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, EmergencyContactRelationshipTypeId, (int)HealthCDMEnums.CodeableConcept_Type.PatientRelationshipType));
                    }

                    if (!string.IsNullOrEmpty(PrimaryContactId))
                    {
                        addContact.msemr_Contact2             = new EntityReference(HealthCDM.Contact.EntityLogicalName, Guid.Parse(PrimaryContactId));
                        addContact.msemr_Contact2Relationship = new EntityReference(HealthCDM.msemr_codeableconcept.EntityLogicalName, GetCodeableConceptId(_serviceProxy, PrimaryContactRelationshipTypeId, (int)HealthCDMEnums.CodeableConcept_Type.PatientRelationshipType));
                    }

                    addContact.msemr_MedicalRecordNumber = PatientMedicalNumber;

                    try
                    {
                        profileId = _serviceProxy.Create(addContact);

                        if (profileId != Guid.Empty)
                        {
                            ContactId = profileId.ToString();

                            // Add alergies to patient
                            if (AllergyIntolerances.Count > 0)
                            {
                                foreach (AllergyIntolerance ai in AllergyIntolerances)
                                {
                                    ai.PatientId = ContactId;
                                    ai.WriteToCDS(_serviceProxy);
                                }
                            }

                            if (NutritionOrders.Count > 0)
                            {
                                foreach (NutritionOrder no in NutritionOrders)
                                {
                                    no.Patient      = ContactId;
                                    no.Practitioner = PrimaryPractitionerId;
                                    no.WriteToCDS(_serviceProxy);
                                }
                            }

                            if (Conditions.Count > 0)
                            {
                                foreach (Condition condition in Conditions)
                                {
                                    condition.PatientId      = ContactId;
                                    condition.PractitionerId = PrimaryPractitionerId;
                                    condition.WriteToCDS(_serviceProxy);
                                }
                            }

                            if (Devices.Count > 0)
                            {
                                foreach (Device device in Devices)
                                {
                                    device.PatientId = ContactId;
                                    device.WriteToCDS(_serviceProxy);
                                }
                            }

                            if (Procedures.Count > 0)
                            {
                                foreach (Procedure procedure in Procedures)
                                {
                                    procedure.PatientId = ContactId;
                                    procedure.WriteToCDS(_serviceProxy);
                                }
                            }

                            if (ReferralRequests.Count > 0)
                            {
                                foreach (ReferralRequest request in ReferralRequests)
                                {
                                    request.PatientId      = ContactId;
                                    request.PractitionerId = primaryPractitionerId;
                                    request.WriteToCDS(_serviceProxy);
                                }
                            }

                            if (MedicationRequests.Count > 0)
                            {
                                foreach (MedicationRequest request in MedicationRequests)
                                {
                                    request.PatientId      = ContactId;
                                    request.PractitionerId = primaryPractitionerId;
                                    request.WriteToCDS(_serviceProxy);
                                }
                            }
                        }
                        else
                        {
                            throw new Exception("Contact Id == null");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return(profileId);
        }