1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Globalization; 5 using System.Text; 6 using System.Workflow.ComponentModel; 7 using System.Workflow.ComponentModel.Design; 8 using System.Drawing.Design; 9 using System.Workflow.Activities.Rules.Design; 10 using System.ComponentModel.Design.Serialization; 11 using System.Workflow.ComponentModel.Compiler; 12 using System.Workflow.ComponentModel.Serialization; 13 using System.Workflow.Activities.Common; 14 15 namespace System.Workflow.Activities.Rules 16 { 17 #region RuleSetReference Class 18 19 [DesignerSerializer(typeof(WorkflowMarkupSerializer), typeof(WorkflowMarkupSerializer))] 20 [DesignerSerializer(typeof(DependencyObjectCodeDomSerializer), typeof(CodeDomSerializer))] 21 [ActivityValidator(typeof(RuleSetReferenceValidator))] 22 [Editor(typeof(RuleSetNameEditor), typeof(UITypeEditor))] 23 [TypeConverter(typeof(RuleSetReferenceTypeConverter))] 24 [Browsable(true)] 25 [Obsolete("The System.Workflow.* types are deprecated. Instead, please use the new types from System.Activities.*")] 26 sealed public class RuleSetReference : DependencyObject 27 { 28 #region members and constructors 29 30 private bool _runtimeInitialized; 31 private string _name; 32 RuleSetReference()33 public RuleSetReference() 34 { 35 } 36 RuleSetReference(string ruleSetName)37 public RuleSetReference(string ruleSetName) 38 { 39 _name = ruleSetName; 40 } 41 #endregion 42 43 #region public members 44 45 public string RuleSetName 46 { 47 get 48 { 49 return this._name; 50 } 51 set 52 { 53 this._name = value; 54 } 55 } 56 57 #endregion 58 59 #region IInitializeForRuntime Members 60 61 [NonSerialized] 62 private object syncLock = new object(); 63 InitializeProperties()64 protected override void InitializeProperties() 65 { 66 lock (syncLock) 67 { 68 if (this._runtimeInitialized) 69 return; 70 71 Activity ownerActivity = base.ParentDependencyObject as Activity; 72 73 CompositeActivity declaringActivity = Helpers.GetDeclaringActivity(ownerActivity); 74 if (declaringActivity == null) 75 { 76 declaringActivity = Helpers.GetRootActivity(ownerActivity) as CompositeActivity; 77 } 78 79 RuleDefinitions rules = ConditionHelper.Load_Rules_RT(declaringActivity); 80 if (rules != null) 81 rules.OnRuntimeInitialized(); 82 base.InitializeProperties(); 83 _runtimeInitialized = true; 84 } 85 } 86 #endregion 87 } 88 89 #endregion 90 91 #region RuleSetReferenceValidator Class 92 93 internal sealed class RuleSetReferenceValidator : Validator 94 { Validate(ValidationManager manager, object obj)95 public override ValidationErrorCollection Validate(ValidationManager manager, object obj) 96 { 97 ValidationErrorCollection validationErrors = base.Validate(manager, obj); 98 99 RuleSetReference ruleSetReference = obj as RuleSetReference; 100 if (ruleSetReference == null) 101 { 102 string message = string.Format(CultureInfo.CurrentCulture, Messages.UnexpectedArgumentType, typeof(RuleSetReference).FullName, "obj"); 103 throw new ArgumentException(message, "obj"); 104 } 105 Activity activity = manager.Context[typeof(Activity)] as Activity; 106 if (activity == null) 107 { 108 string message = string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, typeof(Activity).Name); 109 throw new InvalidOperationException(message); 110 } 111 PropertyValidationContext validationContext = manager.Context[typeof(PropertyValidationContext)] as PropertyValidationContext; 112 if (validationContext == null) 113 { 114 string message = string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, typeof(PropertyValidationContext).Name); 115 throw new InvalidOperationException(message); 116 } 117 if (!string.IsNullOrEmpty(ruleSetReference.RuleSetName)) 118 { 119 RuleDefinitions rules = null; 120 RuleSetCollection ruleSetCollection = null; 121 122 CompositeActivity declaringActivity = Helpers.GetDeclaringActivity(activity); 123 if (declaringActivity == null) 124 { 125 declaringActivity = Helpers.GetRootActivity(activity) as CompositeActivity; 126 } 127 if (activity.Site != null) 128 rules = ConditionHelper.Load_Rules_DT(activity.Site, declaringActivity); 129 else 130 rules = ConditionHelper.Load_Rules_RT(declaringActivity); 131 132 if (rules != null) 133 ruleSetCollection = rules.RuleSets; 134 135 if (ruleSetCollection == null || !ruleSetCollection.Contains(ruleSetReference.RuleSetName)) 136 { 137 string message = string.Format(CultureInfo.CurrentCulture, Messages.RuleSetNotFound, ruleSetReference.RuleSetName); 138 ValidationError validationError = new ValidationError(message, ErrorNumbers.Error_RuleSetNotFound); 139 validationError.PropertyName = GetFullPropertyName(manager) + "." + "RuleSetName"; 140 validationErrors.Add(validationError); 141 } 142 else 143 { 144 RuleSet actualRuleSet = ruleSetCollection[ruleSetReference.RuleSetName]; 145 146 ITypeProvider typeProvider = (ITypeProvider)manager.GetService(typeof(ITypeProvider)); 147 148 IDisposable localContextScope = (WorkflowCompilationContext.Current == null ? WorkflowCompilationContext.CreateScope(manager) : null); 149 try 150 { 151 152 RuleValidation validation = new RuleValidation(activity, typeProvider, WorkflowCompilationContext.Current.CheckTypes); 153 actualRuleSet.Validate(validation); 154 ValidationErrorCollection actualRuleSetErrors = validation.Errors; 155 156 if (actualRuleSetErrors.Count > 0) 157 { 158 string expressionPropertyName = GetFullPropertyName(manager); 159 string genericErrorMsg = string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleSetExpression, expressionPropertyName); 160 int errorNumber = ErrorNumbers.Error_InvalidRuleSetExpression; 161 162 if (activity.Site != null) 163 { 164 foreach (ValidationError actualError in actualRuleSetErrors) 165 { 166 ValidationError validationError = new ValidationError(actualError.ErrorText, errorNumber); 167 validationError.PropertyName = expressionPropertyName + "." + "RuleSet Definition"; 168 validationErrors.Add(validationError); 169 } 170 } 171 else 172 { 173 foreach (ValidationError actualError in actualRuleSetErrors) 174 { 175 ValidationError validationError = new ValidationError(genericErrorMsg + " " + actualError.ErrorText, errorNumber); 176 validationError.PropertyName = expressionPropertyName; 177 validationErrors.Add(validationError); 178 } 179 } 180 } 181 // 182 183 184 185 186 187 188 189 190 191 192 193 } 194 finally 195 { 196 if (localContextScope != null) 197 { 198 localContextScope.Dispose(); 199 } 200 } 201 } 202 } 203 else 204 { 205 string message = string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleSetName, "RuleSetReference"); 206 ValidationError validationError = new ValidationError(message, ErrorNumbers.Error_InvalidRuleSetName); 207 validationError.PropertyName = GetFullPropertyName(manager) + "." + "RuleSetName"; 208 validationErrors.Add(validationError); 209 } 210 return validationErrors; 211 } 212 } 213 214 #endregion 215 216 } 217