/// <summary> /// Remove indicators from a profile /// </summary> public ActionResult DeleteIndicators(IEnumerable <string> jdata, string selectedProfileUrlkey, string selectedProfileName, int selectedDomainId, string selectedDomainName, int selectedAreaTypeId) { var model = new DeleteIndicatorsModel { UrlKey = selectedProfileUrlkey, Profile = GetProfile(selectedProfileUrlkey, selectedDomainId, selectedAreaTypeId), DomainName = selectedDomainName, DomainId = selectedDomainId, ProfileName = selectedProfileName, SelectedAreaTypeId = selectedAreaTypeId, IndicatorsThatCantBeRemoved = null, }; var userPermissions = CommonUtilities.GetUserGroupPermissionsByUserId(_reader.GetUserByUserName(_userName).Id); model.UserGroupPermissions = userPermissions .FirstOrDefault(x => x.ProfileId == _reader.GetProfileDetails(model.UrlKey).Id); var indicatorSpecifiers = IndicatorSpecifierParser.Parse(jdata.ToArray()); var indicatorList = GetSpecifiedIndicatorNames(indicatorSpecifiers, model.Profile.IndicatorNames); var cannotRemove = new List <GroupingPlusName>(); var canRemove = new List <GroupingPlusName>(); foreach (var indicator in indicatorList) { model.IndicatorMetadata = new IndicatorMetadata { OwnerProfileId = _reader.GetIndicatorMetadata(indicator.IndicatorId).OwnerProfileId }; //check to see if the indicator is being used in more than one domain var indicatorGrouping = _reader.DoesIndicatorExistInMoreThanOneGroup(indicator.IndicatorId, indicator.AgeId, indicator.SexId); if (indicatorGrouping.GroupBy(x => x.GroupId).Count() > 1) { if (model.DoesProfileOwnIndicator()) { //If the profile owns the indicator, check to see if this is the last occurance within this profile that is being deleted var groupIds = _reader.GetGroupingIds(model.Profile.Id); var indicatorGroups = _reader.GetGroupingByIndicatorId(new List <int> { indicator.IndicatorId }); var uniqueIndicatorGroups = new List <Grouping>(); foreach (Grouping @group in indicatorGroups.Where(group => groupIds.Contains(@group.GroupId)).Where(@group => uniqueIndicatorGroups.All(x => x.AreaTypeId != @group.AreaTypeId))) { uniqueIndicatorGroups.Add(@group); } var lastIndicatorOccuranceInProfile = uniqueIndicatorGroups.Count == 1; if (!lastIndicatorOccuranceInProfile) { canRemove.Add(indicator); } else { cannotRemove.Add(indicator); } } else { //Indicator Can Be Deleted because it is being used elsewhere and we ARE NOT trying to delete the owner canRemove.Add(indicator); } } else { //Indicator Can Be Deleted because it is NOT being used elsewhere canRemove.Add(indicator); } } model.IndicatorsThatCantBeRemoved = cannotRemove; model.IndicatorsToDelete = canRemove; if (Request.IsAjaxRequest()) { return(PartialView("_DeleteSelectedIndicators", model)); } return(View("ProfilesAndIndicators")); }