1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Tango.Core;
using Tango.Core.Commands;
using Tango.Logging;
using Tango.Synchronization.Local;
namespace Tango.Synchronization.UI.ViewModels
{
public class MainViewVM : ExtendedObject
{
private String _masterDBFile;
private String _slaveDBFile;
private LocalDBComparer _comparer;
#region Properties
private bool _isBusy;
/// <summary>
/// Gets or sets a value indicating whether an operation is in progress.
/// </summary>
public bool IsBusy
{
get { return _isBusy; }
set { _isBusy = value; RaisePropertyChanged(nameof(IsBusy)); }
}
private String _status;
/// <summary>
/// Gets or sets the current operation status.
/// </summary>
public String Status
{
get { return _status; }
set { _status = value; RaisePropertyChanged(nameof(Status)); }
}
private String _log;
/// <summary>
/// Gets or sets the current application log text.
/// </summary>
public String Log
{
get { return _log; }
set { _log = value; RaisePropertyChanged(nameof(Log)); }
}
private ObservableCollection<Diff> _differences;
/// <summary>
/// Gets or sets the differences.
/// </summary>
public ObservableCollection<Diff> Differences
{
get { return _differences; }
set { _differences = value; RaisePropertyChanged(nameof(Differences)); }
}
private Diff _selectedDifference;
/// <summary>
/// Gets or sets the selected difference.
/// </summary>
public Diff SelectedDifference
{
get { return _selectedDifference; }
set { _selectedDifference = value; RaisePropertyChanged(nameof(SelectedDifference)); InvalidateRelayCommands(); }
}
private String _masterDBName;
/// <summary>
/// Gets or sets the name of the master database.
/// </summary>
public String MasterDBName
{
get { return _masterDBName; }
set { _masterDBName = value; RaisePropertyChanged(nameof(MasterDBName)); }
}
private String _slaveDBName;
/// <summary>
/// Gets or sets the name of the slave database.
/// </summary>
public String SlaveDBName
{
get { return _slaveDBName; }
set { _slaveDBName = value; RaisePropertyChanged(nameof(SlaveDBName)); }
}
#endregion
#region Commands
/// <summary>
/// Gets or sets the browse master database command.
/// </summary>
public RelayCommand BrowseMasterDBCommand { get; set; }
/// <summary>
/// Gets or sets the browse slave database command.
/// </summary>
public RelayCommand BrowseSlaveDBCommand { get; set; }
/// <summary>
/// Gets or sets the compare command.
/// </summary>
public RelayCommand CompareCommand { get; set; }
/// <summary>
/// Gets or sets the commit command.
/// </summary>
public RelayCommand CommitCommand { get; set; }
/// <summary>
/// Gets or sets the commit all command.
/// </summary>
public RelayCommand CommitAllCommand { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="MainViewVM"/> class.
/// </summary>
public MainViewVM()
{
SimpleStringLogger logger = new SimpleStringLogger(LogCategory.Critical, LogCategory.Error, LogCategory.General, LogCategory.Warning);
logger.LogReceived += (sender,log) =>
{
Log += log.ToString() + Environment.NewLine;
};
LogManager.RegisterLogger(logger);
LogManager.Log("DB Synchronizer Started!");
Differences = new ObservableCollection<Diff>();
BrowseMasterDBCommand = new RelayCommand(BrowseMasterDB);
BrowseSlaveDBCommand = new RelayCommand(BrowseSlaveDB);
CompareCommand = new RelayCommand(Compare, (x) => _masterDBFile != null && _slaveDBFile != null);
CommitCommand = new RelayCommand(Commit, (x) => SelectedDifference != null);
CommitAllCommand = new RelayCommand(CommitAll, (x) => Differences.Count > 0);
Task.Factory.StartNew(() =>
{
OpenStatus("Loading components, please wait...");
Thread.Sleep(1000);
CloseStatus();
});
}
#endregion
#region Private Methods
private void Compare()
{
_comparer = new LocalDBComparer(new SQLiteDataBase(_masterDBFile), new SQLiteDataBase(_slaveDBFile));
OpenStatus("Comparing DataBase...");
Task.Factory.StartNew(() =>
{
try
{
Thread.Sleep(1500);
var diffs = _comparer.Compare();
Differences = new ObservableCollection<Diff>(diffs);
}
catch (Exception ex)
{
ShowError(ex.Message);
}
finally
{
SelectedDifference = null;
InvalidateRelayCommands();
CloseStatus();
}
});
}
private void Commit()
{
OpenStatus("Committing difference...");
Task.Factory.StartNew(() =>
{
try
{
Thread.Sleep(1500);
SelectedDifference.Commit();
InvokeUINow(() => Differences.Remove(SelectedDifference));
}
catch (Exception ex)
{
ShowError(ex.Message);
}
finally
{
SelectedDifference = null;
InvalidateRelayCommands();
CloseStatus();
}
});
}
private void CommitAll()
{
OpenStatus("Committing all differences...");
Task.Factory.StartNew(() =>
{
try
{
Thread.Sleep(1500);
for (int i = 0; i < Differences.Count; i++)
{
var diff = Differences[i];
OpenStatus("Committing difference " + (Differences.IndexOf(diff) + 1) + "...");
diff.Commit();
InvokeUINow(() => Differences.Remove(diff));
i--;
}
}
catch (Exception ex)
{
ShowError(ex.Message);
}
finally
{
SelectedDifference = null;
InvalidateRelayCommands();
CloseStatus();
}
});
}
private void BrowseSlaveDB()
{
String file = BrowseForFilePath();
if (file != null)
{
_slaveDBFile = file;
SlaveDBName = Path.GetFileName(file);
InvalidateRelayCommands();
}
}
private void BrowseMasterDB()
{
String file = BrowseForFilePath();
if (file != null)
{
_masterDBFile = file;
MasterDBName = Path.GetFileName(file);
InvalidateRelayCommands();
}
}
private String BrowseForFilePath()
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Select SQLite DataBase File";
dlg.Filter = "SQLite DataBase|*.db";
if (dlg.ShowDialog().Value)
{
return dlg.FileName;
}
return null;
}
private void OpenStatus(String status)
{
IsBusy = true;
Status = status;
}
private void CloseStatus()
{
IsBusy = false;
}
private void ShowError(String message)
{
MessageBox.Show(message, "Tango", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.No, MessageBoxOptions.DefaultDesktopOnly);
}
private void ShowInfo(String message)
{
MessageBox.Show(message, "Tango", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.No, MessageBoxOptions.DefaultDesktopOnly);
}
#endregion
}
}
|