Beispiel #1
0
 private void CheckProviderRequiredFields(ProviderV2 provider)
 {
     if (string.IsNullOrEmpty(provider.FirstName))
         throw new InvalidOperationException("Provider FirstName is required.");
     if (string.IsNullOrEmpty(provider.LastName))
         throw new InvalidOperationException("Provider LastName is required.");
 }
        public static void CapturePropertiesForUpdate(ProviderV2 source, Provider existingProvider)
        {
            existingProvider.Npi = (string.IsNullOrEmpty(source.Npi) ? null : (long?)long.Parse(source.Npi, CultureInfo.InvariantCulture)) ?? existingProvider.Npi;

            existingProvider.SetName(
                source.NamePrefix ?? existingProvider.NamePrefix,
                source.FirstName ?? existingProvider.FirstName,
                source.MiddleName ?? existingProvider.MiddleName,
                source.LastName ?? existingProvider.LastName,
                source.NameSuffix ?? existingProvider.NameSuffix,
                source.AdditionalSuffix ?? existingProvider.AdditionalSuffix);

            existingProvider.AdditionalInformation = source.AdditionalInformation ?? existingProvider.AdditionalInformation;
            existingProvider.SetDateOfBirth(source.DateOfBirth != null ? ConvertToNullibleDateTime(source.DateOfBirth, null, "DateOfBirth", existingProvider.FullName) : existingProvider.DateOfBirth);
            existingProvider.SetEmail(source.Email ?? existingProvider.Email);
            existingProvider.SetFax(source.Fax ?? existingProvider.Fax);
            existingProvider.SetInPracticeSince(source.InPracticeSince != null ? ConvertToNullibleDateTime(source.InPracticeSince, null, "InPracticeSince", existingProvider.FullName) : existingProvider.InPracticeSince);
            existingProvider.IsEnabled = source.IsEnabled != null ? ConvertToBool(source.IsEnabled, true, "IsEnabled", existingProvider.FullName) : existingProvider.IsEnabled;
            existingProvider.PhilosophyOfCare = source.PhilosophyOfCare ?? existingProvider.PhilosophyOfCare;
            existingProvider.SetPhone(source.Phone ?? existingProvider.Phone);
            existingProvider.SetWebsite(source.Website ?? existingProvider.Website);
            existingProvider.SetAboutMe(source.AboutMe ?? existingProvider.AboutMe);
            existingProvider.VideoContent = source.VideoContent ?? existingProvider.VideoContent;
            existingProvider.UpdatedDate = DateTime.UtcNow;
            existingProvider.Keywords = source.Keywords ?? existingProvider.Keywords;
            existingProvider.CustomKeywords = source.CustomKeywords ?? existingProvider.CustomKeywords;
            existingProvider.ExcludedKeywords = source.ExcludedKeywords ?? existingProvider.ExcludedKeywords;
            existingProvider.ImageUrl = source.ImageUrl ?? existingProvider.ImageUrl;
            existingProvider.PictureId = source.PictureId == null ? existingProvider.PictureId : ConvertToNullibleInt(source.PictureId, "PictureId", existingProvider.FullName);

            existingProvider.Custom1 = source.Custom1 ?? existingProvider.Custom1;
            existingProvider.Custom2 = source.Custom2 ?? existingProvider.Custom2;
            existingProvider.Custom3 = source.Custom3 ?? existingProvider.Custom3;
        }
        public static void AddExternalIdMapping(ObjectContext context, ProviderV2 source, Provider provider)
        {
            if (!string.IsNullOrEmpty(source.ProviderExternalId))
            {
                var existingMappings = context.CreateObjectSet<DataImportEntityIdMap>()
                    .Where(m => m.EntityName == "Provider" && m.InternalId == provider.Id)
                    .ToList();

                if (existingMappings.Count == 1)
                {
                    if (existingMappings[0].ExternalId != source.ProviderExternalId)
                    {
                        // Update ExternalId on existing mapping
                        existingMappings[0].ExternalId = source.ProviderExternalId;
                    }
                }
                else
                {
                    // Remove ambiguous mappings (if any)
                    if (existingMappings.Count > 1)
                    {
                        foreach (var mapping in existingMappings)
                        {
                            context.DeleteObject(mapping);
                        }
                    }

                    // Create new mapping
                    context.AddObject("DataImportEntityIdMaps", new DataImportEntityIdMap
                    {
                        EntityName = "Provider",
                        DataImportId = 2,
                        InternalId = provider.Id,
                        ExternalId = source.ProviderExternalId
                    });
                }
            }
        }
