public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (!(value is ComparisonOperators)) { return(String.Empty); } return(ComparisonOperatorNode.OperatorToString((ComparisonOperators)value)); }
private void AddOperand() { if (SelectedFilterConfiguration == null || SelectedFilterableArgument == null) { return; } var fieldLeaf = new ExpressionTreeFieldLeaf(null, SelectedFilterableArgument.BoundProperty); object value; if (SelectedFilterableArgument.DefaultValue != null) { value = SelectedFilterableArgument.DefaultValue; } else if (fieldLeaf.Property.PropertyType == typeof(DateTime)) { value = DateTime.Now; } else if (fieldLeaf.Property.PropertyType == typeof(String)) { value = String.Empty; } else if (fieldLeaf.Property.PropertyType.IsAbstract) { value = null; } else { value = Activator.CreateInstance(fieldLeaf.Property.PropertyType); } var valueLeaf = new ExpressionTreeValueLeaf(null, value); var compariosonNode = new ComparisonOperatorNode(null, fieldLeaf, valueLeaf, SelectedFilterableArgument.AvailableOperators.FirstOrDefault()); //First node always add as comparision node if (SelectedFilterConfiguration.Conditions.Root == null) { SelectedFilterConfiguration.Conditions.Root = compariosonNode; } else { //Default add join node as AND operator if (SelectedExpression == null) { //If adding to root var rootBooleanNode = new BooleanOperatorNode(null, compariosonNode, SelectedFilterConfiguration.Conditions.Root, BooleanOperators.And); SelectedFilterConfiguration.Conditions.Root = rootBooleanNode; } else if (SelectedExpression is BooleanOperatorNode && ((BooleanOperatorNode)SelectedExpression).Operator != BooleanOperators.Not && SelectedExpression.Left == null) { //Adding to existing node SelectedExpression.Left = compariosonNode; SelectedFilterConfiguration.Conditions.Root = SelectedFilterConfiguration.Conditions.Root.Clone() as ExpressionTreeNode; } else if (SelectedExpression.Parent is BooleanOperatorNode && ((BooleanOperatorNode)SelectedExpression.Parent).Operator != BooleanOperators.Not && SelectedExpression.Parent.Left == null) { //Adding to existing node SelectedExpression.Parent.Left = compariosonNode; SelectedFilterConfiguration.Conditions.Root = SelectedFilterConfiguration.Conditions.Root.Clone() as ExpressionTreeNode; } else { //Adding to arbitrary node bool isParentNull = SelectedExpression.Parent == null; bool isSelectedBoolNode = SelectedExpression is BooleanOperatorNode; ExpressionTreeElement leftExpession; ExpressionTreeElement rightExpression; if (isSelectedBoolNode) { leftExpession = SelectedExpression.Right; } else if (isParentNull) { leftExpession = SelectedExpression; } else { leftExpession = SelectedExpression.Parent.Right; } if (leftExpession is BooleanOperatorNode) { rightExpression = leftExpession; leftExpession = compariosonNode; } else { rightExpression = compariosonNode; } var newBoolNode = new BooleanOperatorNode( SelectedExpression.Parent, leftExpession != null ? (leftExpession.Clone() as ExpressionTreeElement) : null, rightExpression.Clone() as ExpressionTreeElement, BooleanOperators.And); if (isParentNull && !isSelectedBoolNode) { SelectedFilterConfiguration.Conditions.Root = newBoolNode; } else { Trace.WriteLine("Before adding to SelectedExpression.Parent\r\n" + SelectedExpression.Parent); if (isSelectedBoolNode) { SelectedExpression.Right = newBoolNode; } else { SelectedExpression.Parent.Right = newBoolNode; } Trace.WriteLine("After adding to SelectedExpression.Parent\r\n" + SelectedExpression.Parent); SelectedFilterConfiguration.Conditions.Root = SelectedFilterConfiguration.Conditions.Root.Clone() as ExpressionTreeNode; } Trace.WriteLine("Final expression after adding\r\n" + SelectedFilterConfiguration.Conditions.Root); } } if (SelectedFilterConfiguration.Conditions.Root != null) { Task.Run(() => { Thread.Sleep(100); Application.Current.Dispatcher.Invoke(() => SelectedExpression = SelectedFilterConfiguration.Conditions.Root.Find(compariosonNode) as ExpressionTreeNode); }); } else { SelectedExpression = null; } }
/// <summary> /// Получить строковое выражение фильтрации /// </summary> /// <param name="filterElement">Параметры фильтра</param> /// <param name="filterablePropertyDescription">Описание свойств фильтрации</param> /// <returns>Строковое выражение фильтрации</returns> private string GetStringFilteringConditions(ExpressionTreeElement filterElement, FilterablePropertyDescriptionCollection filterablePropertyDescription) { var fieldLeaf = filterElement as ExpressionTreeFieldLeaf; if (fieldLeaf != null) { var foundFieldDescr = filterablePropertyDescription.FirstOrDefault( prop => prop.BoundProperty.Equals(fieldLeaf.PropertyDescription)); if (foundFieldDescr != null) { return(foundFieldDescr.Title); } } var valueLeaf = filterElement as ExpressionTreeValueLeaf; if (valueLeaf != null) { var fieldForValue = filterElement.Parent.Left as ExpressionTreeFieldLeaf; if (fieldForValue != null) { var foundFieldDescr = filterablePropertyDescription.FirstOrDefault( prop => prop.BoundProperty.Equals(fieldForValue.PropertyDescription)); if (foundFieldDescr != null && foundFieldDescr.ValueToStringConverter != null) { return(foundFieldDescr.ValueToStringConverter.Convert(valueLeaf.FieldValue, valueLeaf.FieldValue.GetType(), null, CultureInfo.CurrentCulture).ToString()); } } if (valueLeaf.FieldValue is string) { return("\"" + valueLeaf.FieldValue + "\""); } if (valueLeaf.FieldValue is DateTime) { return("\"" + ((DateTime)valueLeaf.FieldValue).ToString(CultureInfo.CurrentCulture) + "\""); } if (valueLeaf.FieldValue == null) { return(LocalizationDictionary.Empty); } else { return(valueLeaf.FieldValue.ToString()); } } var comparisonNode = filterElement as ComparisonOperatorNode; if (comparisonNode != null) { if (comparisonNode.Right is ExpressionTreeValueLeaf) { if (((ExpressionTreeValueLeaf)comparisonNode.Right).FieldValue == null || (((ExpressionTreeValueLeaf)comparisonNode.Right).FieldValue is String && String.IsNullOrEmpty(((ExpressionTreeValueLeaf)comparisonNode.Right).FieldValue as String))) { return(GetStringFilteringConditions(comparisonNode.Left, filterablePropertyDescription) + " " + ComparisonOperatorNode.OperatorToString(comparisonNode.Operator) + " " + LocalizationDictionary.Empty); } return(GetStringFilteringConditions(comparisonNode.Left, filterablePropertyDescription) + " " + ComparisonOperatorNode.OperatorToString(comparisonNode.Operator) + " " + GetStringFilteringConditions(comparisonNode.Right, filterablePropertyDescription)); } } var booleanNode = filterElement as BooleanOperatorNode; if (booleanNode != null) { var boolOperatorItemsSource = new LocalizableEnumItemsSource { Type = typeof(BooleanOperators) }; if (booleanNode.Operator == BooleanOperators.Not) { return(boolOperatorItemsSource.Convert(booleanNode.Operator, null, null, null) + " " + (booleanNode.Left is BooleanOperatorNode ? "( " : String.Empty) + GetStringFilteringConditions(booleanNode.Left, filterablePropertyDescription) + (booleanNode.Left is BooleanOperatorNode ? " )" : String.Empty)); } return(GetStringFilteringConditions(booleanNode.Left, filterablePropertyDescription) + " " + boolOperatorItemsSource.Convert(booleanNode.Operator, null, null, null) + " " + GetStringFilteringConditions(booleanNode.Right, filterablePropertyDescription)); } return(String.Empty); }