// Copyright (c) 2017 schick Informatik
// Description: Bearbeitungsdialog für Validierungsregeln
//
using System.Windows;
using System.Collections.Generic;
using bsmd.database;
using ENI2.Controls;
using System;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
namespace ENI2.EditControls
{
///
/// Interaction logic for EditRulesDialog.xaml
///
public partial class EditRulesDialog : EditWindowBase
{
private ConditionGroup rootCondition;
private ObservableCollection rootGroupList = new ObservableCollection();
private ConditionGroupControl groupControl = new ConditionGroupControl();
private ValidationConditionControl vcControl = new ValidationConditionControl();
public EditRulesDialog()
{
InitializeComponent();
Loaded += EditRulesDialog_Loaded;
}
#region Properties
public bsmd.database.ValidationRule ValidationRule { get; set; }
#endregion
private void EditRulesDialog_Loaded(object sender, RoutedEventArgs e)
{
this.textBlockChanged.Text = this.ValidationRule.Changed?.ToString();
this.textBlockChangedBy.Text = this.ValidationRule.ChangedBy;
this.textBlockCreated.Text = this.ValidationRule.Created.ToString();
this.textBlockCreatedBy.Text = this.ValidationRule.CreatedBy;
this.textBoxName.Text = this.ValidationRule.Name;
this.textBoxContext.Text = this.ValidationRule.Context;
this.checkBoxIsActive.IsChecked = this.ValidationRule.IsActive;
// TODO: init logic tree with rule
this.rootCondition = ValidationCondition.LoadFromString(this.ValidationRule.Rule);
if(this.rootCondition == null)
{
this.rootCondition = new ConditionGroup();
this.rootCondition.GroupOperator = ConditionGroup.GroupOperatorEnum.OR;
/* testing
this.rootCondition.SubGroups.Add(new ConditionGroup() { GroupOperator = ConditionGroup.GroupOperatorEnum.NOT });
this.rootCondition.SubGroups.Add(new ConditionGroup() { GroupOperator = ConditionGroup.GroupOperatorEnum.AND });
this.rootCondition.SubGroups.Add(new ConditionGroup() { GroupOperator = ConditionGroup.GroupOperatorEnum.XOR });
this.rootCondition.Conditions.Add(new ValidationCondition()
{
FieldName = "Bla"
});
*/
}
this.rootGroupList.Add(this.rootCondition);
this.treeViewRules.ItemsSource = this.rootGroupList;
this.OKClicked += EditRulesDialog_OKClicked;
this.AddVisible = false;
// init tree context Menu
this.treeViewRules.ContextMenu = new ContextMenu();
MenuItem addItem = new MenuItem();
addItem.Header = Properties.Resources.textNewGroup;
addItem.Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/Resources/add.png")) };
addItem.Click += this.createNewGroup;
this.treeViewRules.ContextMenu.Items.Add(addItem);
MenuItem addConditionItem = new MenuItem();
addConditionItem.Header = Properties.Resources.textNewCondition;
addConditionItem.Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/Resources/check.png")) };
addConditionItem.Click += this.createNewCondition;
this.treeViewRules.ContextMenu.Items.Add(addConditionItem);
MenuItem deleteItem = new MenuItem();
deleteItem.Header = Properties.Resources.textDelete;
deleteItem.Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/Resources/delete.png")) };
deleteItem.Click += this.deleteItem;
this.treeViewRules.ContextMenu.Items.Add(deleteItem);
}
private void deleteItem(object sender, RoutedEventArgs e)
{
object selectedNode = this.treeViewRules.SelectedItem;
if (selectedNode == null) return;
if(selectedNode is ConditionGroup)
{
this.rootCondition.Remove(selectedNode as ConditionGroup);
}
else if (selectedNode is ValidationCondition)
{
this.rootCondition.RemoveCondition(selectedNode as ValidationCondition);
}
}
private void createNewGroup(object sender, RoutedEventArgs e)
{
if (treeViewRules.SelectedItem == null) return;
if (!(treeViewRules.SelectedItem is ConditionGroup)) return;
ConditionGroup newGroup = new ConditionGroup();
((ConditionGroup)treeViewRules.SelectedItem).SubGroups.Add(newGroup);
}
private void createNewCondition(object sender, RoutedEventArgs e)
{
if (treeViewRules.SelectedItem == null) return;
if (!(treeViewRules.SelectedItem is ConditionGroup)) return;
ValidationCondition newCondition = new ValidationCondition();
((ConditionGroup)treeViewRules.SelectedItem).Conditions.Add(newCondition);
}
public void CopyValuesToEntity()
{
this.ValidationRule.Name = this.textBoxName.Text;
this.ValidationRule.Context = this.textBoxContext.Text;
this.ValidationRule.IsActive = this.checkBoxIsActive.IsChecked;
// TODO: serialize logical tree back into string
this.ValidationRule.Rule = bsmd.database.ValidationCondition.SaveToString(this.rootCondition);
}
private void EditRulesDialog_OKClicked()
{
this.CopyValuesToEntity();
}
private void treeViewRules_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs