using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using System.Windows.Input; using BeWo.Annotations; using BeWo.Core; using BeWo.ServiceProxy; using BeWo.Validation; using BeWo.View.Controls; using BeWo.ViewModel; using BeWo.ViewModel.ListViewModel; using BS.Shared; using BS.Shared.Core; using BS.Shared.DataContracts; using BS.Shared.Extensions; using BS.Shared.Translation; using DevExpress.Utils; using DevExpress.Xpf.Editors; using DevExpress.Xpf.Grid; using MessageBox = System.Windows.MessageBox; namespace BeWo.View { public partial class HilfeplanImportView : INotifyPropertyChanged { private HilfeplanImportDC _ViewModel; private String _UploadFileName; private long? _SupportConceptOid; public event PropertyChangedEventHandler PropertyChanged; public event Action CloseButtonClicked; private HilfeplanImportDC ViewModel { get { return _ViewModel; } set { DataContext = value; _ViewModel = value; } } public HilfeplanImportView(long? supportConceptOid) { InitializeComponent(); _SupportConceptOid = supportConceptOid; ClearControls(); } private void ReloadViewModel(HilfeplanImportDC dc) { this.ViewModel = dc; ClearControls(); UpdateControls(dc); } private void ClearControls() { PanelGruppen.Children.Clear(); treeview_Goals.ItemsSource = null; } private void UpdateControls(HilfeplanImportDC dc) { if (dc.Gruppen != null) { foreach (var gruppe in dc.Gruppen) { ErstelleGruppe(gruppe); } } if (dc.Ziele != null) { ErstelleTreeView(dc.Ziele); } GroupBoxMain.Visibility = Visibility.Visible; } private void ErstelleGruppe(HilfeplanImportItemDC gruppe) { var expander = new Expander(); expander.Margin = new Thickness(5); expander.FontSize = 14; expander.Header = gruppe.Label; expander.IsExpanded = true; var itemGrid = new Grid(); //itemGrid.FontWeight = FontWeights.Normal; itemGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); itemGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); expander.Content = itemGrid; if (gruppe.Items != null) { foreach (var item in gruppe.Items) { itemGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); var label = new System.Windows.Controls.Label(); label.FontSize = 12; label.Margin = new Thickness(3); label.Content = item.Label; label.SetValue(Grid.ColumnProperty, 0); label.SetValue(Grid.RowProperty, itemGrid.RowDefinitions.Count - 1); var txt = new System.Windows.Controls.TextBox(); txt.FontSize = 12; txt.Margin = new Thickness(3); txt.Text = item.Content; txt.MinWidth = 300; txt.SetValue(Grid.ColumnProperty, 1); txt.SetValue(Grid.RowProperty, itemGrid.RowDefinitions.Count - 1); txt.Tag = item; itemGrid.Children.Add(label); itemGrid.Children.Add(txt); } } PanelGruppen.Children.Add(expander); } private void ErstelleTreeView(IList ziele) { var treeItems = new BindingList(); foreach (var item in ziele) { GoalTreeItem ziel = new GoalTreeItem { Name = item.ZielName, Notice = item.ZielBeschreibung, HilfeplanImportZiel = item }; GoalTreeItem parent = treeItems.Where(i => i.Name == item.Bereich).FirstOrDefault(); if (parent == null) { parent = new GoalTreeItem(); parent.Name = item.Bereich; treeItems.Add(parent); } parent.Children.Add(ziel); } treeview_Goals.ItemsSource = treeItems; } private void popupedit_file_PopUpClick(object sender, RoutedEventArgs ev) { var dlg = new OpenFileDialog(); if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { try { _UploadFileName = dlg.FileName; popupedit_file.Text = dlg.SafeFileName; ButtonImport.IsEnabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } } private void ButtonImport_Click(object sender, RoutedEventArgs pe) { if (!String.IsNullOrEmpty(popupedit_file.Text)) { try { var upload = new ProgressStream(File.OpenRead(_UploadFileName)); //upload.ProgressChanged += (s, e) => Dispatcher.BeginInvoke( // DispatcherPriority.Normal, // (Action)(() => // { // progressbar_upload.Value = e.Data; // text_progress.Content = string.Format("Bitte warten... {0:00}%", e.Data * 100); // })); long oid = 0; var lUlPackage = new UploadPackage(null, 0, _UploadFileName, null, 0, 0, upload); ServiceFacade.DoStreamingServiceAsync( s => s.UploadPersehFile(lUlPackage), r => this.Dispatch( delegate { upload.Dispose(); if (r.HilfeplanImportData != null) { ReloadViewModel(r.HilfeplanImportData); } }), () => this.Dispatch( delegate { upload.Dispose(); })); } catch (Exception ex) { MessageBox.Show(ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } } private void btnSave_Click(object sender, RoutedEventArgs e) { if (ViewModel != null) { UpdateViewModel(); var result = ServiceFacade.DoCustomerServiceSync(s => s.ImportSupportConcept(ViewModel)); if (!String.IsNullOrEmpty(result)) { MessageBox.Show(result, "Import", MessageBoxButton.OK, MessageBoxImage.Information); } CloseButtonClicked?.Invoke(); } } private void UpdateViewModel() { ViewModel.SupportConceptOid = _SupportConceptOid; //Update Stammdaten foreach (var item in PanelGruppen.Children) { var exp = item as Expander; if (exp != null) { var g = exp.Content as Grid; foreach (var gridItem in g.Children) { var txt = gridItem as System.Windows.Controls.TextBox; if (txt != null && txt.Tag != null) { var ii = txt.Tag as HilfeplanImportItemDC; if (ii != null) { ii.Content = txt.Text; } } } } } //Update Ziele foreach (var item in treeview_Goals.ItemsSource) { var gti = item as GoalTreeItem; if (gti != null) { UpdateGoalWithChildren(null, gti); } } } private void UpdateGoalWithChildren(GoalTreeItem parent, GoalTreeItem gti) { if (gti.HilfeplanImportZiel != null) { if (parent != null) { gti.HilfeplanImportZiel.Bereich = parent.Name; } gti.HilfeplanImportZiel.ZielName = gti.Name; } if (gti.Children != null) { foreach (var c in gti.Children) { UpdateGoalWithChildren(gti, c); } } } private void btnClose_Click(object sender, RoutedEventArgs e) { CloseButtonClicked?.Invoke(); } } public class GoalTreeItem { private BindingList _Children; public GoalTreeItem() { } public bool IsHidden { get; set; } public String Name { get; set; } public String Notice { get; set; } public HilfeplanImportZielDC HilfeplanImportZiel { get; set; } public BindingList Children { get => _Children ?? (_Children = new BindingList()); set => _Children = value; } } }