public void Modify(string wamsId, int apiClientId, string claimSetName, int?[] selectedSchoolIds = null)
        {
            var client        = _apiClientBusiness.GetApiClient(apiClientId);
            int applicationId = client.ApplicationApplicationId.GetValueOrDefault();
            int vendorId      = client.Application.VendorVendorId.GetValueOrDefault();
            int profileId     = client.Application.Profiles.Select(p => p.ProfileId).FirstOrDefault();

            //claimset change will mean choosing/adding new application
            if (client.Application.ClaimSetName != claimSetName)
            {
                Application app = _applicationBusiness.GetOrCreateApplication(wamsId, vendorId,
                                                                              claimSetName, profileId, $"{client.Application.Vendor.VendorName} {claimSetName}");

                applicationId = app.ApplicationId;
                //keep applicationids aligned
                client.ApplicationApplicationId = applicationId;
                foreach (var clientApplicationEducationOrganization in client.ApplicationEducationOrganizations.Where(e => e.Application.VendorVendorId == vendorId))
                {
                    clientApplicationEducationOrganization.ApplicationApplicationId =
                        applicationId;
                }
                _apiClientBusiness.Update(wamsId, client);
                _apiClientBusiness.SaveChanges(wamsId);
            }

            //the only other update would be new or fewer schools
            if (selectedSchoolIds != null && selectedSchoolIds.Length > 0)
            {
                bool changes = false;
                //schools not in the current list of edorgs
                foreach (var schoolId in selectedSchoolIds.AsQueryable().Except(client.ApplicationEducationOrganizations.Select(a => (int?)a.EducationOrganizationId)))
                {
                    if (schoolId > 0)
                    {
                        var schoolApplicationEdOrg =
                            _applicationEducationOrganizationBusiness.GetOrCreateApplicationEducationOrganization(wamsId, schoolId.Value, vendorId, applicationId);
                        client.ApplicationEducationOrganizations.Add(schoolApplicationEdOrg);
                        changes = true;
                    }
                }
                //edorgs not in the current list of schools
                IQueryable <int?> currentOrgs = client.ApplicationEducationOrganizations.Select(a => (int?)a.EducationOrganizationId).ToArray().AsQueryable();
                foreach (var schoolId in currentOrgs.Except(selectedSchoolIds))
                {
                    var org = client.ApplicationEducationOrganizations.FirstOrDefault(e =>
                                                                                      e.EducationOrganizationId == schoolId);
                    if (org != null)
                    {
                        client.ApplicationEducationOrganizations.Remove(org);
                        changes = true;
                    }
                }
                if (changes)
                {
                    _apiClientBusiness.Update(wamsId, client);
                    _apiClientBusiness.SaveChanges(wamsId);
                }
            }
        }
        public JsonResult ApiClient_Update2(ApiClientModel model)
        {
            if (model != null && ModelState.IsValid)
            {
                try
                {
                    ValidateSaveApiClient(model);
                    if (HasErrors)
                    {
                        return(Json(new
                        {
                            success = false,
                            Errors = string.Format("Could not update Client {0} : {1}", model.Name, model.Errors)
                        }));
                    }

                    var client = _apiClientBusiness.GetApiClient(model.ApiClientId);
                    client.Key = model.Key;
                    if (model.SecretDisplay != Domain.SecretDisplayConstant.SecretDisplayHashed)
                    {
                        client.Secret         = model.SecretDisplay;
                        client.SecretIsHashed = false;
                    }
                    client.Name        = model.Name;
                    client.IsApproved  = model.IsApproved;
                    client.UseSandbox  = model.UseSandbox;
                    client.SandboxType = model.SandboxType;
                    client.UserUserId  = model.UserUserId;
                    if (client.ApplicationApplicationId != model.ApplicationApplicationId)
                    {
                        var application =
                            _applicationBusiness.GetApplicationById(model.ApplicationApplicationId.GetValueOrDefault());
                        client.ApplicationApplicationId = model.ApplicationApplicationId;
                        foreach (var clientApplicationEducationOrganization in client.ApplicationEducationOrganizations.Where(e => e.Application.VendorVendorId == application.VendorVendorId))
                        {
                            clientApplicationEducationOrganization.ApplicationApplicationId =
                                model.ApplicationApplicationId;
                        }
                    }
                    _apiClientBusiness.Update(WamsUser.WamsId, client);
                    _apiClientBusiness.SaveChanges(WamsUser.WamsId);
                }
                catch (Exception)
                {
                    return(Json(new
                    {
                        success = false,
                        Errors = string.Format("Could not update Client {0}", model.Name)
                    }));
                }
            }

            return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
        }