private static void AssignAreaTypeData(IndicatorGridModel model, Profile profile)
        {
            var builder = new AreaTypeSelectListBuilder(profile.AreaTypes, profile.SelectedAreaType);

            model.SelectedAreaTypeId = builder.SelectedAreaTypeId;
            model.AreaTypeList       = builder.SelectListItems;
        }
        public ActionResult Index(int profileId = -1 /*this is currently ignored but one for the future*/)
        {
            var model = new IndicatorGridModel
            {
                SortBy           = "Sequence",
                SortAscending    = true,
                CurrentPageIndex = 1,
                PageSize         = 200
            };

            GetProfiles(model);
            GetDomains(model, null);

            Profile profile        = null;
            var     firstOrDefault = model.DomainList.FirstOrDefault();

            if (firstOrDefault != null)
            {
                profile = GetProfile(model.ProfileKey, Convert.ToInt32(firstOrDefault.Value), -1);
            }

            if (profile != null)
            {
                IQueryable <GroupingPlusName> indicators = profile.IndicatorNames.AsQueryable();

                model.Profile = profile;
                model.UrlKey  = model.ProfileKey;

                var profileContact = _reader.GetUserByUserId(profile.ContactUserId);
                model.ContactUserName = profileContact.DisplayName;
                model.EmailAddress    = profileContact.EmailAddress;

                AssignAreaTypeData(model, profile);

                var userPermissions = GetUserGroupPermissions();

                model.UserHasAssignedPermissions = userPermissions.Any();

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

                indicators = indicators.Where(i => i.AreaTypeId == profile.SelectedAreaType);

                model.TotalRecordCount = indicators.Count();

                model.IndicatorNamesGrid = indicators
                                           .Distinct()
                                           .OrderBy(model.SortExpression)
                                           .Skip((model.CurrentPageIndex - 1) * model.PageSize)
                                           .Take(model.PageSize);
            }

            return(View(ProfilesAndIndicators, model));
        }
        private void GetProfiles(IndicatorGridModel model)
        {
            var profiles = _reader.GetProfiles().OrderBy(x => x.Name);
            IList <ProfileDetails> profilesWithAssignedIndicators = profiles.ToList();
            var listOfProfiles = CommonUtilities.GetOrderedListOfProfiles(profilesWithAssignedIndicators);

            model.ProfileList = listOfProfiles;
            var firstOrDefault = model.ProfileList.FirstOrDefault();

            if (firstOrDefault != null)
            {
                model.ProfileKey = firstOrDefault.Value;
            }
        }
        private void GetDomains(IndicatorGridModel model, int?domainSequence)
        {
            var reloadedDomains = ReloadGridDomains(model.ProfileKey, _profileRepository);

            if (domainSequence.HasValue)
            {
                model.SelectedGroupId =
                    reloadedDomains.Where(x => x.Sequence == domainSequence).Select(x => x.GroupId).FirstOrDefault();
            }
            else
            {
                model.SelectedGroupId = reloadedDomains.Select(x => x.GroupId).FirstOrDefault();
            }

            model.DomainList = new SelectList(reloadedDomains.OrderBy(g => g.Sequence), "Sequence", "GroupName");
        }
        public ActionResult SortPageAndFilter(int?domainSequence, int?indicatorId, string resetArea,
                                              int selectedAreaTypeId = -1, string search_text = "", string sortBy = "Sequence",
                                              bool ascending         = true, int page = 1, int pageSize = 200, string profileKey = null)
        {
            var model = new IndicatorGridModel
            {
                SortBy           = sortBy,
                SortAscending    = @ascending,
                CurrentPageIndex = page,
                PageSize         = pageSize
            };

            // Get all the profiles
            GetProfiles(model);
            var firstOrDefault = model.ProfileList.FirstOrDefault();

            if (firstOrDefault != null)
            {
                model.ProfileKey = string.IsNullOrEmpty(profileKey) ? firstOrDefault.Value : profileKey;
            }
            GetDomains(model, domainSequence);

            var areaTypeId = resetArea == "True"
                ? -1
                : selectedAreaTypeId;

            if (domainSequence == null)
            {
                var selectListItem = model.DomainList.FirstOrDefault();
                if (selectListItem != null)
                {
                    domainSequence = int.Parse(selectListItem.Value);
                }
            }

            Profile profile = null;

            if (domainSequence.HasValue)
            {
                profile = GetProfile(model.ProfileKey, domainSequence.Value, areaTypeId);
            }

            if (profile != null)
            {
                IQueryable <GroupingPlusName> indicators = profile.IndicatorNames.AsQueryable();

                AssignAreaTypeData(model, profile);

                model.Profile = profile;
                model.UrlKey  = model.ProfileKey;

                var profileContact = _reader.GetUserByUserId(profile.ContactUserId);
                model.ContactUserName = profileContact.DisplayName;
                model.EmailAddress    = profileContact.EmailAddress;

                // Filter indicators for any search terms
                if (indicatorId != null)
                {
                    indicators = indicators.Where(i => i.IndicatorId.ToString().Contains(indicatorId.ToString()));
                }

                if (string.IsNullOrEmpty(search_text) == false)
                {
                    indicators = indicators.Where(i => i.IndicatorName.ToLower().Contains(search_text.ToLower()));
                }

                // Filter indicators by area type
                indicators = indicators.Where(i => i.AreaTypeId == profile.SelectedAreaType);

                model.TotalRecordCount = indicators.Count();

                var userPermissions = GetUserGroupPermissions();

                model.UserHasAssignedPermissions = userPermissions.Any();

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

                model.IndicatorNamesGrid = indicators
                                           .Skip((model.CurrentPageIndex - 1) * model.PageSize)
                                           .Take(model.PageSize);
            }

            return(View("ProfilesAndIndicators", model));
        }