private void AddWorkflowFilter(FilterGroup filter) { if (_indexConfig?.Fields != null && _indexConfig.Fields.ContainsKey(AppConfig.FieldNamePublishStatus)) { filter.AddRule(new FilterRule() { Field = AppConfig.FieldNamePublishStatus, Value = new StringRuleValue("published"), FieldType = FieldTypeEnum.KEY }); } if (_indexConfig?.Fields != null && _indexConfig.Fields.ContainsKey(AppConfig.FieldNamePublishStartDate)) { filter.AddRule(new FilterRule() { Field = AppConfig.FieldNamePublishStartDate, Value = new DateTimeRuleValue(DateTime.Today), FieldOperator = OperatorEnum.LESS_THEN_OR_EQUALS, FieldType = FieldTypeEnum.DATETIME }); } if (_indexConfig?.Fields != null && _indexConfig.Fields.ContainsKey(AppConfig.FieldNamePublishEndDate)) { filter.AddRule(new FilterRule() { Field = AppConfig.FieldNamePublishEndDate, Value = new DateTimeRuleValue(DateTime.Today), FieldOperator = OperatorEnum.GREATER_THEN_OR_EQUALS, FieldType = FieldTypeEnum.DATETIME }); } }
private void AddRolesFilter(FilterGroup filter, IList <UserRoleInfo> roles) { string fieldName = ""; if (IndexConfig?.Fields != null && IndexConfig.Fields.ContainsKey("userrole")) { fieldName = "userrole"; } else if (IndexConfig?.Fields != null && IndexConfig.Fields.ContainsKey("userroles")) { fieldName = "userroles"; } if (!string.IsNullOrEmpty(fieldName)) { List <string> roleLst; if (roles.Any()) { roleLst = roles.Select(r => r.RoleID.ToString()).ToList(); } else { roleLst = new List <string>(); roleLst.Add("Unauthenticated"); } roleLst.Add("AllUsers"); filter.AddRule(new FilterRule() { Field = fieldName, FieldOperator = OperatorEnum.IN, MultiValue = roleLst.OrderBy(r => r).Select(r => new StringRuleValue(r)), FieldType = FieldTypeEnum.KEY }); } }
/// <summary> /// 获取查询规则 /// </summary> /// <param name="query">当前查询对象</param> /// <returns></returns> protected virtual PageRequest GetQueryRules(object query) { PageRequest request = new PageRequest(); var pros = query.GetType().GetProperties(); var rules = pros.Select(x => x.GetAttribute <QueryRuleAttribute>()).Where(x => x != null).ToList(); var groups = rules.Where(x => !string.IsNullOrEmpty(x.Group)).ToList(); var normal = rules.Except(groups).ToList(); var filter = new FilterGroup(); normal.ForEach(x => { var p = pros.FirstOrDefault(m => m.GetAttribute <QueryRuleAttribute>().FieldName == x.FieldName && m.GetAttribute <QueryRuleAttribute>().Operate == x.Operate); if (!IsIgnore(query, p)) { filter.AddRule(new FilterRule { Field = x.FieldName, Operate = x.Operate, Value = p.GetValue(query) }); } }); groups.DistinctBy(x => x.Group).ToList().ForEach(x => { FilterGroup g = new FilterGroup(x.GroupOperate); groups.Where(m => m.Group == x.Group).ToList().ForEach(m => { var p = pros.FirstOrDefault(m => m.GetAttribute <QueryRuleAttribute>().FieldName == x.FieldName && m.GetAttribute <QueryRuleAttribute>().Operate == x.Operate); if (!IsIgnore(query, p)) { g.AddRule(new FilterRule { Field = m.FieldName, Operate = m.Operate, Value = p.GetValue(query) }); } }); filter.Groups.Add(g); }); request.FilterGroup = filter; return(request); }
private void BuildQueryStringFilter(NameValueCollection queryString, FilterGroup workFlowFilter) { if (queryString != null) { foreach (string key in queryString) { if (IndexConfig != null && IndexConfig.Fields != null && IndexConfig.Fields.Any(f => f.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase))) { var indexConfig = IndexConfig.Fields.Single(f => f.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)); string val = queryString[key]; workFlowFilter.AddRule(new FilterRule() { Field = indexConfig.Key, Value = new StringRuleValue(val), FieldOperator = OperatorEnum.EQUAL, FieldType = FieldConfigUtils.GetFieldType(indexConfig.Value != null ? indexConfig.Value.IndexType : string.Empty) }); } } } }
private QueryBuilder BuildFilter(JObject query, bool addWorkflowFilter, int userId, string cultureCode, IList <UserRoleInfo> roles, NameValueCollection queryString = null) { var workFlowFilter = Select.Filter; var vExcludeCurrentItem = query["ExcludeCurrentItem"] as JValue; bool excludeCurrentItem = false; if (vExcludeCurrentItem != null && vExcludeCurrentItem.Type == JTokenType.Boolean) { excludeCurrentItem = (bool)vExcludeCurrentItem.Value; } if (excludeCurrentItem && queryString?["id"] != null) { workFlowFilter.AddRule(new FilterRule() { Field = "id", Value = new StringRuleValue(queryString["id"]), FieldOperator = OperatorEnum.NOT_EQUAL }); } var vCurrentUserItems = query["CurrentUserItems"] as JValue; bool currentUserItems = false; if (vCurrentUserItems != null && vCurrentUserItems.Type == JTokenType.Boolean) { currentUserItems = (bool)vCurrentUserItems.Value; } if (currentUserItems) { workFlowFilter.AddRule(new FilterRule() { Field = "userid", Value = new StringRuleValue(userId.ToString()), FieldOperator = OperatorEnum.EQUAL }); } var filter = query["Filter"] as JObject; if (filter != null) { foreach (var item in filter.Properties()) { var fieldConfig = FieldConfigUtils.GetField(IndexConfig, item.Name); if (item.Value is JValue) // text, int { var val = item.Value.ToString(); if (fieldConfig != null && fieldConfig.IndexType == "boolean") { bool bval; if (bool.TryParse(val, out bval)) { workFlowFilter.AddRule(new FilterRule() { Field = item.Name, FieldType = FieldTypeEnum.BOOLEAN, Value = new BooleanRuleValue(bval) }); } } else if (fieldConfig != null && fieldConfig.IndexType == "float") { float fval; if (float.TryParse(val, out fval)) { workFlowFilter.AddRule(new FilterRule() { Field = item.Name, FieldType = FieldTypeEnum.FLOAT, Value = new FloatRuleValue(fval) }); } } else if (!string.IsNullOrEmpty(val)) { workFlowFilter.AddRule(FieldConfigUtils.CreateFilterRule(IndexConfig, cultureCode, item.Name, OperatorEnum.START_WITH, new StringRuleValue(val) )); } } else if (item.Value is JArray) // enum { var arr = (JArray)item.Value; if (arr.Children().Any()) { var arrGroup = new FilterGroup(); foreach (var arrItem in arr.Children()) { if (arrItem is JValue) { var val = (JValue)arrItem; arrGroup.AddRule(FieldConfigUtils.CreateFilterRule(IndexConfig, cultureCode, item.Name, OperatorEnum.EQUAL, new StringRuleValue(val.ToString()) )); } } workFlowFilter.FilterGroups.Add(arrGroup); } else if (queryString?[item.Name] != null) { workFlowFilter.AddRule(FieldConfigUtils.CreateFilterRule(IndexConfig, cultureCode, item.Name, OperatorEnum.EQUAL, new StringRuleValue(queryString[item.Name]) )); } } else if (item.Value is JObject) // range { var valObj = (JObject)item.Value; var startDays = valObj["StartDays"] as JValue; var endDays = valObj["EndDays"] as JValue; if ((startDays?.Value != null) || (endDays?.Value != null)) { var startDate = DateTime.MinValue; var endDate = DateTime.MaxValue; var useTime = false; try { useTime = (bool)(valObj["UseTime"] as JValue).Value; } catch (Exception) { } DateTime currentDateTime = useTime ? DateTime.Now : DateTime.Today; try { startDate = currentDateTime.AddDays(-(long)startDays.Value); } catch (Exception) { } try { endDate = currentDateTime.AddDays((long)endDays.Value); } catch (Exception) { } workFlowFilter.AddRule(new FilterRule() { Field = item.Name, FieldType = FieldTypeEnum.DATETIME, LowerValue = new DateTimeRuleValue(startDate), UpperValue = new DateTimeRuleValue(endDate), FieldOperator = OperatorEnum.BETWEEN }); } } } } BuildQueryStringFilter(queryString, workFlowFilter); if (addWorkflowFilter) { AddWorkflowFilter(workFlowFilter); AddRolesFilter(workFlowFilter, roles); } //Filter = Filter.FilterRules.Any() || Filter.FilterGroups.Any() > 0 ? q : null; return(this); }
public static Select MergeJpListQuery(FieldConfig config, Select select, List <StatusDTO> statuses, string cultureCode) { var query = select.Query; foreach (StatusDTO status in statuses) { switch (status.action) { case "paging": { int number; // string value (it could be number or "all") int.TryParse(status.data.number, out number); select.PageSize = number; select.PageIndex = status.data.currentPage; break; } case "filter": { if (status.data != null) { if (status.type == "textbox" && !string.IsNullOrEmpty(status.name) && !string.IsNullOrEmpty(status.data.value)) { var names = status.name.Split(','); if (names.Length == 1) { query.AddRule(FieldConfigUtils.CreateFilterRule(config, cultureCode, status.name, OperatorEnum.START_WITH, new StringRuleValue(status.data.value) )); } else { var group = new FilterGroup() { Condition = ConditionEnum.OR }; foreach (var n in names) { group.AddRule(FieldConfigUtils.CreateFilterRule(config, cultureCode, n, OperatorEnum.START_WITH, new StringRuleValue(status.data.value) )); } query.FilterGroups.Add(group); } } else if ((status.type == "checkbox-group-filter" || status.type == "button-filter-group" || status.type == "combined") && !string.IsNullOrEmpty(status.name)) { if (status.data.filterType == "pathGroup" && status.data != null && status.data.pathGroup != null && status.data.pathGroup.Count > 0 && status.data.pathGroup[0] != "*") { query.AddRule(FieldConfigUtils.CreateFilterRule(config, cultureCode, status.name, OperatorEnum.IN, status.data.pathGroup.Select(s => new StringRuleValue(s)) )); } } else if ((status.type == "filter-select" || status.type == "filter-drop-down") && !string.IsNullOrEmpty(status.name)) { if (status.data.filterType == "path" && !string.IsNullOrEmpty(status.data.path) && (status.data.path != "*")) { query.AddRule(FieldConfigUtils.CreateFilterRule(config, cultureCode, status.name, OperatorEnum.EQUAL, new StringRuleValue(status.data.path) )); } } else if (status.type == "autocomplete" && !string.IsNullOrEmpty(status.data.path) && !string.IsNullOrEmpty(status.data.value)) { var names = status.data.path.Split(','); if (names.Length == 1) { query.AddRule(FieldConfigUtils.CreateFilterRule(config, cultureCode, status.data.path, OperatorEnum.START_WITH, new StringRuleValue(status.data.value) )); } else { var group = new FilterGroup() { Condition = ConditionEnum.OR }; foreach (var n in names) { group.AddRule(FieldConfigUtils.CreateFilterRule(config, cultureCode, n, OperatorEnum.START_WITH, new StringRuleValue(status.data.value) )); } query.FilterGroups.Add(group); } } } break; } case "sort": { select.Sort.Clear(); select.Sort.Add(FieldConfigUtils.CreateSortRule(config, cultureCode, status.data.path, status.data.order == "desc" )); break; } } } return(select); }