public static List<SubEntityRecord> GetDropdownValues(ProviderAdminDetailView parent, int providerId)
        {
            IEnumerable<ProviderLanguageDto> existingLanguages = new List<ProviderLanguageDto>();
            if (providerId != 0)
                existingLanguages = GetProviderLanguages(parent, providerId);

            var request = new ListLanguagesRequest() { IsEnabled = true };
            var languages = parent.ProcessRequest<ListLanguagesResponse>(request).Languages;
            var records = new List<SubEntityRecord>();

            foreach (var language in languages.Where(l => !existingLanguages.Any(e => e.LanguageId == l.Id)))
            {
                var record = new SubEntityRecord();
                record.DisplayName = language.Name;
                record.Id = language.Id;
                record.Fields.Add(new Field(_id, _id, FieldTypes.Hidden, null));
                record.Fields.Add(new Field(_providerId, _providerId, FieldTypes.Hidden, providerId));
                record.Fields.Add(new Field(_languageId, _languageId, FieldTypes.Hidden, language.Id));
                record.Fields.Add(new Field(_languageName, "Language Name", FieldTypes.Label, language.Name));
                record.Fields.Add(new Field(_isPrimary, "Is Primary", FieldTypes.Checkbox, false) { OnChangeCommand = "PrimaryLanguageChanged" });
                record.Fields.Add(new Field(_isFluent, "Is Fluent", FieldTypes.Checkbox, false));

                records.Add(record);
            }

            return records;
        }
        public static List<SubEntityRecord> GetDropdownValues(ProviderAdminDetailView parent, int providerId)
        {
            IEnumerable<ProviderCertificationSpecialtyDto> existintCerts = new List<ProviderCertificationSpecialtyDto>();
            if (providerId != 0)
                existintCerts = GetProviderCertificationSpecialties(parent, providerId);

            var request = new ListCertificationSpecialtiesRequest();
            var certificationSpecialties = parent.ProcessRequest<ListCertificationSpecialtiesResponse>(request).CertificationSpecialties;
            var records = new List<SubEntityRecord>();

            foreach (var certificationSpecialty in certificationSpecialties.Where(c => c.IsEnabled && c.CertificationAgencyIsEnabled && !existintCerts.Any(e => e.CertificationSpecialtyId == c.Id)))
            {

                var record = new SubEntityRecord();
                record.DisplayName = certificationSpecialty.Name + " (" + certificationSpecialty.CertificationBoardName + ", " + certificationSpecialty.CertificationAgencyName + ")";
                record.Id = certificationSpecialty.Id;
                record.Fields.Add(new Field(_providerId, _providerId, FieldTypes.Hidden, providerId));
                record.Fields.Add(new Field(_certificationSpecialtyId, _certificationSpecialtyId, FieldTypes.Hidden, certificationSpecialty.Id));
                record.Fields.Add(new Field(_certificationSpecialtyName, "Certification Specialty", FieldTypes.Label, certificationSpecialty.Name));
                record.Fields.Add(new Field(_certificationBoardName, "Board Name", FieldTypes.Label, certificationSpecialty.CertificationBoardName));
                record.Fields.Add(new Field(_certificationAgencyName, "Certification Agency", FieldTypes.Label, certificationSpecialty.CertificationBoardName));
                record.Fields.Add(new Field(_yearOfIssuance, "Year Of Issuance", FieldTypes.Text, null, true));

                records.Add(record);
            }

            return records;
        }
        public static SubEntity BuildLocationsSubEntity(ProviderAdminDetailView parent, int providerId)
        {
            _subEntity = parent._viewModel.SubEntities.FirstOrDefault(s => s.EntityName == ProviderAdminDetailView._providerOrgUnitSubEntity);

            if (_subEntity == null)
                _subEntity = new SubEntity() { EntityName = ProviderAdminDetailView._providerOrgUnitSubEntity, DisplayName = "Locations", CanAddExisting = true, CanDelete = true };
            else
                _subEntity.Records = new List<SubEntityRecord>();

            var providerOrgUnits = GetProviderLocations(parent, providerId);

            foreach (var providerOrgUnit in providerOrgUnits)
            {
                var record = new SubEntityRecord();

                record.Id = providerOrgUnit.Id;
                record.Fields.Add(new Field(_id, _id, FieldTypes.Hidden, providerOrgUnit.Id));
                record.Fields.Add(new Field(_providerId, _providerId, FieldTypes.Hidden, providerId));
                record.Fields.Add(new Field(_orgUntiId, _orgUntiId, FieldTypes.Hidden, providerOrgUnit.OrgUnitId));
                record.Fields.Add(new Field(_orgUnitName, "Location Name", FieldTypes.Label, providerOrgUnit.OrgUnitName));
                record.Fields.Add(new Field(_isPrimary, "Is Primary", FieldTypes.Checkbox, providerOrgUnit.IsPrimary));
                record.Fields.Add(new Field(_phone, "Phone Number", FieldTypes.Text, providerOrgUnit.DisplayPhone));
                record.Fields.Add(new Field(_fax, "Fax Number", FieldTypes.Text, providerOrgUnit.DisplayFax));
                record.Fields.Add(new Field(_isAcceptingNewPatients, "Is Accepting New Patients", FieldTypes.Checkbox, providerOrgUnit.IsAcceptingNewPatients));
                record.Fields.Add(new Field(_allowAppointmentRequests, "Allow Appointment Requests", FieldTypes.Checkbox, providerOrgUnit.AllowAppointmentRequests));

                record.SubEntities.Add(BuildInsurancesSubEntity(parent, providerOrgUnit));
                record.SubEntities.Add(BuildServicesSubEntity(parent, providerOrgUnit));
                record.SubEntities.Add(BuildHoursSubEntity(parent, providerOrgUnit));

                _subEntity.Records.Add(record);
            }

            return _subEntity;
        }
        public static List<SubEntityRecord> GetDropdownValues(ProviderAdminDetailView parent, int providerId)
        {
            IEnumerable<ProviderClinicalInterestDto> existingInterests = new List<ProviderClinicalInterestDto>();
            if (providerId != 0)
                existingInterests = GetProviderClinicalInterests(parent, providerId);

            var request = new ListClinicalInterestsRequest() { IsEnabled = true };
            var clinicalInterests = parent.ProcessRequest<ListClinicalInterestsResponse>(request).ClinicalInterests;
            var records = new List<SubEntityRecord>();

            foreach (var clinicalInterest in clinicalInterests.Where(c => !existingInterests.Any(e => e.ClinicalInterestId == c.Id)))
            {
                var record = new SubEntityRecord();
                record.DisplayName = clinicalInterest.Name;
                record.Id = clinicalInterest.Id;
                record.Fields.Add(new Field(_id, _id, FieldTypes.Hidden, null));
                record.Fields.Add(new Field(_providerId, _providerId, FieldTypes.Hidden, providerId));
                record.Fields.Add(new Field(_clinicalInterestId, _clinicalInterestId, FieldTypes.Hidden, clinicalInterest.Id));
                record.Fields.Add(new Field(_clinicalInterestName, "Clinical Interest Name", FieldTypes.Label, clinicalInterest.Name));

                records.Add(record);
            }

            return records;
        }
        public static List<SubEntityRecord> GetDropdownValues(ProviderAdminDetailView parent, int providerId)
        {
            IEnumerable<ProviderProviderTypeDto> existingTypes = new List<ProviderProviderTypeDto>();
            if (providerId != 0)
                existingTypes = GetProviderProviderTypes(parent, providerId);

            var request = new ListProviderTypeRequest() { IsEnabled = true };
            var types = parent.ProcessRequest<ListProviderTypeResponse>(request).ProviderTypes;
            var records = new List<SubEntityRecord>();

            foreach (var type in types.Where(t => !existingTypes.Any(e => e.ProviderTypeId == t.Id)))
            {
                var record = new SubEntityRecord();
                record.DisplayName = type.Name;
                record.Id = type.Id;
                record.Fields.Add(new Field(_id, _id, FieldTypes.Hidden, null));
                record.Fields.Add(new Field(_providerId, _providerId, FieldTypes.Hidden, providerId));
                record.Fields.Add(new Field(_providerTypeId, _providerTypeId, FieldTypes.Hidden, type.Id));
                record.Fields.Add(new Field(_providerTypeName, "Type Name", FieldTypes.Label, type.Name));

                records.Add(record);
            }

            return records;
        }
        public static List<SubEntityRecord> GetDropdownValues(ProviderAdminDetailView parent, int providerId)
        {
            IEnumerable<ProviderSpecialtyDto> existingSpecialties = new List<ProviderSpecialtyDto>();
            if(providerId != 0)
                existingSpecialties = GetProviderSpecialties(parent, providerId);

            var request = new ListSpecialtiesRequest() { IsEnabled = true };
            var specialties = parent.ProcessRequest<ListSpecialtiesResponse>(request).Specialties;
            var records = new List<SubEntityRecord>();

            foreach (var specialty in specialties.Where(s => !existingSpecialties.Any(e => e.SpecialtyId == s.Id)))
            {
                var record = new SubEntityRecord();
                record.DisplayName = specialty.Name + " (" + specialty.SpecialtyType + ")";
                record.Id = specialty.Id;
                record.Fields.Add(new Field(_id, _id, FieldTypes.Hidden, null));
                record.Fields.Add(new Field(_providerId, _providerId, FieldTypes.Hidden, providerId));
                record.Fields.Add(new Field(_specialtyId, _specialtyId, FieldTypes.Hidden, specialty.Id));
                record.Fields.Add(new Field(_specialtyName, specialty.SpecialtyType == "Specialty" ? "Specialty Name" : "Condition Name", FieldTypes.Label, specialty.Name));

                if (specialty.SpecialtyType == "Specialty")
                {
                    record.Fields.Add(new Field(_isPrimary, "Is Primary", FieldTypes.Checkbox, false));
                    record.Fields.Add(new Field(_isBoardCertified, "Is Board Certified", FieldTypes.Checkbox, false));
                }

                records.Add(record);
            }

            return records;
        }
        public static List<SubEntityRecord> GetDropdownValues(ProviderAdminDetailView parent, int providerId)
        {
            IEnumerable<ProviderEducationDto> existingEducations = new List<ProviderEducationDto>();
            if (providerId != 0)
                existingEducations = GetProviderEducations(parent, providerId);

            var request = new ListEducationTypesRequest() { IsEnabled = true };
            var educationTypes = parent.ProcessRequest<ListEducationTypesResponse>(request).EducationTypes;
            var records = new List<SubEntityRecord>();

            foreach (var educationType in educationTypes.Where(e => !existingEducations.Any(x => x.EducationTypeId == e.Id)))
            {
                var record = new SubEntityRecord();
                record.DisplayName = educationType.Name;
                record.Id = educationType.Id;
                record.Fields.Add(new Field(_id, _id, FieldTypes.Hidden, null));
                record.Fields.Add(new Field(_providerId, _providerId, FieldTypes.Hidden, providerId));
                record.Fields.Add(new Field(_educationTypeId, _educationTypeId, FieldTypes.Hidden, educationType.Id));
                record.Fields.Add(new Field(_educationTypeName, "Education Type", FieldTypes.Label, educationType.Name));
                record.Fields.Add(new Field(_institutionName, "Institution Name", FieldTypes.Text, string.Empty, true));
                record.Fields.Add(new Field(_isCompleted, "Is Completed", FieldTypes.Checkbox, false));
                record.Fields.Add(new Field(_yearCompleted, "Year Completed", FieldTypes.Text, string.Empty));

                records.Add(record);
            }

            return records;
        }
        public static void Save(ProviderAdminDetailView parent, SubEntity providerTypes, int providerId)
        {
            try
            {
                providerTypes.ErrorMessage = string.Empty;

                //Existing Records Added
                foreach (var record in providerTypes.Records.Where(r => r.Action == RecordActions.Added))
                {
                    var request = new AddProviderProviderTypeRequest()
                    {
                        ProviderId = providerId,
                        ProviderTypeId = int.Parse(parent.GetFieldValue(record.Fields, _providerTypeId))
                    };

                    parent.ProcessRequest<AddProviderProviderTypeResponse>(request);
                }

                //New Records Created
                foreach (var record in providerTypes.Records.Where(r => r.Action == RecordActions.New))
                {
                    //Add New Provider Type
                    var addTypeRequest = new AddProviderTypeRequest()
                    {
                        Name = parent.GetFieldValue(record.Fields, _providerTypeName),
                        IsEnabled = true
                    };
                    var addedTypeId = parent.ProcessRequest<AddProviderTypeResponse>(addTypeRequest).Id;

                    //Add newly created type to provider
                    var request = new AddProviderProviderTypeRequest()
                    {
                        ProviderId = providerId,
                        ProviderTypeId = addedTypeId
                    };

                    parent.ProcessRequest<AddProviderProviderTypeResponse>(request);
                }

                //Deletes
                foreach (var record in providerTypes.Records.Where(r => r.Action == RecordActions.Deleted))
                {
                    var request = new DeleteProviderProviderTypeRequest()
                    {
                        ProviderId = providerId,
                        ProviderTypeId = int.Parse(parent.GetFieldValue(record.Fields, _providerTypeId))
                    };
                    parent.ProcessRequest<DeleteProviderProviderTypeResponse>(request);
                }
            }
            catch (Exception ex)
            {
                providerTypes.ErrorMessage = ex.Message;
                throw;
            }

            providerTypes = BuildProviderTypesSubEntity(parent, providerId);
        }
        public static void Save(ProviderAdminDetailView parent, SubEntity providerCertificationSpecialties, int providerId)
        {
            try
            {
                providerCertificationSpecialties.ErrorMessage = string.Empty;

                //Edits
                foreach (var record in providerCertificationSpecialties.Records.Where(r => r.Action == RecordActions.Edited))
                {
                    var yearOfIssuance = Validate(parent, record);

                    var request = new UpdateProviderCertificationSpecialtyRequest();

                    request.Id = int.Parse(parent.GetFieldValue(record.Fields, _id));
                    request.ProviderId = providerId;
                    request.CertificationSpecialtyId = int.Parse(parent.GetFieldValue(record.Fields, _certificationSpecialtyId));
                    request.YearOfIssuance = yearOfIssuance;

                    parent.ProcessRequest<UpdateProviderCertificationSpecialtyResponse>(request);
                }

                //Existing Records Added
                foreach (var record in providerCertificationSpecialties.Records.Where(r => r.Action == RecordActions.Added))
                {
                    var yearOfIssuance = Validate(parent, record);

                    var request = new AddProviderCertificationSpecialtyRequest()
                    {
                        ProviderId = providerId,
                        CertificationSpecialtyId = int.Parse(parent.GetFieldValue(record.Fields, _certificationSpecialtyId)),
                        YearOfIssuance = yearOfIssuance
                    };

                    parent.ProcessRequest<AddProviderCertificationSpecialtyResponse>(request);
                }

                //Deletes
                foreach (var record in providerCertificationSpecialties.Records.Where(r => r.Action == RecordActions.Deleted))
                {
                    var request = new DeleteProviderCertificationSpecialtyRequest()
                    {
                        ProviderId = providerId,
                        CertificationSpecialtyId = int.Parse(parent.GetFieldValue(record.Fields, _certificationSpecialtyId))
                    };
                    parent.ProcessRequest<DeleteProviderCertificationSpecialtyResponse>(request);
                }
            }
            catch (Exception ex)
            {
                providerCertificationSpecialties.ErrorMessage = ex.Message;
                throw;
            }

            providerCertificationSpecialties = BuildCertificationSubEntity(parent, providerId);
        }
        public static SubEntity BuildDirectUrlHistorySubEntity(ProviderAdminDetailView parent, int providerId)
        {
            _subEntity = parent._viewModel.SubEntities.FirstOrDefault(s => s.EntityName == ProviderAdminDetailView._directUrlHistorySubEntity);

            if (_subEntity == null)
                _subEntity = new SubEntity() { EntityName = ProviderAdminDetailView._directUrlHistorySubEntity, DisplayName = "Direct URL History", CanAddExisting = false, CanCreateNew = false, CanDelete = true };
            else
                _subEntity.Records = new List<SubEntityRecord>();

            AddExistingRecords(parent, providerId);

            return _subEntity;
        }
        public static SubEntity BuildCertificationSubEntity(ProviderAdminDetailView parent, int providerId)
        {
            _subEntity = parent._viewModel.SubEntities.FirstOrDefault(s => s.EntityName == ProviderAdminDetailView._providerCertificationSubEntity);

            if (_subEntity == null)
                _subEntity = new SubEntity() { EntityName = ProviderAdminDetailView._providerCertificationSubEntity, DisplayName = "Board Certifications", CanAddExisting = true, CanDelete = true };
            else
                _subEntity.Records = new List<SubEntityRecord>();

            AddExistingRecords(parent, providerId);

            return _subEntity;
        }
        public static SubEntity BuildClinicalInterestSubEntity(ProviderAdminDetailView parent, int providerId)
        {
            _subEntity = parent._viewModel.SubEntities.FirstOrDefault(s => s.EntityName == ProviderAdminDetailView._providerClinicalInterestSubEntity);

            if (_subEntity == null)
                _subEntity = new SubEntity() { EntityName = ProviderAdminDetailView._providerClinicalInterestSubEntity, DisplayName = "Clinical Interests", CanAddExisting = true, CanCreateNew = true, CanDelete = true };
            else
                _subEntity.Records = new List<SubEntityRecord>();

            AddExistingRecords(parent, providerId);
            AddNewRecordTemplate(parent, providerId);

            return _subEntity;
        }
        private static void AddExistingRecords(ProviderAdminDetailView parent, int providerId)
        {
            var request = new ListDirectUrlHistoryRequest() { EntityId = providerId, EntityTypeId = EntityTypeId.Provider };
            var urlHistoryLinks = parent.ProcessRequest<ListDirectUrlHistoryResponse>(request).DirectUrlHistory;

            foreach (var directUrl in urlHistoryLinks)
            {
                var record = new SubEntityRecord();
                var fields = new List<Field>();

                fields.Add(new Field(_id, _id, FieldTypes.Hidden, directUrl.Id));
                fields.Add(new Field(_directUrl, directUrl.DateAdded.ToString("M/d/yy h:m:s tt"), FieldTypes.Label, directUrl.DirectUrl));

                record.Fields = fields;

                _subEntity.Records.Add(record);
            }
        }
        public static List<SubEntityRecord> GetInsuranceDropdownValues(ProviderAdminDetailView parent, int providerOrgUnitId)
        {
            var existingInsurances = parent.ProcessRequest<ListProviderOrgUnitInsurancesResponse>(new ListProviderOrgUnitInsurancesRequest() { ProviderOrgUnitId = providerOrgUnitId }).Insurances;
            var request = new ListInsurancesRequest() { IsEnabled = true };
            var insurances = parent.ProcessRequest<ListInsurancesResponse>(request).Insurances;
            var records = new List<SubEntityRecord>();

            foreach (var insurance in insurances.Where(i => !existingInsurances.Any(x => x.InsuranceId == i.Id)))
            {
                var record = new SubEntityRecord();
                record.DisplayName = insurance.Name;
                record.Id = insurance.Id;
                record.Fields.Add(new Field(_insuranceId, _orgUntiId, FieldTypes.Hidden, insurance.Id));
                record.Fields.Add(new Field("InsuranceName", "Insurance Name", FieldTypes.Label, insurance.Name));
                records.Add(record);
            }

            return records;
        }
        public static void Save(ProviderAdminDetailView parent, SubEntity directUrls, int providerId)
        {
            try
            {
                directUrls.ErrorMessage = string.Empty;

                //Deletes
                foreach (var record in directUrls.Records.Where(r => r.Action == RecordActions.Deleted))
                {
                    var request = new DeleteDirectUrlHistoryRequest()
                    {
                        Id = int.Parse(parent.GetFieldValue(record.Fields, _id))
                    };
                    parent.ProcessRequest<DeleteDirectUrlHistoryResponse>(request);
                }
            }
            catch (Exception ex)
            {
                directUrls.ErrorMessage = ex.Message;
                throw;
            }

            directUrls = BuildDirectUrlHistorySubEntity(parent, providerId);
        }
        private static void AddExistingRecords(ProviderAdminDetailView parent, int providerId)
        {
            var providerTypes = GetProviderProviderTypes(parent, providerId);

            foreach (var type in providerTypes)
            {
                var record = new SubEntityRecord();
                var fields = new List<Field>();

                fields.Add(new Field(_id, _id, FieldTypes.Hidden, type.Id));
                fields.Add(new Field(_providerId, _providerId, FieldTypes.Hidden, providerId));
                fields.Add(new Field(_providerTypeId, _providerTypeId, FieldTypes.Hidden, type.ProviderTypeId));
                fields.Add(new Field(_providerTypeName, "Type Name", FieldTypes.Label, type.ProviderTypeName));

                record.Fields = fields;

                _subEntity.Records.Add(record);
            }
        }
        public static List<SubEntityRecord> GetServicesDropdownValues(ProviderAdminDetailView parent, int providerOrgUnitId)
        {
            var request = new ListAvailableServicesRequest() { ProviderOrgUnitId = providerOrgUnitId };
            var services = parent.ProcessRequest<ListAvailableServicesResponse>(request).Services;
            var records = new List<SubEntityRecord>();

            foreach (var service in services)
            {
                var record = new SubEntityRecord();
                record.DisplayName = service.Name;
                record.Id = service.Id;
                record.Fields.Add(new Field(_serviceId, _serviceId, FieldTypes.Hidden, service.Id));
                record.Fields.Add(new Field("ServiceName", "Service Name", FieldTypes.Label, service.Name));
                records.Add(record);
            }

            return records;
        }
        private static void AddExistingRecords(ProviderAdminDetailView parent, int providerId)
        {
            var providerClinicalInterests = GetProviderClinicalInterests(parent, providerId);

            foreach (var providerClinicalInterest in providerClinicalInterests)
            {
                var record = new SubEntityRecord();
                var fields = new List<Field>();

                fields.Add(new Field(_id, _id, FieldTypes.Hidden, providerClinicalInterest.Id));
                fields.Add(new Field(_providerId, _providerId, FieldTypes.Hidden, providerId));
                fields.Add(new Field(_clinicalInterestId, _clinicalInterestId, FieldTypes.Hidden, providerClinicalInterest.ClinicalInterestId));
                fields.Add(new Field(_clinicalInterestName, "Clinical Interest Name", FieldTypes.Label, providerClinicalInterest.ClinicalInterestName));

                record.Fields = fields;

                _subEntity.Records.Add(record);
            }
        }
 private static IEnumerable<ProviderClinicalInterestDto> GetProviderClinicalInterests(ProviderAdminDetailView parent, int providerId)
 {
     var request = new ListProviderClinicalInterestRequest() { ProviderId = providerId };
     return parent.ProcessRequest<ListProviderClinicalInterestResponse>(request).ClinicalInterests;
 }
        public static void Save(ProviderAdminDetailView parent, SubEntity providerOrgUnits, int providerId)
        {
            foreach (var record in providerOrgUnits.Records)
            {
                Validate(parent, record);

                //Update Record
                try
                {
                    //Edits
                    if (record.Action == RecordActions.Edited)
                    {
                        var request = new UpdateProviderOrgUnitRequest
                        {
                            Id = int.Parse(parent.GetFieldValue(record.Fields, _id), CultureInfo.InvariantCulture),
                            IsPrimary = bool.Parse(parent.GetFieldValue(record.Fields, _isPrimary)),
                            DisplayPhone = parent.GetFieldValue(record.Fields, _phone),
                            DisplayFax = parent.GetFieldValue(record.Fields, _fax),
                            IsAcceptingNewPatients = bool.Parse(parent.GetFieldValue(record.Fields, _isAcceptingNewPatients)),
                            AllowAppointmentRequests = bool.Parse(parent.GetFieldValue(record.Fields, _allowAppointmentRequests))
                        };
                        parent.ProcessRequest<UpdateProviderOrgUnitResponse>(request);
                    }

                    //Adds
                    if (record.Action == RecordActions.Added)
                    {
                        var request = new AddProviderOrgUnitRequest
                        {
                            ProviderId = providerId,
                            OrgUnitId = int.Parse(parent.GetFieldValue(record.Fields, _orgUntiId)),
                            IsPrimary = bool.Parse(parent.GetFieldValue(record.Fields, _isPrimary)),
                            DisplayPhone = parent.GetFieldValue(record.Fields, _phone),
                            DisplayFax = parent.GetFieldValue(record.Fields, _fax),
                            IsAcceptingNewPatients = bool.Parse(parent.GetFieldValue(record.Fields, _isAcceptingNewPatients)),
                            AllowAppointmentRequests = bool.Parse(parent.GetFieldValue(record.Fields, _allowAppointmentRequests))
                        };
                        parent.ProcessRequest<AddProviderOrgUnitResponse>(request);
                    }

                    //Deletes
                    if (record.Action == RecordActions.Deleted)
                    {
                        var request = new DeleteProviderOrgUnitRequest()
                        {
                            ProviderId = providerId,
                            OrgUnitId = int.Parse(parent.GetFieldValue(record.Fields, _orgUntiId))
                        };
                        parent.ProcessRequest<DeleteProviderOrgUnitResponse>(request);
                    }
                }
                catch (Exception ex)
                {
                    providerOrgUnits.ErrorMessage = ex.Message;
                    throw;
                }

                //Update Subentities for Record
                foreach (var subentity in record.SubEntities)
                {
                    if (subentity.EntityName == ProviderAdminDetailView._providerOrgUnitInsurancesSubEntity)
                        SaveInsurances(parent, subentity, int.Parse(parent.GetFieldValue(record.Fields, _id)));
                    else if (subentity.EntityName == ProviderAdminDetailView._providerOrgUnitServicesSubEntity)
                        SaveServices(parent, subentity, int.Parse(parent.GetFieldValue(record.Fields, _id)));
                    else if (subentity.EntityName == _hoursSubEntity)

                        SaveHours(parent, subentity);
                }
            }

            providerOrgUnits = BuildLocationsSubEntity(parent, providerId);
        }
        public static List<SubEntityRecord> GetLocationDropdownValues(ProviderAdminDetailView parent, int providerId)
        {
            IEnumerable<ProviderOrgUnitDto> existingLocations = new List<ProviderOrgUnitDto>();
            if (providerId != 0)
                existingLocations = GetProviderLocations(parent, providerId);

            var request = new ListOrgUnitsRequest();
            var orgUnits = parent.ProcessRequest<ListOrgUnitsResponse>(request).OrgUnits;
            var records = new List<SubEntityRecord>();

            foreach (var orgUnit in orgUnits.Where(o => !existingLocations.Any(e => e.OrgUnitId == o.Id)))
            {
                var record = new SubEntityRecord();
                record.DisplayName = orgUnit.Name;
                record.Id = orgUnit.Id;
                record.Fields.Add(new Field(_providerId, _providerId, FieldTypes.Hidden, providerId));
                record.Fields.Add(new Field(_orgUntiId, _orgUntiId, FieldTypes.Hidden, orgUnit.Id));
                record.Fields.Add(new Field(_orgUnitName, "Location Name", FieldTypes.Label, orgUnit.Name));
                record.Fields.Add(new Field(_isPrimary, "Is Primary", FieldTypes.Checkbox, false));
                record.Fields.Add(new Field(_phone, "Phone Number", FieldTypes.Text, null));
                record.Fields.Add(new Field(_fax, "Fax Number", FieldTypes.Text, null));
                record.Fields.Add(new Field(_isAcceptingNewPatients, "Is Accepting New Patients", FieldTypes.Checkbox, true));
                record.Fields.Add(new Field(_allowAppointmentRequests, "Allow Appointment Requests", FieldTypes.Checkbox, true));

                records.Add(record);
            }

            return records;
        }
        private static SubEntity BuildHoursSubEntity(ProviderAdminDetailView parent, ProviderOrgUnitDto providerOrgUnit)
        {
            var subEntity = new SubEntity() { EntityName = _hoursSubEntity, DisplayName = "Hours", CanCreateNew = true, CanDelete = true };

            var scheduleRequest = new ListProviderOrgUnitScheduleRequest()
            {
                ProviderOrgUnitId = providerOrgUnit.Id
            };
            var schedules = parent.ProcessRequest<ListProviderOrgUnitScheduleResponse>(scheduleRequest).ScheduleTimeSpans;

            //Existing Records
            foreach (var schedule in schedules)
            {
                var record = new SubEntityRecord();

                record.Fields.Add(new Field(_id, _id, FieldTypes.Hidden, schedule.Id));
                record.Fields.Add(new Field(_providerOrgUnitId, _providerOrgUnitId, FieldTypes.Hidden, providerOrgUnit.Id));
                record.Fields.Add(new Field("Description", schedule.Day, FieldTypes.Label, schedule.Open + " - " + schedule.Close));

                subEntity.Records.Add(record);
            }

            //New Record Template
            subEntity.NewRecordTemplate.Fields.Add(new Field(_providerOrgUnitId, _providerOrgUnitId, FieldTypes.Hidden, providerOrgUnit.Id));
            subEntity.NewRecordTemplate.Fields.Add(CommonUtils.CreateDayDropDownField());
            subEntity.NewRecordTemplate.Fields.Add(new Field("OpenTime", "Open Time", FieldTypes.Text, "8:00 AM"));
            subEntity.NewRecordTemplate.Fields.Add(new Field("CloseTime", "Close Time", FieldTypes.Text, "5:00 PM"));

            return subEntity;
        }
 private static void AddNewRecordTemplate(ProviderAdminDetailView parent, int providerId)
 {
     _subEntity.NewRecordTemplate = new SubEntityRecord()
     {
         Fields =
         {
             new Field(_providerId, _providerId, FieldTypes.Hidden, providerId),
             new Field(_providerTypeName, "Type Name", FieldTypes.Text, string.Empty, true){ Focus = true},
         }
     };
 }
        private static void SaveServices(ProviderAdminDetailView parent, SubEntity subentity, int providerOrgUnitId)
        {
            try
            {
                //New Records
                foreach (var record in subentity.Records.Where(r => r.Action == RecordActions.New))
                {
                    var addServiceRequest = new AddServiceRequest()
                    {
                        Name = parent.GetFieldValue(record.Fields, "ServiceName"),
                        IsEnabled = true
                    };
                    var serviceResponseId = parent.ProcessRequest<AddServiceResponse>(addServiceRequest).Id;

                    var request = new AddProviderOrgUnitServiceRequest()
                    {
                        ProviderOrgUnitId = providerOrgUnitId,
                        ServiceId = serviceResponseId
                    };
                    parent.ProcessRequest<AddProviderOrgUnitServiceResponse>(request);
                }

                //Added Records
                foreach (var record in subentity.Records.Where(r => r.Action == RecordActions.Added))
                {
                    var request = new AddProviderOrgUnitServiceRequest()
                    {
                        ProviderOrgUnitId = providerOrgUnitId,
                        ServiceId = int.Parse(parent.GetFieldValue(record.Fields, _serviceId))
                    };
                    parent.ProcessRequest<AddProviderOrgUnitServiceResponse>(request);
                }

                //Deletes
                foreach (var record in subentity.Records.Where(r => r.Action == RecordActions.Deleted))
                {
                    var request = new DeleteProviderOrgUnitServiceRequest()
                    {
                        ProviderOrgUnitId = providerOrgUnitId,
                        ServiceId = int.Parse(parent.GetFieldValue(record.Fields, _serviceId))
                    };
                    parent.ProcessRequest<DeleteProviderOrgUnitServiceResponse>(request);
                }
            }
            catch (Exception ex)
            {
                subentity.ErrorMessage = ex.Message;
                throw;
            }
        }
        private static SubEntity BuildServicesSubEntity(ProviderAdminDetailView parent, ProviderOrgUnitDto providerOrgUnit)
        {
            var subEntity = new SubEntity() { EntityName = ProviderAdminDetailView._providerOrgUnitServicesSubEntity, DisplayName = "Services", CanAddExisting = true, CanCreateNew = true, CanDelete = true };

            foreach (var service in providerOrgUnit.Service)
            {
                var record = new SubEntityRecord();

                record.Fields.Add(new Field(_serviceId, _serviceId, FieldTypes.Hidden, service.Id));
                record.Fields.Add(new Field("ServiceName", "Service Name", FieldTypes.Label, service.ServiceName));

                subEntity.Records.Add(record);
            }

            subEntity.NewRecordTemplate.Fields.Add(new Field(_providerOrgUnitId, _providerOrgUnitId, FieldTypes.Hidden, providerOrgUnit.Id));
            subEntity.NewRecordTemplate.Fields.Add(new Field("ServiceName", "Service Name", FieldTypes.Text, null, true) { Focus = true });

            return subEntity;
        }
 private static IEnumerable<ProviderOrgUnitDto> GetProviderLocations(ProviderAdminDetailView parent, int providerId)
 {
     var request = new ListProviderOrgUnitsRequest() { ProviderId = providerId };
     return parent.ProcessRequest<ListProviderOrgUnitsResponse>(request).OrgUnits;
 }
 private static IEnumerable<ProviderProviderTypeDto> GetProviderProviderTypes(ProviderAdminDetailView parent, int providerId)
 {
     var request = new ListProviderProviderTypeRequest() { ProviderId = providerId };
     return parent.ProcessRequest<ListProviderProviderTypeResponse>(request).ProviderTypes;
 }
        private static void Validate(ProviderAdminDetailView parent, SubEntityRecord record)
        {
            if (!CommonUtils.IsPhoneNumber(parent.GetFieldValue(record.Fields, _phone)))
                throw new BusinessException("Location Phone Number is invalid");

            if (!CommonUtils.IsPhoneNumber(parent.GetFieldValue(record.Fields, _fax)))
                throw new BusinessException("Location Fax Number is invalid");
        }
        private static void SaveHours(ProviderAdminDetailView parent, SubEntity subentity)
        {
            try
            {
                //Deletes
                foreach (var record in subentity.Records.Where(r => r.Action == RecordActions.Deleted))
                {
                    var request = new DeleteProviderOrgUnitScheduleRequest()
                    {
                        ProviderOrgUnitScheduleId = int.Parse(parent.GetFieldValue(record.Fields, _id))
                    };
                    var response = parent.ProcessRequest<DeleteProviderOrgUnitScheduleResponse>(request);
                }

                //New
                foreach (var record in subentity.Records.Where(r => r.Action == RecordActions.New))
                {
                    var timeSpan = CommonUtils.CreateScheduleTimeSpan(parent.GetFieldValue(record.Fields, "Day"), parent.GetFieldValue(record.Fields, "OpenTime"), parent.GetFieldValue(record.Fields, "CloseTime"));

                    var request = new AddProviderOrgUnitScheduleRequest()
                    {
                        ProviderOrgUnitId = int.Parse(parent.GetFieldValue(record.Fields, _providerOrgUnitId)),
                        TimeSpan = timeSpan
                    };
                    var response = parent.ProcessRequest<AddProviderOrgUnitScheduleResponse>(request);
                }
            }
            catch (Exception ex)
            {
                subentity.ErrorMessage = ex.Message;
                throw;
            }
        }
        private static void AddNewRecordTemplate(ProviderAdminDetailView parent, int providerId)
        {
            _subEntity.NewRecordTemplate = new SubEntityRecord(){};

            _subEntity.NewRecordTemplate.Fields.Add(new Field(_providerId, "ProviderId", FieldTypes.Hidden, providerId));
            _subEntity.NewRecordTemplate.Fields.Add(new Field(_name, "Name", FieldTypes.Text, string.Empty, true) { Focus = true });

            var specialtyTypeField = new Field(_specialtyType, "Type", FieldTypes.Dropdown, "Specialty") { OnChangeCommand = "SpecialtyTypeChanged" };
            specialtyTypeField.Options.Add(new KeyValuePair<object, object>("Specialty", "Specialty"));
            specialtyTypeField.Options.Add(new KeyValuePair<object, object>("Condition", "Condition"));
            _subEntity.NewRecordTemplate.Fields.Add(specialtyTypeField);

            _subEntity.NewRecordTemplate.Fields.Add(new Field(_isPrimary, "Is Primary", FieldTypes.Checkbox, false));
            _subEntity.NewRecordTemplate.Fields.Add(new Field(_isBoardCertified, "Is Board Certified", FieldTypes.Checkbox, false));
        }