/// <summary> /// Checks the int value between minimum maximum. /// </summary> /// <param name="fieldName">Name of the field.</param> /// <param name="value">The value.</param> /// <param name="errors">The errors.</param> /// <returns></returns> public bool CheckIntBetweenMinMax(string fieldName, int value, BusinessRuleErrorList errors, int?minValue, int?maxValue) { if (minValue.HasValue == false && maxValue.HasValue == false) { return(true); } int min = minValue.HasValue ? minValue.Value : int.MinValue; int max = minValue.HasValue ? maxValue.Value : int.MaxValue; if (minValue.HasValue && maxValue.HasValue) { if (!(value >= min && value <= max)) { errors.Add(fieldName, string.Format(StringMsgs.BusinessErrors.ValueBetweenMinMax, GetFieldTitle(fieldName), min.ToString(), max.ToString())); return(false); } } if (minValue.HasValue && value < min) { errors.Add(fieldName, string.Format(StringMsgs.BusinessErrors.ValueMin, GetFieldTitle(fieldName), min.ToString())); return(false); } if (maxValue.HasValue && value > max) { errors.Add(fieldName, string.Format(StringMsgs.BusinessErrors.ValueMax, GetFieldTitle(fieldName), max.ToString())); return(false); } return(true); }
/// <summary> /// Checks the DateTime value between minimum maximum. /// </summary> /// <param name="fieldName">Name of the field.</param> /// <param name="value">The value.</param> /// <param name="errors">The errors.</param> /// <returns></returns> public bool CheckDateTimeBetweenMinMax(string fieldName, DateTime value, BusinessRuleErrorList errors, DateTime?minValue, DateTime?maxValue) { if (minValue.HasValue == false && maxValue.HasValue == false) { return(true); } DateTime min = minValue.HasValue ? minValue.Value : FWConsts.MinDateTime; DateTime max = minValue.HasValue ? maxValue.Value : FWConsts.MaxDateTime; // since date time can get a value more than .NET min and max, // we should check min and max all the time. //if (minValue.HasValue && maxValue.HasValue) //{ if (!(value >= min && value <= max)) { errors.Add(fieldName, string.Format(StringMsgs.BusinessErrors.ValueBetweenMinMax, GetFieldTitle(fieldName), min.ToString(), max.ToString())); return(false); } //} //if (minValue.HasValue && value < min) //{ // errors.Add(fieldName, string.Format(StringMsgs.BusinessErrors.ValueMin, GetFieldTitle(fieldName), min.ToString())); // return false; //} //if (maxValue.HasValue && value > max) //{ // errors.Add(fieldName, string.Format(StringMsgs.BusinessErrors.ValueMax, GetFieldTitle(fieldName), max.ToString())); // return false; //} return(true); }
/// <summary> /// Checks the detail entity rules. /// </summary> /// <param name="detailList">The list.</param> /// <param name="fnName">Name of the function.</param> /// <param name="errors">The errors.</param> private void CheckDetailEntityRules(List <DetailObjectInfo> detailList, BusinessRuleErrorList errors) { if (detailList == null) { return; } foreach (var detailInfo in detailList) { string entityName = detailInfo.EntityName; IBusinessRuleBase biz = EntityFactory.GetEntityBusinessRuleByName(entityName, detailInfo.AdditionalDataKey); biz.CheckRules(detailInfo.EntitySet, detailInfo.FnName, errors); } }
/// <summary> /// Deletes the specified entity set. /// It doesn't delete weak entititied included here, unless database does it using integrity rules /// </summary> /// <param name="entitySet">The entity set.</param> /// <param name="parameters">The parameters.</param> /// <exception cref="BRException"></exception> public void Delete(object entitySet, DeleteParameters parameters) { Check.Require(_IsInitialized, "The class is not initialized yet."); BusinessRuleErrorList errors = new BusinessRuleErrorList(); CheckRules(entitySet, RuleFunctionSEnum.Delete, errors); if (errors.Count > 0) { throw new BRException(errors); } this.DataAccessObject.Delete(entitySet, parameters); }
public bool CheckNotEqual(object fieldValue, object value, string fieldName, BusinessRuleErrorList errors) { if (fieldValue.Equals(value)) { string valueString = "null"; if (value != null) { valueString = value.ToString(); } errors.Add(fieldName, string.Format(StringMsgs.BusinessErrors.ValueNotEqual, GetFieldTitle(fieldName), valueString)); return(false); } return(true); }
/// <summary> /// Checks the string length between minimum and maximum. /// </summary> /// <param name="fieldName">Name of the field.</param> /// <param name="value">The value.</param> /// <param name="errors">The errors.</param> /// <returns></returns> public bool CheckStringLenBetweenMinAndMax(string fieldName, string value, BusinessRuleErrorList errors, int min, int max) { if (value != null) { if (!(value.Length >= min && value.Length <= max)) { errors.Add(fieldName, string.Format(StringMsgs.BusinessErrors.StringLenBetweenMinMax, GetFieldTitle(fieldName), min, max) ); return(false); } return(true); } return(false); }
/// <summary> /// Inserts the specified parameters. /// </summary> /// <param name="entitySet">The entity set.</param> /// <param name="parameters">The parameters.</param> /// <exception cref="BRException"></exception> public void Insert(object entitySet, InsertParameters parameters) { Check.Require(_IsInitialized, "The class is not initialized yet."); BusinessRuleErrorList errors = new BusinessRuleErrorList(); CheckRules(entitySet, RuleFunctionSEnum.Insert, errors); CheckDetailEntityRules(parameters.DetailEntityObjects, errors); if (errors.Count > 0) { throw new BRException(errors); } this.DataAccessObject.Insert(entitySet, parameters); }
/// <summary> /// Checks the rules. /// </summary> /// <param name="entitySet">The entity set.</param> /// <param name="fnName">Name of the fn.</param> /// <param name="errors">The errors.</param> /// <exception cref="System.NotImplementedException"></exception> public void CheckRulesT(T entitySet, RuleFunctionSEnum fnName, BusinessRuleErrorList errors) { CheckRules(entitySet, fnName, errors); }
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 }
/// <summary> /// Checks the rules. /// </summary> /// <param name="entitySet">The entity set.</param> /// <param name="fnName">Name of the fn.</param> /// <param name="errors">The errors.</param> public virtual void CheckRules(object entitySet, RuleFunctionSEnum fnName, BusinessRuleErrorList errors) { CheckAutomatedRules(entitySet, fnName, errors); }
/// <summary> /// Checks the string should not be null or empty. /// </summary> /// <param name="value">The value.</param> /// <param name="fieldName">Name of the field.</param> /// <param name="fieldTitle">The field title.</param> /// <param name="errors">The errors.</param> /// <returns></returns> public bool CheckStringShouldNotBeNullOrEmpty(string fieldName, string value, BusinessRuleErrorList errors) { if (string.IsNullOrEmpty(value)) { string fieldTitle = GetFieldTitle(fieldName); errors.Add(fieldName, string.Format(StringMsgs.BusinessErrors.StringNotNullOrEmpty, fieldTitle)); return(false); } return(true); }
/// <summary> /// Checks that a dependant entity doesn't exists /// </summary> /// <param name="entityName">dependant entity name</param> /// <param name="adk">additional data key</param> /// <param name="filter">filter to check existance</param> /// <param name="errors">errors</param> /// <param name="customErrorMessage">custom error message</param> /// <returns></returns> public bool CheckNoDependantRecordExists(string entityName, string adk, FilterExpression filter, BusinessRuleErrorList errors, string customErrorMessage) { var service = EntityFactory.GetEntityServiceByName(entityName, adk); if (service.GetCount(filter) > 0) { errors.Add(service.Entity.IDFieldName, customErrorMessage); return(false); } return(true); }
/// <summary> /// Checks the duplicate value not to be exists. /// </summary> /// <param name="fieldName">Name of the field.</param> /// <param name="value">The value.</param> /// <param name="idvalue">The idvalue.</param> /// <param name="errors">The errors.</param> /// <param name="additionalFilter">The additional filter.</param> /// <param name="isInsert">if set to <c>true</c> [is insert].</param> /// <param name="customErrorMessage">The custom error message.</param> /// <returns></returns> public bool CheckDuplicateTwoKeyNotToBeExists(object entitySet, string fieldName1, string fieldName2, string idFieldName, BusinessRuleErrorList errors, Filter additionalFilter, bool isInsert, string customErrorMessage) { Check.Require(string.IsNullOrEmpty(fieldName1) == false, "CheckDuplicateTwoKeyNotToBeExists fieldName1 can't be empty"); Check.Require(string.IsNullOrEmpty(fieldName2) == false, "CheckDuplicateTwoKeyNotToBeExists fieldName2 can't be empty"); if (isInsert == false) { Check.Require(string.IsNullOrEmpty(idFieldName) == false, "idvalue can not be null when CheckDuplicateTwoKeyNotToBeExists is called in update mode."); } object f1Value = FWUtils.EntityUtils.GetObjectFieldValue(entitySet, fieldName1); object f2Value = FWUtils.EntityUtils.GetObjectFieldValue(entitySet, fieldName2); if (string.IsNullOrEmpty(customErrorMessage)) { customErrorMessage = StringMsgs.BusinessErrors.DuplicateTwoKeyValueExists; } FilterExpression filter = new FilterExpression(new Filter(fieldName1, f1Value)); filter.AddFilter(fieldName2, f2Value); if (additionalFilter != null) { filter.AddFilter(additionalFilter); } if (isInsert == false) // in the case of update, we check against id { object idvalue = FWUtils.EntityUtils.GetObjectFieldValue(entitySet, idFieldName); filter.AddFilter(new Filter(this.Business.Entity.IDFieldName, idvalue, FilterOperatorEnum.NotEqualTo)); Check.Ensure(idvalue != null, "No value provided for id."); } var count = Business.GetCount(filter); if (count > 0) { errors.Add(fieldName1, string.Format(customErrorMessage, GetFieldTitle(fieldName1))); //errors.Add(fieldName2, string.Format(customErrorMessage, GetFieldTitle(fieldName2))); return(false); } return(true); }
/// <summary> /// Checks the duplicate value not to be exists. /// </summary> /// <param name="fieldName">Name of the field.</param> /// <param name="value">The value.</param> /// <param name="idvalue">The idvalue.</param> /// <param name="errors">The errors.</param> /// <param name="additionalFilter">The additional filter.</param> /// <param name="isInsert">if set to <c>true</c> [is insert].</param> /// <param name="customErrorMessage">The custom error message.</param> /// <returns></returns> public bool CheckDuplicateValueNotToBeExists(string fieldName, object value, object idvalue, BusinessRuleErrorList errors, Filter additionalFilter, bool isInsert, string customErrorMessage) { if (value == null) { return(true); } if (isInsert == false) { Check.Require(idvalue != null, "idvalue can not be null when CheckDuplicateValueNotToBeExists is called in update mode."); } if (string.IsNullOrEmpty(customErrorMessage)) { customErrorMessage = StringMsgs.BusinessErrors.DuplicateValueExists; } FilterExpression filter = new FilterExpression(new Filter(fieldName, value)); if (additionalFilter != null) { filter.AddFilter(additionalFilter); } if (isInsert == false) // in the case of update, we check against id { filter.AddFilter(new Filter(this.Business.Entity.IDFieldName, idvalue, FilterOperatorEnum.NotEqualTo)); Check.Ensure(idvalue != null, "No value provided for id."); } var count = Business.GetCount(filter); if (count > 0) { errors.Add(fieldName, string.Format(customErrorMessage, GetFieldTitle(fieldName))); return(false); } return(true); }
/// <summary> /// Dates the time is in unix range. /// </summary> /// <param name="value">The value.</param> /// <param name="fieldName">Name of the field.</param> /// <param name="errors">The errors.</param> /// <returns></returns> public bool DateTimeIsInUnixRange(DateTime value, string fieldName, BusinessRuleErrorList errors) { return(CheckDateTimeBetweenMinMax(fieldName, value, errors, DateTimeEpoch.UnixMinDate, DateTimeEpoch.UnixMaxDate)); }
/// <summary> /// Dates the time greater than now. /// </summary> /// <param name="value">The value.</param> /// <param name="fieldName">Name of the field.</param> /// <param name="errors">The errors.</param> /// <returns></returns> public bool DateTimeLessThanUtcNow(DateTime value, string fieldName, BusinessRuleErrorList errors) { return(CheckDateTimeBetweenMinMax(fieldName, value, errors, null, DateTime.UtcNow)); }