diff --git a/GUIs.Common/DocumentResultList/Watcher.vb b/GUIs.Common/DocumentResultList/Watcher.vb index 12be8da1..a91de14e 100644 --- a/GUIs.Common/DocumentResultList/Watcher.vb +++ b/GUIs.Common/DocumentResultList/Watcher.vb @@ -28,6 +28,7 @@ Namespace DocumentResultList Private ReadOnly ProcessedFiles As New List(Of OpenFile) Public Event FileChanged As EventHandler(Of FileChangedArgs) + Public Event FileOpened As EventHandler(Of FileOpenedArgs) Public Class OpenFile Public Document As Document @@ -40,6 +41,10 @@ Namespace DocumentResultList Public File As OpenFile End Class + Public Class FileOpenedArgs + Public File As OpenFile + End Class + Public Sub New(pLogConfig As LogConfig) MyBase.New(pLogConfig) FileEx = New Modules.Filesystem.File(pLogConfig) @@ -67,14 +72,19 @@ Namespace DocumentResultList Logger.Debug("File [{0}] opened with ProcessId [{1}]", oFilePath, oProcess.Id) - OpenFiles.Add(New OpenFile With { + Dim oOpenFile = New OpenFile With { .Document = pDocument, .FilePath = oFilePath, .ProcessId = oProcess.Id - }) + } + OpenFiles.Add(oOpenFile) + RaiseEvent FileOpened(Me, New FileOpenedArgs With {.File = oOpenFile}) If FileOpenTimer.Enabled = False Then + ' Waiting a while before actually starting the timer to allow for + ' opening the file without checking for use already. + Await Task.Delay(FILE_OPEN_HANDLE_INTERVAL) FileOpenTimer.Interval = FILE_OPEN_HANDLE_INTERVAL FileOpenTimer.Start() End If diff --git a/GUIs.Common/frmDocumentResultList.vb b/GUIs.Common/frmDocumentResultList.vb index 910cea49..6b02393c 100644 --- a/GUIs.Common/frmDocumentResultList.vb +++ b/GUIs.Common/frmDocumentResultList.vb @@ -14,10 +14,12 @@ Imports DevExpress.XtraGrid.Views.Grid.ViewInfo Imports DevExpress.XtraPrinting Imports DigitalData.Modules.Config Imports DigitalData.Modules.EDMI.API +Imports DigitalData.Modules.EDMI.API.EDMIServiceReference Imports DigitalData.Modules.Language Imports DigitalData.Modules.Logging Imports DigitalData.Modules.ZooFlow Imports DigitalData.Modules.ZooFlow.Constants +Imports DigitalData.Modules.Base.IDB.FileStore Public Class frmDocumentResultList Implements IResultForm @@ -236,10 +238,17 @@ Public Class frmDocumentResultList End Try End Sub + Public Async Sub Watcher_FileOpened(sender As Object, e As DocumentResultList.Watcher.FileOpenedArgs) Handles Watcher.FileOpened + Await _IDBClient.SetObjectStateAsync(e.File.Document.Id, OBJECT_STATE_FILE_OPENED, New Options.SetObjectStateOptions With { + .Language = Environment.User.Language, + .Username = Environment.User.UserName + }) + End Sub + Public Async Sub Watcher_FileChanged(sender As Object, e As DocumentResultList.Watcher.FileChangedArgs) Handles Watcher.FileChanged Try - Dim oDoctype = Nothing + Dim oDoctype As GlobalStateDoctype = Nothing If e.File.Document.DocumentType IsNot Nothing Then oDoctype = _IDBClient.ClientConfig.DocumentTypes. @@ -247,33 +256,26 @@ Public Class frmDocumentResultList FirstOrDefault() End If - Dim oFileInfo = New FileInfo(e.File.FilePath) - Dim oMessage = $"Die Datei '{oFileInfo.Name}' wurde außerhalb des Systems verändert. Wollen Sie diese Änderung als neue Version in das System übernehmen? 'Nein' überschreibt die ursprüngliche Datei." - Dim oResult As DialogResult = MsgBox(oMessage, MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Question, "Datei verändert") + If oDoctype IsNot Nothing Then + Select Case oDoctype.FileChangedAction + Case FILE_CHANGED_OVERWRITE + Await Watcher_OverwriteFile(e.File) - ' Three possibilities: - ' - ' - Yes: Version file - ' - No: Overwrite file - ' - Cancel: Abort update - Select Case oResult - Case DialogResult.Cancel - ' MsgBox("Abbruch!") + Case FILE_CHANGED_VERSION + Await Watcher_VersionFile(e.File) - Case Else - Dim oCreateNewFileVersion = IIf(oResult = DialogResult.Yes, True, False) - Dim oObjectId = Await _IDBClient.UpdateFileAsync(e.File.Document.Id, e.File.FilePath, New Options.UpdateFileOptions With { - .CreateNewFileVersion = oCreateNewFileVersion, - .Language = Environment.User.Language, - .Username = Environment.User.UserName - }) + Case FILE_CHANGED_QUESTION + Await Watcher_Ask(e.File) - If IsNothing(oObjectId) Then - MsgBox($"Beim Speichern der Datei '{oFileInfo.Name}' Fehler ist ein Fehler aufgetreten!", MsgBoxStyle.Critical, Text) - Else - MsgBox($"Die Datei '{oFileInfo.Name}' wurde erfolgreich gespeichert!", MsgBoxStyle.Information, Text) - End If - End Select + Case Else + Await Watcher_Ask(e.File) + + End Select + + Else + Await Watcher_Ask(e.File) + + End If Catch ex As Exception Logger.Error(ex) @@ -286,6 +288,56 @@ Public Class frmDocumentResultList End Try End Sub + Private Async Function Watcher_OverwriteFile(pFile As DocumentResultList.Watcher.OpenFile) As Task + Await Watcher_UpdateFile(pFile, pCreateNewVersion:=False) + Await _IDBClient.SetObjectStateAsync(pFile.Document.Id, OBJECT_STATE_FILE_CHANGED, New Options.SetObjectStateOptions With { + .Language = Environment.User.Language, + .Username = Environment.User.UserName + }) + End Function + + Private Async Function Watcher_VersionFile(pFile As DocumentResultList.Watcher.OpenFile) As Task + Await Watcher_UpdateFile(pFile, pCreateNewVersion:=True) + Await _IDBClient.SetObjectStateAsync(pFile.Document.Id, OBJECT_STATE_FILE_VERSIONED, New Options.SetObjectStateOptions With { + .Language = Environment.User.Language, + .Username = Environment.User.UserName + }) + End Function + + Private Async Function Watcher_UpdateFile(pFile As DocumentResultList.Watcher.OpenFile, pCreateNewVersion As Boolean) As Task + Dim oFileInfo As New FileInfo(pFile.FilePath) + Dim oObjectId = Await _IDBClient.UpdateFileAsync(pFile.Document.Id, pFile.FilePath, New Options.UpdateFileOptions With { + .CreateNewFileVersion = pCreateNewVersion, + .Language = Environment.User.Language, + .Username = Environment.User.UserName + }) + + If IsNothing(oObjectId) Then + MsgBox($"Beim Speichern der Datei '{oFileInfo.Name}' Fehler ist ein Fehler aufgetreten!", MsgBoxStyle.Critical, Text) + Else + MsgBox($"Die Datei '{oFileInfo.Name}' wurde erfolgreich gespeichert!", MsgBoxStyle.Information, Text) + End If + End Function + + Private Async Function Watcher_Ask(pFile As DocumentResultList.Watcher.OpenFile) As Task + Dim oFileInfo = New FileInfo(pFile.FilePath) + Dim oMessage = $"Die Datei '{oFileInfo.Name}' wurde außerhalb des Systems verändert. Wollen Sie diese Änderung als neue Version in das System übernehmen? 'Nein' überschreibt die ursprüngliche Datei." + Dim oResult As DialogResult = MsgBox(oMessage, MsgBoxStyle.YesNoCancel Or MsgBoxStyle.Question, "Datei verändert") + + Select Case oResult + Case DialogResult.Yes + Await Watcher_VersionFile(pFile) + + Case DialogResult.No + Await Watcher_OverwriteFile(pFile) + + Case Else + ' Cancel, do nothing + + End Select + + End Function + Private Function InitAppServer() As Boolean Dim oSplit As List(Of String) = Environment.Service.Address.Split(":").ToList()