Exemplo n.º 1
0
        protected virtual void CheckAutomatedRules(object entitySet, RuleFunctionSEnum fnName, BusinessRuleErrorList errors)
        {
            foreach (var colInfo in this.Entity.EntityColumns)
            {
                ColumnInfo col = colInfo.Value;

                if (
                    (col.IsUpdatable && fnName == RuleFunctionSEnum.Update) ||
                    (col.IsInsertable && fnName == RuleFunctionSEnum.Insert)
                    )
                {
                    object val = FWUtils.EntityUtils.GetObjectFieldValue(entitySet, col.Name);

                    // check allow blank for all types
                    if (col.ValidationIsNullable == false)
                    {
                        if (val == null)
                        {
                            errors.Add(col.Name, string.Format(StringMsgs.BusinessErrors.NotNull, col.Caption));
                            continue;
                        }
                        // TODO: fix foreign key integer value checking for int
                        //// for integer values if the column is id column, we check not to enter -1 value (that is the default for not having a value)
                        //if (col.Name.EndsWith("ID") && (val is Int16 || val is Int32 || val is Int64))
                        //    if (Convert.ToInt64(val) == -1)
                        //    {
                        //        errors.Add(col.Name, string.Format(StringMsgs.BusinessErrors.NotNull, col.Caption));
                        //        continue;
                        //    }
                    }

                    // checking string length
                    if (col.DataTypeDotNet == typeof(string))
                    {
                        if (col.ValidationIsNullable == false && ((string)val) == "")
                        {
                            errors.Add(col.Name, string.Format(StringMsgs.BusinessErrors.StringNotNullOrEmpty, col.Caption));
                        }

                        CheckUtils.CheckStringLenBetweenMinAndMax(col.Name, (string)val, errors, col.ValidationMinLength, col.ValidationMaxLength);
                    }

                    // checking minimum maximum value if existed
                    if (col.ValidationMaxValue != null || col.ValidationMinValue != null)
                    {
                        if (col.DataTypeDotNet == typeof(int))
                        {
                            CheckUtils.CheckIntBetweenMinMax(col.Name, (int)val, errors, (int?)col.ValidationMinValue, (int?)col.ValidationMaxValue);
                        }
                        else if (col.DataTypeDotNet == typeof(long))
                        {
                            CheckUtils.CheckLongBetweenMinMax(col.Name, (long)val, errors, (long?)col.ValidationMinValue, (long?)col.ValidationMaxValue);
                        }
                        else if (col.DataTypeDotNet == typeof(double))
                        {
                            CheckUtils.CheckDoubleBetweenMinMax(col.Name, (double)val, errors, (double?)col.ValidationMinValue, (double?)col.ValidationMaxValue);
                        }
                        else if (col.DataTypeDotNet == typeof(float))
                        {
                            CheckUtils.CheckFloatBetweenMinMax(col.Name, (float)val, errors, (float?)col.ValidationMinValue, (float?)col.ValidationMaxValue);
                        }
                        else if (col.DataTypeDotNet == typeof(decimal))
                        {
                            CheckUtils.CheckDecimalBetweenMinMax(col.Name, (decimal)val, errors, (decimal?)col.ValidationMinValue, (decimal?)col.ValidationMaxValue);
                        }
                        else if (col.DataTypeDotNet == typeof(DateTime))
                        {
                            CheckUtils.CheckDateTimeBetweenMinMax(col.Name, (DateTime)val, errors, (DateTime?)col.ValidationMinValue, (DateTime?)col.ValidationMaxValue);
                        }
                        else if (col.DataTypeDotNet == typeof(short))
                        {
                            CheckUtils.CheckShortBetweenMinMax(col.Name, (short)val, errors, (short?)col.ValidationMinValue, (short?)col.ValidationMaxValue);
                        }
                        else if (col.DataTypeDotNet == typeof(byte))
                        {
                            CheckUtils.CheckByteBetweenMinMax(col.Name, (byte)val, errors, (byte?)col.ValidationMinValue, (byte?)col.ValidationMaxValue);
                        }
                    }

                    // duplicate check
                    if (errors.Count == 0 && col.ValidationNoDuplicate) // Perfomance: We check database only if no error is there.
                    {
                        object idValue = ((EntityObjectBase)entitySet).GetPrimaryKeyValue();
                        CheckUtils.CheckDuplicateValueNotToBeExists(col.Name, val, idValue, errors, null, fnName == RuleFunctionSEnum.Insert, null);
                    }
                } // end of if column should be validated or not
            }     // end foreach column
        }