Ejemplo n.º 1
0
        /// <summary>
        /// Geta a filter expression for an attribute value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public virtual Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            if (filterValues.Count >= 2)
            {
                string comparisonValue = filterValues[0];

                if (comparisonValue == "0")
                {
                    return(null);
                }

                ComparisonType comparisonType = comparisonValue.ConvertToEnum <ComparisonType>(ComparisonType.EqualTo);

                string filterValue = filterValues[1];

                if (string.IsNullOrWhiteSpace(filterValue) &&
                    comparisonType != ComparisonType.IsBlank && comparisonType != ComparisonType.IsNotBlank)
                {
                    return(null);
                }

                //string comparisonValue = filterValues[0];
                //if ( comparisonValue != "0" )
                //{
                //ComparisonType comparisonType = comparisonValue.ConvertToEnum<ComparisonType>( ComparisonType.EqualTo );
                MemberExpression   propertyExpression = Expression.Property(parameterExpression, "Value");
                ConstantExpression constantExpression = Expression.Constant(filterValues[1], typeof(string));

                return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpression));
                //}
            }

            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a filter expression for an entity property value.
        /// </summary>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <returns></returns>
        public static Expression PropertyFilterExpression(List <string> filterValues, Expression parameterExpression, string propertyName, Type propertyType)
        {
            if (filterValues.Count >= 2)
            {
                string comparisonValue = filterValues[0];
                if (comparisonValue != "0")
                {
                    MemberExpression propertyExpression = Expression.Property(parameterExpression, propertyName);

                    var  type           = propertyType;
                    bool isNullableType = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>);
                    if (isNullableType)
                    {
                        type = Nullable.GetUnderlyingType(type);
                    }

                    object         value          = ConvertValueToPropertyType(filterValues[1], type, isNullableType);
                    ComparisonType comparisonType = comparisonValue.ConvertToEnum <ComparisonType>(ComparisonType.EqualTo);

                    bool valueNotNeeded = (ComparisonType.IsBlank | ComparisonType.IsNotBlank).HasFlag(comparisonType);

                    if (value != null || valueNotNeeded)
                    {
                        ConstantExpression constantExpression = value != null?Expression.Constant(value, type) : null;

                        return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpression));
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets a filter expression for an attribute value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public override System.Linq.Expressions.Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, System.Linq.Expressions.ParameterExpression parameterExpression)
        {
            if (filterValues.Count >= 2)
            {
                string comparisonValue = filterValues[0];
                if (comparisonValue != "0")
                {
                    Guid guid     = filterValues[1].AsGuid();
                    int  personId = new PersonAliasService(new RockContext()).Queryable()
                                    .Where(a => a.Guid.Equals(guid))
                                    .Select(a => a.PersonId)
                                    .FirstOrDefault();

                    if (personId > 0)
                    {
                        ComparisonType     comparisonType     = comparisonValue.ConvertToEnum <ComparisonType>(ComparisonType.EqualTo);
                        MemberExpression   propertyExpression = Expression.Property(parameterExpression, "ValueAsPersonId");
                        ConstantExpression constantExpression = Expression.Constant(personId, typeof(int));
                        return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpression));
                    }
                }
            }

            return(new NoAttributeFilterExpression());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets a filter expression for an entity property value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <returns></returns>
        public override Expression PropertyFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, Expression parameterExpression, string propertyName, Type propertyType)
        {
            if (filterValues.Count >= 2)
            {
                // uses Tab Delimited since slidingDateRangePicker is | delimited
                var filterValueValues = filterValues[1].Split(new string[] { "\t" }, StringSplitOptions.None);

                // Parse for RelativeValue of DateTime (if specified)
                filterValueValues[0] = ParseRelativeValue(filterValueValues[0]);

                string comparisonValue = filterValues[0];
                if (comparisonValue != "0" && comparisonValue.IsNotNullOrWhiteSpace())
                {
                    ComparisonType   comparisonType     = comparisonValue.ConvertToEnum <ComparisonType>(ComparisonType.EqualTo);
                    MemberExpression propertyExpression = Expression.Property(parameterExpression, propertyName);
                    if (comparisonType == ComparisonType.Between && filterValueValues.Length > 1)
                    {
                        var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(filterValueValues[1]);
                        ConstantExpression constantExpressionLower = dateRange.Start.HasValue
                            ? Expression.Constant(dateRange.Start, typeof(DateTime))
                            : null;

                        ConstantExpression constantExpressionUpper = dateRange.End.HasValue
                            ? Expression.Constant(dateRange.End, typeof(DateTime))
                            : null;

                        if (constantExpressionLower == null && constantExpressionUpper == null)
                        {
                            return(new NoAttributeFilterExpression());
                        }
                        else
                        {
                            return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpressionLower, constantExpressionUpper));
                        }
                    }
                    else
                    {
                        var dateTime = filterValueValues[0].AsDateTime();
                        if (dateTime.HasValue)
                        {
                            ConstantExpression constantExpression = Expression.Constant(dateTime, typeof(DateTime));
                            return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpression));
                        }
                        else
                        {
                            if (comparisonType == ComparisonType.IsBlank || comparisonType == ComparisonType.IsNotBlank)
                            {
                                return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, null));
                            }
                            else
                            {
                                return(new NoAttributeFilterExpression());
                            }
                        }
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets a filter expression to be used as part of a AttributeValue Query or EntityAttributeQueryExpression
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public virtual Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            if (filterValues.Count >= 2)
            {
                ComparisonType?comparisonType = filterValues[0].ConvertToEnumOrNull <ComparisonType>();
                if (comparisonType.HasValue)
                {
                    string compareToValue = filterValues[1];
                    bool   valueNotNeeded = (ComparisonType.IsBlank | ComparisonType.IsNotBlank).HasFlag(comparisonType);

                    if (valueNotNeeded || !string.IsNullOrWhiteSpace(compareToValue))
                    {
                        MemberExpression propertyExpression = Expression.Property(parameterExpression, this.AttributeValueFieldName);
                        return(ComparisonHelper.ComparisonExpression(comparisonType.Value, propertyExpression, AttributeConstantExpression(compareToValue)));
                    }
                }
                else
                {
                    // No comparison type specified, so return NoAttributeFilterExpression ( which means don't filter )
                    return(new NoAttributeFilterExpression());
                }
            }

            // return null if there isn't an additional expression that will help narrow down which AttributeValue records to include
            return(null);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets a filter expression for an entity property value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <returns></returns>
        public virtual Expression PropertyFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, Expression parameterExpression, string propertyName, Type propertyType)
        {
            if (filterValues.Count >= 2)
            {
                string comparisonValue = filterValues[0];
                if (comparisonValue != "0")
                {
                    MemberExpression propertyExpression = Expression.Property(parameterExpression, propertyName);

                    var  type           = propertyType;
                    bool isNullableType = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>);
                    if (isNullableType)
                    {
                        type = Nullable.GetUnderlyingType(type);
                    }

                    object value = ConvertValueToPropertyType(filterValues[1], type);
                    if (value != null)
                    {
                        ComparisonType     comparisonType     = comparisonValue.ConvertToEnum <ComparisonType>(ComparisonType.EqualTo);
                        ConstantExpression constantExpression = Expression.Constant(value, type);
                        return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpression));
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the filters expression.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public override Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            if (filterValues.Count == 1)
            {
                List <string>    selectedValues     = filterValues[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                int              valueCount         = selectedValues.Count();
                MemberExpression propertyExpression = Expression.Property(parameterExpression, "Value");
                if (valueCount == 0)
                {
                    // No Value specified, so return NoAttributeFilterExpression ( which means don't filter )
                    return(new NoAttributeFilterExpression());
                }
                else if (valueCount == 1)
                {
                    // only one value, so do an Equal instead of Contains which might compile a little bit faster
                    ComparisonType comparisonType = ComparisonType.EqualTo;
                    return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, AttributeConstantExpression(selectedValues[0])));
                }
                else
                {
                    ConstantExpression constantExpression = Expression.Constant(selectedValues, typeof(List <string>));
                    return(Expression.Call(constantExpression, typeof(List <string>).GetMethod("Contains", new Type[] { typeof(string) }), propertyExpression));
                }
            }

            return(base.AttributeFilterExpression(configurationValues, filterValues, parameterExpression));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets a filter expression for an attribute value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public override Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            List <string>  selectedValues = null;
            ComparisonType comparisonType = ComparisonType.Contains;

            if (filterValues.Count == 1)
            {
                // if there is only one filter value, it is a Contains comparison for the selectedValues
                // This is the normal thing that DataViews would do with a SelectSingleFieldType
                selectedValues = filterValues[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            }
            else if (filterValues.Count >= 2)
            {
                // if there are 2 (or more) filter values, the first is the comparison type and the 2nd is the selected value(s)
                // Note: Rock Lava Entity commands could do this, DataViews don't currently support more than just 'Contains'
                comparisonType = filterValues[0].ConvertToEnum <ComparisonType>(ComparisonType.Contains);

                if (comparisonType == ComparisonType.EqualTo)
                {
                    // If EqualTo was specified, treat it as Contains
                    comparisonType = ComparisonType.Contains;
                }

                if (comparisonType == ComparisonType.NotEqualTo)
                {
                    // If NotEqualTo was specified, treat it as DoesNotContain
                    comparisonType = ComparisonType.DoesNotContain;
                }

                selectedValues = filterValues[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            }

            // if IsBlank (or IsNotBlank)
            if ((ComparisonType.IsBlank | ComparisonType.IsNotBlank).HasFlag(comparisonType))
            {
                // Just checking if IsBlank or IsNotBlank, so let ComparisonExpression do its thing
                MemberExpression propertyExpression = Expression.Property(parameterExpression, this.AttributeValueFieldName);
                return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, AttributeConstantExpression(string.Empty)));
            }

            if (selectedValues?.Any() == true)
            {
                MemberExpression   propertyExpression = Expression.Property(parameterExpression, "Value");
                ConstantExpression constantExpression = Expression.Constant(selectedValues, typeof(List <string>));
                Expression         expression         = Expression.Call(constantExpression, typeof(List <string>).GetMethod("Contains", new Type[] { typeof(string) }), propertyExpression);
                if (comparisonType == ComparisonType.DoesNotContain)
                {
                    expression = Expression.Not(expression);
                }

                return(expression);
            }


            return(new NoAttributeFilterExpression());
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets a filter expression for an attribute value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public override Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            Expression comparison = null;

            if (filterValues.Count > 1)
            {
                ComparisonType?comparisonType = filterValues[0].ConvertToEnumOrNull <ComparisonType>();
                if (comparisonType.HasValue)
                {
                    string           compareToValue     = filterValues[1];
                    MemberExpression propertyExpression = Expression.Property(parameterExpression, this.AttributeValueFieldName);

                    if (!string.IsNullOrWhiteSpace(compareToValue))
                    {
                        List <string> selectedValues = compareToValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();

                        foreach (var selectedValue in selectedValues)
                        {
                            var searchValue     = "," + selectedValue + ",";
                            var qryToExtract    = new AttributeValueService(new Data.RockContext()).Queryable().Where(a => ("," + a.Value + ",").Contains(searchValue));
                            var valueExpression = FilterExpressionExtractor.Extract <AttributeValue>(qryToExtract, parameterExpression, "a");

                            if (comparisonType.Value != ComparisonType.Contains)
                            {
                                valueExpression = Expression.Not(valueExpression);
                            }

                            if (comparison == null)
                            {
                                comparison = valueExpression;
                            }
                            else
                            {
                                comparison = Expression.Or(comparison, valueExpression);
                            }
                        }
                    }
                    else
                    {
                        // No comparison value was specified, so we can filter if the Comparison Type using no value still makes sense
                        if ((ComparisonType.IsBlank | ComparisonType.IsNotBlank).HasFlag(comparisonType))
                        {
                            // Just checking if IsBlank or IsNotBlank, so let ComparisonExpression do its thing
                            return(ComparisonHelper.ComparisonExpression(comparisonType.Value, propertyExpression, AttributeConstantExpression(string.Empty)));
                        }
                    }
                }
            }

            if (comparison == null)
            {
                return(new Rock.Data.NoAttributeFilterExpression());
            }

            return(comparison);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Gets a filter expression for an attribute value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public override Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            Expression comparison = null;

            if (filterValues.Count > 1)
            {
                //// OR up the where clauses for each of the selected values
                // and make sure to wrap commas around things so we don't collide with partial matches
                // so it'll do something like this:
                //
                // WHERE ',' + Value + ',' like '%,bacon,%'
                // OR ',' + Value + ',' like '%,lettuce,%'
                // OR ',' + Value + ',' like '%,tomato,%'

                // should be either "Contains" or "Not Contains"
                ComparisonType comparisonType = filterValues[0].ConvertToEnum <ComparisonType>(ComparisonType.Contains);

                // No comparison value was specified, so we can filter if the Comparison Type using no value still makes sense
                if ((ComparisonType.IsBlank | ComparisonType.IsNotBlank).HasFlag(comparisonType))
                {
                    // Just checking if IsBlank or IsNotBlank, so let ComparisonExpression do its thing
                    MemberExpression propertyExpression = Expression.Property(parameterExpression, this.AttributeValueFieldName);
                    return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, AttributeConstantExpression(string.Empty)));
                }

                List <string> selectedValues = filterValues[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();

                foreach (var selectedValue in selectedValues)
                {
                    var searchValue     = "," + selectedValue + ",";
                    var qryToExtract    = new AttributeValueService(new Data.RockContext()).Queryable().Where(a => ("," + a.Value + ",").Contains(searchValue));
                    var valueExpression = FilterExpressionExtractor.Extract <AttributeValue>(qryToExtract, parameterExpression, "a");

                    if (comparisonType != ComparisonType.Contains)
                    {
                        valueExpression = Expression.Not(valueExpression);
                    }

                    if (comparison == null)
                    {
                        comparison = valueExpression;
                    }
                    else
                    {
                        comparison = Expression.Or(comparison, valueExpression);
                    }
                }
            }

            if (comparison == null)
            {
                return(new NoAttributeFilterExpression());
            }

            return(comparison);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Gets a filter expression for an attribute value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public override Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            if (filterValues.Count == 1)
            {
                MemberExpression propertyExpression = Expression.Property(parameterExpression, "ValueAsNumeric");
                ComparisonType   comparisonType     = ComparisonType.EqualTo;
                return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, AttributeConstantExpression(filterValues[0])));
            }

            return(base.AttributeFilterExpression(configurationValues, filterValues, parameterExpression));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Gets a filter expression for an entity property value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <returns></returns>
        public override Expression PropertyFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, Expression parameterExpression, string propertyName, Type propertyType)
        {
            if (filterValues.Count == 1)
            {
                MemberExpression   propertyExpression = Expression.Property(parameterExpression, propertyName);
                ConstantExpression constantExpression = Expression.Constant(bool.Parse(filterValues[0]));
                ComparisonType     comparisonType     = ComparisonType.EqualTo;
                return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpression));
            }

            return(null);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Gets a filter expression for an entity property value.
        /// </summary>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <returns></returns>
        public static Expression PropertyFilterExpression(List <string> filterValues, Expression parameterExpression, string propertyName, Type propertyType)
        {
            /* 2020-08-17 MDP
             * If it isn't fully configured we won't filter. We can detect if the filter isn't configured by..
             *
             *   1) There are less than 2 filterValues
             *   2) A comparisonType isn't specified ("0" means not specified)
             *   3) Except of in the case of IsBlank or IsNotBlank, a "CompareTo null" (filterValues[1]) value means the filter value isn't specified
             *
             *   If we have any of the above cases, we'll return Expression.Const(true), which means we won't filter on this)
             */

            if (filterValues.Count < 2)
            {
                // if PropertyFilter needs at least 2 parameters. If it doesn't, don't filter
                return(Expression.Constant(true));
            }

            string comparisonValue = filterValues[0];

            if (comparisonValue == "0")
            {
                // if the comparison as a string is "0", that means no comparison type is specified (comparisonType enum starts at 1)
                return(Expression.Constant(true));
            }

            var  type           = propertyType;
            bool isNullableType = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>);

            if (isNullableType)
            {
                type = Nullable.GetUnderlyingType(type);
            }

            object         value          = ConvertValueToPropertyType(filterValues[1], type, isNullableType);
            ComparisonType comparisonType = comparisonValue.ConvertToEnum <ComparisonType>(ComparisonType.EqualTo);
            bool           valueNotNeeded = (ComparisonType.IsBlank | ComparisonType.IsNotBlank).HasFlag(comparisonType);

            if (value != null || valueNotNeeded)
            {
                // a valid value is specified or we are doing a NotBlank/IsNotBlank, so build an filter expression
                MemberExpression   propertyExpression = Expression.Property(parameterExpression, propertyName);
                ConstantExpression constantExpression = value != null?Expression.Constant(value, type) : null;

                return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpression));
            }
            else
            {
                // if Property Filter isn't fully configured (for example "Birthday(int) greaterThan null"), don't filter the results
                return(Expression.Constant(true));
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Gets a filter expression to be used as part of a AttributeValue Query or EntityAttributeQueryExpression
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values: FieldName, <see cref="ComparisonType">Comparison Type</see>, (optional) Comparison Value(s)</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public virtual Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            // If filterValues.Count >= 2, then filterValues[0] is ComparisonType, and filterValues[1] is a CompareToValue. Otherwise, filterValues[0] is a CompareToValue (for example, a SingleSelect attribute)
            if (filterValues.Count >= 2)
            {
                ComparisonType?comparisonType = filterValues[0].ConvertToEnumOrNull <ComparisonType>();
                if (comparisonType.HasValue)
                {
                    string           compareToValue     = filterValues[1];
                    MemberExpression propertyExpression = Expression.Property(parameterExpression, this.AttributeValueFieldName);

                    if (!string.IsNullOrWhiteSpace(compareToValue))
                    {
                        // both a comparison type and value are specified, so we can process normally
                        return(ComparisonHelper.ComparisonExpression(comparisonType.Value, propertyExpression, AttributeConstantExpression(compareToValue)));
                    }
                    else
                    {
                        // No comparison value was specified, so we can filter if the Comparison Type using no value still makes sense
                        if ((ComparisonType.IsBlank | ComparisonType.IsNotBlank).HasFlag(comparisonType))
                        {
                            // Just checking if IsBlank or IsNotBlank, so let ComparisonExpression do its thing
                            return(ComparisonHelper.ComparisonExpression(comparisonType.Value, propertyExpression, AttributeConstantExpression(string.Empty)));
                        }
                        else if (this.FilterComparisonType.HasFlag(ComparisonType.IsBlank))
                        {
                            // if this Filter supports IsBlank/IsNotBlank, we can convert this to IsBlank/IsNotBlank if no value was specified
                            if (comparisonType == ComparisonType.EqualTo)
                            {
                                // an EqualTo  was specified, but no value was specified, so convert it to a IsBlank
                                return(ComparisonHelper.ComparisonExpression(ComparisonType.IsBlank, propertyExpression, AttributeConstantExpression(string.Empty)));
                            }
                            else if (comparisonType == ComparisonType.NotEqualTo)
                            {
                                // a NotEqualTo was specified, but no value was specified, so convert it to a IsNotBlank
                                return(ComparisonHelper.ComparisonExpression(ComparisonType.IsNotBlank, propertyExpression, AttributeConstantExpression(string.Empty)));
                            }
                        }
                    }
                }
                else
                {
                    // No comparison type specified, so return NoAttributeFilterExpression ( which means don't filter )
                    return(new NoAttributeFilterExpression());
                }
            }

            // return NoAttributeFilterExpression ( which means don't filter ) if there isn't enough information to make a Comparison Expression
            return(new NoAttributeFilterExpression());
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Gets a filter expression for an entity property value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <returns></returns>
        public override Expression PropertyFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, Expression parameterExpression, string propertyName, Type propertyType)
        {
            if (filterValues.Count < 2)
            {
                return(null);
            }

            var filterCompareValues = filterValues[1].FromJsonOrNull <List <Guid> >() ?? new List <Guid>();

            if (!filterCompareValues.Any())
            {
                // if there aren't any filterCompareValues specified, interpret that as 'don't filter'
                return(Expression.Constant(true));
            }

            var configurationJSON = configurationValues.GetValueOrNull(ConfigurationKey.ConfigurationJSON);
            List <ConditionalScaleRangeRule> conditionalScaleRangeRuleList = configurationJSON.FromJsonOrNull <List <ConditionalScaleRangeRule> >() ?? new List <ConditionalScaleRangeRule>();

            MemberExpression propertyExpression = Expression.Property(parameterExpression, propertyName);

            Expression rangeComparison = null;

            var conditionalScaleRangeRuleListToFilter = conditionalScaleRangeRuleList.Where(a => filterCompareValues.Contains(a.Guid)).ToList();

            foreach (var conditionalScaleRangeRule in conditionalScaleRangeRuleListToFilter)
            {
                decimal lowValue  = conditionalScaleRangeRule.LowValue ?? decimal.MinValue;
                decimal highValue = conditionalScaleRangeRule.HighValue ?? decimal.MaxValue;

                ConstantExpression constantExpressionLowValue  = Expression.Constant(lowValue, typeof(decimal));
                ConstantExpression constantExpressionHighValue = Expression.Constant(highValue, typeof(decimal));

                var rangeBetweenExpression = ComparisonHelper.ComparisonExpression(ComparisonType.Between, propertyExpression, constantExpressionLowValue, constantExpressionHighValue);

                if (rangeComparison == null)
                {
                    rangeComparison = rangeBetweenExpression;
                }
                else
                {
                    rangeComparison = Expression.Or(rangeComparison, rangeBetweenExpression);
                }
            }

            var notNullComparison = ComparisonHelper.ComparisonExpression(ComparisonType.IsNotBlank, propertyExpression, AttributeConstantExpression(string.Empty));

            var expression = Expression.AndAlso(notNullComparison, rangeComparison);

            return(expression);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Gets a filter expression for an entity property value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <returns></returns>
        public virtual Expression PropertyFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, Expression parameterExpression, string propertyName, Type propertyType)
        {
            if (filterValues.Count >= 2)
            {
                string comparisonValue = filterValues[0];
                if (comparisonValue != "0")
                {
                    MemberExpression propertyExpression = Expression.Property(parameterExpression, propertyName);

                    var  type           = propertyType;
                    bool isNullableType = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>);
                    if (isNullableType)
                    {
                        type = Nullable.GetUnderlyingType(type);
                    }

                    ComparisonType comparisonType = comparisonValue.ConvertToEnum <ComparisonType>(ComparisonType.EqualTo);

                    // Parse the comparison string for multiple values.
                    var values = filterValues[1].Split(new string[] { "||" }, StringSplitOptions.None);

                    // Create an Expression with an OR relationship between each value.
                    Expression comparisonExpression = null;

                    foreach (var value in values)
                    {
                        object typedValue = ConvertValueToPropertyType(value, type);

                        if (typedValue != null)
                        {
                            ConstantExpression constantExpression = Expression.Constant(typedValue, type);

                            if (comparisonExpression == null)
                            {
                                comparisonExpression = ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpression);
                            }
                            else
                            {
                                comparisonExpression = Expression.OrElse(comparisonExpression, ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpression));
                            }
                        }
                    }

                    return(comparisonExpression);
                }
            }

            return(null);
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Gets a filter expression for an attribute value.
 /// </summary>
 /// <param name="configurationValues">The configuration values.</param>
 /// <param name="filterValues">The filter values.</param>
 /// <param name="parameterExpression">The parameter expression.</param>
 /// <returns></returns>
 public override Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
 {
     if (filterValues.Count == 1)
     {
         // NOTE: this is for backwards compatibility for filters that were saved when Boolean DataFilters didn't have a Compare Option
         MemberExpression   propertyExpression = Expression.Property(parameterExpression, "Value");
         ConstantExpression constantExpression = Expression.Constant(filterValues[0]);
         ComparisonType     comparisonType     = ComparisonType.EqualTo;
         return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpression));
     }
     else
     {
         return(base.AttributeFilterExpression(configurationValues, filterValues, parameterExpression));
     }
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Gets a filter expression for an attribute value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public virtual Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            if (filterValues.Count >= 2)
            {
                string comparisonValue = filterValues[0];
                if (comparisonValue != "0")
                {
                    ComparisonType   comparisonType     = comparisonValue.ConvertToEnum <ComparisonType>(ComparisonType.EqualTo);
                    MemberExpression propertyExpression = Expression.Property(parameterExpression, this.AttributeValueFieldName);
                    return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, AttributeConstantExpression(filterValues[1])));
                }
            }

            return(null);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Geta a filter expression for an attribute value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public override Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            if (filterValues.Count >= 2)
            {
                string comparisonValue = filterValues[0];
                if (comparisonValue != "0")
                {
                    filterValues[1] = ParseRelativeValue(filterValues[1]);
                    DateTime date = filterValues[1].AsDateTime() ?? DateTime.MinValue;

                    ComparisonType     comparisonType     = comparisonValue.ConvertToEnum <ComparisonType>(ComparisonType.EqualTo);
                    MemberExpression   propertyExpression = Expression.Property(parameterExpression, "ValueAsDateTime");
                    ConstantExpression constantExpression = Expression.Constant(date, typeof(DateTime));

                    return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, constantExpression));
                }
            }

            return(null);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Gets a filter expression for an attribute value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public virtual Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            if (filterValues.Count >= 2)
            {
                string comparisonValue = filterValues[0];
                if (comparisonValue != "0")
                {
                    ComparisonType comparisonType = comparisonValue.ConvertToEnum <ComparisonType>(ComparisonType.EqualTo);

                    bool valueNotNeeded = (ComparisonType.IsBlank | ComparisonType.IsNotBlank).HasFlag(comparisonType);
                    if (valueNotNeeded || !string.IsNullOrWhiteSpace(filterValues[1]))
                    {
                        MemberExpression propertyExpression = Expression.Property(parameterExpression, this.AttributeValueFieldName);
                        return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, AttributeConstantExpression(filterValues[1])));
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Geta a filter expression for an attribute value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public override Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            if (filterValues.Count < 2)
            {
                return(null);
            }

            // Get Comparison Type or exit if none specified.
            string comparisonTypeValue = filterValues[0];

            if (comparisonTypeValue == "0")
            {
                return(null);
            }

            ComparisonType?comparisonType = comparisonTypeValue.ConvertToEnumOrNull <ComparisonType>();

            // If no comparison has been specified, we cannot form a valid filter expression.
            if (!comparisonType.HasValue)
            {
                return(null);
            }

            // Get Comparison Date. If a date is required but not specified, we cannot form a valid filter expression.
            string dateValue = filterValues[1];

            if (string.IsNullOrWhiteSpace(dateValue) &&
                comparisonType != ComparisonType.IsBlank && comparisonType != ComparisonType.IsNotBlank)
            {
                return(null);
            }

            filterValues[1] = ParseRelativeValue(filterValues[1]);
            DateTime date = filterValues[1].AsDateTime() ?? DateTime.MinValue;

            MemberExpression   propertyExpression = Expression.Property(parameterExpression, "ValueAsDateTime");
            ConstantExpression constantExpression = Expression.Constant(date, typeof(DateTime));

            return(ComparisonHelper.ComparisonExpression(comparisonType.Value, propertyExpression, constantExpression));
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Gets a filter expression for an attribute value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <returns></returns>
        public override Expression AttributeFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, ParameterExpression parameterExpression)
        {
            bool          allowMultiple = configurationValues != null && configurationValues.ContainsKey(ALLOW_MULTIPLE_KEY) && configurationValues[ALLOW_MULTIPLE_KEY].Value.AsBoolean();
            List <string> selectedValues;

            if (allowMultiple || filterValues.Count != 1)
            {
                ComparisonType comparisonType = filterValues[0].ConvertToEnum <ComparisonType>(ComparisonType.Contains);

                // if it isn't either "Contains" or "Not Contains", just use the base AttributeFilterExpression
                if (!(new ComparisonType[] { ComparisonType.Contains, ComparisonType.DoesNotContain }).Contains(comparisonType))
                {
                    return(base.AttributeFilterExpression(configurationValues, filterValues, parameterExpression));
                }

                //// OR up the where clauses for each of the selected values
                // and make sure to wrap commas around things so we don't collide with partial matches
                // so it'll do something like this:
                //
                // WHERE ',' + Value + ',' like '%,bacon,%'
                // OR ',' + Value + ',' like '%,lettuce,%'
                // OR ',' + Value + ',' like '%,tomato,%'

                if (filterValues.Count > 1)
                {
                    selectedValues = filterValues[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                }
                else
                {
                    selectedValues = new List <string>();
                }

                Expression comparison = null;

                foreach (var selectedValue in selectedValues)
                {
                    var searchValue     = "," + selectedValue + ",";
                    var qryToExtract    = new AttributeValueService(new Data.RockContext()).Queryable().Where(a => ("," + a.Value + ",").Contains(searchValue));
                    var valueExpression = FilterExpressionExtractor.Extract <AttributeValue>(qryToExtract, parameterExpression, "a");

                    if (comparisonType != ComparisonType.Contains)
                    {
                        valueExpression = Expression.Not(valueExpression);
                    }

                    if (comparison == null)
                    {
                        comparison = valueExpression;
                    }
                    else
                    {
                        comparison = Expression.Or(comparison, valueExpression);
                    }
                }

                if (comparison == null)
                {
                    // No Value specified, so return NoAttributeFilterExpression ( which means don't filter )
                    return(new NoAttributeFilterExpression());
                }
                else
                {
                    return(comparison);
                }
            }

            selectedValues = filterValues[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            int valueCount = selectedValues.Count();
            MemberExpression propertyExpression = Expression.Property(parameterExpression, "Value");

            if (valueCount == 0)
            {
                // No Value specified, so return NoAttributeFilterExpression ( which means don't filter )
                return(new NoAttributeFilterExpression());
            }
            else if (valueCount == 1)
            {
                // only one value, so do an Equal instead of Contains which might compile a little bit faster
                ComparisonType comparisonType = ComparisonType.EqualTo;
                return(ComparisonHelper.ComparisonExpression(comparisonType, propertyExpression, AttributeConstantExpression(selectedValues[0])));
            }
            else
            {
                ConstantExpression constantExpression = Expression.Constant(selectedValues, typeof(List <string>));
                return(Expression.Call(constantExpression, typeof(List <string>).GetMethod("Contains", new Type[] { typeof(string) }), propertyExpression));
            }
        }