Example #1
0
        public ActionResult IndicatorEdit(string urlKey, int areaType, int selectedDomainNumber, int indicatorId, int ageId, int sexId)
        {
            Profile profile = GetProfile(urlKey, selectedDomainNumber, areaType);

            // Get text properties of selected indicator
            IList <IndicatorMetadataTextProperty> properties = _reader.GetIndicatorMetadataTextProperties();
            int groupId = profile.GetSelectedGroupingMetadata(selectedDomainNumber).GroupId;

            // Assemble model
            var model = new IndicatorEdit
            {
                SelectedIndicatorId = indicatorId,
                UrlKey     = urlKey,
                Profile    = profile,
                TextValues = _reader.GetIndicatorTextValues(indicatorId, properties, profile.Id).ToList()
            };

            // Prev/Next
            if (profile.IndicatorNames.Count > 0)
            {
                int index = -1;
                IList <GroupingPlusName> names = profile.IndicatorNames;
                for (int i = 0; i < names.Count(); i++)
                {
                    if (names[i].IndicatorId == indicatorId)
                    {
                        index = i;
                        break;
                    }
                }

                int prevIndex = index > 0 ? index - 1 : names.Count - 1;
                int nextIndex = index == names.Count - 1 ? 0 : index + 1;
                model.IndicatorIdNext     = names[nextIndex].IndicatorId;
                model.IndicatorIdPrevious = names[prevIndex].IndicatorId;
            }

            //Get the indicatore meta data
            IndicatorMetadata indicatorMetaData = _reader.GetIndicatorMetadata(indicatorId);

            model.IndicatorMetadata = indicatorMetaData;

            IList <Grouping>       groupList          = _reader.GetGroupings(groupId);
            IEnumerable <Grouping> indicatorGroupData =
                groupList.Where(
                    g =>
                    g.IndicatorId == indicatorId &&
                    g.GroupId == groupId &&
                    g.AreaTypeId == areaType &&
                    g.AgeId == ageId &&
                    g.SexId == sexId);

            Grouping[] groupData = indicatorGroupData as Grouping[] ?? indicatorGroupData.ToArray();
            model.Grouping = groupData.FirstOrDefault();

            //Set the comparator Id if this is a multiple comparator grouping record
            if (groupData.Count() > 1)
            {
                //There are multiple comparator indicators
                if (model.Grouping != null)
                {
                    model.Grouping.ComparatorId = ComparatorIds.NationalAndSubnational;
                }
            }

            model.urlKey = urlKey;

            var listOfProfiles = CommonUtilities.GetOrderedListOfProfilesForCurrentUser(urlKey);

            ViewBag.listOfProfiles = listOfProfiles;

            var domains = new ProfileMembers();

            var defaultProfile = listOfProfiles.FirstOrDefault(x => x.Selected) ?? listOfProfiles.FirstOrDefault();

            if (defaultProfile != null)
            {
                defaultProfile.Selected = true;
            }

            ViewBag.listOfDomains = CommonUtilities.GetOrderedListOfDomainsWithGroupId(domains, defaultProfile, _profileRepository);

            IEnumerable <UserGroupPermissions> userPermissions =
                CommonUtilities.GetUserGroupPermissionsByUserId(_reader.GetUserByUserName(_userName).Id);

            model.UserGroupPermissions =
                userPermissions.FirstOrDefault(x => x.ProfileId == _reader.GetProfileDetails(model.UrlKey).Id);

            if (HttpContext.Request.UrlReferrer != null)
            {
                model.ReturnUrl = HttpContext.Request.UrlReferrer.ToString();
            }

            ViewBag.SexId = new SelectList(_lookUpsRepository.GetSexes(), "SexID", "Description");

            ViewBag.AgeId = new SelectList(_lookUpsRepository.GetAges(), "AgeID", "Description");

            ViewBag.YearTypeId = new SelectList(_lookUpsRepository.GetYearTypes(), "Id", "Label");

            ViewBag.ValueTypeId = new SelectList(_lookUpsRepository.GetIndicatorValueTypes(), "Id", "Label");

            ViewBag.CIMethodId = new SelectList(_lookUpsRepository.GetConfidenceIntervalMethods(), "Id", "Name");

            var unitList = new SelectList(_lookUpsRepository.GetUnits(), "Id", "Label");

            ViewBag.UnitId = unitList;

            ViewBag.DenominatorTypeId = new SelectList(_lookUpsRepository.GetDenominatorTypes(), "Id", "Name");

            return(View("IndicatorEdit", model));
        }
        /// <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"));
        }