Example #1
0
        public object Post(string id, ListQueryModel model) {
            if (string.IsNullOrEmpty(id)) {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
            var part = GetProjectionPartRecord(model.ViewId);
            if (part == null) {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
            var pluralService = PluralizationService.CreateService(new CultureInfo("en-US"));
            id = pluralService.Singularize(id);
            string filterDescription = null;
            
            return GetFilteredRecords(id, part, out filterDescription, model, p => {
                _gridService.GenerateSortCriteria(id, model.Sidx, model.Sord, p.Record.QueryPartRecord.Id);
                var totalRecords = _projectionManager.GetCount(p.Record.QueryPartRecord.Id);
                var pageSize = model.Rows;
                var totalPages = (int) Math.Ceiling((float) totalRecords/(float) pageSize);
                var pager = new Pager(Services.WorkContext.CurrentSite, model.Page, pageSize);
                var records = GetLayoutComponents(p, pager.GetStartIndex(), pager.PageSize);

                return new {
                    totalPages = totalPages,
                    page = model.Page,
                    totalRecords = totalRecords,
                    rows = records,
                    filterDescription = filterDescription
                };
            });
        }
Example #2
0
        public HttpResponseMessage Post(string id, ListQueryModel model) {
            if (string.IsNullOrEmpty(id)) {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
            var pluralService = PluralizationService.CreateService(new CultureInfo("en-US"));
            id = pluralService.Singularize(id);

            var part = GetProjectionPartRecord(model.ViewId);
            IEnumerable<JObject> entityRecords = new List<JObject>();
            int totalNumber = 0;
            if (part != null) {
                var filterRecords = CreateFilters(id, model);
                var filters = part.Record.QueryPartRecord.FilterGroups.First().Filters;
                filterRecords.ForEach(filters.Add);

                totalNumber = _projectionManager.GetCount(part.Record.QueryPartRecord.Id);
                int skipCount = model.PageSize*(model.Page - 1);
                int pageCount = totalNumber <= model.PageSize*model.Page ? totalNumber - model.PageSize*(model.Page - 1) : model.PageSize;
                entityRecords = GetLayoutComponents(part, skipCount, pageCount);

                foreach (var record in filterRecords) {
                    filters.Remove(record);
                    if (model.FilterGroupId == 0) {
                        _filterRepository.Delete(record);
                    }
                }
            }
            var returnResult = new {TotalNumber = totalNumber, EntityRecords = entityRecords};
            var json = JsonConvert.SerializeObject(returnResult);
            var message = new HttpResponseMessage {Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")};
            return message;
        }
Example #3
0
        private object GetFilteredRecords(string id, ProjectionPart part, out string filterDescription, ListQueryModel model, Func<ProjectionPart, object> query) {
            model.Filters = model.Filters ?? new FilterData[] {};

            var filterRecords = CreateFilters(id, model, out filterDescription);
            var filters = part.Record.QueryPartRecord.FilterGroups.First().Filters;
            filterRecords.ForEach(filters.Add);
            try {
                return query(part);
            }

            finally {
                foreach (var record in filterRecords) {
                    filters.Remove(record);
                    if (model.FilterGroupId == 0) {
                        _filterRepository.Delete(record);
                    }
                }
            }
        }
Example #4
0
        private IList<FilterRecord> CreateFilters(string entityName, ListQueryModel model, out string filterDescription) {
            IList<FilterRecord> filterRecords;
            filterDescription = string.Empty;
            if (model.FilterGroupId == 0) {
                var filterDescriptors = _projectionManager.DescribeFilters()
                    .Where(x => x.Category == entityName + "ContentFields")
                    .SelectMany(x => x.Descriptors).ToList();
                filterRecords = new List<FilterRecord>();
                if (model.IsRelationList) {
                    if (model.RelationType == "OneToMany") {
                        var settings = new Dictionary<string, string> {
                            {"Operator","MatchesAny"},
                            {"Value",model.CurrentItem.ToString("D")}
                        };
                        var relationFilter = new FilterRecord {
                            Category = entityName + "ContentFields",
                            Type = entityName + "." + model.RelationId + ".",
                            State = FormParametersHelper.ToString(settings),
                            Description = "Only show entries related to current item."
                        };
                        filterRecords.Add(relationFilter);
                        var descriptor = filterDescriptors.First(x => x.Type == relationFilter.Type);
                        filterDescription += descriptor.Display(new FilterContext { State = FormParametersHelper.ToDynamic(relationFilter.State) }).Text;
                    }
                }

                foreach (var filter in model.Filters) {
                    if (filter.FormData.Length == 0) {
                        continue;
                    }
                    var record = new FilterRecord {
                        Category = entityName + "ContentFields",
                        Type = filter.Type,
                    };
                    var dictionary = new Dictionary<string, string>();
                    foreach (var data in filter.FormData) {
                        if (dictionary.ContainsKey(data.Name)) {
                            dictionary[data.Name] += "&" + data.Value;
                        }
                        else {
                            dictionary.Add(data.Name, data.Value);
                        }
                    }
                    record.State = FormParametersHelper.ToString(dictionary);
                    filterRecords.Add(record);
                    var descriptor = filterDescriptors.First(x => x.Type == filter.Type);
                    filterDescription += descriptor.Display(new FilterContext {State = FormParametersHelper.ToDynamic(record.State)}).Text;
                }
            }
            else {
                filterRecords = _filterGroupRepository.Get(model.FilterGroupId).Filters;
            }
            return filterRecords;
        }
Example #5
0
 private IList<FilterRecord> CreateFilters(string entityName, ListQueryModel model) {
     IList<FilterRecord> filterRecords;
     if (model.FilterGroupId == 0) {
         filterRecords = new List<FilterRecord>();
         foreach (var filter in model.Filters) {
             if (filter.FormData.Length == 0) {
                 continue;
             }
             var record = new FilterRecord {
                 Category = entityName + "ContentFields",
                 Type = filter.Type,
             };
             var dictionary = filter.FormData.ToDictionary(x => x.Name, x => x.Value);
             record.State = FormParametersHelper.ToString(dictionary);
             filterRecords.Add(record);
         }
     }
     else {
         filterRecords = _filterGroupRepository.Get(model.FilterGroupId).Filters;
     }
     return filterRecords;
 }