2017-11-17 22:24:00 +01:00
using System ;
using System.Collections.Generic ;
2018-01-05 19:13:18 -04:00
using System.ComponentModel ;
2017-11-17 22:24:00 +01:00
using System.Globalization ;
using System.Linq ;
2018-01-05 19:13:18 -04:00
using System.Runtime.CompilerServices ;
2017-11-17 22:24:00 +01:00
using System.Windows ;
2018-01-02 18:47:59 -04:00
using System.Windows.Controls ;
2017-11-17 22:24:00 +01:00
using System.Windows.Data ;
2018-01-05 19:13:18 -04:00
using System.Windows.Input ;
2017-11-17 22:24:00 +01:00
2018-01-05 19:13:18 -04:00
using BeWo.Annotations ;
2017-11-17 22:24:00 +01:00
using BeWo.ServiceProxy ;
using BeWo.ViewModel ;
using BeWo.ViewModel.ListViewModel ;
using BS.Shared ;
using BS.Shared.Extensions ;
2018-01-05 19:13:18 -04:00
using DevExpress.Xpf.Editors ;
2017-11-17 22:24:00 +01:00
using DevExpress.Xpf.Grid ;
namespace BeWo.View.Detail
{
2018-01-05 19:13:18 -04:00
public partial class TextbausteinView : INotifyPropertyChanged
2017-11-17 22:24:00 +01:00
{
private TextModuleListVM _ViewModel ;
public TextModuleListVM ViewModel
{
2018-01-02 18:47:59 -04:00
get = > _ViewModel ;
2017-11-17 22:24:00 +01:00
set
{
_ViewModel = value ;
root . DataContext = value ;
}
}
2018-01-02 18:47:59 -04:00
public override bool IsDirty = > ViewModel ! = null & & ViewModel . IsDirty ;
2017-11-17 22:24:00 +01:00
2018-01-05 19:13:18 -04:00
private readonly bool _IsInAdministrationView ;
2017-11-17 22:24:00 +01:00
public TextbausteinView ( TextModuleListVM pViewModel , bool pIsInAdministrationView = false )
{
InitializeComponent ( ) ;
2018-01-05 19:13:18 -04:00
_IsInAdministrationView = pIsInAdministrationView ;
2017-11-17 22:24:00 +01:00
ViewModel = pViewModel ;
}
protected override void Save ( )
{
2018-01-05 19:13:18 -04:00
Save ( null ) ;
2017-11-17 22:24:00 +01:00
}
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 ( ) ) ) ;
}
2018-01-05 19:13:18 -04:00
ServiceFacade . DoMultipleOperationsServicesAsync ( lToDos , ( ) = >
{
pCallBack ? . Invoke ( ) ;
ReloadViewModel ( ) ;
} ) ;
2017-11-17 22:24:00 +01:00
}
private void ReloadViewModel ( )
{
VMFactory . CreateTextModuleListVMAsync ( r = > this . Dispatch ( delegate
{
2018-01-05 19:13:18 -04:00
TreeListControl . BeginDataUpdate ( ) ;
2017-11-17 22:24:00 +01:00
ViewModel = r ;
2018-01-05 19:13:18 -04:00
TreeListControl . EndDataUpdate ( ) ;
TreeListControl . SelectedItems . Clear ( ) ;
} ) , _IsInAdministrationView ) ;
2017-11-17 22:24:00 +01:00
}
private void DeleteButton_Click ( object sender , RoutedEventArgs e )
{
2018-01-05 19:13:18 -04:00
var focusedNode = TreeListView . FocusedNode ;
var selectedTextModule = ( TextModuleVM ) focusedNode . Content ;
2017-11-17 22:24:00 +01:00
2018-01-05 19:13:18 -04:00
_ChildrenToDelete . Add ( selectedTextModule ) ;
if ( selectedTextModule . IsParent )
2017-11-17 22:24:00 +01:00
{
2018-01-05 19:13:18 -04:00
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 ) ;
2017-11-17 22:24:00 +01:00
}
2018-01-05 19:13:18 -04:00
ViewModel . VMList . RemoveRange ( _ChildrenToDelete ) ;
_ChildrenToDelete . Clear ( ) ;
2017-11-17 22:24:00 +01:00
}
2018-01-05 19:13:18 -04:00
private readonly List < TextModuleVM > _ChildrenToDelete = new List < TextModuleVM > ( ) ;
private void RetrieveChildTextModulesFromParent ( TextModuleVM parent )
2017-11-17 22:24:00 +01:00
{
2018-01-05 19:13:18 -04:00
var children = ViewModel . VMList . Where ( w = > w . Parent ! = null & & w . Parent . Equals ( parent ) ) ;
2017-11-17 22:24:00 +01:00
2018-01-05 19:13:18 -04:00
foreach ( var child in children )
{
_ChildrenToDelete . Add ( child ) ;
RetrieveChildTextModulesFromParent ( child ) ;
2017-11-17 22:24:00 +01:00
}
}
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 ;
}
2018-01-02 18:47:59 -04:00
2017-11-17 22:24:00 +01:00
return false ;
}
2018-01-02 18:47:59 -04:00
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" ) ;
2018-01-05 19:13:18 -04:00
ViewModel . NewVM . IsExpanded = twi . Header . Equals ( "Neue Textbausteinkategorie" ) ;
2018-01-02 18:47:59 -04:00
ViewModel . NewVM . IsOnlyForEmployee = true ;
2018-01-05 19:13:18 -04:00
ViewModel . NewVM . Name = twi . Header . ToString ( ) ;
2018-01-02 18:47:59 -04:00
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 ) )
{
2018-01-05 19:13:18 -04:00
if ( textModule ! = null & & ! textModule . IsParent )
2018-01-02 18:47:59 -04:00
{
menuitem . IsEnabled = false ;
}
else
{
menuitem . IsEnabled = true ;
}
}
}
}
2018-01-05 19:13:18 -04:00
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 ) ) ;
}
2017-11-17 22:24:00 +01:00
}
public class IsOnlyForEmployeeRightsConverter : IValueConverter
{
public object Convert ( object value , Type targetType , object parameter , CultureInfo culture )
{
var textbausteinVM = ( TextModuleVM ) value ;
2018-01-05 19:13:18 -04:00
return TextbausteinView . CheckEditorRights ( textbausteinVM , parameter ) & & textbausteinVM ! = null & & ! textbausteinVM . IsParent ;
2017-11-17 22:24:00 +01:00
}
public object ConvertBack ( object value , Type targetType , object parameter , CultureInfo culture )
{
throw new NotImplementedException ( ) ;
}
}
}