private AddProviderLanguageRequest CreateRequest(int providerId, int languageId)
 {
     var request = new AddProviderLanguageRequest();
     request.ProviderId = providerId;
     request.LanguageId = languageId;
     return request;
 }
        public static void Save(ProviderAdminDetailView parent, SubEntity providerLanguages, int providerId)
        {
            try
            {
                providerLanguages.ErrorMessage = string.Empty;

                if (providerLanguages.Records.Count(s => bool.Parse(parent.GetFieldValue(s.Fields, _isPrimary))) > 1)
                {
                    throw new BusinessException("Only one Provider Language can be set as the primary language");
                }

                //Edits
                foreach (var record in providerLanguages.Records.Where(r => r.Action == RecordActions.Edited))
                {
                    var request = new UpdateProviderLanguageRequest();

                    request.Id = int.Parse(parent.GetFieldValue(record.Fields, _id));
                    request.IsPrimary = bool.Parse(parent.GetFieldValue(record.Fields, _isPrimary));
                    request.IsFluent = bool.Parse(parent.GetFieldValue(record.Fields, _isFluent));

                    parent.ProcessRequest<UpdateProviderLanguageResponse>(request);
                }

                //Existing Records Added
                foreach (var record in providerLanguages.Records.Where(r => r.Action == RecordActions.Added))
                {
                    var request = new AddProviderLanguageRequest()
                    {
                        ProviderId = providerId,
                        LanguageId = int.Parse(parent.GetFieldValue(record.Fields, _languageId)),
                        IsPrimary = bool.Parse(parent.GetFieldValue(record.Fields, _isPrimary)),
                        IsFluent = bool.Parse(parent.GetFieldValue(record.Fields, _isFluent))
                    };

                    parent.ProcessRequest<AddProviderLanguageResponse>(request);
                }

                //New Records Created
                foreach (var record in providerLanguages.Records.Where(r => r.Action == RecordActions.New))
                {
                    //Add New Language
                    var addLanguageRequest = new AddLanguageRequest()
                    {
                        Name = parent.GetFieldValue(record.Fields, _languageName),
                        IsEnabled = true
                    };
                    var addedLanguageId = parent.ProcessRequest<AddLanguageResponse>(addLanguageRequest).Id;

                    //Add newly created language to provider
                    var request = new AddProviderLanguageRequest()
                    {
                        ProviderId = providerId,
                        LanguageId = addedLanguageId,
                        IsPrimary = bool.Parse(parent.GetFieldValue(record.Fields, _isPrimary)),
                        IsFluent = bool.Parse(parent.GetFieldValue(record.Fields, _isFluent))
                    };

                    parent.ProcessRequest<AddProviderLanguageResponse>(request);
                }

                //Deletes
                foreach (var record in providerLanguages.Records.Where(r => r.Action == RecordActions.Deleted))
                {
                    var request = new DeleteProviderLanguageRequest()
                    {
                        ProviderId = providerId,
                        LanguageId = int.Parse(parent.GetFieldValue(record.Fields, _languageId))
                    };
                    parent.ProcessRequest<DeleteProviderLanguageResponse>(request);
                }
            }
            catch (Exception ex)
            {
                providerLanguages.ErrorMessage = ex.Message;
                throw;
            }

            providerLanguages = BuildLanguagesSubEntity(parent, providerId);
        }