/
HistoryItemVM.vb
413 lines (337 loc) · 15.4 KB
/
HistoryItemVM.vb
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Input
Imports ComplianceManager.Model
Imports Microsoft.Win32
Imports SPS.ViewModel.Infrastructure
Public Class HistoryItemVM
Inherits ViewModelBase
Friend _historyItem As Model.HistoryItem
Public settPath As String
Public saveFolder As String = settPath & "\" & Now.Year & "\"
Private _db As Context.CompContext
Private IsUserAdmin As Boolean = False
Public Event Refresh()
Private AreThereChanges As Boolean = False
Public Sub New()
If DesignerProperties.GetIsInDesignMode(New DependencyObject) Then
_historyItem = New HistoryItem() With {.CreatedBy = "User", .CreationDate = Now, .Description = "Description of HistoryItemVM", .LastChange = Now, .LastEditedBy = "User", .Title = "Title"}
_historyItem.Attachments = New List(Of Model.ComplianteAttachment)
_historyItem.Attachments.Add(New ComplianteAttachment() With {.CreatedBy = "User", .CreationDate = Now, .LastChange = Now, .LastEditedBy = "User", .Title = "Test1"})
_historyItem.Attachments.Add(New ComplianteAttachment() With {.CreatedBy = "User", .CreationDate = Now, .LastChange = Now, .LastEditedBy = "User", .Title = "Test2", .RelativeFilePath = "Test2.png", .IsDeleted = True})
_historyItem.Attachments.Add(New ComplianteAttachment() With {.CreatedBy = "User", .CreationDate = Now, .LastChange = Now, .LastEditedBy = "User", .Title = "Test3", .RelativeFilePath = "Test3.png", .IsDeleted = True})
Else
_historyItem = New Model.HistoryItem
AviableUsers = _db.Users.Where(Function(u) u.IsActive = True).ToList
'Using settDb As New Context.CompContext
' settPath = settDb.Settings.Where(Function(s) s.Key = "AttachmentsPath").FirstOrDefault.Value
'End Using
'If _historyItem.Attachments Is Nothing Then _historyItem.Attachments = New List(Of ComplianteAttachment)
AreThereChanges = False
End If
End Sub
Public Sub New(hItem As Model.HistoryItem, db As Context.CompContext)
_historyItem = hItem
If _historyItem.Attachments Is Nothing Then _historyItem.Attachments = New List(Of ComplianteAttachment)
_db = db
Using settDb As New Context.CompContext
settPath = settDb.Settings.Where(Function(s) s.Key = "AttachmentsPath").FirstOrDefault.Value
IsUserAdmin = settDb.Users.Where(Function(u) u.UserName = Environment.UserName).First.IsAdmin
End Using
AviableUsers = _db.Users.Where(Function(u) u.IsActive = True).ToList
AreThereChanges = False
End Sub
Public Sub RefreshView()
RaisePropertyChanged("Attachments")
RaisePropertyChanged("Attachments")
RaisePropertyChanged("Attachments")
RaiseEvent Refresh()
End Sub
Private _aviableUsers As List(Of Model.User)
Public Property AviableUsers() As List(Of Model.User)
Get
Return _aviableUsers
End Get
Set(ByVal value As List(Of Model.User))
_aviableUsers = value
End Set
End Property
Public Property CreationDate As DateTime
Get
Return _historyItem.CreationDate
End Get
Set(value As DateTime)
_historyItem.CreationDate = value
AreThereChanges = True
RaisePropertyChanged("CreationDate")
RaisePropertyChanged("CreatedString")
End Set
End Property
Public ReadOnly Property CreatedString As String
Get
Return String.Format("Erstellt am {0} durch {1}", CreationDate.ToShortDateString, CreatedBy)
End Get
End Property
Public ReadOnly Property ChangedString As String
Get
Return String.Format("Zuletzt geändert durch {0} am {1}", LastEditedBy, LastChange.ToShortDateString)
End Get
End Property
Public Property CreatedBy As String
Get
Return _historyItem.CreatedBy
End Get
Set(value As String)
_historyItem.CreatedBy = value
RaisePropertyChanged("CreatedBy")
RaisePropertyChanged("CreatedString")
AreThereChanges = True
End Set
End Property
Public Property LastEditedBy As String
Get
Return _historyItem.LastEditedBy
End Get
Set(value As String)
_historyItem.LastEditedBy = value
RaisePropertyChanged("LastEditedBy")
RaisePropertyChanged("ChangedString")
AreThereChanges = True
End Set
End Property
Public Property LastChange As DateTime
Get
Return _historyItem.LastChange
End Get
Set(value As DateTime)
_historyItem.LastChange = value
RaisePropertyChanged("LastChange")
RaisePropertyChanged("ChangedString")
AreThereChanges = True
End Set
End Property
Public Property Title As String
Get
Return _historyItem.Title
End Get
Set(value As String)
_historyItem.Title = value
RaisePropertyChanged("Title")
AreThereChanges = True
End Set
End Property
Public Property Description As String
Get
Return _historyItem.Description
End Get
Set(value As String)
_historyItem.Description = value
RaisePropertyChanged("Description")
AreThereChanges = True
End Set
End Property
' Private _attachments As ObservableCollection(Of ComplianteAttachment)
Public Property Attachments As ICollection(Of ComplianteAttachment)
Get
Return _historyItem.Attachments
End Get
Set(value As ICollection(Of ComplianteAttachment))
_historyItem.Attachments = value
RaisePropertyChanged("Attachments")
AreThereChanges = True
End Set
End Property
'Private _addItemCommand As ICommand
'Public Property AddItemCommand() As ICommand
' Get
' If _addItemCommand Is Nothing Then _addItemCommand = New RelayCommand(AddressOf AddItemCommand_Execute, AddressOf AddItemCommand_CanExecute)
' Return _addItemCommand
' End Get
' Set(ByVal value As ICommand)
' _addItemCommand = value
' RaisePropertyChanged("AddItemCommand")
' End Set
'End Property
'Private Function AddItemCommand_CanExecute(obj As Object) As Boolean
' Return True
'End Function
'Private Sub AddItemCommand_Execute(obj As Object)
' Using settDb As New Context.CompContext
' Dim settPath As String = settDb.Settings.Where(Function(s) s.Key = "AttachmentsPath").FirstOrDefault.Value
' Dim saveFolder As String = settPath & "\" & Now.Year & "\"
' If Not IO.Directory.Exists(saveFolder) Then IO.Directory.CreateDirectory(saveFolder)
' Dim ofDiag As New Forms.OpenFileDialog
' ofDiag.Multiselect = True
' ofDiag.RestoreDirectory = True
' ofDiag.Title = "Wählen Sie die Dateien welches Sie hinzufügen möchten..."
' If Attachments Is Nothing Then Attachments = New List(Of Model.ComplianteAttachment)
' Attachments.Add(New ComplianteAttachment())
' If ofDiag.ShowDialog = Forms.DialogResult.OK Then
' If Attachments Is Nothing Then Attachments = New List(Of Model.ComplianteAttachment)
' For Each f In ofDiag.FileNames
' Dim fi As IO.FileInfo = New IO.FileInfo(f)
' Attachments.Add(New ComplianteAttachment() _
' With {.Title = fi.Name, .RelativeFilePath = Now.Year & "\" & fi.Name, .CreatedBy = Environment.UserName, .LastChange = Environment.UserName})
' Next
' End If
' End Using
'End Sub
Private _editCommand As ICommand
Public Property EditCommand() As ICommand
Get
If _editCommand Is Nothing Then _editCommand = New RelayCommand(AddressOf EditCommand_Execute, AddressOf EditCommand_CanExecute)
Return _editCommand
End Get
Set(ByVal value As ICommand)
_editCommand = value
RaisePropertyChanged("EditCommand")
AreThereChanges = True
End Set
End Property
Private Function EditCommand_CanExecute(obj As Object) As Boolean
Return Environment.UserName = Me.CreatedBy OrElse IsUserAdmin
End Function
Private Sub EditCommand_Execute(obj As Object)
' Using db As New Context.CompContext
Dim win As New MahApps.Metro.Controls.MetroWindow
win.Title = "Historie bearbeiten..."
win.Width = 800
win.Height = 600
win.WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen
win.DataContext = Me
win.Content = New ContentPresenter With {.Content = Me}
If win.ShowDialog Then
If Attachments Is Nothing Then Attachments = New List(Of ComplianteAttachment)
If AreThereChanges Then
LastChange = Now
LastEditedBy = Environment.UserName
End If
Dim anz As Integer = _db.SaveChanges()
RefreshView()
RaiseEvent Refresh()
End If
' End Using
End Sub
Private _addAttachmentCommand As ICommand
Public Property AddAttachmentCommand() As ICommand
Get
If _addAttachmentCommand Is Nothing Then _addAttachmentCommand = New RelayCommand(AddressOf AddAttachmentCommand_execute, AddressOf AddAttachmentCommand_CanExecute)
Return _addAttachmentCommand
End Get
Set(ByVal value As ICommand)
_addAttachmentCommand = value
RaisePropertyChanged("AddAttachmentCommand")
End Set
End Property
Private Function AddAttachmentCommand_CanExecute(obj As Object) As Boolean
Return True
End Function
Private Sub AddAttachmentCommand_execute(obj As Object)
'Using settDb As New Context.CompContext
Dim settPath As String = _db.Settings.Where(Function(s) s.Key = "AttachmentsPath").FirstOrDefault.Value
Dim saveFolder As String = settPath & "\" & Now.Year & "\"
If Not IO.Directory.Exists(saveFolder) Then IO.Directory.CreateDirectory(saveFolder)
Dim ofDiag As New Forms.OpenFileDialog
ofDiag.Multiselect = True
ofDiag.RestoreDirectory = True
ofDiag.Title = "Wählen Sie die Dateien welches Sie hinzufügen möchten..."
If Attachments Is Nothing Then Attachments = New List(Of Model.ComplianteAttachment)
'Attachments.Add(New ComplianteAttachment())
If ofDiag.ShowDialog = Forms.DialogResult.OK Then
If Attachments Is Nothing Then Attachments = New List(Of Model.ComplianteAttachment)
For Each f In ofDiag.FileNames
Dim fi As IO.FileInfo = New IO.FileInfo(f)
Dim NewFilename As String = Now.Ticks & Mid(fi.Name, fi.Name.Length - 3, 4)
IO.File.Copy(fi.FullName, settPath & Now.Year & "\" & NewFilename)
Attachments.Add(New ComplianteAttachment() _
With {.Title = Replace(fi.Name, fi.Extension, ""), .RelativeFilePath = Now.Year & "\" & NewFilename, .CreatedBy = Environment.UserName, .LastEditedBy = Environment.UserName})
Next
End If
'End Using
RefreshView()
RaiseEvent Refresh()
End Sub
Private _deleteAttachmentCommand As ICommand
Public Property DeleteAttachmentcommand() As ICommand
Get
If _deleteAttachmentCommand Is Nothing Then _deleteAttachmentCommand = New RelayCommand(AddressOf DeleteAttachmentcommand_Execute, AddressOf DeleteAttachmentcommand_Canexecute)
Return _deleteAttachmentCommand
End Get
Set(ByVal value As ICommand)
_deleteAttachmentCommand = value
RaisePropertyChanged("DeleteAttachmentcommand")
End Set
End Property
Private Function DeleteAttachmentcommand_Canexecute(obj As Object) As Boolean
Return obj IsNot Nothing AndAlso IsUserAdmin
End Function
Private Sub DeleteAttachmentcommand_Execute(obj As Object)
Attachments.Where(Function(a) a.RelativeFilePath = DirectCast(obj, Model.ComplianteAttachment).RelativeFilePath).First.IsDeleted = True
RefreshView()
RaiseEvent Refresh()
End Sub
Private _dedeleteAttachmentCommand As ICommand
Public Property DeDeleteAttachmentcommand() As ICommand
Get
If _dedeleteAttachmentCommand Is Nothing Then _dedeleteAttachmentCommand = New RelayCommand(AddressOf DeDeleteAttachmentcommand_Execute, AddressOf DeDeleteAttachmentcommand_Canexecute)
Return _dedeleteAttachmentCommand
End Get
Set(ByVal value As ICommand)
_dedeleteAttachmentCommand = value
RaisePropertyChanged("DeDeleteAttachmentcommand")
End Set
End Property
Private Function DeDeleteAttachmentcommand_Canexecute(obj As Object) As Boolean
Return obj IsNot Nothing
End Function
Private Sub DeDeleteAttachmentcommand_Execute(obj As Object)
Attachments.Where(Function(a) a.RelativeFilePath = DirectCast(obj, Model.ComplianteAttachment).RelativeFilePath).First.IsDeleted = False
RefreshView()
RaiseEvent Refresh()
End Sub
Private _removeHistoryItem As ICommand
Public Property RemoveHistoryItem() As ICommand
Get
If _removeHistoryItem Is Nothing Then _removeHistoryItem = New RelayCommand(AddressOf RemoveHistoryItem_Execute, AddressOf RemoveHistoryItem_CanExecute)
Return _removeHistoryItem
End Get
Set(ByVal value As ICommand)
_removeHistoryItem = value
RaisePropertyChanged("RemoveHistoryItem")
End Set
End Property
Private Function RemoveHistoryItem_CanExecute(obj As Object) As Boolean
Return IsUserAdmin AndAlso obj IsNot Nothing
End Function
Private Sub RemoveHistoryItem_Execute(obj As Object)
If MsgBox("Möchtest du den Eintrag wirklich löschen?", MsgBoxStyle.YesNo, "Wirklich löschen?") = MsgBoxResult.Yes Then
_db.HistoryItems.Remove(_db.HistoryItems.Where(Function(i) i.ID = DirectCast(obj, HistoryItemVM)._historyItem.ID).First)
_db.SaveChanges()
RefreshView()
RaiseEvent Refresh()
End If
End Sub
Private _OpenAttachmentCommand As ICommand
Public Property OpenAttachmentCommand() As ICommand
Get
If _OpenAttachmentCommand Is Nothing Then _OpenAttachmentCommand = New RelayCommand(AddressOf OpenAttachmentCommand_execute, AddressOf OpenAttachmentCommand_CanExecute)
Return _OpenAttachmentCommand
End Get
Set(ByVal value As ICommand)
_OpenAttachmentCommand = value
RaisePropertyChanged("OpenAttachmentCommand")
End Set
End Property
Private Function OpenAttachmentCommand_CanExecute(obj As Object) As Boolean
Return obj IsNot Nothing
End Function
Private Sub OpenAttachmentCommand_execute(obj As Object)
Try
Process.Start(settPath & DirectCast(obj, Model.ComplianteAttachment).RelativeFilePath)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkCancel, "Fehler beim laden der Datei")
End Try
End Sub
End Class