Exemple #1
0
        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));
        }