-
Notifications
You must be signed in to change notification settings - Fork 134
/
DefaultColumnFilter.cs
281 lines (239 loc) · 12.1 KB
/
DefaultColumnFilter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using GridShared.Filtering.Types;
namespace GridShared.Filtering
{
/// <summary>
/// Default Grid.Mvc filter. Provides logic for filtering items collection.
/// </summary>
public class DefaultColumnFilter<T, TData> : IColumnFilter<T>
{
private readonly Expression<Func<T, TData>> _expression;
private readonly FilterTypeResolver _typeResolver = new FilterTypeResolver();
private readonly PropertyInfo _pi;
public bool IsNullable { get; private set; }
public DefaultColumnFilter(Expression<Func<T, TData>> expression)
{
_expression = expression;
_pi = (PropertyInfo)((MemberExpression)_expression.Body).Member;
IsNullable = _pi.PropertyType.GetTypeInfo().IsGenericType &&
_pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
}
#region IColumnFilter<T> Members
[Obsolete("This method is obsolete. Use the ApplyFilter method including an expresion parameter.", true)]
public IQueryable<T> ApplyFilter(IQueryable<T> items, IEnumerable<ColumnFilterValue> values, MethodInfo removeDiacritics = null)
{
throw new NotImplementedException();
}
public IQueryable<T> ApplyFilter(IQueryable<T> items, IEnumerable<ColumnFilterValue> values,
Expression source, MethodInfo removeDiacritics = null)
{
if (values == null && values.Where(r => r != ColumnFilterValue.Null).Count() <= 0)
throw new ArgumentNullException("values");
GridFilterCondition condition;
var cond = values.SingleOrDefault(r => r != ColumnFilterValue.Null
&& r.FilterType == GridFilterType.Condition);
if (!Enum.TryParse(cond.FilterValue, true, out condition))
condition = GridFilterCondition.And;
values = values.Where(r => r != ColumnFilterValue.Null && r.FilterType != GridFilterType.Condition);
Expression<Func<T, bool>> expr = GetFilterExpression(values, condition, source, removeDiacritics);
if (expr == null)
return items;
return items.Where(expr);
}
#endregion
private Expression<Func<T, bool>> GetFilterExpression(IEnumerable<ColumnFilterValue> values,
GridFilterCondition condition, Expression source, MethodInfo removeDiacritics)
{
Expression binaryExpression = null;
foreach (var value in values)
{
if (value == ColumnFilterValue.Null)
continue;
Expression expression = GetExpression(value, source, removeDiacritics);
if (expression != null)
{
if (binaryExpression == null)
binaryExpression = expression;
else if (condition == GridFilterCondition.Or)
binaryExpression = Expression.OrElse(binaryExpression, expression);
else
binaryExpression = Expression.AndAlso(binaryExpression, expression);
}
}
if (binaryExpression == null)
return null;
//build expression to filter collection:
ParameterExpression entityParam = _expression.Parameters[0];
//return filter expression
return Expression.Lambda<Func<T, bool>>(binaryExpression, entityParam);
}
private Expression GetExpression(ColumnFilterValue value, Expression source, MethodInfo removeDiacritics)
{
//get target type:
Type targetType = IsNullable ? Nullable.GetUnderlyingType(_pi.PropertyType) : _pi.PropertyType;
List<string> names = new List<string>();
Expression expression = _expression.Body;
while (expression.NodeType != ExpressionType.Parameter)
{
names.Add(((MemberExpression)expression).Member.Name);
expression = ((MemberExpression)expression).Expression;
}
Expression binaryExpression = null;
for (int i = names.Count - 1; i >= 0; i--)
{
expression = Expression.Property(expression, names[i]);
var nestedPi = (PropertyInfo)((MemberExpression)expression).Member;
//detect nullable
bool nestedIsNullable = nestedPi.PropertyType.GetTypeInfo().IsGenericType &&
nestedPi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
//get target type:
Type nestedTargetType = nestedIsNullable ? Nullable.GetUnderlyingType(nestedPi.PropertyType) : nestedPi.PropertyType;
// Check for null on nested properties and not value type (string and objects are reference type)
// It's ok for ORM, but throw exception in linq to objects
if (nestedIsNullable || !nestedTargetType.IsValueType)
{
binaryExpression = binaryExpression == null ?
Expression.NotEqual(expression, Expression.Constant(null)) :
Expression.AndAlso(binaryExpression, Expression.NotEqual(expression, Expression.Constant(null)));
}
}
// return expression for IsNull and IsNotNull
if (value.FilterType == GridFilterType.IsNull)
{
value.FilterValue = "";
if (targetType == typeof(string))
/* returns expression for IsNull, checking if the strings of a column
* are null or empty */
return GetExpression(null, value, targetType, source, removeDiacritics);
else if (IsNullable)
return binaryExpression == null ?
Expression.Equal(_expression.Body, Expression.Constant(null)) :
Expression.OrElse(Expression.Not(binaryExpression), Expression.Equal(expression, Expression.Constant(null)));
else
return binaryExpression == null ? null : Expression.Not(binaryExpression);
}
else if (value.FilterType == GridFilterType.IsNotNull)
{
value.FilterValue = "";
if (targetType == typeof(string))
return GetExpression(binaryExpression, value, targetType, source, removeDiacritics);
else if (IsNullable)
return binaryExpression == null ?
Expression.NotEqual(_expression.Body, Expression.Constant(null)) :
Expression.AndAlso(binaryExpression, Expression.NotEqual(expression, Expression.Constant(null)));
else
return binaryExpression;
}
else
{
return GetExpression(binaryExpression, value, targetType, source, removeDiacritics);
}
}
private Expression GetExpression(Expression binaryExpression, ColumnFilterValue value, Type targetType,
Expression source, MethodInfo removeDiacritics)
{
IFilterType filterType = _typeResolver.GetFilterType(targetType);
//support nullable types:
Expression firstExpr = IsNullable
? Expression.Property(_expression.Body, _pi.PropertyType.GetProperty("Value"))
: _expression.Body;
var filterExpression = filterType.GetFilterExpression<T>(firstExpr, value.FilterValue, value.FilterType, source,
removeDiacritics);
if (filterExpression == null) return null;
if (IsNullable && targetType != typeof(string))
{
//add additional filter condition for check items on NULL with invoring "HasValue" method.
//for example: result of this expression will like - c=> c.HasValue && c.Value = 3
MemberExpression hasValueExpr = Expression.Property(_expression.Body,
_pi.PropertyType.GetProperty("HasValue"));
filterExpression = Expression.AndAlso(hasValueExpr, filterExpression);
}
binaryExpression = binaryExpression == null ? filterExpression :
Expression.AndAlso(binaryExpression, filterExpression);
//return filter expression
return binaryExpression;
}
#region OData
public string GetFilter(IEnumerable<ColumnFilterValue> values)
{
string result = "";
if (values == null && values.Where(r => r != ColumnFilterValue.Null).Count() <= 0)
throw new ArgumentNullException("values");
// fet condition for multiple filters
GridFilterCondition condition;
var cond = values.SingleOrDefault(r => r != ColumnFilterValue.Null
&& r.FilterType == GridFilterType.Condition);
if (!Enum.TryParse(cond.FilterValue, true, out condition))
condition = GridFilterCondition.And;
values = values.Where(r => r != ColumnFilterValue.Null && r.FilterType != GridFilterType.Condition);
var pi = (PropertyInfo)((MemberExpression)_expression.Body).Member;
foreach (var value in values)
{
if (value == ColumnFilterValue.Null)
continue;
else if (value.FilterType != GridFilterType.IsNull && value.FilterType != GridFilterType.IsNotNull
&& string.IsNullOrWhiteSpace(value.FilterValue))
continue;
string filterString = GetFilterString(value);
if (!string.IsNullOrWhiteSpace(filterString))
{
if (string.IsNullOrWhiteSpace(result))
result = filterString;
else if (condition == GridFilterCondition.Or)
result += " or " + filterString;
else
result += " and " + filterString;
}
}
return result;
}
private string GetFilterString(ColumnFilterValue value)
{
string result = "";
// get column name
List<string> names = new List<string>();
Expression expression = _expression.Body;
while (expression.NodeType != ExpressionType.Parameter)
{
names.Add(((MemberExpression)expression).Member.Name);
expression = ((MemberExpression)expression).Expression;
}
for (int i = names.Count - 1; i >= 0; i--)
{
result += names[i];
if (i != 0)
result += "/";
}
if (!string.IsNullOrWhiteSpace(result))
{
if (value.FilterType == GridFilterType.IsNull)
{
result += " eq null";
}
else if (value.FilterType == GridFilterType.IsNotNull)
{
result += " ne null";
}
else
{
//get target type:
Type targetType = IsNullable ? Nullable.GetUnderlyingType(_pi.PropertyType) : _pi.PropertyType;
IFilterType filterType = _typeResolver.GetFilterType(targetType);
result = filterType.GetFilterExpression(result, value.FilterValue, value.FilterType);
}
}
return result;
}
public bool IsTextColumn()
{
Type targetType = IsNullable ? Nullable.GetUnderlyingType(_pi.PropertyType) : _pi.PropertyType;
IFilterType filterType = _typeResolver.GetFilterType(targetType);
return filterType.GetType() == typeof(TextFilterType);
}
#endregion
}
}