Files
BeWoPlaner/BeWo/View/Detail/TextbausteinView.xaml.cs
2018-01-05 19:13:18 -04:00

272 lines
8.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using BeWo.Annotations;
using BeWo.ServiceProxy;
using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;
using BS.Shared;
using BS.Shared.Extensions;
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.Grid;
namespace BeWo.View.Detail
{
public partial class TextbausteinView : INotifyPropertyChanged
{
private TextModuleListVM _ViewModel;
public TextModuleListVM ViewModel
{
get => _ViewModel;
set
{
_ViewModel = value;
root.DataContext = value;
}
}
public override bool IsDirty => ViewModel != null && ViewModel.IsDirty;
private readonly bool _IsInAdministrationView;
public TextbausteinView(TextModuleListVM pViewModel, bool pIsInAdministrationView = false)
{
InitializeComponent();
_IsInAdministrationView = pIsInAdministrationView;
ViewModel = pViewModel;
}
protected override void Save()
{
Save(null);
}
public void Save(Action pCallBack)
{
var lToDos = new List<Action<IOperationsService>>();
if (ViewModel.AddedCount > 0)
{
lToDos.Add(s => s.InsertNewTextModules(ViewModel.CommitAdded()));
}
if (ViewModel.RemovedCount > 0)
{
lToDos.Add(s => s.DeleteTextModules(ViewModel.GetRemoved().ToDictionary(dc => dc.TextModuleOid.Value, dc => dc.TextModuleVersion.Value)));
}
if (ViewModel.ChangedCount > 0)
{
lToDos.Add(s => s.UpdateTextModules(ViewModel.CommitChanged()));
}
ServiceFacade.DoMultipleOperationsServicesAsync(lToDos, () =>
{
pCallBack?.Invoke();
ReloadViewModel();
});
}
private void ReloadViewModel()
{
VMFactory.CreateTextModuleListVMAsync(r => this.Dispatch(delegate
{
TreeListControl.BeginDataUpdate();
ViewModel = r;
TreeListControl.EndDataUpdate();
TreeListControl.SelectedItems.Clear();
}), _IsInAdministrationView);
}
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
var focusedNode = TreeListView.FocusedNode;
var selectedTextModule = (TextModuleVM) focusedNode.Content;
_ChildrenToDelete.Add(selectedTextModule);
if(selectedTextModule.IsParent)
{
if(ViewModel.VMList.Any(textModule => selectedTextModule.Equals(textModule.Parent)))
{
if(MessageBox.Show("Die Textbausteinkategorie enthält weitere Textbausteine und/oder Textbausteinkategorien. Wenn Sie fortfahren, werden alle Unterelemente von \"" + selectedTextModule.Name + "\" gelöscht.", "Textbausteine löschen", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.Cancel)
{
_ChildrenToDelete.Clear();
return;
}
}
RetrieveChildTextModulesFromParent(selectedTextModule);
}
ViewModel.VMList.RemoveRange(_ChildrenToDelete);
_ChildrenToDelete.Clear();
}
private readonly List<TextModuleVM> _ChildrenToDelete = new List<TextModuleVM>();
private void RetrieveChildTextModulesFromParent(TextModuleVM parent)
{
var children = ViewModel.VMList.Where(w => w.Parent != null && w.Parent.Equals(parent));
foreach(var child in children)
{
_ChildrenToDelete.Add(child);
RetrieveChildTextModulesFromParent(child);
}
}
public static bool CheckEditorRights(TextModuleVM textbausteinVM, object parameter)
{
if (textbausteinVM != null)
{
var isOwn = textbausteinVM.Employee.EmployeeOid.Equals(BeWoApp.LoggedOnEmployee.EmployeeOid);
var hasRight2EditAll = BeWoApp.LoggedOnUser.HasRight(UserRightType.TextbausteineAlleBearbeiten);
var hasRight2EditOwn = BeWoApp.LoggedOnUser.HasRight(UserRightType.TextbausteineNurEigeneBearbeiten);
var hasRight2Create4All = BeWoApp.LoggedOnUser.HasRight(UserRightType.TextbausteineAlleBearbeiten);
if (parameter != null && parameter.ToString().Equals("IsOnlyForEmployeeCheckBox"))
{
return isOwn && hasRight2EditOwn && hasRight2Create4All || hasRight2EditAll;
}
return isOwn && hasRight2EditOwn || hasRight2EditAll;
}
return false;
}
private void ContextMenuTextModules_Click(object sender, RoutedEventArgs e)
{
if(e.OriginalSource is MenuItem twi)
{
var selectedItem = TreeListControl.SelectedItem as TextModuleVM;
ViewModel.NewVM.IsParent = twi.Header.Equals("Neue Textbausteinkategorie");
ViewModel.NewVM.IsExpanded = twi.Header.Equals("Neue Textbausteinkategorie");
ViewModel.NewVM.IsOnlyForEmployee = true;
ViewModel.NewVM.Name = twi.Header.ToString();
if(selectedItem != null)
{
ViewModel.NewVM.Parent = selectedItem;
}
ViewModel.AddNewVMToList(false);
Save();
}
}
private void ContextMenuTextModules_Opened(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is ContextMenu menu)
{
var textModule = TreeListControl.SelectedItem as TextModuleVM;
foreach (var menuitem in menu.Items.OfType<MenuItem>().Select(item => item))
{
if(textModule != null && !textModule.IsParent)
{
menuitem.IsEnabled = false;
}
else
{
menuitem.IsEnabled = true;
}
}
}
}
private void TreeListControl_OnItemsSourceChanged(object sender, ItemsSourceChangedEventArgs e)
{
TreeListControl.SelectedItem = null;
}
private void TreeListControl_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var hitInfo = TreeListView.CalcHitInfo(e.OriginalSource as DependencyObject);
if(!hitInfo.InRowCell)
{
TreeListControl.SelectedItem = null;
}
e.Handled = false;
}
private void ButtonInfo_OnClick(object sender, RoutedEventArgs e)
{
if(sender is Button btn && btn.TemplatedParent is ButtonContainer bc)
{
if(bc.Content is ButtonInfo bi)
{
if(bi.Tag is EditGridCellData cellData)
{
var vm = (TextModuleVM) cellData.RowData.Row;
vm.Parent = null;
vm.IsParent = false;
}
}
}
}
private void RemoveServiceCategory_OnClick(object sender, RoutedEventArgs e)
{
if(sender is Button btn && btn.TemplatedParent is ButtonContainer bc)
{
if(bc.Content is ButtonInfo bi)
{
if(bi.Tag is EditGridCellData cellData)
{
var vm = (TextModuleVM) cellData.RowData.Row;
vm.ServiceCategory = null;
}
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class IsOnlyForEmployeeRightsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var textbausteinVM = (TextModuleVM)value;
return TextbausteinView.CheckEditorRights(textbausteinVM, parameter) && textbausteinVM != null && !textbausteinVM.IsParent;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}