Files
BeWoPlaner/BeWo/View/Detail/AI/AiPromptbausteinView.xaml.cs
2025-11-28 13:15:38 +01:00

332 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using BeWo.ServiceProxy;
using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;
using BS.Shared;
using BS.Shared.Extensions;
using BS.Shared.Translation;
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.Grid;
namespace BeWo.View.Detail.AI
{
public partial class AiPromptbausteinView
{
private AiPromptbausteinListVM _ViewModel;
public AiPromptbausteinListVM ViewModel
{
get => _ViewModel;
set
{
_ViewModel = value;
root.DataContext = value;
}
}
public override bool IsDirty => ViewModel != null && ViewModel.IsDirty;
private readonly bool _IsInAdministrationView;
public AiPromptbausteinView(AiPromptbausteinListVM viewmodel, bool pIsInAdministrationView = false)
{
InitializeComponent();
_IsInAdministrationView = pIsInAdministrationView;
ViewModel = viewmodel;
}
protected override void Save()
{
Save(null);
}
public void Save(Action pCallBack)
{
var lToDos = new List<Action<IOperationsService>>();
if(ViewModel.ChangedCount == 0 && ViewModel.RemovedCount == 0 && ViewModel.AddedCount == 0)
{
return;
}
if (ViewModel.AddedCount > 0)
{
//lToDos.Add(s => s.InsertNewAiPromptbausteine(ViewModel.CommitAdded()));
}
if (ViewModel.RemovedCount > 0)
{
//lToDos.Add(s => s.DeleteAiPromptbausteine(ViewModel.GetRemoved().ToDictionary(dc => dc.Oid.Value, dc => dc.Version.Value)));
}
if (ViewModel.ChangedCount > 0)
{
//lToDos.Add(s => s.UpdateAiPromptbausteine(ViewModel.CommitChanged()));
}
ServiceFacade.DoMultipleOperationsServicesAsync(lToDos, () =>
{
pCallBack?.Invoke();
ReloadViewModel();
});
}
private void ReloadViewModel()
{
//VMFactory.CreateAiPromptbausteinListVMAsync(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 selectedAiPromptbaustein = (AiPromptbausteinVM) focusedNode.Content;
String msg = "der ausgewähte KI-Prompt";
if (selectedAiPromptbaustein.IsParent)
{
msg = "die ausgewähte Kategorie";
}
if (MessageBox.Show(Translator.Translate("Sie Sie sicher, dass {0} \"{1}\" gelöscht werden soll?", msg, selectedAiPromptbaustein.Name), "Löschen", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
{
_ChildrenToDelete.Add(selectedAiPromptbaustein);
if (selectedAiPromptbaustein.IsParent)
{
RetrieveChildAiPromptbausteinsFromParent(selectedAiPromptbaustein);
}
ViewModel.VMList.RemoveRange(_ChildrenToDelete);
_ChildrenToDelete.Clear();
}
}
private readonly List<AiPromptbausteinVM> _ChildrenToDelete = new List<AiPromptbausteinVM>();
private void RetrieveChildAiPromptbausteinsFromParent(AiPromptbausteinVM parent)
{
var children = ViewModel.VMList.Where(w => w.Parent != null && w.Parent.Equals(parent));
foreach(var child in children)
{
_ChildrenToDelete.Add(child);
RetrieveChildAiPromptbausteinsFromParent(child);
}
}
public static bool CheckEditorRights(AiPromptbausteinVM textbausteinVM, object parameter)
{
if (textbausteinVM != null)
{
var isOwn = textbausteinVM.Employee != null && 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 ContextMenuAiPromptbausteins_Click(object sender, RoutedEventArgs e)
{
if(e.OriginalSource is MenuItem twi)
{
var selectedItem = TreeListControl.SelectedItem as AiPromptbausteinVM;
ViewModel.NewVM.IsParent = twi.Header.Equals("Neue Textbausteinkategorie");
ViewModel.NewVM.IsExpanded = twi.Header.Equals("Neue Textbausteinkategorie");
ViewModel.NewVM.IsOnlyForEmployee = !_IsInAdministrationView;
ViewModel.NewVM.Name = GenerateNewAiPromptbausteinName(twi.Header.ToString());
if(selectedItem != null)
{
ViewModel.NewVM.Parent = selectedItem;
}
ViewModel.AddNewVMToList(false);
Save();
}
}
private string GenerateNewAiPromptbausteinName(string pName)
{
var newName = pName;
var counter = 1;
while(ViewModel.VMList.Any(a => a.Name != null && a.Name.Equals(newName)))
{
newName = pName + " " + counter;
counter++;
}
return newName;
}
private void ContextMenuAiPromptbausteins_Opened(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is ContextMenu menu)
{
var aiPromptbaustein = TreeListControl.SelectedItem as AiPromptbausteinVM;
foreach (var menuitem in menu.Items.OfType<MenuItem>().Select(item => item))
{
if(aiPromptbaustein != null && !aiPromptbaustein.IsParent)
{
menuitem.IsEnabled = false;
}
else
{
menuitem.IsEnabled = true;
}
}
}
}
private void TreeListControl_ItemsSourceChanged(object sender, DevExpress.Xpf.Grid.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 = (AiPromptbausteinVM) cellData.RowData.Row;
vm.Parent = null;
vm.IsParent = false;
}
}
}
}
// TODO: wird das gebraucht? Was tut es?
private void PART_Editor_OnEditValueChanged(object sender, EditValueChangedEventArgs e)
{
var comboBoxEdit = (ComboBoxEdit) sender;
var selectedAiPromptbaustein = (AiPromptbausteinVM) comboBoxEdit.Tag;
var oldValue = e.OldValue;
var newValue = e.NewValue;
if(e.NewValue != null)
{
selectedAiPromptbaustein.Parent = (AiPromptbausteinVM) e.NewValue;
}
}
private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
{
var comboBoxEdit = (ComboBoxEdit) sender;
var aiPromptbaustein = (AiPromptbausteinVM) comboBoxEdit.Tag;
comboBoxEdit.ItemsSource = ViewModel.GetAiPromptbausteinParents(aiPromptbaustein);
}
private void RemoveParentAiPromptbausteinFromNewVM_OnClick(object sender, RoutedEventArgs e)
{
ViewModel.NewVM.Parent = null;
}
private void IsAiPromptbausteinParent_Checked(object sender, RoutedEventArgs e)
{
ViewModel.NewVM.Text = null;
}
private void AddAiPromptbausteinButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.NewVM.IsOnlyForEmployee = !_IsInAdministrationView;
ViewModel.AddNewVMToList(false);
Save();
}
private void ContextMenuAiPromptbausteine_Click(object sender, RoutedEventArgs e)
{
}
private void ContextMenuAiPromptbausteine_Opened(object sender, RoutedEventArgs e)
{
}
}
public class IsOnlyForEmployeeRightsConverter2 : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var aiPromptbausteinVM = (AiPromptbausteinVM)value;
return AiPromptbausteinView.CheckEditorRights(aiPromptbausteinVM, parameter) && aiPromptbausteinVM != null && !aiPromptbausteinVM.IsParent;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class AiPromptbausteinEditConverter : 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 = (AiPromptbausteinVM)values[1];
return AiPromptbausteinView.CheckEditorRights(textModule, parameter) && shuoldBeEditableDespiteBeingFromAParentElement;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}