Esempio n. 1
0
        public static void Execute(ViewDataDictionary viewData, DataRuleContext dataRuleContext, IEnumerable <DataRuleSetting> dataRules)
        {
            foreach (var item in dataRules)
            {
                var    contentQuery = item.DataRule.Execute(dataRuleContext);
                object data         = contentQuery;
                if (item.DataRule is DataRuleBase)
                {
                    var dataRuleBase = (DataRuleBase)item.DataRule;
                    if (!string.IsNullOrEmpty(dataRuleBase.SortField))
                    {
                        if (dataRuleBase.SortDirection == DataRule.SortDirection.Ascending)
                        {
                            contentQuery = contentQuery.OrderBy(dataRuleBase.SortField);
                        }
                        else
                        {
                            contentQuery = contentQuery.OrderByDescending(dataRuleBase.SortField);
                        }
                    }
                    if (item.DataRule.EnablePaging())
                    {
                        string pageIndexParameterName;
                        var    pageIndexValue    = ParameterizedFieldValue.GetFieldValue(dataRuleBase.PageIndex, dataRuleContext.ValueProvider, out pageIndexParameterName);
                        var    intPageIndexValue = 1;
                        int.TryParse(pageIndexValue, out intPageIndexValue);
                        if (intPageIndexValue < 1)
                        {
                            intPageIndexValue = 1;
                        }

                        string pageSizeParameterName;
                        var    pageSizeValue = ParameterizedFieldValue.GetFieldValue(dataRuleBase.PageSize, dataRuleContext.ValueProvider, out pageSizeParameterName);
                        var    intPageSize   = 10;
                        int.TryParse(pageSizeValue, out intPageSize);
                        if (intPageSize < 1)
                        {
                            intPageSize = 10;
                        }

                        var totalCount = contentQuery.Count();

                        data = new DataRulePagedList(contentQuery.Skip((intPageIndexValue - 1) * intPageSize).Take(intPageSize)
                                                     , intPageIndexValue
                                                     , intPageSize
                                                     , totalCount)
                        {
                            PageIndexParameterName = pageIndexParameterName
                        };
                    }
                    else if (!string.IsNullOrEmpty(dataRuleBase.Top))
                    {
                        string fieldName;
                        var    topValue    = ParameterizedFieldValue.GetFieldValue(dataRuleBase.Top, dataRuleContext.ValueProvider, out fieldName);
                        var    intTopValue = 1;
                        int.TryParse(topValue, out intTopValue);

                        data = contentQuery.Take(intTopValue);
                    }
                }

                if (data is IContentQuery <ContentBase> )
                {
                    data = GetData(item, (IContentQuery <TextContent>)data);
                }


                if (item.DataName.EqualsOrNullEmpty(ModelName, StringComparison.CurrentCultureIgnoreCase))
                {
                    viewData.Model = data;
                }
                viewData[item.DataName] = data;
            }
        }
Esempio n. 2
0
        public object Execute(DataRuleContext dataRuleContext, TakeOperation operation, int cacheDuration)
        {
            var    contentQuery = this.GetContentQuery(dataRuleContext);
            object data         = contentQuery;

            if (!string.IsNullOrEmpty(this.SortField))
            {
                if (this.SortDirection == DataRule.SortDirection.Ascending)
                {
                    contentQuery = contentQuery.OrderBy(this.SortField);
                }
                else
                {
                    contentQuery = contentQuery.OrderByDescending(this.SortField);
                }
            }
            if (this.EnablePaging.Value)
            {
                string pageIndexParameterName;
                var    pageIndexValue    = ParameterizedFieldValue.GetFieldValue(this.PageIndex, dataRuleContext.ValueProvider, out pageIndexParameterName);
                var    intPageIndexValue = 1;
                int.TryParse(pageIndexValue, out intPageIndexValue);
                if (intPageIndexValue < 1)
                {
                    intPageIndexValue = 1;
                }

                string pageSizeParameterName;
                var    pageSizeValue = ParameterizedFieldValue.GetFieldValue(this.PageSize, dataRuleContext.ValueProvider, out pageSizeParameterName);
                var    intPageSize   = 10;
                int.TryParse(pageSizeValue, out intPageSize);
                if (intPageSize < 1)
                {
                    intPageSize = 10;
                }

                var totalCount = contentQuery.Count();

                data = new DataRulePagedList(contentQuery.Skip((intPageIndexValue - 1) * intPageSize).Take(intPageSize)
                                             , intPageIndexValue
                                             , intPageSize
                                             , totalCount)
                {
                    PageIndexParameterName = pageIndexParameterName
                };
            }
            else if (!string.IsNullOrEmpty(this.Top))
            {
                string fieldName;
                var    topValue    = ParameterizedFieldValue.GetFieldValue(this.Top, dataRuleContext.ValueProvider, out fieldName);
                var    intTopValue = 1;
                int.TryParse(topValue, out intTopValue);

                data = contentQuery.Take(intTopValue);
            }
            else
            {
                data = contentQuery;
            }


            if (data is IContentQuery <ContentBase> )
            {
                data = GetData((IContentQuery <TextContent>)data, operation, cacheDuration);
            }

            return(data);
        }