/// <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");
        }
        public ActionResult ConfirmDeleteIndicators(DeleteIndicatorsModel indicatorsModel, string indicatorDeleteDetails)
        {
            if (indicatorDeleteDetails != string.Empty)
            {
                var areaTypeId = indicatorsModel.SelectedAreaTypeId;
                var groupId = GetSelectedGroupIdUsingProfileKeyDomainAndAreaTypeId(
                    indicatorsModel.UrlKey, indicatorsModel.DomainId, areaTypeId);

                var indicatorSpecifierStrings = indicatorDeleteDetails.Split(',').ToList();
                var indicatorSpecifiers = IndicatorSpecifierParser.Parse(indicatorSpecifierStrings);

                foreach (var indicatorSpecifier in indicatorSpecifiers)
                {
                    var indicatorId = indicatorSpecifier.IndicatorId;
                    var sexId = indicatorSpecifier.SexId;
                    var ageId = indicatorSpecifier.AgeId;
                    var profileId = indicatorsModel.Profile.Id;

                    var indicatorMetadata = _reader.GetIndicatorMetadata(indicatorId);

                    if (indicatorMetadata.OwnerProfileId == profileId)
                    {
                        // Indicator owned by profile

                        var groupings = GetGroupingsForIndicatorInProfile(profileId, indicatorId);

                        var distinctGroupingsForProfile = 
                            GetDistinctGroupingsByGroupIdAndAreaTypeId(groupings);

                        if (distinctGroupingsForProfile.Count > 1)
                        {
                            //This isn't the last occurance of this indicator in this profile so it doesn't need to be archived and can simply be deleted from the grouping table
                            DeleteFromGrouping(groupId, indicatorId, areaTypeId, sexId, ageId);
                        }
                        else
                        {
                            //Indicator is owned by the profile so archive it
                            _profileRepository.ArchiveIndicatorFromGrouping(groupId, indicatorId,
                                areaTypeId, sexId, ageId);

                            //Set the indicator ownership to the archive profile
                            new IndicatorOwnerChanger(_reader, _profileRepository)
                            .AssignIndicatorToProfile(indicatorId, ProfileIds.ArchivedIndicators);

                            _profileRepository.LogAuditChange("Indicator " + indicatorId +
                                " (Area: " + areaTypeId + ", SexId:" + sexId +
                                ", AgeId:" + ageId + " )  has been archived.",
                                indicatorId, null, _userName, DateTime.Now,
                                CommonUtilities.AuditType.Delete.ToString());
                        }
                    }
                    else
                    {
                        // Indicator not owned by profile
                        DeleteFromGrouping(groupId, indicatorId, areaTypeId, sexId, ageId);
                    }
                }

                _profileRepository.Dispose();

                _writer.ReorderIndicatorSequence(groupId, areaTypeId);
            }

            return Redirect(Request.UrlReferrer.AbsoluteUri);
        }
        public ActionResult ConfirmDeleteIndicators(DeleteIndicatorsModel dim, string indicatorDeleteDetails)
        {
            if (indicatorDeleteDetails != string.Empty)
            {
                var areaTypeId = dim.SelectedAreaTypeId;
                var groupId = GetSelectedGroupIdUsingProfileKeyDomainAndAreaTypeId(dim.UrlKey, dim.DomainId, areaTypeId);

                var indicatorSpecifierStrings = indicatorDeleteDetails.Split(',').ToList();
                var indicatorSpecifiers = IndicatorSpecifierParser.Parse(indicatorSpecifierStrings);

                foreach (var indicatorSpecifier in indicatorSpecifiers)
                {
                    var indicatorId = indicatorSpecifier.IndicatorId;
                    var sexId = indicatorSpecifier.SexId;
                    var ageId = indicatorSpecifier.AgeId;

                    var indicatorMetadata = _reader.GetIndicatorMetadata(indicatorId);

                    if (indicatorMetadata.OwnerProfileId == dim.Profile.Id)
                    {
                        //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.GetProfileDetailsByProfileId(dim.Profile.Id).GroupIds;
                        var indicatorList = _reader.GetGroupingByIndicatorId(new List<int> { indicatorId });

                        var indicatorGroups = new List<Grouping>();
                        foreach (Grouping @group in indicatorList.Where(group => groupIds.Contains(@group.GroupId)).Where(@group => indicatorGroups.All(x => x.AreaTypeId != @group.AreaTypeId)))
                        {
                            indicatorGroups.Add(@group);
                        }

                        var lastIndicatorOccuranceInProfile = indicatorGroups.Count == 1;

                        if (!lastIndicatorOccuranceInProfile)
                        {
                            //This isn't the last occurance of this indicator in this profile so it doesn't need to be archived and can simply be deleted from the grouping table
                            DeleteFromGrouping(groupId, indicatorId, areaTypeId, sexId, ageId);
                        }
                        else
                        {
                            //Indicator is owned by the profile so archive it
                            _dataAccess.ArchiveIndicatorFromGrouping(groupId, indicatorId,
                                areaTypeId, sexId, ageId);

                            //Set the indicator ownership to the archive profile
                            _dataAccess.UpdateIndicatorOwnership(CommonUtilities.ArchiveProfileId,
                                indicatorId);

                            _dataAccess.LogAuditChange("Indicator " + indicatorId +
                                " (Area: " + areaTypeId + ", SexId:" + sexId +
                                ", AgeId:" + ageId + " )  has been archived.",
                                indicatorId, null, _userName, DateTime.Now,
                                CommonUtilities.AuditType.Delete.ToString());
                        }
                    }
                    else
                    {
                        DeleteFromGrouping(groupId, indicatorId, areaTypeId, sexId, ageId);
                    }
                }

                _writer.ResetSequences(groupId, areaTypeId);
            }

            return Redirect(Request.UrlReferrer.AbsoluteUri);
        }