Files
BeWoPlaner/BeWo/View/Detail/TextModuleView.xaml.cs
2021-12-05 19:21:17 +01:00

202 lines
6.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using BeWo.Core.Service;
using BeWo.ServiceProxy;
using BeWo.Validation;
using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;
using BS.Shared;
using BS.Shared.Extensions;
using DevExpress.Xpf.Grid;
namespace BeWo.View.Detail
{
public partial class TextModuleView
{
private TextModuleListVM _ViewModel;
public TextModuleListVM ViewModel
{
get => _ViewModel;
set
{
_ViewModel = value;
root.DataContext = _ViewModel;
}
}
public override bool IsDirty => ViewModel != null && ViewModel.IsDirty;
internal override DependencyObject ValidationOnSaveRootElement => AddTextModuleButton;
public bool IsInAdministrationView { get; }
public bool AreUserControlsEnabled => IsInAdministrationView ? BeWoApp.LoggedOnUser.HasRight(UserRightType.TextbausteineAlleBearbeiten) : BeWoApp.LoggedOnUser.HasRight(UserRightType.TextbausteineNurEigeneBearbeiten);
public TextModuleView(TextModuleListVM pViewModel, bool pIsInAdministrationView = false)
{
InitializeComponent();
IsInAdministrationView = pIsInAdministrationView;
ViewModel = pViewModel;
}
public void Save(Action pCallBack)
{
var lToDos = new List<Action<IOperationsService>>();
if (ViewModel.AddedCount > 0)
{
ViewModel.NewVM.IsOnlyForEmployee = true;
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);
}
protected override void Save()
{
Save(ReloadViewModel);
}
private void ReloadViewModel()
{
Cache.GetInstance().ClearServiceCategoryDescriptions();
VMFactory.CreateTextModuleListVMAsync(r => this.Dispatch(delegate
{
ViewModel = r;
TreeListControl.UpdateLayout();
}), IsInAdministrationView, true);
}
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);
}
}
private void ButtonAddTextModule_Click(object sender, RoutedEventArgs e)
{
if (ValidationTrigger.Validate(this))
{
ViewModel.NewVM.IsOnlyForEmployee = !IsInAdministrationView;
ViewModel.AddNewVMToList();
Save(ReloadViewModel);
}
}
public static bool CheckEditorRights(TextModuleVM textModuleVM, object parameter)
{
if (textModuleVM != null)
{
var isOwn = textModuleVM.Employee != null && textModuleVM.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 SaveAsTextModuleCategory_OnChecked(object sender, RoutedEventArgs e)
{
ViewModel.NewVM.Text = string.Empty;
}
private void SaveButton_OnClick(object sender, RoutedEventArgs e)
{
Save(ReloadViewModel);
}
}
//CBEINKOMMENTIEREN
//public class IsOnlyForEmployeeRightsConverter : IValueConverter
//{
// public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
// {
// var textModule = (TextModuleVM) value;
// return textModule != null && TextModuleView.CheckEditorRights(textModule, parameter) && !textModule.IsParent;
// }
// public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
// {
// throw new NotImplementedException();
// }
//}
public class TextModuleEditConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var fieldname = ((EditGridCellData) values[0]).Column.FieldName;
var shuoldBeEditableDespiteBeingFromAParentElement = fieldname.Equals("Name") || fieldname.Equals("IsDeleteable");
var textModule = (TextModuleVM) values[1];
return TextModuleView.CheckEditorRights(textModule, parameter) && shuoldBeEditableDespiteBeingFromAParentElement;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}