diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DB/ViewModels/DbTableViewModel.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DB/ViewModels/DbTableViewModel.cs | 126 |
1 files changed, 113 insertions, 13 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DB/ViewModels/DbTableViewModel.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DB/ViewModels/DbTableViewModel.cs index 7f6dd8d5f..b6d77748e 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DB/ViewModels/DbTableViewModel.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DB/ViewModels/DbTableViewModel.cs @@ -9,10 +9,17 @@ using Tango.MachineStudio.Common.Notifications; using Tango.MachineStudio.DB.Managers; using Tango.SharedUI; using Tango.MachineStudio.DB.ExtensionMethods; +using System.Data.Entity.Infrastructure; +using GalaSoft.MvvmLight.Messaging; +using Tango.MachineStudio.DB.Messages; +using System.Collections.ObjectModel; +using System.Reflection; +using Tango.MachineStudio.Common.StudioApplication; +using System.ComponentModel; namespace Tango.MachineStudio.DB.ViewModels { - public abstract class DbTableViewModel<T> : ViewModel where T : class, IObservableEntity + public abstract class DbTableViewModel<T> : ViewModel, IShutdownRequestBlocker where T : class, IObservableEntity { private INotificationProvider _notification; @@ -23,9 +30,10 @@ namespace Tango.MachineStudio.DB.ViewModels { _notification = notification; Adapter = ObservablesEntitiesAdapter.Instance; + ValidationErrors = new ObservableCollection<string>(); AddCommand = new RelayCommand(OnAdd); - EditCommand = new RelayCommand(OnEdit,(x) => SelectedEntity != null); + EditCommand = new RelayCommand(OnEdit, (x) => SelectedEntity != null); DeleteCommand = new RelayCommand(OnDelete, (x) => SelectedEntity != null); DialogOKCommand = new RelayCommand(() => OnDialogOKPressed(DialogOpenMode, EditEntity)); DialogCancelCommand = new RelayCommand(() => OnDialogCancelPressed(DialogOpenMode, EditEntity)); @@ -93,6 +101,16 @@ namespace Tango.MachineStudio.DB.ViewModels set { _filter = value; RaisePropertyChangedAuto(); OnFilterChanged(value); } } + private ObservableCollection<String> _validationErrors; + /// <summary> + /// Gets or sets the validation errors. + /// </summary> + public ObservableCollection<String> ValidationErrors + { + get { return _validationErrors; } + set { _validationErrors = value; RaisePropertyChangedAuto(); } + } + /// <summary> /// Gets or sets the dialog OK command. /// </summary> @@ -118,13 +136,49 @@ namespace Tango.MachineStudio.DB.ViewModels /// </summary> public RelayCommand DeleteCommand { get; set; } + protected override void OnValidating() + { + base.OnValidating(); + ValidationErrors.Clear(); + + foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => !x.PropertyType.IsGenericType && x.PropertyType.IsClass && !x.Name.Contains("Guid"))) + { + if (prop.GetValue(EditEntity) == null) + { + ValidationErrors.Add(prop.Name + " is required"); + } + } + } + /// <summary> /// Called when delete command invoked. /// </summary> - protected virtual void OnDelete() + protected virtual async void OnDelete() { - SelectedEntity.Deleted = true; - SelectedEntity.Save(); + using (_notification.PushTaskItem("Saving changes to database...")) + { + var dependenctEntities = SelectedEntity.GetDependentEntitiesNameAndGuid(); + + if (dependenctEntities.Count > 0) + { + _notification.ShowError("The selected entity is being used by " + dependenctEntities.Count + " other entities." + Environment.NewLine + "Please delete any dependencies and try again." + Environment.NewLine + Environment.NewLine + String.Join(Environment.NewLine,dependenctEntities.Select(x => x.Key + ", ID: " + x.Value))); + return; + } + + try + { + SelectedEntity.Deleted = true; + await SelectedEntity.SaveAsync(); + } + catch (Exception ex) + { + SelectedEntity.Deleted = false; + Adapter.Invalidate(); + _notification.ShowError("Could not delete entity."); + } + + SelectedEntity = null; + } } /// <summary> @@ -132,9 +186,11 @@ namespace Tango.MachineStudio.DB.ViewModels /// </summary> protected virtual void OnEdit() { + ValidationErrors.Clear(); DialogOpenMode = DialogOpenMode.Editing; EditEntity = GetEditableEntity(DialogOpenMode); - _notification.ShowDialog(DialogOpenMode, this); + //_notification.ShowDialog(DialogOpenMode, this); + Messenger.Default.Send(new OpenEntityEditViewMessage(DialogOpenMode, this, typeof(T))); IsDialogOpen = true; } @@ -143,9 +199,11 @@ namespace Tango.MachineStudio.DB.ViewModels /// </summary> protected virtual void OnAdd() { + ValidationErrors.Clear(); DialogOpenMode = DialogOpenMode.Adding; EditEntity = GetEditableEntity(DialogOpenMode); - _notification.ShowDialog(DialogOpenMode, this); + //_notification.ShowDialog(DialogOpenMode, this); + Messenger.Default.Send(new OpenEntityEditViewMessage(DialogOpenMode, this, typeof(T))); IsDialogOpen = true; } @@ -153,8 +211,14 @@ namespace Tango.MachineStudio.DB.ViewModels /// Called when dialog closes with OK button. /// </summary> /// <param name="mode">The mode.</param> - protected virtual void OnDialogOKPressed(DialogOpenMode mode, T entity) + protected virtual async void OnDialogOKPressed(DialogOpenMode mode, T entity) { + if (!Validate()) return; + + if (ValidationErrors.Count > 0) return; + + Messenger.Default.Send(new CloseEntityEditViewMessage()); + if (mode == DialogOpenMode.Editing) { entity.ShallowCopyTo(SelectedEntity); @@ -163,10 +227,26 @@ namespace Tango.MachineStudio.DB.ViewModels OnBeforeEntitySave(mode, entity); - entity.Save(); - IsDialogOpen = false; - SelectedEntity = EditEntity; - SelectedEntity = null; + using (_notification.PushTaskItem("Saving changes to database...")) + { + try + { + await entity.SaveAsync(); + } + catch (DbUpdateException ex) + { + Adapter.Invalidate(); + _notification.ShowError("Could not save entity." + Environment.NewLine + ex.InnerException.InnerException != null ? ex.InnerException.InnerException.Message : ex.InnerException.Message); + } + catch (Exception ex) + { + Adapter.Invalidate(); + _notification.ShowError("Could not save entity." + Environment.NewLine + "Please make sure all fields are properly populated."); + } + IsDialogOpen = false; + SelectedEntity = EditEntity; + SelectedEntity = null; + } } /// <summary> @@ -174,7 +254,7 @@ namespace Tango.MachineStudio.DB.ViewModels /// </summary> /// <param name="mode">The mode.</param> /// <param name="entity">The entity.</param> - protected virtual void OnBeforeEntitySave(DialogOpenMode mode,T entity) + protected virtual void OnBeforeEntitySave(DialogOpenMode mode, T entity) { } @@ -185,6 +265,7 @@ namespace Tango.MachineStudio.DB.ViewModels /// <param name="mode">The mode.</param> protected virtual void OnDialogCancelPressed(DialogOpenMode mode, T entity) { + Messenger.Default.Send(new CloseEntityEditViewMessage()); IsDialogOpen = false; } @@ -209,12 +290,31 @@ namespace Tango.MachineStudio.DB.ViewModels protected virtual void OnFilterChanged(String filter) { + String viewSourceName = this.GetType().Name.Replace("ViewVM", "ViewSource"); + ICollectionView collectionView = Adapter.GetType().GetProperty(viewSourceName).GetValue(Adapter) as ICollectionView; + collectionView.Filter = (entity) => + { + return + entity. + GetType(). + GetProperties(BindingFlags.Public | BindingFlags.Instance). + Where(x => x.Name != "Deleted" && x.Name != "ID" && x.Name != "LastUpdated"). + Where(x => !x.PropertyType.IsGenericType && (x.PropertyType.IsClass || x.PropertyType == typeof(String))). + Select(prop => prop.GetValue(entity).ToString()). + ToList(). + Any(x => x.ToLower().Contains(filter.ToLower())); + }; } protected virtual void InitializeEntity(T entity) { } + + public Task<bool> OnShutdownRequest() + { + return Task.FromResult(true); + } } } |
