/// <summary>
        /// Get the result of the query
        /// </summary>
        /// <param name="tempResult">Temporal result</param>
        /// <param name="model">Model</param>
        /// <param name="queryUsers">User list</param>
        /// <param name="parameters">Parameters</param>
        /// <param name="context">Database context</param>
        /// <returns></returns>
        private static List<UserGridModel> GetResultUserQuery(List<UserGridModel> tempResult, UserGridModel model, List<string> queryUsers, ParameterSearchUser parameters, VestalisEntities context)
        {
            string businessApps = string.Empty;
            List<string> businessApplicationsLocalAdmin = new List<string>();
            List<string> queryBusinessAppByUser = new List<string>();
            List<BusinessApplication> businessAppUserLocal = null;

            if (!parameters.IsGlobalAdmin)
            {
                businessAppUserLocal = GetBusinessAppLocalAdminLogged(parameters.LoggedUserName, context);
                string[] rolesforUser = Roles.GetRolesForUser(parameters.LoggedUserName);
                foreach (var item in businessAppUserLocal)
                {
                    string localAdminRoles = string.Format("ApplicationAdministrator_{0}", item.Prefix);
                    if (rolesforUser.Contains(localAdminRoles))
                        businessApplicationsLocalAdmin.Add(item.BusinessApplicationName);
                }
            }

            foreach (string user in queryUsers)
            {
                if (!Roles.IsUserInRole(user, "GlobalAdministrator"))
                {
                    if (!parameters.IsGlobalAdmin)
                    {
                        queryBusinessAppByUser = (from vestalisBusinessApp in context.VestalisUserApplications
                                                  join businessApplication in context.BusinessApplications on vestalisBusinessApp.BusinessApplicationId equals businessApplication.BusinessApplicationId
                                                  where vestalisBusinessApp.IsDeleted == false && businessApplication.IsDeleted == false
                                                  && vestalisBusinessApp.UserName == user
                                                  && businessApplicationsLocalAdmin.Contains(businessApplication.BusinessApplicationName)
                                                  orderby businessApplication.BusinessApplicationName
                                                  select businessApplication.BusinessApplicationName).ToList();
                    }
                    else
                    {
                        queryBusinessAppByUser = (from vestalisBusinessApp in context.VestalisUserApplications
                                                  join businessApplication in context.BusinessApplications on vestalisBusinessApp.BusinessApplicationId equals businessApplication.BusinessApplicationId
                                                  where vestalisBusinessApp.IsDeleted == false && businessApplication.IsDeleted == false
                                                  && vestalisBusinessApp.UserName == user
                                                  orderby businessApplication.BusinessApplicationName
                                                  select businessApplication.BusinessApplicationName).ToList();
                    }

                    string roles = string.Empty;
                    foreach (var item in queryBusinessAppByUser)
                    {
                        roles = GetRolesOfBusinessApplicationByUser(context, item, user);
                        if (item != queryBusinessAppByUser.Last())
                            businessApps += string.Format("{0} ({1})",item,roles) + " | ";
                        else
                            businessApps += string.Format("{0} ({1})", item, roles);
                    }
                }
                else
                {
                    businessApps = LanguageResource.All;
                }
                string obtainedUserType = GetUserParameter(user, "UserType");
                int userType = string.IsNullOrEmpty(obtainedUserType) ? 0 : int.Parse(obtainedUserType);

                if (!parameters.IsGlobalAdmin)
                {
                    if (userType != 1 && queryBusinessAppByUser.Count > 0)
                    {
                        model = new UserGridModel
                        {
                            Email = user,
                            UserType = parameters.UserTypes.FirstOrDefault(data => data.Key == userType).Value,
                            BusinessApplications = businessApps
                        };
                        tempResult.Add(model);
                    }
                }
                else
                {
                    model = new UserGridModel
                    {
                        Email = user,
                        UserType = parameters.UserTypes.FirstOrDefault(data => data.Key == userType).Value,
                        BusinessApplications = businessApps
                    };
                    tempResult.Add(model);
                }

                businessApps = string.Empty;
            }

            return tempResult;
        }
        /// <summary>
        /// Perform the search operation of the users
        /// </summary>
        /// <param name="sortDirection">Sort direction</param>
        /// <param name="sortedColumn">Sorted column</param>
        /// <param name="page">Current page</param>
        /// <returns></returns>
        public PartialViewResult SearchUsersPaginated(SortDirection? sortDirection, string sortedColumn, int? page)
        {
            PaginatedList<UserGridModel> model = new PaginatedList<UserGridModel>();
            Guid? businessApplicationId = Session["selectedAppId"] as Guid?;
            page = page == null ? 1 : page;
            int pageSize = Cotecna.Vestalis.Web.Properties.Settings.Default.PageSize;

            ParameterSearchUser parameters = new ParameterSearchUser
            {
                BusinessApplicationId = businessApplicationId.GetValueOrDefault(),
                PageSize = pageSize,
                SelectedPage = page.GetValueOrDefault(),
                SortDirection = sortDirection.GetValueOrDefault(),
                SortedColumn = sortedColumn,
                UserTypes = UserTypes,
                IsGlobalAdmin = IsGlobalAdmin,
                LoggedUserName = UserName
            };

            model = AuthorizationBusiness.Instance.GetUserList(parameters);
            Session.Add("selectedAppId", businessApplicationId);
            Session.Add("UserSearch", model);
            return PartialView("_UsersGrid", model);
        }
        /// <summary>
        /// Get the list of user and by business applications
        /// </summary>
        /// <param name="parameters">The list of parameters</param>
        /// <returns></returns>
        public PaginatedList<UserGridModel> GetUserList(ParameterSearchUser parameters)
        {
            PaginatedList<UserGridModel> finalResult = new PaginatedList<UserGridModel>();
            List<UserGridModel> tempResult = new List<UserGridModel>();
            UserGridModel model = null;
            int currentIndex = (parameters.SelectedPage - 1) * parameters.PageSize;
            List<string> queryUsers = null;
            using (VestalisEntities context = new VestalisEntities())
            {
                queryUsers = GetUserQuery(parameters.BusinessApplicationId, context);

                if (queryUsers != null)
                {
                    tempResult = GetResultUserQuery(tempResult, model, queryUsers,parameters, context);
                }
                if (tempResult.Count > 0)
                {
                    finalResult.SortDirection = parameters.SortDirection;
                    finalResult.SortedColumn = parameters.SortedColumn;

                    //order the result
                    tempResult = (parameters.SortDirection == SortDirection.Ascending
                                           ? tempResult.OrderBy(ExtensionMethods.GetField<UserGridModel>(parameters.SortedColumn))
                                           : tempResult.OrderByDescending(ExtensionMethods.GetField<UserGridModel>(parameters.SortedColumn))).ToList();

                    //set the paginated colletion
                    if (!parameters.IsExport)
                        finalResult.Collection = tempResult.Skip(currentIndex).Take(parameters.PageSize).ToList();
                    else
                        finalResult.Collection = tempResult;

                    //set the quantity of elements without pagination
                    finalResult.TotalCount = tempResult.Count;
                    //set the number of pages
                    finalResult.NumberOfPages = (int)Math.Ceiling((double)finalResult.TotalCount / (double)parameters.PageSize);
                    //set the current page
                    finalResult.Page = parameters.SelectedPage;
                    //set the page size
                    finalResult.PageSize = parameters.PageSize;
                }

            }
            return finalResult;
        }
        /// <summary>
        /// Perform the search operation for export the result to an excel file
        /// </summary>
        /// <param name="businessAppId">Id of business application</param>
        public void SeachUsersExport(Guid? businessAppId)
        {
            PaginatedList<UserGridModel> model = new PaginatedList<UserGridModel>();
            int pageSize = Cotecna.Vestalis.Web.Properties.Settings.Default.PageSize;

            ParameterSearchUser parameters = new ParameterSearchUser
            {
                BusinessApplicationId = businessAppId.GetValueOrDefault(),
                PageSize = pageSize,
                SortDirection = SortDirection.Ascending,
                SortedColumn = "UserType",
                UserTypes = UserTypes,
                IsExport = true,
                IsGlobalAdmin = IsGlobalAdmin,
                LoggedUserName = UserName
            };
            model = AuthorizationBusiness.Instance.GetUserList(parameters);
            Session.Add("ResultSearchUsers", model);
        }