public JqGridPagingHelper(JqGridSearchModel searchModel)
 {
     PageSize   = searchModel.PageSize;
     PageNumber = searchModel.Page;
     Skip       = (PageSize * (PageNumber - 1)) < 0 ? 0 : PageSize * (PageNumber - 1);
     Result     = new JqGridResultViewModel <T>();
 }
        public JqGridResultViewModel <UserViewModel> GetUsers(JqGridSearchModel jqGridSearchModel)
        {
            try
            {
                //TODO add support for join query
                if (jqGridSearchModel != null)
                {
                    List <UserViewModel> model = new List <UserViewModel>();


                    JqGridPaging <User, UserViewModel> JqGridPaging = new JqGridPaging <User, UserViewModel>(unitOfWork.UserRepository.GetAllQueryable(), "Id", jqGridSearchModel);
                    var users = JqGridPaging.GetResult();
                    var test  = UserManager.Users.ToList();

                    if (users != null)
                    {
                        foreach (var user in users)
                        {
                            model.Add(new UserViewModel(user));
                        }
                    }

                    return(JqGridPaging.GetJqGridResult(model));
                }
                return(null);
            }
            catch (Exception e)
            {
                LogError("GetUsers", e);

                Trace.TraceError("GetUsers in user service error : {0}", e.Message);
                throw e;
            }
        }
示例#3
0
        public JqGridResultViewModel <ClientViewModel> GetWebApiClients(JqGridSearchModel jqGridSearchModel)
        {
            try
            {
                if (jqGridSearchModel != null)
                {
                    List <ClientViewModel> model = new List <ClientViewModel>();

                    JqGridPaging <DAL.Models.Authentication.Client, ClientViewModel> JqGridPaging = new JqGridPaging <DAL.Models.Authentication.Client, ClientViewModel>(unitOfWork.ClientRepository.GetAllQueryable(), "ClientId", jqGridSearchModel);
                    var clients = JqGridPaging.GetResult();

                    if (clients != null)
                    {
                        foreach (var client in clients)
                        {
                            model.Add(new ClientViewModel(client));
                        }
                    }

                    return(JqGridPaging.GetJqGridResult(model));
                }
                return(null);
            }
            catch (Exception e)
            {
                LogError("GetWebApiClients", e);
                Trace.TraceError(string.Format("GetWebApiClients in account service error: {0}", e.Message));
                throw e;
            }
        }