Beispiel #4
0
        private string GetProvider(ConvertServiceRequest request)
        {
            var providerId = 0;
            if (request.Parameters.ContainsKey("externalId"))
            {
                providerId = LookupDataEntityMapId(request.Parameters["externalId"], false);
            }
            else if (request.Parameters.ContainsKey("id"))
            {
                if (!int.TryParse(request.Parameters["id"], out providerId))
                    throw new BusinessException("Provider Id must be a valid integer");
            }

            // create the requests
            var profileRequest = new ReadProviderProfileRequest { ProviderId = providerId, BypassCache = true };
            var locationsRequest = new ListProviderOrgUnitsRequest { ProviderId = providerId, BypassCache = true, IncludeExternalIds = true };
            var educationRequest = new ListProviderEducationRequest { ProviderId = providerId, BypassCache = true, IncludeIncomplete = true };
            var languageRequst = new ListProviderLanguagesRequest { ProviderId = providerId, BypassCache = true };
            var clinicalRequest = new ListProviderClinicalInterestRequest { ProviderId = providerId, BypassCache = true };
            var certificationRequest = new ListProviderCertificationSpecialtyRequest { ProviderId = providerId, BypassCache = true };
            var orgUnitAffiliationRequest = new ListProviderOrgUnitAffiliationRequest { ProviderId = providerId, ShowAdminList = true, BypassCache = true };
            var providerTypesRequest = new ListProviderProviderTypeRequest { ProviderId = providerId, BypassCache = true };
            var providerSpecialtiesRequest = new ListProviderSpecialtiesRequest { ProviderId = providerId, BypassCache = true };

            // add the requests to the dispatcher
            var provider = ProcessRequest<ReadProviderProfileResponse>(profileRequest).Provider;
            var locations = ProcessRequest<ListProviderOrgUnitsResponse>(locationsRequest).OrgUnits;
            var educations = ProcessRequest<ListProviderEducationResponse>(educationRequest).Records;
            var languages = ProcessRequest<ListProviderLanguagesResponse>(languageRequst).Languages;
            var clinicalInterests = ProcessRequest<ListProviderClinicalInterestResponse>(clinicalRequest).ClinicalInterests;
            var certifications = ProcessRequest<ListProviderCertificationSpecialtyResponse>(certificationRequest).CertificationSpecialties;
            var orgUnitAffiliations = ProcessRequest<ListProviderOrgUnitAffiliationResponse>(orgUnitAffiliationRequest).Records;
            var providerTypes = ProcessRequest<ListProviderProviderTypeResponse>(providerTypesRequest).ProviderTypes;
            var providerSpecialties = ProcessRequest<ListProviderSpecialtiesResponse>(providerSpecialtiesRequest).ProviderSpecialties;

            var viewModel = new ProviderV2
            {
                ProviderId = provider.Id.ToString(CultureInfo.InvariantCulture),
                Npi = provider.Npi.HasValue ? provider.Npi.Value.ToString(CultureInfo.InvariantCulture) : null,
                NamePrefix = provider.NamePrefix,
                FirstName = provider.FirstName,
                MiddleName = provider.MiddleName,
                LastName = provider.LastName,
                NameSuffix = provider.NameSuffix,
                AdditionalSuffix = provider.AdditionalSuffix,
                Gender = provider.Gender,
                DateOfBirth = provider.DateOfBirth.HasValue ? provider.DateOfBirth.Value.ToString(CultureInfo.InvariantCulture) : string.Empty,
                Phone = provider.Phone,
                Fax = provider.Fax,
                Email = provider.Email,
                Website = provider.Website,
                PhilosophyOfCare = provider.PhilosophyOfCare,
                AdditionalInformation = provider.AdditionalInformation,
                InPracticeSince = provider.InPracticeSince.HasValue ? provider.InPracticeSince.Value.ToString(CultureInfo.InvariantCulture) : string.Empty,
                ProviderGroup = provider.ProviderGroupName,
                PictureId = provider.PictureId == 0 ? null : provider.PictureId.ToString(CultureInfo.InvariantCulture),
                ImageUrl = provider.ImageUrl,
                VideoContent = provider.VideoContent,
                AboutMe = provider.AboutMe,
                IsEnabled = provider.IsEnabled.ToString(CultureInfo.InvariantCulture),
                ProviderExternalId = provider.ProviderExternalId,
                Keywords = provider.Keywords,
                CustomKeywords = provider.CustomKeywords,
                ExcludedKeywords = provider.ExcludedKeywords,
                Custom1 = provider.Custom1,
                Custom2 = provider.Custom2,
                Custom3 = provider.Custom3,
                DynamicColumns = provider.DynamicColumns,

                Specialties = providerSpecialties.Select(s => new ProviderSpecialtyV2
                {
                    SpecialtyName = s.SpecialtyName,
                    IsPrimary = s.IsPrimary.ToString(CultureInfo.InvariantCulture),
                    IsBoardCertified = s.IsBoardCertified.ToString(CultureInfo.InvariantCulture),
                    SpecialtyType = s.SpecialtyType
                }).ToList(),

                Languages = languages.Select(l => new ProviderLanguageV2
                {
                    LanguageName = l.LanguageName,
                    IsFluent = l.IsFluent.ToString(CultureInfo.InvariantCulture),
                    IsPrimary = l.IsPrimary.ToString(CultureInfo.InvariantCulture)
                }).ToList(),

                EducationTypes = educations.Select(e => new ProviderEducationV2
                {
                    EducationTypeName = e.EducationTypeName,
                    YearCompleted = e.YearCompleted,
                    InstitutionName = e.InstitutionName,
                    IsComplete = e.IsCompleted.ToString(CultureInfo.InvariantCulture)
                }).ToList(),

                ClinicalInterests = clinicalInterests.Select(c => new ProviderClinicalInterestV2
                {
                    Name = c.ClinicalInterestName
                }).ToList(),

                OrgUnits = locations.Select(o => new ProviderOrgUnitV2
                {
                    OrgUnitId = o.OrgUnitId.ToString(CultureInfo.InvariantCulture),
                    OrgUnitExternalId = o.OrgUnitExternalId,
                    Phone = o.DisplayPhone,
                    Fax = o.DisplayFax,
                    IsPrimary = o.IsPrimary.ToString(CultureInfo.InvariantCulture),
                    IsAcceptingNewPatients = o.IsAcceptingNewPatients.ToString(CultureInfo.InvariantCulture),
                    ProviderOrgUnitId = o.Id.ToString(CultureInfo.InvariantCulture),
                    Insurances = o.Insurance.Where(i => !i.IsInheritedDisabled).Select(i => new ProviderOrgUnitInsuranceV2
                    {
                        Name = i.InsuranceName
                    }).ToList(),
                    Services = o.Service.Select(s => new ProviderOrgUnitServiceV2
                    {
                        Name = s.ServiceName
                    }).ToList(),
                    Schedules = o.Schedule.Select(s => new ProviderOrgUnitScheduleV2
                    {
                        OpenTime = s.Open,
                        CloseTime = s.Close,
                        DayOfWeek = s.Day
                    }).ToList(),
                    DisabledInheritedInsurances = o.Insurance.Where(i => i.IsInherited && i.IsInheritedDisabled).Select(i => new DisabledInheritedInsuranceV2
                    {
                        Name = i.InsuranceName
                    }).ToList()
                }).ToList(),

                CertificationAgencies = certifications.Select(c => new CertificationAgencyV2
                {
                    AgencyName = c.CertificationAgencyName,
                    CertificationBoards = new CertificationBoardV2[1]{new CertificationBoardV2
                {
                    BoardName = c.CertificationBoardName,
                    CertificationSpecialties = new CertificationSpecialtyV2[1]{new CertificationSpecialtyV2
                    {
                        Name = c.CertificationSpecialtyName,
                        YearOfIssuance = c.YearOfIssuance
                    }}.ToList()
                }}.ToList()
                }).ToList(),

                OrgUnitAffiliations = orgUnitAffiliations.Select(a => new ProviderOrgUnitAffiliationV2
                {
                    OrgUnitId = a.OrgUnitId.ToString(CultureInfo.InvariantCulture),
                    OrgUnitTypeId = a.OrgUnitTypeId.ToString(CultureInfo.InvariantCulture),
                    Service = a.ServiceName
                }).ToList(),

                ProviderTypes = providerTypes.Select(t => new ProviderTypeV2
                {
                    Name = t.ProviderTypeName
                }).ToList()
            };

            return CommonUtils.XmlSerialize(viewModel);
        }
        private static void CheckForDuplicateOrgUnits(ProviderV2 source)
        {
            //check for duplicate orgUnitExternalId's
            var list = source.OrgUnits.Where(o => o.OrgUnitExternalId != null);

            var dupCheck = list.GroupBy(o => o.OrgUnitExternalId).Where(g => g.Count() > 1);
            if (dupCheck.Count() > 0)
                throw new BusinessException("Duplicate OrgUnitExternalIds found.");

            //check for duplicate orgUnitId's
            list = source.OrgUnits.Where(o => o.OrgUnitId != null);

            dupCheck = list.GroupBy(o => o.OrgUnitId).Where(g => g.Count() > 1);
            if (dupCheck.Count() > 0)
                throw new BusinessException("Duplicate OrgUnitIds found.");
        }
        public static void SetProviderSpecialties(ObjectContext context, ProviderV2 source, Provider provider)
        {
            if (source.Specialties == null)
                return;

            try
            {
                foreach (var item in provider.ProviderSpecialties.ToArray())
                    context.DeleteObject(item);

                var specialtiesObjectSet = context.CreateObjectSet<Specialty>();

                //Get all referenced parent specialties
                var uniqueParentSpecialties = source.Specialties
                    .Where(s => !string.IsNullOrEmpty(s.ParentSpecialtyName))
                    .Select(s => new KeyValuePair<string, string>(s.ParentSpecialtyName, s.SpecialtyType))
                    .Distinct();

                //Ensure parent specialties exist
                var parentSpecialties = EnsureParentSpecialtiesExist(uniqueParentSpecialties, specialtiesObjectSet);

                var providerSpecialties = new List<ProviderSpecialty>();

                foreach (var item in source.Specialties)
                {
                    var specialty = specialtiesObjectSet.FirstOrDefault(s => s.Name == item.SpecialtyName);

                    if (specialty == null)
                    {
                        specialty = parentSpecialties.FirstOrDefault(s => s.Name == item.SpecialtyName);

                        if (specialty == null)
                        {
                            var specialtyType = string.IsNullOrEmpty(item.SpecialtyType) ? SpecialtyType.Specialty : item.SpecialtyType;

                            specialty = new Specialty() { Name = item.SpecialtyName, IsEnabled = true, SpecialtyType = specialtyType };
                            if (!string.IsNullOrEmpty(item.ParentSpecialtyName))
                            {
                                specialty.ParentSpecialty = parentSpecialties.FirstOrDefault(s => s.Name == item.ParentSpecialtyName && s.SpecialtyType == specialtyType);
                            }
                            specialtiesObjectSet.AddObject(specialty);
                        }
                    }

                    providerSpecialties.Add(new ProviderSpecialty(provider, specialty)
                    {
                        IsBoardCertified = ConvertToBool(item.IsBoardCertified, false, "Specialty/IsBoardCertified", provider.FullName),
                        IsPrimary = ConvertToBool(item.IsPrimary, false, "Specialty/IsPrimary", provider.FullName),
                    });
                }

                provider.ProviderSpecialties = providerSpecialties;
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing specialties for provider '" + provider.Name + "' - " + ex.Message, ex);
            }
        }
        public static void SetProviderProviderTypes(ObjectContext context, ProviderV2 source, Provider provider)
        {
            if (source.ProviderTypes == null)
                return;

            try
            {
                var existingProviderTypes = provider.ProviderProviderTypes.ToArray();
                foreach (var item in existingProviderTypes)
                    context.DeleteObject(item);

                var providerTypes = context.CreateObjectSet<ProviderType>();
                var providerProviderTypes = new List<ProviderProviderType>();

                foreach (var item in source.ProviderTypes)
                {
                    //Ensure Provider Type Exists
                    var pt = providerTypes.FirstOrDefault(s => s.Name == item.Name);
                    if (pt == null)
                    {
                        pt = new ProviderType
                        {
                            Name = item.Name,
                            IsEnabled = true
                        };
                        providerTypes.AddObject(pt);
                    }

                    var addedProviderType = new ProviderProviderType(provider, pt);
                    providerProviderTypes.Add(addedProviderType);
                }

                provider.ProviderProviderTypes = providerProviderTypes;
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing provider types for provider '" + provider.Name + "' - " + ex.Message, ex);
            }
        }
        public static void SetProviderOrgUnits(ObjectContext context, ProviderV2 source, Provider provider)
        {
            if (source.OrgUnits == null)
                return;

            if (source.OrgUnits.Where(o => ConvertToBool(o.IsPrimary, false, "OrgUnit/IsPrimary", provider.FullName)).Count() > 1)
                throw new BusinessException("Only 1 org unit can be specified as the primary org unit.");

            try
            {
                var providerOrgUnits = provider.ProviderOrgUnits.ToArray();
                List<int> currentOrgUnitIds = new List<int>();

                CheckForDuplicateOrgUnits(source);

                foreach (var item in source.OrgUnits)
                {
                    int orgUnitId = 0;

                    if (string.IsNullOrEmpty(item.OrgUnitId) && string.IsNullOrEmpty(item.OrgUnitExternalId))
                        throw new BusinessException("Please supply either the OrgUnitId or the OrgUnitExternalId");

                    if (!string.IsNullOrEmpty(item.OrgUnitExternalId))
                        orgUnitId = LookupInternalId(context, item.OrgUnitExternalId, "OrgUnit");
                    else if (!string.IsNullOrEmpty(item.OrgUnitId))
                        orgUnitId = ConvertToInt(item.OrgUnitId, "OrgUnitId", provider.FullName);

                    currentOrgUnitIds.Add(orgUnitId);

                    var existingProviderOrgUnit = providerOrgUnits.SingleOrDefault(pou => pou.OrgUnitId == orgUnitId);
                    if (existingProviderOrgUnit != null)
                    {
                        existingProviderOrgUnit.Phone = item.Phone;
                        existingProviderOrgUnit.IsPrimary = ConvertToBool(item.IsPrimary, false, "OrgUnit/IsPrimary", provider.FullName);
                        existingProviderOrgUnit.Fax = item.Fax;
                        existingProviderOrgUnit.IsAcceptingNewPatients = ConvertToBool(item.IsAcceptingNewPatients, false, "OrgUnit/IsAcceptingNewPatients", provider.FullName);
                        existingProviderOrgUnit.OrgUnitExternalId = item.OrgUnitExternalId;
                        existingProviderOrgUnit.ProviderOrgUnitInsurances = SetInsurances(context, item, provider, orgUnitId, existingProviderOrgUnit.ProviderOrgUnitInsurances);
                        existingProviderOrgUnit.ProviderOrgUnitServices = SetServices(context, item, provider, existingProviderOrgUnit.ProviderOrgUnitServices);
                        existingProviderOrgUnit.ProviderOrgUnitInsurancesInheritedDisableds = SetDisabledInheritedInsurances(context, item, provider, existingProviderOrgUnit.ProviderOrgUnitInsurancesInheritedDisableds);
                        existingProviderOrgUnit.Schedules = SetSchedules(context, item, existingProviderOrgUnit.Schedules);
                        existingProviderOrgUnit.AllowAppointmentRequests = ConvertToBool(item.AllowAppointmentRequests, true, "OrgUnit/AllowAppointmentRequests", provider.FullName);

                    }
                    else
                    {
                        var orgUnit = context.CreateObjectSet<OrgUnit>().SingleOrDefault(o => o.Id == orgUnitId);

                        if (orgUnit == null)
                            throw new BusinessException("Unable to find Org Unit with Internal Id of " + orgUnitId);

                        var providerOrgUnit = new ProviderOrgUnit(provider, orgUnit)
                        {
                            Phone = item.Phone,
                            IsPrimary = ConvertToBool(item.IsPrimary, false, "OrgUnit/IsPrimary", provider.FullName),
                            Fax = item.Fax,
                            IsAcceptingNewPatients = ConvertToBool(item.IsAcceptingNewPatients, false, "OrgUnit/IsAcceptingNewPatients", provider.FullName),
                            AllowAppointmentRequests = ConvertToBool(item.AllowAppointmentRequests, true, "OrgUnit/AllowAppointmentRequests", provider.FullName),
                            OrgUnitExternalId = item.OrgUnitExternalId,
                            ProviderOrgUnitInsurances = SetInsurances(context, item, provider, orgUnitId, new List<ProviderOrgUnitInsurance>()),
                            ProviderOrgUnitServices = SetServices(context, item, provider, new List<ProviderOrgUnitService>()),
                            ProviderOrgUnitInsurancesInheritedDisableds = SetDisabledInheritedInsurances(context, item, provider, new List<ProviderOrgUnitInsurancesInheritedDisabled>()),
                            Schedules = SetSchedules(context, item, new List<ProviderOrgUnitSchedule>())
                        };
                        provider.ProviderOrgUnits.Add(providerOrgUnit);
                    }
                }

                //Remove necessary org units
                var orgUnitIdsToDelete = providerOrgUnits.Select(o => o.OrgUnitId).Except(currentOrgUnitIds);
                var providerOrgUnitsToDelete = provider.ProviderOrgUnits.Where(o => orgUnitIdsToDelete.Contains(o.OrgUnitId)).ToList();
                foreach (var pou in providerOrgUnitsToDelete)
                    pou.ProfileProviderOrgUnits.ToList().ForEach(p => context.DeleteObject(p));
                providerOrgUnitsToDelete.ForEach(p => context.DeleteObject(p));
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing org units for provider '" + provider.Name + "' - " + ex.Message, ex);
            }
        }
        public static void SetProviderOrgUnitAffiliations(ObjectContext context, ProviderV2 source, Provider provider)
        {
            if (source.OrgUnitAffiliations == null)
                return;

            try
            {
                var existingOrgUntiAffiliation = provider.ProviderOrgUnitAffiliations.ToArray();
                foreach (var item in existingOrgUntiAffiliation)
                    context.DeleteObject(item);

                var providerOrgUnitAffiliations = new List<ProviderOrgUnitAffiliation>();
                var services = context.CreateObjectSet<Service>();

                foreach (var item in source.OrgUnitAffiliations)
                {
                    var orgUnitId = ConvertToInt(item.OrgUnitId, "HospitalAffiliation/OrgUnitId", provider.FullName);
                    var orgUnitTypeId = ConvertToInt(item.OrgUnitTypeId, "HospitalAffiliation/OrgUnitId", provider.FullName);
                    var orgUnit = context.CreateObjectSet<OrgUnit>().SingleOrDefault(o => o.Id == orgUnitId && o.OrgUnitTypeAssociations.Any(t => t.OrgUnitType.Id == orgUnitTypeId));
                    if (orgUnit == null)
                        throw new BusinessException("No org unit exists for org unit id '" + item.OrgUnitId + "' and org unit type id '" + item.OrgUnitTypeId + "'");

                    var service = services.SingleOrDefault(s => s.Name == item.Service);
                    if (service == null)
                    {
                        service = new Service
                        {
                            Name = item.Service,
                            Description = item.Service,
                            IsEnabled = true
                        };
                        services.AddObject(service);
                    }

                    providerOrgUnitAffiliations.Add(new ProviderOrgUnitAffiliation
                        {
                            OrgUnitId = orgUnitId,
                            OrgUnitTypeId = orgUnitTypeId,
                            Service = service
                        });
                }

                provider.ProviderOrgUnitAffiliations = providerOrgUnitAffiliations;
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing clinical interests for provider '" + provider.Name + "' - " + ex.Message, ex);
            }
        }
        public static void SetProviderLanguages(ObjectContext context, ProviderV2 source, Provider provider)
        {
            if (source.Languages == null)
                return;

            try
            {
                var existingLanguages = provider.ProviderLanguages.ToArray();
                foreach (var item in existingLanguages)
                    context.DeleteObject(item);

                var languages = context.CreateObjectSet<Language>();
                var providerLanguages = new List<ProviderLanguage>();

                foreach (var item in source.Languages)
                {
                    //Ensure Language Exists
                    var language = languages.FirstOrDefault(s => s.Name == item.LanguageName);
                    if (language == null)
                    {
                        language = new Language(true, 0, item.LanguageName, true);
                        languages.AddObject(language);
                    }

                    providerLanguages.Add(new ProviderLanguage(provider, language, ConvertToBool(item.IsPrimary, false, "Language/IsPrimary", provider.FullName))
                    {
                        IsFluent = ConvertToBool(item.IsFluent, false, "Language/IsFluent", provider.FullName)
                    });
                }

                provider.ProviderLanguages = providerLanguages;
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing languages for provider '" + provider.Name + "' - " + ex.Message, ex);
            }
        }
        public static void SetProviderGroup(ObjectContext context, ProviderV2 source, Provider provider)
        {
            try
            {
                var groups = context.CreateObjectSet<ProviderGroup>();

                if (string.IsNullOrEmpty(source.ProviderGroup))
                {
                    provider.ProviderGroup = groups.Single(g => g.Name == "None");
                }
                else
                {
                    var group = groups.SingleOrDefault(g => g.Name.ToUpper() == source.ProviderGroup.ToUpper());

                    if (group == null)
                        throw new BusinessException("Invalid Provider Group Specified");

                    provider.ProviderGroup = group;
                }
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing provider group for provider '" + provider.Name + "' - " + ex.Message, ex);
            }
        }
        public static void SetProviderGender(ObjectContext context, ProviderV2 source, Provider provider)
        {
            if (source.Gender == null)
                return;

            try
            {
                var genders = context.CreateObjectSet<Gender>();
                var gender = genders.SingleOrDefault(g => g.Name == source.Gender);

                if (gender == null)
                {
                    gender = new Gender(true, 0, source.Gender, true);
                    genders.AddObject(gender);
                }

                provider.Gender = gender;
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing gender for provider '" + provider.Name + "' - " + ex.Message, ex);
            }
        }
        public static void SetProviderEducationTypes(ObjectContext context, ProviderV2 source, Provider provider)
        {
            if (source.EducationTypes == null)
                return;

            try
            {
                var existingEducationTypes = provider.ProviderEducations.ToArray();
                foreach (var item in existingEducationTypes)
                    context.DeleteObject(item);

                var educationTypes = context.CreateObjectSet<EducationType>();
                var providerEducations = new List<ProviderEducation>();

                foreach (var item in source.EducationTypes)
                {
                    //Ensure Education Type Exists
                    var education = educationTypes.FirstOrDefault(s => s.Name == item.EducationTypeName);
                    if (education == null)
                    {
                        education = new EducationType(item.EducationTypeName, true);
                        educationTypes.AddObject(education);
                    }

                    var addedEducation = new ProviderEducation(provider, education, item.InstitutionName);
                    addedEducation.SetYearCompleted(item.YearCompleted);

                    if (ConvertToBool(item.IsComplete, false, "Education/IsCompleted", provider.Name) || !string.IsNullOrEmpty(item.YearCompleted))
                        addedEducation.IsCompleted = true;

                    providerEducations.Add(addedEducation);
                }

                provider.ProviderEducations = providerEducations;
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing educations for provider '" + provider.Name + "' - " + ex.Message, ex);
            }
        }
        public static void SetProviderClinicalInterests(ObjectContext context, ProviderV2 source, Provider provider)
        {
            if (source.ClinicalInterests == null)
                return;

            try
            {
                var existingClinicalInterests = provider.ProviderClinicalInterests.ToArray();
                foreach (var item in existingClinicalInterests)
                    context.DeleteObject(item);

                var clinicalInterests = context.CreateObjectSet<ClinicalInterest>();
                var providerClinicalInterests = new List<ProviderClinicalInterest>();

                foreach (var item in source.ClinicalInterests)
                {
                    //Ensure Clinical Interest Exists
                    var clinicalInterest = clinicalInterests.FirstOrDefault(s => s.Name == item.Name);
                    if (clinicalInterest == null)
                    {
                        clinicalInterest = new ClinicalInterest(item.Name, true);
                        clinicalInterests.AddObject(clinicalInterest);
                    }

                    providerClinicalInterests.Add(new ProviderClinicalInterest(provider, clinicalInterest));
                }

                provider.ProviderClinicalInterests = providerClinicalInterests;
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing clinical interests for provider '" + provider.Name + "' - " + ex.Message, ex);
            }
        }
        public static void SetProviderCertificationAgencies(ObjectContext context, ProviderV2 source, Provider provider)
        {
            if (source.CertificationAgencies == null)
                return;

            try
            {
                var existingCertificationSpecialties = provider.ProviderCertificationSpecialties.ToArray();
                foreach (var item in existingCertificationSpecialties)
                    context.DeleteObject(item);

                var providerCertificationSpecialties = new List<ProviderCertificationSpecialty>();

                foreach (var certificationAgency in source.CertificationAgencies)
                {
                    var agencies = context.CreateObjectSet<CertificationAgency>();

                    //Ensure Agency Exists
                    var agency = agencies.FirstOrDefault(s => s.Name == certificationAgency.AgencyName);
                    if (agency == null)
                    {
                        agency = new CertificationAgency(certificationAgency.AgencyName);
                        agencies.AddObject(agency);
                    }

                    foreach (var certificationBoard in certificationAgency.CertificationBoards)
                    {
                        var boards = agency.CertificationBoards;

                        //Ensure Board Exists
                        var board = boards.FirstOrDefault(s => s.Name == certificationBoard.BoardName);
                        if (board == null)
                        {
                            board = agency.AddCertificationBoard(certificationBoard.BoardName);
                        }

                        foreach (var certificationSpecialty in certificationBoard.CertificationSpecialties)
                        {
                            var specialties = board.CertificationSpecialties;

                            //Ensure Specialty Exists
                            var specialty = specialties.FirstOrDefault(s => s.Name == certificationSpecialty.Name);
                            if (specialty == null)
                            {
                                specialty = board.AddCertificationSpecialty(certificationSpecialty.Name);
                                specialty.IsSubspecialty = ConvertToBool(certificationSpecialty.IsSubSpecialty, false, "CertificationSpecialty/IsSubSpecialty", provider.FullName);
                            }

                            providerCertificationSpecialties.Add(new ProviderCertificationSpecialty(provider, specialty, certificationSpecialty.YearOfIssuance));
                        }
                    }
                }

                provider.ProviderCertificationSpecialties = providerCertificationSpecialties;
            }
            catch (Exception ex)
            {
                throw new BusinessException("Error processing certification specialties for provider '" + provider.Name + "' - " + ex.Message, ex);
            }
        }
        public static void SetDynamicColumns(ObjectContext context, ProviderV2 source, Provider provider)
        {
            if (source.DynamicColumns == null)
                return;

            var existingDynamicColumns = context.CreateObjectSet<DynamicColumnEntity>()
                .First(d => d.EntityName == "Providers")
                .DynamicColumnInstances;

            if (!existingDynamicColumns.Any())
                provider.DynamicColumnData = DynamicColumnUtility.ConvertFomList(new List<DynamicColumnDto>());

            //Make sure column names specified are valid
            var invalidColumnNames = new List<string>();
            foreach (var item in source.DynamicColumns)
            {
                var column = existingDynamicColumns.FirstOrDefault(d => d.Name == item.FieldName);
                if (column == null)
                    invalidColumnNames.Add(item.FieldName);
            }
            if (invalidColumnNames.Any())
            {
                var errorMessage = "Invalid Column Names Specified: " + string.Join(",", invalidColumnNames.ToArray());
                throw new BusinessException("Error processing dynamic columns for provider '" + provider.Name + "' - " + errorMessage);
            }

            provider.DynamicColumnData = DynamicColumnUtility.ConvertFomList(source.DynamicColumns);
        }