| ofs | hex dump | ascii |
|---|
| 0000 | ff d8 ff e1 00 18 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 00 00 00 00 00 00 00 00 ff ec 00 11 | ......Exif..II*................. |
| 0020 | 44 75 63 6b 79 00 01 00 04 00 00 00 64 00 00 ff ee 00 0e 41 64 6f 62 65 00 64 c0 00 00 00 01 ff | Ducky.......d......Adobe.d...... |
| 0040 | db 00 84 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 | ................................ |
| 0060 | 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 02 02 02 02 02 02 02 02 02 02 02 03 03 03 03 03 03 | ................................ |
| 0080 | 03 03 03 03 01 01 01 01 01 01 01 02 01 01 02 02 02 01 02 02 03 03 03 03 03 03 03 03 03 03 03 03 | ................................ |
| 00a0 | 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 | ................................ |
| 00c0 | 03 03 03 03 03 ff c0 00 11 08 00 fa 02 26 03 01 11 00 02 11 01 03 11 01 ff c4 00 6e 00 01 01 01 | .............&.............n.... |
| 00e0 | 01 01 01 00 00 00 00 00 00 00 00 00 00 00 05 07 06 04 0a 01 01 01 00 00 00 00 00 00 00 00 00 00 | ................................ |
| 0100 | 00 00 00 00 00 01 10 00 02 02 01 01 06 05 05 01 01 01 01 00 00 00 00 00 05 03 04 02 01 15 55 75 | ..............................Uu |
| 0120 | 95 d5 06 13 34 b4 35 b5 53 93 d3 14 07 11 12 23 22 11 01 00 03 01 01 00 00 00 00 00 00 00 00 00 | ....4.5.S......#"............... |
| 0140 | 00 00 00 01 11 21 41 31 ff da 00 0c 03 01 00 02 11 03 11 00 3f 00 fb f8 00 00 09 aa bc ac bc 49 | .....!A1............?..........I |
| 0160 | cf cc 5e 24 0a 45 00 00 00 01 36 2f 78 bd c3 55 7a a7 24 e8 a4 50 00 00 00 13 6f 79 a4 dc 4a 5f | ..^$.E....6/x..Uz.$..P....oy..J_ |
| 0180 | 87 6a 41 48 a0 00 00 00 26 b9 f6 76 bc 36 f7 a5 94 93 e0 a4 50 00 00 00 00 26 aa f2 b2 f1 27 3f | .jAH....&..v.6......P....&....'? |
| 01a0 | 31 78 90 29 14 00 00 00 04 d8 bd e2 f7 0d 55 ea 9c 93 a2 91 40 00 00 00 4d bd e6 93 71 29 7e 1d | 1x.)..........U.....@...M...q)~. |
| 01c0 | a9 05 22 80 00 00 00 9a e7 d9 da f0 db de 96 52 4f 82 91 40 00 00 00 00 9a ab ca cb c4 9c fc c5 | .."............RO..@............ |
| 01e0 | e2 40 a4 50 00 00 00 13 62 f7 8b dc 35 57 aa 72 4e 8a 45 00 00 00 01 36 f7 9a 4d c4 a5 f8 76 a4 | .@.P....b...5W.rN.E....6..M...v. |
| 0200 | 14 8a 00 00 00 00 04 d5 5e 56 5e 24 e7 e6 2f 12 05 22 80 00 00 00 9b 17 bc 5e e1 aa bd 53 92 74 | ........^V^$../..".......^...S.t |
| 0220 | 52 28 00 00 00 09 b7 bc d2 6e 25 2f c3 b5 20 a4 50 00 00 00 13 5c fb 3b 5e 1b 7b d2 ca 49 f0 52 | R(.......n%/....P....\.;^.{..I.R |
| 0240 | 28 00 00 00 00 13 55 79 59 78 93 9f 98 bc 48 14 8a 00 00 00 02 6c 5e f1 7b 86 aa f5 4e 49 d1 48 | (.....UyYx....H......l^.{...NI.H |
| 0260 | a0 00 00 00 26 de f3 49 b8 94 bf 0e d4 82 91 40 00 00 00 4d 73 ec ed 78 6d ef 4b 29 27 c1 48 a0 | ....&..I.......@...Ms..xm.K)'.H. |
| 0280 | 00 00 00 00 4d 55 e5 65 e2 4e 7e 62 f1 20 52 28 00 00 00 09 b1 7b c5 ee 1a ab d5 39 27 45 22 80 | ....MU.e.N~b..R(.....{.....9'E". |
| 02a0 | 00 00 00 9b 7b cd 26 e2 52 fc 3b 52 0a 45 00 00 00 00 02 6a af 2b 2f 12 73 f3 17 89 02 91 40 00 | ....{.&.R.;R.E.....j.+/.s.....@. |
| 02c0 | 00 00 4d 8b de 2f 70 d5 5e a9 c9 3a 29 14 00 00 00 04 db de 69 37 12 97 e1 da 90 52 28 00 00 00 | ..M../p.^..:).......i7.....R(... |
| 02e0 | 09 ae 7d 9d af 0d bd e9 65 24 f8 29 14 00 00 00 00 09 aa bc ac bc 49 cf cc 5e 24 0a 45 00 00 00 | ..}.....e$.)..........I..^$.E... |
| 0300 | 01 36 2f 78 bd c3 55 7a a7 24 e8 a4 50 00 00 00 13 6f 79 a4 dc 4a 5f 87 6a 41 48 a0 00 00 00 26 | .6/x..Uz.$..P....oy..J_.jAH....& |
| 0320 | b9 f6 76 bc 36 f7 a5 94 93 e0 a4 50 00 00 00 00 26 aa f2 b2 f1 27 3f 31 78 90 29 14 00 00 00 04 | ..v.6......P....&....'?1x.)..... |
| 0340 | d8 bd e2 f7 0d 55 ea 9c 93 a2 91 40 00 00 00 4d bd e6 93 71 29 7e 1d a9 05 22 80 00 24 f8 2f 77 | .....U.....@...M...q)~..."..$./w |
| 0360 | 8a 9e 4b 73 af 93 43 c1 7b bc 54 f2 5b 9d 7c 68 78 2f 77 8a 9e 4b 73 af 8d 19 e7 f3 45 9f d2 e8 | ..Ks..C.{.T.[.|hx/w..Ks.....E... |
| 0380 | a2 b7 17 79 f7 12 66 4c f5 6b 36 b0 cf 1d 68 d8 e1 84 31 51 5f 5a ec 59 4a bd 47 63 45 fb 1a 3d | ...y..fL.k6...h...1Q_Z.YJ.GcE..= |
| 03a0 | af 73 29 e6 fd 6d 70 bf 3e 59 dd 82 15 75 ad 40 89 50 68 7e 0b dd e2 a7 92 dc eb e3 43 c1 7b bc | .s)..mp.>Y...u.@.Ph~........C.{. |
| 03c0 | 54 f2 5b 9d 7c 68 78 2f 77 8a 9e 4b 73 af 8d 0f 05 ee f1 53 c9 6e 75 f1 a1 e0 bd de 2a 79 2d ce | T.[.|hx/w..Ks......S.nu.....*y-. |
| 03e0 | be 34 67 8b 56 7f 4b c3 fa 33 fb 97 3b 89 34 9d a5 22 a8 b4 5c bb 4a d1 c9 34 13 49 0a 28 ea 4b | .4g.V.K..3..;.4.."..\.J..4.I.(.K |
| 0400 | 2d 48 d4 52 b9 fa ff 00 b9 49 b6 b0 c3 ab 69 33 a5 9c 93 4d 34 cc 63 63 4e 9f 6c 86 87 e0 bd de | -H.R.....I....i3...M4.ccN.l..... |
| 0420 | 2a 79 2d ce be 34 3c 17 bb c5 4f 25 b9 d7 c6 87 82 f7 78 a9 e4 b7 3a f8 d0 f0 5e ef 15 3c 96 e7 | *y-..4<...O%......x...:...^..<.. |
| 0440 | 5f 1a 1e 0b dd e2 a7 92 dc eb e3 46 79 de 4b 3f a5 d9 7b d9 32 f6 ef 71 26 a6 b2 bb 5d 75 7d 06 | _..........Fy.K?..{.2..q&...]u}. |
| 0460 | 75 a3 a9 9d d8 74 bc 9e ce 71 57 8a da 8e e6 97 4b 1b 0a a3 2c 75 9b 1b 30 61 9c 19 cb 4b 48 63 | u....t...qW.....K...,u..0a...KHc |
| 0480 | b3 7a ab de df 0d 0f c1 7b bc 54 f2 5b 9d 7c 68 78 2f 77 8a 9e 4b 73 af 8d 0f 05 ee f1 53 c9 6e | .z......{.T.[.|hx/w..Ks......S.n |
| 04a0 | 75 f1 a1 e0 bd de 2a 79 2d ce be 34 3c 17 bb c5 4f 25 b9 d7 c6 8e 4f be 29 77 bd 8e d7 67 1f 6f | u.....*y-..4<...O%....O.)w...g.o |
| 04c0 | bc 47 49 9f fb 42 5c 6c d8 51 3e 95 a3 a3 03 2a 73 b7 d2 d4 12 5c 65 93 1a b2 28 8e 7c 65 a5 16 | .GI..B\l.Q>....*s....\e...(.|e.. |
| 04e0 | b4 2c 5e 8f 2d 6b c0 c5 54 d2 e0 ca a0 55 ed fa 7d e3 5d 0a 4a ef 1b ab b0 ea 05 0b 61 71 3e 74 | .,^.-k..T....U..}.].J.......aq>t |
| 0500 | f5 67 9c ed 22 a7 0e 0c 26 cd 95 08 bb 3a 8b 0c a5 b7 8e 79 6b 3c 2a 15 45 2e ba ff 00 de 14 eb | .g.."...&....:.....yk<*.E....... |
| 0520 | 63 ae 90 e1 74 57 f0 5e ef 15 3c 96 e7 5f 26 87 82 f7 78 a9 e4 b7 3a f8 d0 f0 5e ef 15 3c 96 e7 | c...tW.^..<.._&...x...:...^..<.. |
| 0540 | 5f 1a 1e 0b dd e2 a7 92 dc eb e3 43 c1 7b bc 54 f2 5b 9d 7c 68 cf 3f 9a 2c fe 97 45 15 b8 bb cf | _..........C.{.T.[.|h.?.,..E.... |
| 0560 | b8 93 32 67 ab 59 b5 86 78 eb 46 c7 0c 21 8a 8a fa d7 62 ca 55 ea 3b 1a 2f d8 d1 ed 7b 99 4f 37 | ..2g.Y..x.F..!....b.U.;./...{.O7 |
| 0580 | eb 6b 85 f9 f2 ce ec 10 ab ad 6a 04 4a 83 43 f0 5e ef 15 3c 96 e7 5f 1a 1e 0b dd e2 a7 92 dc eb | .k........j.J.C.^..<.._......... |
| 05a0 | e3 43 c1 7b bc 54 f2 5b 9d 7c 68 78 2f 77 8a 9e 4b 73 af 8d 0f 05 ee f1 53 c9 6e 75 f1 a3 3c 5a | .C.{.T.[.|hx/w..Ks......S.nu..<Z |
| 05c0 | b3 fa 5e 1f d1 9f dc b9 dc 49 a4 ed 29 15 45 a2 e5 da 56 8e 49 a0 9a 48 51 47 52 59 6a 46 a2 95 | ..^......I..).E...V.I..HQGRYjF.. |
| 05e0 | cf d7 fd ca 4d b5 86 1d 5b 49 9d 2c e4 9a 69 a6 63 1b 1a 74 fb 64 34 3f 05 ee f1 53 c9 6e 75 f1 | ....M...[I.,..i.c..t.d4?...S.nu. |
| 0600 | a1 e0 bd de 2a 79 2d ce be 34 3c 17 bb c5 4f 25 b9 d7 c6 87 82 f7 78 a9 e4 b7 3a f8 d0 f0 5e ef | ....*y-..4<...O%......x...:...^. |
| 0620 | 15 3c 96 e7 5f 1a 33 ce f2 59 fd 2e cb de c9 97 b7 7b 89 35 35 95 da eb ab e8 33 ad 1d 4c ee c3 | .<.._.3..Y.......{.55.....3..L.. |
| 0640 | a5 e4 f6 73 8a bc 56 d4 77 34 ba 58 d8 55 19 63 ac d8 d9 83 0c e0 ce 5a 5a 43 1d 9b d5 5e f6 f8 | ...s..V.w4.X.U.c.......ZZC...^.. |
| 0660 | 68 7e 0b dd e2 a7 92 dc eb e3 43 c1 7b bc 54 f2 5b 9d 7c 68 78 2f 77 8a 9e 4b 73 af 8d 0f 05 ee | h~........C.{.T.[.|hx/w..Ks..... |
| 0680 | f1 53 c9 6e 75 f1 a1 e0 bd de 2a 79 2d ce be 34 72 7d f1 4b bd ec 76 bb 38 fb 7d e2 3a 4c ff 00 | .S.nu.....*y-..4r}.K..v.8.}.:L.. |
| 06a0 | da 12 e3 66 c2 89 f4 ad 1d 18 19 53 9d be 96 a0 92 e3 2c 98 d5 91 44 73 e3 2d 28 b5 a1 62 f4 79 | ...f.......S......,...Ds.-(..b.y |
| 06c0 | 6b 5e 06 2a a6 97 06 55 02 af 6f d3 ef 1a e8 52 57 78 dd 5d 87 50 28 5b 0b 89 f3 a7 ab 3c e7 69 | k^.*...U..o....RWx.].P([.....<.i |
| 06e0 | 15 38 70 61 36 6c a8 45 d9 d4 58 65 2d bcusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.BL.Entities;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.DB.Managers;
using Tango.SharedUI;
using System.Data.Entity.Infrastructure;
using Tango.MachineStudio.DB.Messages;
using System.Collections.ObjectModel;
using System.Reflection;
using Tango.MachineStudio.Common.StudioApplication;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using Tango.BL;
using Tango.Core.DI;
using Tango.Core.ExtensionMethods;
namespace Tango.MachineStudio.DB.ViewModels
{
public abstract class DbTableViewModel<T> : ViewModel where T : class, IObservableEntity
{
protected INotificationProvider _notification;
/// <summary>
/// Initializes a new instance of the <see cref="DbTableViewModel"/> class.
/// </summary>
public DbTableViewModel(INotificationProvider notification) : base()
{
_notification = notification;
Adapter = ObservablesEntitiesAdapter.Instance;
ValidationErrors = new ObservableCollection<string>();
AddCommand = new RelayCommand(OnAdd);
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));
IsDialogOpen = false;
}
private T _editEntity;
/// <summary>
/// Gets or sets the edit entity.
/// </summary>
public T EditEntity
{
get { return _editEntity; }
set { _editEntity = value; RaisePropertyChangedAuto(); }
}
private DialogOpenMode _dialogOpenMode;
/// <summary>
/// Gets or sets the dialog open mode.
/// </summary>
public DialogOpenMode DialogOpenMode
{
get { return _dialogOpenMode; }
set { _dialogOpenMode = value; RaisePropertyChangedAuto(); }
}
private bool _isDialogOpen;
/// <summary>
/// Gets or sets a value indicating whether this instance is dialog open.
/// </summary>
public bool IsDialogOpen
{
get { return _isDialogOpen; }
set { _isDialogOpen = value; RaisePropertyChangedAuto(); }
}
private ObservablesEntitiesAdapter _adapter;
/// <summary>
/// Gets or sets the DB adapter.
/// </summary>
public ObservablesEntitiesAdapter Adapter
{
get { return _adapter; }
set { _adapter = value; RaisePropertyChangedAuto(); }
}
private T _selectedEntity;
/// <summary>
/// Gets or sets the selected entity.
/// </summary>
public T SelectedEntity
{
get { return _selectedEntity; }
set { _selectedEntity = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
private String _filter;
/// <summary>
/// Gets or sets the search filter.
/// </summary>
public String Filter
{
get { return _filter; }
set { _filter = value; RaisePropertyChangedAuto(); OnFilterChanged(value); }
}
/// <summary>
/// Gets or sets the dialog OK command.
/// </summary>
public RelayCommand DialogOKCommand { get; set; }
/// <summary>
/// Gets or sets the dialog cancel command.
/// </summary>
public RelayCommand DialogCancelCommand { get; set; }
/// <summary>
/// Gets or sets the add command.
/// </summary>
public RelayCommand AddCommand { get; set; }
/// <summary>
/// Gets or sets the edit command.
/// </summary>
public RelayCommand EditCommand { get; set; }
/// <summary>
/// Gets or sets the delete command.
/// </summary>
public RelayCommand DeleteCommand { get; set; }
protected override void OnValidating()
{
base.OnValidating();
ValidationErrors.Clear();
foreach (var prop in typeof(T).GetPropertiesWithAttribute<ColumnAttribute>(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()
{
using (_notification.PushTaskItem("Saving changes to database..."))
{
try
{
SelectedEntity.Detach(Adapter.Context);
await Adapter.Context.SaveChangesAsync();
}
catch (Exception ex)
{
Adapter.Invalidate();
_notification.ShowError("Could not delete entity." + Environment.NewLine + ex.Message);
}
SelectedEntity = null;
}
}
/// <summary>
/// Called when edit command invoked.
/// </summary>
protected virtual void OnEdit()
{
ValidationErrors.Clear();
DialogOpenMode = DialogOpenMode.Editing;
EditEntity = GetEditableEntity(DialogOpenMode);
TangoMessenger.Default.Send(new OpenEntityEditViewMessage(DialogOpenMode, this, typeof(T)));
IsDialogOpen = true;
}
/// <summary>
/// Called when add command invoked.
/// </summary>
protected virtual void OnAdd()
{
ValidationErrors.Clear();
DialogOpenMode = DialogOpenMode.Adding;
EditEntity = GetEditableEntity(DialogOpenMode);
var codeProp = EditEntity.GetType().GetProperty("Code");
if (codeProp != null)
{
if (EditEntity.GetDbSet<T>(Adapter.Context).Count() > 0)
{
int lastCode = EditEntity.GetDbSet<T>(Adapter.Context).ToList().Max(x => (int)codeProp.GetValue(x));
codeProp.SetValue(EditEntity, lastCode + 1);
}
}
TangoMessenger.Default.Send(new OpenEntityEditViewMessage(DialogOpenMode, this, typeof(T)));
IsDialogOpen = true;
}
/// <summary>
/// Called when dialog closes with OK button.
/// </summary>
/// <param name="mode">The mode.</param>
protected virtual async void OnDialogOKPressed(DialogOpenMode mode, T entity)
{
if (!Validate()) return;
if (ValidationErrors.Count > 0) return;
var codeProp = entity.GetType().GetProperty("Code");
if (codeProp != null)
{
int code = (int)codeProp.GetValue(entity);
if (entity.GetDbSet<T>(Adapter.Context).ToList().Where(x => x.Guid != entity.Guid).Select(x => (int)codeProp.GetValue(x)).Contains(code))
{
_notification.ShowError("The entity code specified already exists.");
return;
}
}
TangoMessenger.Default.Send(new CloseEntityEditViewMessage());
if (mode == DialogOpenMode.Editing)
{
entity.ShallowCopyTo(SelectedEntity);
entity = SelectedEntity;
}
OnBeforeEntitySave(mode, entity);
using (_notification.PushTaskItem("Saving changes to database..."))
{
if (mode == DialogOpenMode.Adding)
{
entity.Attach(Adapter.Context);
}
try
{
await entity.SaveAsync(Adapter.Context);
}
catch (DbUpdateException ex)
{
if (mode == DialogOpenMode.Adding)
{
entity.Detach(Adapter.Context);
}
Adapter.Invalidate();
_notification.ShowError("Could not save entity." + Environment.NewLine + ex.InnerException.InnerException != null ? ex.InnerException.InnerException.Message : ex.InnerException.Message);
}
catch (Exception)
{
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>
/// Called when [before entity save].
/// </summary>
/// <param name="mode">The mode.</param>
/// <param name="entity">The entity.</param>
protected virtual void OnBeforeEntitySave(DialogOpenMode mode, T entity)
{
}
/// <summary>
/// Called when dialog closes with cancel button.
/// </summary>
/// <param name="mode">The mode.</param>
protected virtual void OnDialogCancelPressed(DialogOpenMode mode, T entity)
{
TangoMessenger.Default.Send(new CloseEntityEditViewMessage());
IsDialogOpen = false;
}
/// <summary>
/// Gets the editable entity.
/// </summary>
/// <param name="mode">The mode.</param>
/// <returns></returns>
private T GetEditableEntity(DialogOpenMode mode)
{
if (mode == DialogOpenMode.Adding)
{
var newEntity = Activator.CreateInstance<T>();
InitializeEntity(newEntity);
return newEntity;
}
else
{
return SelectedEntity.ShallowClone();
}
}
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 FilterEntity((T)entity, filter);
};
}
private bool FilterEntity(T entity, String filter)
{
foreach (var prop in entity.GetType().GetPropertiesWithAttribute<ColumnAttribute>(BindingFlags.Public | BindingFlags.Instance).Where(x => x.PropertyType.IsClass && x.PropertyType != typeof(String) && x.PropertyType != typeof(byte[]) && !x.PropertyType.IsGenericType))
{
object obj = prop.GetValue(entity);
if (obj != null)
{
foreach (var innerProp in obj.GetType().GetPropertiesWithAttribute<ColumnAttribute>(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))))
{
object value = innerProp.GetValue(obj);
if (value != null)
{
if (value.ToString().ToLower().Contains(filter.ToLower()))
{
return true;
}
}
}
}
}
return
entity.
GetType().
GetPropertiesWithAttribute<ColumnAttribute>(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)
{
}
}
}
|