Esempio n. 1
0
        private void InitializeFilter(TPropEnum prop, FilterConditions condition, FilterOperators op, object val)
        {
            var propertyInt = prop.ToInt32(null);

            if (!Lookup.PropTypes[_currentType].ContainsKey(propertyInt))
            {
                throw new ArgumentException("Property " + prop.ToString() + " is not supported for this Entity");
            }
            _propertyId    = propertyInt;
            _property      = prop;
            _propName      = _property.ToString();
            _type          = Lookup.PropTypes[_currentType][propertyInt];
            _typeCode      = Type.GetTypeCode(_type);
            _typeName      = _type.Name;
            _condition     = condition;
            _isNullable    = Lookup.NullableProps[_currentType].Contains(propertyInt);
            _isDateFilter  = _supportedConditions[_dateKey].Contains(_condition);
            _isYearFilter  = _supportedConditions[_yearKey].Contains(_condition);
            _isMonthFilter = _supportedConditions[_monthKey].Contains(_condition);
            _isDayFilter   = _supportedConditions[_dayKey].Contains(_condition);
            var isContains  = _supportedConditions[_containKey].Contains(_condition);
            var isDatePart  = _isDateFilter || _isYearFilter || _isMonthFilter || _isDayFilter;
            var requiresInt = _isYearFilter || _isMonthFilter || _isDayFilter;

            _isBoolean = _type == typeof(bool);
            _operator  = op;
            _value     = val;
            if (_value == null)
            {
                _hasValue = false;
                if (!_isNullable)
                {
                    throw new ArgumentException("Property: " + _propName + " is not nullable.");
                }
                if (isContains)
                {
                    throw new ArgumentException("Any Contains or DoesNotContain condition requires a non null value.");
                }
                if (isDatePart)
                {
                    throw new ArgumentException("Condition: " + _condition.ToString() + " cannot be applied to null values.");
                }
                if (_condition != FilterConditions.Equals && _condition != FilterConditions.NotEquals)
                {
                    throw new ArgumentException("When Value is null the property: " + _propName + " supports only Equals, NotEquals.");
                }
                _isArray = false;
            }
            else
            {
                var valType     = _value.GetType();
                var valTypeName = string.Empty;
                if (valType.IsArray)
                {
                    valTypeName = valType.GetElementType().Name;
                    _isArray    = true;
                    _hasValue   = ((Array)_value).Length > 0;
                }
                else
                {
                    valTypeName = valType.Name;
                    _isArray    = false;
                }
                if (requiresInt)
                {
                    if (string.CompareOrdinal(valTypeName, "Int32") != 0)
                    {
                        throw new ArgumentException("Invalid type. Condition: " + _condition.ToString() + " expects int or int[]).");
                    }
                }
                else
                {
                    if (string.CompareOrdinal(_typeName, "Int64") == 0)
                    {
                        if (string.CompareOrdinal(valTypeName, "Int64") != 0 && string.CompareOrdinal(valTypeName, "Int32") != 0)
                        {
                            throw new ArgumentException("Invalid type. Property: " + _propName + " expects " + _typeName + " or " + _typeName + "[]).");
                        }
                    }
                    else
                    {
                        if (string.CompareOrdinal(valTypeName, _typeName) != 0)
                        {
                            throw new ArgumentException("Invalid type. Property: " + _propName + " expects " + _typeName + " or " + _typeName + "[]).");
                        }
                    }
                }
            }
            if (_isArray)
            {
                if (Lookup.BinaryProps[_currentType].Contains(propertyInt))
                {
                    if (_condition != FilterConditions.Equals && _condition != FilterConditions.NotEquals)
                    {
                        throw new ArgumentException("When Value is byte[] and the property is binary... " + _propName + " supports only Equals, NotEquals.");
                    }
                }
                else
                {
                    if (!isContains)
                    {
                        throw new ArgumentException("When Value is array " + _typeName + "[] the property: " + _propName + " supports only one of the Contains or DoesNotContain condition.");
                    }
                }
            }
            else
            {
                if (!GetPropertySupportsCondition(_typeName, _condition))
                {
                    throw new ArgumentException("Condition " + _condition.ToString() + " is not supported for type " + _typeName);
                }
            }
        }