示例#4
0
        public ActionResult GetUsers(JqGridPostData jqGridPostData)
        {
            try
            {
                JqGridSearchModel search = new JqGridSearchModel();
                search.Search     = jqGridPostData.Search;
                search.Filters    = jqGridPostData.GetFilter();
                search.PageSize   = jqGridPostData.PageSize;
                search.SortColumn = jqGridPostData.SortColumn;
                search.SortOrder  = jqGridPostData.SortOrder;
                search.Page       = jqGridPostData.Page;

                var result = service.GetUsers(search);
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
示例#5
0
        public static Expression <Func <TClass, bool> > CreateLambdaFilterExpression <TClass>(JqGridSearchModel search)
        {
            if (search != null)
            {
                if (search.Filters != null)
                {
                    //lists for filters
                    List <MethodCallExpression> lambdaBodyArray       = new List <MethodCallExpression>();
                    List <BinaryExpression>     lambdaBinaryBodyArray = new List <BinaryExpression>();

                    //p =>
                    var item = Expression.Parameter(typeof(TClass), "p");

                    //create expression for each filter
                    for (var i = 0; i < search.Filters.Rules.Count; i++)
                    {
                        Type t = typeof(TClass);

                        PropertyInfo propInfo = t.GetProperty(search.Filters.Rules[i].Field);

                        var type           = propInfo.PropertyType;
                        var underlyingType = Nullable.GetUnderlyingType(type);//will be null if type is not nullable

                        if (propInfo.PropertyType.FullName == typeof(System.Int32).ToString())
                        {
                            int  value;
                            bool isParsed;

                            isParsed = int.TryParse(search.Filters.Rules[i].Data, out value);

                            if (isParsed)
                            {
                                if (search.Filters.Rules[i].Op == "lt")
                                {
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(int));
                                    var        bodyItem = Expression.LessThan(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                                else if (search.Filters.Rules[i].Op == "le")
                                {
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(int));
                                    var        bodyItem = Expression.LessThanOrEqual(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                                else if (search.Filters.Rules[i].Op == "gt")
                                {
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(int));
                                    var        bodyItem = Expression.GreaterThan(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                                else if (search.Filters.Rules[i].Op == "ge")
                                {
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(int));
                                    var        bodyItem = Expression.GreaterThanOrEqual(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                                else if (search.Filters.Rules[i].Op == "ne")
                                {
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(int));
                                    var        bodyItem = Expression.NotEqual(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                                else
                                {
                                    //p.Property
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(int));
                                    var        bodyItem = Expression.Equal(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                            }
                        }
                        else if (propInfo.PropertyType.FullName == typeof(System.Double).ToString())
                        {
                            double value;
                            bool   isParsed;

                            isParsed = double.TryParse(search.Filters.Rules[i].Data, out value);

                            if (isParsed)
                            {
                                if (search.Filters.Rules[i].Op == "lt")
                                {
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(double));
                                    var        bodyItem = Expression.LessThan(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                                else if (search.Filters.Rules[i].Op == "le")
                                {
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(double));
                                    var        bodyItem = Expression.LessThanOrEqual(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                                else if (search.Filters.Rules[i].Op == "gt")
                                {
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(double));
                                    var        bodyItem = Expression.GreaterThan(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                                else if (search.Filters.Rules[i].Op == "ge")
                                {
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(double));
                                    var        bodyItem = Expression.GreaterThanOrEqual(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                                else if (search.Filters.Rules[i].Op == "ne")
                                {
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(double));
                                    var        bodyItem = Expression.NotEqual(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                                else
                                {
                                    //p.Property
                                    Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                    Expression right    = Expression.Constant(value, typeof(double));
                                    var        bodyItem = Expression.Equal(left, right);

                                    lambdaBinaryBodyArray.Add(bodyItem);
                                }
                            }
                        }
                        else if (propInfo.PropertyType.FullName == typeof(System.Guid).ToString())
                        {
                            //do nothing
                        }
                        else if (propInfo.PropertyType.FullName == typeof(System.DateTime).ToString())
                        {
                            bool     isDateTime = false;
                            DateTime dt         = new DateTime();

                            try
                            {
                                dt         = Convert.ToDateTime(search.Filters.Rules[i].Data);
                                isDateTime = true;
                            }
                            catch (Exception e)
                            {
                            }

                            if (isDateTime && search.Filters.Rules[i].Op == "lt")
                            {
                                Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                Expression right    = Expression.Constant(dt, typeof(DateTime));
                                var        bodyItem = Expression.LessThan(left, right);

                                lambdaBinaryBodyArray.Add(bodyItem);
                            }

                            else if (isDateTime && search.Filters.Rules[i].Op == "le")
                            {
                                DateTime   dt1      = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
                                Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                Expression right    = Expression.Constant(dt1, typeof(DateTime));
                                var        bodyItem = Expression.LessThanOrEqual(left, right);

                                lambdaBinaryBodyArray.Add(bodyItem);
                            }

                            else if (isDateTime && search.Filters.Rules[i].Op == "ne")
                            {
                                Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                Expression right    = Expression.Constant(dt, typeof(DateTime));
                                var        bodyItem = Expression.NotEqual(left, right);

                                lambdaBinaryBodyArray.Add(bodyItem);
                            }

                            else if (isDateTime && search.Filters.Rules[i].Op == "gt")
                            {
                                DateTime   dt1      = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
                                Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                Expression right    = Expression.Constant(dt1, typeof(DateTime));
                                var        bodyItem = Expression.GreaterThan(left, right);

                                lambdaBinaryBodyArray.Add(bodyItem);
                            }

                            else if (isDateTime && search.Filters.Rules[i].Op == "ge")
                            {
                                Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                Expression right    = Expression.Constant(dt, typeof(DateTime));
                                var        bodyItem = Expression.GreaterThanOrEqual(left, right);

                                lambdaBinaryBodyArray.Add(bodyItem);
                            }

                            else if (isDateTime)
                            {
                                Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                Expression right    = Expression.Constant(dt, typeof(DateTime));
                                var        bodyItem = Expression.GreaterThanOrEqual(left, right);

                                DateTime   dt1       = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
                                Expression right1    = Expression.Constant(dt1, typeof(DateTime));
                                var        bodyItem1 = Expression.LessThanOrEqual(left, right1);

                                var combinedLambda = Expression.AndAlso(bodyItem, bodyItem1);

                                lambdaBinaryBodyArray.Add(combinedLambda);
                            }
                        }
                        else if (underlyingType != null && underlyingType.FullName == typeof(System.DateTime).ToString())
                        {
                            bool     isDateTime = false;
                            DateTime dt         = new DateTime();

                            try
                            {
                                dt         = Convert.ToDateTime(search.Filters.Rules[i].Data);
                                isDateTime = true;
                            }
                            catch (Exception e)
                            {
                            }

                            if (isDateTime)
                            {
                                Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                Expression right    = Expression.Constant(dt, typeof(DateTime?));
                                var        bodyItem = Expression.GreaterThanOrEqual(left, right);

                                DateTime?  dt1       = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
                                Expression right1    = Expression.Constant(dt1, typeof(DateTime?));
                                var        bodyItem1 = Expression.LessThanOrEqual(left, right1);

                                var combinedLambda = Expression.AndAlso(bodyItem, bodyItem1);

                                lambdaBinaryBodyArray.Add(combinedLambda);
                            }
                        }
                        else
                        {
                            //p.Property

                            if (search.Filters.Rules[i].Op == "ne")
                            {
                                Expression left     = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                Expression right    = Expression.Constant(search.Filters.Rules[i].Data, typeof(string));
                                var        bodyItem = Expression.NotEqual(left, right);

                                lambdaBinaryBodyArray.Add(bodyItem);
                            }

                            else
                            {
                                var property = Expression.PropertyOrField(item, search.Filters.Rules[i].Field);
                                var toLower  = typeof(string).GetMethod("ToLower", System.Type.EmptyTypes);
                                var method   = GetMethod(search.Filters.Rules[i].Op);
                                var argument = Expression.Constant(search.Filters.Rules[i].Data);

                                var tempBodyItem = Expression.Call(property, toLower);
                                var bodyItem     = Expression.Call(tempBodyItem, method, argument);

                                lambdaBodyArray.Add(bodyItem);
                            }
                        }
                    }

                    Expression tempLambdaBody = null;

                    //join filters to one expression
                    string searchString = search.Filters.Rules.FirstOrDefault().Data;

                    if (!String.IsNullOrEmpty(searchString))
                    {
                        GroupOperator ope = search.Filters.GroupOp;

                        if (ope == GroupOperator.AND)
                        {
                            if (lambdaBodyArray != null && lambdaBodyArray.Count != 0)
                            {
                                tempLambdaBody = lambdaBodyArray[0];

                                for (var j = 0; j < lambdaBodyArray.Count - 1; j++)
                                {
                                    var second     = lambdaBodyArray[j + 1];
                                    var lambdaBody = Expression.AndAlso(tempLambdaBody, second);
                                    tempLambdaBody = lambdaBody;
                                }
                            }

                            if (lambdaBinaryBodyArray != null && lambdaBinaryBodyArray.Count > 0)
                            {
                                if (tempLambdaBody == null)
                                {
                                    tempLambdaBody = lambdaBinaryBodyArray[0];
                                }

                                for (var j = 0; j < lambdaBinaryBodyArray.Count - 1; j++)
                                {
                                    var binaryItem = lambdaBinaryBodyArray[j + 1];
                                    var lambdaBody = Expression.AndAlso(tempLambdaBody, binaryItem);
                                    tempLambdaBody = lambdaBody;
                                }
                            }
                        }
                        else
                        {
                            if (lambdaBodyArray != null && lambdaBodyArray.Count != 0)
                            {
                                tempLambdaBody = lambdaBodyArray[0];

                                for (var j = 0; j < lambdaBodyArray.Count - 1; j++)
                                {
                                    var second     = lambdaBodyArray[j + 1];
                                    var lambdaBody = Expression.OrElse(tempLambdaBody, second);
                                    tempLambdaBody = lambdaBody;
                                }
                            }

                            if (lambdaBinaryBodyArray != null && lambdaBinaryBodyArray.Count > 0)
                            {
                                if (tempLambdaBody == null)
                                {
                                    tempLambdaBody = lambdaBinaryBodyArray[0];
                                }

                                for (var j = 0; j < lambdaBinaryBodyArray.Count; j++)
                                {
                                    var binaryItem = lambdaBinaryBodyArray[j];
                                    var lambdaBody = Expression.OrElse(tempLambdaBody, binaryItem);
                                    tempLambdaBody = lambdaBody;
                                }
                            }
                        }
                    }

                    //create final expression
                    if (tempLambdaBody != null)
                    {
                        var lambda = (Expression <Func <TClass, bool> >)Expression.Lambda(tempLambdaBody, item);

                        return(lambda);
                    }
                }
            }
            return(null);
        }
示例#6
0
        public JqGridResultViewModel <RefreshTokenViewModel> GetClientUsersRefreshTokens(Guid clientId, JqGridSearchModel jqGridSearchModel = null)
        {
            try
            {
                if (jqGridSearchModel != null)
                {
                    List <RefreshTokenViewModel> model = new List <RefreshTokenViewModel>();

                    JqGridPaging <RefreshToken, RefreshTokenViewModel> JqGridPaging = new JqGridPaging <RefreshToken, RefreshTokenViewModel>(unitOfWork.RefreshTokenRepository.GetAllQueryable(), "RefreshTokenId", jqGridSearchModel, "ClientId", clientId);
                    var refreshTokens = JqGridPaging.GetResult();

                    if (refreshTokens != null)
                    {
                        foreach (var token in refreshTokens)
                        {
                            model.Add(new RefreshTokenViewModel(token));
                        }
                    }

                    return(JqGridPaging.GetJqGridResult(model));
                }
                return(null);
            }
            catch (Exception e)
            {
                LogError("GetClientUsersRefreshTokens", e);
                Trace.TraceError("GetClientUsersRefreshTokens in webapi service error : {0}", e.Message);
                throw e;
            }
        }