private void DeleteSelectedRulesCommandHandler(object signalRulesObject) { if (signalRulesObject != null) { if (signalRulesObject is string value) { if (value.Equals("ALL")) { this.SignalRules.ToList().ForEach(signalRule => signalRule.Element.Remove()); this.SignalRules.Clear(); this.IsDirty = true; this.PrimaryCommand.RaiseCanExecuteChanged(); } } else { IList items = (IList)signalRulesObject; List <SignalRule> signalRules = items.Cast <SignalRule>().ToList(); for (int index = 0; index < signalRules.Count(); index++) { SignalRule signalRule = signalRules[index]; this.DeleteRuleCommandHandler(signalRule); } } } }
private void NewRuleCommandHandler() { XElement element = null; SignalRule signalRule = null; element = XElementExtensions.NewSignalRule(); signalRule = element.ToTypeSignalRule(); if (element != null && signalRule != null) { NewRuleViewModel newRuleViewModel = new NewRuleViewModel { SignalRule = signalRule }; // here we have to open a new rule dialog. _dialogContainerService.ShowDialog(newRuleViewModel); if (newRuleViewModel.Result == DialogResult.Success) { if (this.SignalRules != null && _document != null) { _document.Root.Add(element, Environment.NewLine); this.SignalRules.Add(signalRule); this.IsDirty = true; this.PrimaryCommand.RaiseCanExecuteChanged(); } } } }
private ElementProperty GetTextProperty(SignalRule signalRule, PropertyInfo propertyInfo) { return(new TextProperty { PerformValidationOnLoad = _performValidationOnLoad, SignalRule = signalRule, PropertyName = propertyInfo.Name, PropertyValue = propertyInfo.GetValue(signalRule) }); }
private ElementProperty GetArrayTextProperty(SignalRule signalRule, PropertyInfo propertyInfo) { object value = propertyInfo.GetValue(signalRule); return(new TextProperty { PerformValidationOnLoad = _performValidationOnLoad, SignalRule = signalRule, PropertyName = propertyInfo.Name, PropertyValue = value != null?string.Join(";", value as string[]) : null }); }
private void DeleteRuleCommandHandler(SignalRule signalRule) { if (signalRule != null) { if (this.SignalRules.Contains(signalRule)) { signalRule.Element.Remove(); this.SignalRules.Remove(signalRule); this.IsDirty = true; this.PrimaryCommand.RaiseCanExecuteChanged(); } } }
private ElementProperty GetComboBoxProperty(SignalRule signalRule, PropertyInfo propertyInfo) { ElementProperty elementProperty = new ComboBoxProperty { PerformValidationOnLoad = _performValidationOnLoad, SignalRule = signalRule, PropertyName = propertyInfo.Name, PropertyValue = propertyInfo.GetValue(signalRule) }; foreach (var item in propertyInfo.PropertyType.GetEnumValues()) { (elementProperty as ComboBoxProperty).Lookups.Add(item.ToString()); } return(elementProperty); }
private void EditRuleCommandHandler(SignalRule signalRule) { if (signalRule != null) { SignalRule preservedSignalRule = signalRule.DeepCopy(); NewRuleViewModel editRuleViewModel = new NewRuleViewModel(performValidationOnLoad: false) { SignalRule = signalRule }; editRuleViewModel.Title = "Edit Rule"; editRuleViewModel.PrimaryButtonText = "Save"; // here we have to open a new rule dialog. _dialogContainerService.ShowDialog(editRuleViewModel); if (editRuleViewModel.Result == DialogResult.Success) { this.IsDirty = true; this.PrimaryCommand.RaiseCanExecuteChanged(); } else { signalRule.AllowedValues = preservedSignalRule.AllowedValues; signalRule.AllowFutureDate = preservedSignalRule.AllowFutureDate; signalRule.AllowNull = preservedSignalRule.AllowNull; signalRule.DateFormat = preservedSignalRule.DateFormat; signalRule.Element = preservedSignalRule.Element; signalRule.IsActive = preservedSignalRule.IsActive; signalRule.MaxDate = preservedSignalRule.MaxDate; signalRule.MaxLength = preservedSignalRule.MaxLength; signalRule.MaxValue = preservedSignalRule.MaxValue; signalRule.MinDate = preservedSignalRule.MinDate; signalRule.MinLength = preservedSignalRule.MinLength; signalRule.MinValue = preservedSignalRule.MinValue; signalRule.NotAllowedValues = preservedSignalRule.NotAllowedValues; signalRule.SignalID = preservedSignalRule.SignalID; signalRule.ValueType = preservedSignalRule.ValueType; } } }
private void Clear(ValueDataType valueType, SignalRule signalRule) { PropertyInfo[] propertiesInfo = null; if (valueType.HasFlag(ValueDataType.Integer)) { propertiesInfo = signalRule.GetType().GetIntegerWhereNotExemptProperties(); if (propertiesInfo != null) { foreach (PropertyInfo propertyInfo in propertiesInfo) { propertyInfo.SetValue(signalRule, null); } } } if (valueType.HasFlag(ValueDataType.String)) { propertiesInfo = signalRule.GetType().GetStringWhereNotExemptProperties(); if (propertiesInfo != null) { foreach (PropertyInfo propertyInfo in propertiesInfo) { propertyInfo.SetValue(signalRule, null); } } } if (valueType.HasFlag(ValueDataType.Datetime)) { propertiesInfo = signalRule.GetType().GetDateTimeWhereNotExemptProperties(); if (propertiesInfo != null) { foreach (PropertyInfo propertyInfo in propertiesInfo) { propertyInfo.SetValue(signalRule, null); } } } }
public static SignalRule ToTypeSignalRule(this XElement element) { SignalRule signalRule = new SignalRule(); XAttribute attribute = element.Attribute("SignalID"); if (attribute != null) { signalRule.SignalID = attribute.Value; } attribute = element.Attribute("IsMandatory"); if (attribute != null) { if (Enum.TryParse(attribute.Value, out TrueFalse isMandatory)) { signalRule.AllowNull = isMandatory; } } attribute = element.Attribute("IsActive"); if (attribute != null) { if (Enum.TryParse(attribute.Value, out TrueFalse isActive)) { signalRule.IsActive = isActive; } } attribute = element.Attribute("ValueType"); if (attribute != null) { if (Enum.TryParse(attribute.Value, out ValueDataType valueType)) { signalRule.ValueType = valueType; } } signalRule.Element = element; return(signalRule); }
public List <SignalModel> ValidateSignalAgainstRule(List <SignalModel> signals, List <SignalRule> signalRules) { List <SignalModel> badData = new List <SignalModel>(); foreach (var signal in signals) { SignalRule signalRule = signalRules.FirstOrDefault(q => q.SignalName == signal.Signal); if (signalRule != null && signalRule.AvailableRules != null) { List <Rule> matchingRules = signalRule.AvailableRules.Where(q => q.ValueType == signal.Value_type).ToList(); if (matchingRules != null) { IValidator validator = ValidatorFactory.GetValidator(signal.Value_type); bool isValid = validator.Validate(signal, matchingRules); if (!isValid) { badData.Add(signal); } } } } return(badData); }
public void ValidateCrossFieldDependency(SignalRule signalRule) { if (signalRule != null) { PropertyInfo[] propertiesInfo = null; switch (signalRule.ValueType) { case ValueDataType.Integer: propertiesInfo = signalRule.GetType().GetIntegerWhereNotExemptProperties(); break; case ValueDataType.String: propertiesInfo = signalRule.GetType().GetStringWhereNotExemptProperties(); break; case ValueDataType.Datetime: propertiesInfo = signalRule.GetType().GetDateTimeWhereNotExemptProperties(); break; } if (propertiesInfo != null) { bool isAnySet = false; foreach (PropertyInfo propertyInfo in propertiesInfo) { if (propertyInfo.GetValue(signalRule) != null) { if (propertyInfo.PropertyType.IsArray) { if (propertyInfo.PropertyType.Name == "String[]") { string[] forLength = propertyInfo.GetValue(signalRule) as string[]; if (forLength.Length != 0) { isAnySet = true; break; } } } else { isAnySet = true; break; } } } foreach (PropertyInfo property in propertiesInfo) { ElementProperty elementProperty = this.FirstOrDefault(x => x.PropertyName == property.Name); if (elementProperty != null) { elementProperty.HasDataError = false; if (signalRule.ValueType == ValueDataType.Integer) { if (signalRule.MaxValue.HasValue && signalRule.MinValue.HasValue) { if (signalRule.MaxValue.Value < signalRule.MinValue.Value) { elementProperty.HasDataError = true; } } } else if (signalRule.ValueType == ValueDataType.String) { if (signalRule.MaxLength.HasValue && signalRule.MinLength.HasValue) { if (signalRule.MaxLength.Value < signalRule.MinLength.Value) { elementProperty.HasDataError = true; } } } else if (signalRule.ValueType == ValueDataType.Datetime) { if (!string.IsNullOrEmpty(signalRule.MaxDate) && !string.IsNullOrEmpty(signalRule.MinDate)) { if (DateTime.TryParse(signalRule.MinDate, out DateTime minDate)) { if (DateTime.TryParse(signalRule.MaxDate, out DateTime maxDate)) { if (maxDate < minDate) { elementProperty.HasDataError = true; } } } } } elementProperty.IsMandatory = !isAnySet; elementProperty.Validate(nameof(elementProperty.PropertyValue)); } } } } }
private void FillSignalProperties(SignalRule signalRule) { this.ElementProperties.ToList().ForEach(property => property.PropertyChanged -= this.ElementPropertyChanged); this.ElementProperties.Clear(); if (signalRule != null) { ElementProperties properties = new ElementProperties(); PropertyInfo[] propertiesInfo = null; switch (signalRule.ValueType) { case ValueDataType.Integer: propertiesInfo = signalRule.GetType().GetIntegerProperties(); this.Clear(ValueDataType.Datetime | ValueDataType.String, signalRule); break; case ValueDataType.String: propertiesInfo = signalRule.GetType().GetStringProperties(); this.Clear(ValueDataType.Integer | ValueDataType.Datetime, signalRule); break; case ValueDataType.Datetime: propertiesInfo = signalRule.GetType().GetDateTimeProperties(); this.Clear(ValueDataType.Integer | ValueDataType.String, signalRule); break; default: propertiesInfo = signalRule.GetType().GetExemptProperties(); this.Clear(ValueDataType.Integer | ValueDataType.String | ValueDataType.Datetime, signalRule); break; } foreach (PropertyInfo propertyInfo in propertiesInfo) { ElementProperty elementProperty = null; if (propertyInfo.PropertyType.IsEnum) { elementProperty = this.GetComboBoxProperty(signalRule, propertyInfo); } else if (propertyInfo.PropertyType == typeof(double?)) { elementProperty = this.GetDoubleProperty(signalRule, propertyInfo); } else if (propertyInfo.PropertyType == typeof(int?)) { elementProperty = this.GetIntegerProperty(signalRule, propertyInfo); } else if (propertyInfo.PropertyType.IsArray) { switch (propertyInfo.PropertyType.Name) { case "String[]": elementProperty = this.GetArrayTextProperty(signalRule, propertyInfo); break; default: break; } } else { elementProperty = this.GetTextProperty(signalRule, propertyInfo); } properties.Add(elementProperty); } this.ElementProperties = properties; this.ElementProperties.ToList().ForEach(property => { if (!property.IsReadOnly) { property.PropertyChanged += this.ElementPropertyChanged; } }); } this.PrimaryCommand.RaiseCanExecuteChanged(); }
public static List <SignalRule> ToListOfTypeSignalRule(this List <XElement> elements) { List <SignalRule> signalRules = new List <SignalRule>(); elements.ForEach((Action <XElement>)(element => { SignalRule signalRule = new SignalRule(); XAttribute attribute = element.Attribute("SignalID"); if (attribute != null) { signalRule.SignalID = attribute.Value; } attribute = element.Attribute("ValueType"); if (attribute != null) { if (Enum.TryParse(attribute.Value, out ValueDataType valueType)) { signalRule.ValueType = valueType; } } attribute = element.Attribute("IsActive"); if (attribute != null) { if (Enum.TryParse(attribute.Value, out TrueFalse isActive)) { signalRule.IsActive = isActive; } } attribute = element.Attribute("AllowNull"); if (attribute != null) { if (Enum.TryParse(attribute.Value, out TrueFalse allowNull)) { signalRule.AllowNull = allowNull; } } attribute = element.Attribute("MinValue"); if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Value)) { if (double.TryParse(attribute.Value, out double value)) { signalRule.MinValue = value; } } attribute = element.Attribute("MaxValue"); if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Value)) { if (double.TryParse(attribute.Value, out double value)) { signalRule.MaxValue = value; } } attribute = element.Attribute("AllowedValues"); if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Value)) { signalRule.AllowedValues = attribute.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); } attribute = element.Attribute("NotAllowedValues"); if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Value)) { signalRule.NotAllowedValues = attribute.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); } attribute = element.Attribute("MinLength"); if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Value)) { if (int.TryParse(attribute.Value, out int value)) { signalRule.MinLength = value; } } attribute = element.Attribute("MaxLength"); if (attribute != null && !string.IsNullOrWhiteSpace(attribute.Value)) { if (int.TryParse(attribute.Value, out int value)) { signalRule.MaxLength = value; } } attribute = element.Attribute("AllowFutureDate"); if (attribute != null) { if (Enum.TryParse(attribute.Value, out TrueFalse allow)) { signalRule.AllowFutureDate = allow; } } attribute = element.Attribute("DateFormat"); if (attribute != null) { signalRule.DateFormat = attribute.Value; } attribute = element.Attribute("MinDate"); if (attribute != null) { signalRule.MinDate = attribute.Value; } attribute = element.Attribute("MaxDate"); if (attribute != null) { signalRule.MaxDate = attribute.Value; } signalRule.Element = element; signalRules.Add(signalRule); })); return(signalRules); }
public bool Validate(JsonSignal jsonSignal) { if (jsonSignal != null) { SignalRule signalRule = this.SignalRules.FirstOrDefault(rule => rule.IsActive == TrueFalse.True && rule.SignalID.Equals(jsonSignal.SignalID, StringComparison.OrdinalIgnoreCase) && rule.ValueType.ToString().Equals(jsonSignal.ValueType, StringComparison.OrdinalIgnoreCase)); if (signalRule != null) { string jsonValue = null; if (jsonSignal.Value != null) { jsonValue = jsonSignal.Value.ToString(); } if (signalRule.AllowNull == TrueFalse.False) { if (string.IsNullOrEmpty(jsonValue)) { return(false); } } switch (signalRule.ValueType) { case ValueDataType.Integer: { double doubleValue; if (!double.TryParse(jsonValue, out doubleValue)) { return(false); } if (signalRule.MinValue.HasValue) { if (doubleValue < signalRule.MinValue.Value) { return(false); } } if (signalRule.MaxValue.HasValue) { if (doubleValue > signalRule.MaxValue.Value) { return(false); } } } break; case ValueDataType.String: { // preference given to allowed values.. if there is no entry in allowed values then will look for not allowed values if (signalRule.AllowedValues != null && signalRule.AllowedValues.Length > 0) { if (!signalRule.AllowedValues.Any(x => x.Equals(jsonValue))) { return(false); } } if ((signalRule.AllowedValues == null || (signalRule.AllowedValues != null && signalRule.AllowedValues.Length == 0)) && signalRule.NotAllowedValues != null && signalRule.NotAllowedValues.Length > 0) { if (signalRule.NotAllowedValues.Any(x => x.Equals(jsonValue))) { return(false); } } if (signalRule.MinLength.HasValue) { if (jsonValue.Length < signalRule.MinLength.Value) { return(false); } } if (signalRule.MaxLength.HasValue) { if (jsonValue.Length > signalRule.MaxLength.Value) { return(false); } } } break; case ValueDataType.Datetime: { DateTime dateTime; if (!DateTime.TryParse(jsonValue, out dateTime)) { return(false); } if (signalRule.AllowFutureDate == TrueFalse.False) { if (dateTime.Date > DateTime.Today) { return(false); } } if (!string.IsNullOrWhiteSpace(signalRule.MinDate)) { DateTime minDate; if (DateTime.TryParse(signalRule.MinDate, out minDate)) { if (dateTime.Date < minDate.Date) { return(false); } } } if (!string.IsNullOrWhiteSpace(signalRule.MaxDate)) { DateTime maxDate; if (DateTime.TryParse(signalRule.MaxDate, out maxDate)) { if (dateTime.Date > maxDate.Date) { return(false); } } } } break; } } } return(true); }
private bool CanExecuteEditRuleCommand(SignalRule signalRule) { return(this.SelectedSignalRule != null); }