2017-09-26 13:30:20 +02:00
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
{
2018-01-05 19:13:18 -04:00
get = > _ViewModel ;
2017-09-26 13:30:20 +02:00
set
{
_ViewModel = value ;
root . DataContext = _ViewModel ;
}
}
public override bool IsDirty = > ViewModel ! = null & & ViewModel . IsDirty ;
internal override DependencyObject ValidationOnSaveRootElement = > AddTextModuleButton ;
public bool IsInAdministrationView { get ; }
2017-10-13 11:10:37 +02:00
public bool AreUserControlsEnabled = > IsInAdministrationView ? BeWoApp . LoggedOnUser . HasRight ( UserRightType . TextbausteineAlleBearbeiten ) : BeWoApp . LoggedOnUser . HasRight ( UserRightType . TextbausteineNurEigeneBearbeiten ) ;
2017-09-26 13:30:20 +02:00
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 )
{
2021-12-05 19:21:17 +01:00
var isOwn = textModuleVM . Employee ! = null & & textModuleVM . Employee . EmployeeOid . Equals ( BeWoApp . LoggedOnEmployee . EmployeeOid ) ;
2017-09-26 13:30:20 +02:00
2017-10-13 11:10:37 +02:00
var hasRight2EditAll = BeWoApp . LoggedOnUser . HasRight ( UserRightType . TextbausteineAlleBearbeiten ) ;
var hasRight2EditOwn = BeWoApp . LoggedOnUser . HasRight ( UserRightType . TextbausteineNurEigeneBearbeiten ) ;
var hasRight2Create4All = BeWoApp . LoggedOnUser . HasRight ( UserRightType . TextbausteineAlleBearbeiten ) ;
2017-09-26 13:30:20 +02:00
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 ) ;
}
}
2017-11-17 22:24:00 +01:00
//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();
// }
//}
2017-09-26 13:30:20 +02:00
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 ( ) ;
}
}
}