aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DB/ViewModels/DbTableViewModel.cs
diff options
context:
space:
mode:
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.cs90
1 files changed, 83 insertions, 7 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 8f7681718..b0a261dd0 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,6 +9,11 @@ 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;
namespace Tango.MachineStudio.DB.ViewModels
{
@@ -23,9 +28,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 +99,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 +134,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 async void OnDelete()
{
- SelectedEntity.Deleted = true;
- await SelectedEntity.SaveAsync();
+ using (_notification.PushTaskItem("Saving changes to database..."))
+ {
+ int count = SelectedEntity.GetDependentEntitiesCount();
+
+ if (count > 0)
+ {
+ _notification.ShowError("The selected entity is being used by " + count + " other entities." + Environment.NewLine + "Please delete any dependencies and try again.");
+ 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 +184,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 +197,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;
}
@@ -155,6 +211,12 @@ namespace Tango.MachineStudio.DB.ViewModels
/// <param name="mode">The mode.</param>
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);
@@ -165,7 +227,20 @@ namespace Tango.MachineStudio.DB.ViewModels
using (_notification.PushTaskItem("Saving changes to database..."))
{
- await entity.SaveAsync();
+ 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;
@@ -177,7 +252,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)
{
}
@@ -188,6 +263,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;
}