internal void Validate(RuleValidation validation) { int oldErrorCount = validation.Errors.Count; if (string.IsNullOrEmpty(name)) { validation.Errors.Add(new ValidationError(Messages.RuleNameMissing, ErrorNumbers.Error_InvalidConditionName)); } // check the condition if (condition == null) { validation.Errors.Add(new ValidationError(Messages.MissingRuleCondition, ErrorNumbers.Error_MissingRuleCondition)); } else { condition.Validate(validation); } // check the optional then actions if (thenActions != null) { ValidateRuleActions(thenActions, validation); } // check the optional else actions if (elseActions != null) { ValidateRuleActions(elseActions, validation); } // fix up the error messages by prepending the rule name ValidationErrorCollection errors = validation.Errors; if (errors.Count > oldErrorCount) { string prefix = string.Format(CultureInfo.CurrentCulture, Messages.RuleValidationError, name); int newErrorCount = errors.Count; for (int i = oldErrorCount; i < newErrorCount; ++i) { ValidationError oldError = errors[i]; ValidationError newError = new ValidationError(prefix + oldError.ErrorText, oldError.ErrorNumber, oldError.IsWarning); foreach (DictionaryEntry de in oldError.UserData) { newError.UserData[de.Key] = de.Value; } errors[i] = newError; } } }
public override bool Evaluate(Activity activity, IServiceProvider provider) { if (activity == null) { throw new ArgumentNullException("activity"); } if (string.IsNullOrEmpty(this._condition)) { throw new InvalidOperationException(SR.GetString(SR.Error_MissingConditionName, activity.Name)); } RuleDefinitions defs = null; if (string.IsNullOrEmpty(this.declaringActivityId)) { // No Runtime Initialization. CompositeActivity declaringActivity = null; defs = RuleConditionReference.GetRuleDefinitions(activity, out declaringActivity); } else { // Runtime Initialized. defs = (RuleDefinitions)activity.GetActivityByName(declaringActivityId).GetValue(RuleDefinitions.RuleDefinitionsProperty); } if ((defs == null) || (defs.Conditions == null)) { throw new InvalidOperationException(SR.GetString(SR.Error_MissingRuleConditions)); } RuleCondition conditionDefinitionToEvaluate = defs.Conditions[this._condition]; if (conditionDefinitionToEvaluate != null) { Activity contextActivity = System.Workflow.Activities.Common.Helpers.GetEnclosingActivity(activity); RuleValidation validation = new RuleValidation(contextActivity); if (!conditionDefinitionToEvaluate.Validate(validation)) { string message = string.Format(CultureInfo.CurrentCulture, Messages.ConditionValidationFailed, this._condition); throw new InvalidOperationException(message); } RuleExecution context = new RuleExecution(validation, contextActivity, provider as ActivityExecutionContext); return(conditionDefinitionToEvaluate.Evaluate(context)); } else { // no condition, so defaults to true return(true); } }
public override bool Evaluate(Activity activity, IServiceProvider provider) { if (activity == null) { throw new ArgumentNullException("activity"); } if (string.IsNullOrEmpty(this._condition)) { throw new InvalidOperationException(SR.GetString("Error_MissingConditionName", new object[] { activity.Name })); } RuleDefinitions ruleDefinitions = null; if (string.IsNullOrEmpty(this.declaringActivityId)) { CompositeActivity declaringActivity = null; ruleDefinitions = GetRuleDefinitions(activity, out declaringActivity); } else { ruleDefinitions = (RuleDefinitions)activity.GetActivityByName(this.declaringActivityId).GetValue(RuleDefinitions.RuleDefinitionsProperty); } if ((ruleDefinitions == null) || (ruleDefinitions.Conditions == null)) { throw new InvalidOperationException(SR.GetString("Error_MissingRuleConditions")); } RuleCondition condition = ruleDefinitions.Conditions[this._condition]; if (condition == null) { return(true); } Activity enclosingActivity = Helpers.GetEnclosingActivity(activity); RuleValidation validation = new RuleValidation(enclosingActivity); if (!condition.Validate(validation)) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ConditionValidationFailed, new object[] { this._condition })); } RuleExecution execution = new RuleExecution(validation, enclosingActivity, provider as ActivityExecutionContext); return(condition.Evaluate(execution)); }
public override ValidationErrorCollection Validate(ValidationManager manager, object obj) { if (manager == null) { throw new ArgumentNullException("manager"); } if (manager.Context == null) { throw new InvalidOperationException(Messages.ContextStackMissing); } ValidationErrorCollection errors = base.Validate(manager, obj); RuleConditionReference reference = obj as RuleConditionReference; if (reference == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Messages.UnexpectedArgumentType, new object[] { typeof(RuleConditionReference).FullName, "obj" }), "obj"); } Activity activity = manager.Context[typeof(Activity)] as Activity; if (activity == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, new object[] { typeof(Activity).Name })); } if (!(manager.Context[typeof(PropertyValidationContext)] is PropertyValidationContext)) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, new object[] { typeof(PropertyValidationContext).Name })); } if (!string.IsNullOrEmpty(reference.ConditionName)) { RuleDefinitions definitions = null; RuleConditionCollection conditions = null; CompositeActivity declaringActivity = Helpers.GetDeclaringActivity(activity); if (declaringActivity == null) { declaringActivity = Helpers.GetRootActivity(activity) as CompositeActivity; } if (activity.Site != null) { definitions = ConditionHelper.Load_Rules_DT(activity.Site, declaringActivity); } else { definitions = ConditionHelper.Load_Rules_RT(declaringActivity); } if (definitions != null) { conditions = definitions.Conditions; } if ((conditions == null) || !conditions.Contains(reference.ConditionName)) { ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ConditionNotFound, new object[] { reference.ConditionName }), 0x53e) { PropertyName = base.GetFullPropertyName(manager) + ".ConditionName" }; errors.Add(error); return(errors); } RuleCondition condition = conditions[reference.ConditionName]; ITypeProvider service = (ITypeProvider)manager.GetService(typeof(ITypeProvider)); using ((WorkflowCompilationContext.Current == null) ? WorkflowCompilationContext.CreateScope(manager) : null) { RuleValidation validation = new RuleValidation(activity, service, WorkflowCompilationContext.Current.CheckTypes); condition.Validate(validation); ValidationErrorCollection errors2 = validation.Errors; if (errors2.Count > 0) { string fullPropertyName = base.GetFullPropertyName(manager); string errorText = string.Format(CultureInfo.CurrentCulture, Messages.InvalidConditionExpression, new object[] { fullPropertyName }); int errorNumber = 0x558; if (activity.Site != null) { ValidationError error2 = new ValidationError(errorText, errorNumber) { PropertyName = fullPropertyName + ".Expression" }; errors.Add(error2); } else { foreach (ValidationError error3 in errors2) { ValidationError error4 = new ValidationError(errorText + " " + error3.ErrorText, errorNumber) { PropertyName = fullPropertyName + ".Expression" }; errors.Add(error4); } } } foreach (RuleCondition condition2 in conditions) { if ((condition2.Name == reference.ConditionName) && (condition2 != condition)) { ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.DuplicateConditions, new object[] { reference.ConditionName }), 0x53f) { PropertyName = base.GetFullPropertyName(manager) + ".ConditionName" }; errors.Add(error5); } } return(errors); } } ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InvalidConditionName, new object[] { "ConditionName" }), 0x540) { PropertyName = base.GetFullPropertyName(manager) + ".ConditionName" }; errors.Add(item); return(errors); }
public override ValidationErrorCollection Validate(ValidationManager manager, object obj) { if (manager == null) { throw new ArgumentNullException("manager"); } if (manager.Context == null) { throw new InvalidOperationException(Messages.ContextStackMissing); } ValidationErrorCollection validationErrors = base.Validate(manager, obj); RuleConditionReference declarativeCondition = obj as RuleConditionReference; if (declarativeCondition == null) { string message = string.Format(CultureInfo.CurrentCulture, Messages.UnexpectedArgumentType, typeof(RuleConditionReference).FullName, "obj"); throw new ArgumentException(message, "obj"); } Activity activity = manager.Context[typeof(Activity)] as Activity; if (activity == null) { string message = string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, typeof(Activity).Name); throw new InvalidOperationException(message); } PropertyValidationContext validationContext = manager.Context[typeof(PropertyValidationContext)] as PropertyValidationContext; if (validationContext == null) { string message = string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, typeof(PropertyValidationContext).Name); throw new InvalidOperationException(message); } if (!string.IsNullOrEmpty(declarativeCondition.ConditionName)) { RuleDefinitions rules = null; RuleConditionCollection conditionDefinitions = null; CompositeActivity declaringActivity = Helpers.GetDeclaringActivity(activity); if (declaringActivity == null) { declaringActivity = Helpers.GetRootActivity(activity) as CompositeActivity; } if (activity.Site != null) { rules = ConditionHelper.Load_Rules_DT(activity.Site, declaringActivity); } else { rules = ConditionHelper.Load_Rules_RT(declaringActivity); } if (rules != null) { conditionDefinitions = rules.Conditions; } if (conditionDefinitions == null || !conditionDefinitions.Contains(declarativeCondition.ConditionName)) { string message = string.Format(CultureInfo.CurrentCulture, Messages.ConditionNotFound, declarativeCondition.ConditionName); ValidationError validationError = new ValidationError(message, ErrorNumbers.Error_ConditionNotFound); validationError.PropertyName = GetFullPropertyName(manager) + "." + "ConditionName"; validationErrors.Add(validationError); } else { RuleCondition actualCondition = conditionDefinitions[declarativeCondition.ConditionName]; ITypeProvider typeProvider = (ITypeProvider)manager.GetService(typeof(ITypeProvider)); IDisposable localContextScope = (WorkflowCompilationContext.Current == null ? WorkflowCompilationContext.CreateScope(manager) : null); try { RuleValidation ruleValidator = new RuleValidation(activity, typeProvider, WorkflowCompilationContext.Current.CheckTypes); actualCondition.Validate(ruleValidator); ValidationErrorCollection actualConditionErrors = ruleValidator.Errors; if (actualConditionErrors.Count > 0) { string expressionPropertyName = GetFullPropertyName(manager); string genericErrorMsg = string.Format(CultureInfo.CurrentCulture, Messages.InvalidConditionExpression, expressionPropertyName); int errorNumber = ErrorNumbers.Error_InvalidConditionExpression; if (activity.Site != null) { ValidationError validationError = new ValidationError(genericErrorMsg, errorNumber); validationError.PropertyName = expressionPropertyName + "." + "Expression"; validationErrors.Add(validationError); } else { foreach (ValidationError actualError in actualConditionErrors) { ValidationError validationError = new ValidationError(genericErrorMsg + " " + actualError.ErrorText, errorNumber); validationError.PropertyName = expressionPropertyName + "." + "Expression"; validationErrors.Add(validationError); } } } // Test duplicates foreach (RuleCondition definition in conditionDefinitions) { if (definition.Name == declarativeCondition.ConditionName && definition != actualCondition) { string message = string.Format(CultureInfo.CurrentCulture, Messages.DuplicateConditions, declarativeCondition.ConditionName); ValidationError validationError = new ValidationError(message, ErrorNumbers.Error_DuplicateConditions); validationError.PropertyName = GetFullPropertyName(manager) + "." + "ConditionName"; validationErrors.Add(validationError); } } } finally { if (localContextScope != null) { localContextScope.Dispose(); } } } } else { string message = string.Format(CultureInfo.CurrentCulture, Messages.InvalidConditionName, "ConditionName"); ValidationError validationError = new ValidationError(message, ErrorNumbers.Error_InvalidConditionName); validationError.PropertyName = GetFullPropertyName(manager) + "." + "ConditionName"; validationErrors.Add(validationError); } return(validationErrors); }