From 74a9dbc486d777f2bfa1b7936b41cf42d107c532 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Fri, 22 Sep 2023 13:56:40 +0200 Subject: [PATCH] slt: Get Document Details from Database --- Connectors.Common/Config.vb | 11 +++- .../Modules/slt/Entities/sltDocument.vb | 24 ++++++++- Connectors.Common/Modules/slt/sltSync.vb | 52 ++++++++++++++++--- Connectors.Test/sltTest.vb | 43 +++++++++++++++ 4 files changed, 119 insertions(+), 11 deletions(-) create mode 100644 Connectors.Test/sltTest.vb diff --git a/Connectors.Common/Config.vb b/Connectors.Common/Config.vb index eb4083b..93e4835 100644 --- a/Connectors.Common/Config.vb +++ b/Connectors.Common/Config.vb @@ -4,8 +4,8 @@ ''' ''' This connection string needs to point to the database where the document data will be written to ''' - ''' Public Property ConnectionString As String = "" + ''' ''' This query must contain two placeholders: ''' @@ -18,6 +18,7 @@ ''' - not used in slt Module ''' Public Property SQLQueryExport As String = "" + ''' ''' This query needs to return a table with exactly one column, which represents the ExtDocId ''' @@ -37,7 +38,7 @@ Public Class SharepointConfig ''' - ''' The connection string of the sharepoint database. Should contain the tables AllDocs and AllDocStreams + ''' The name of the sharepoint database. Should contain the tables AllDocs and AllDocStreams ''' Public Property SharepointDatabase As String = "" End Class @@ -63,5 +64,11 @@ ''' System or Mandator to use in slt ''' Public Property SystemId As String = "" + + ''' + ''' Then name of the slt Database. Should contain the tables EXDOCS. + ''' Used for fetching the file extension if mime-type matching failed + ''' + Public Property sltDatabase As String = "" End Class End Class diff --git a/Connectors.Common/Modules/slt/Entities/sltDocument.vb b/Connectors.Common/Modules/slt/Entities/sltDocument.vb index 67e801a..dd05c64 100644 --- a/Connectors.Common/Modules/slt/Entities/sltDocument.vb +++ b/Connectors.Common/Modules/slt/Entities/sltDocument.vb @@ -1,4 +1,7 @@ -Namespace slt.Entities +Imports System.IO +Imports DigitalData.Modules.Base + +Namespace slt.Entities Public Class sltDocument Public Property ExtDocId As String Public Property Name As String @@ -11,5 +14,24 @@ Public Property StorPlaceId As String Public Property ExternalId As String Public Property Data As Byte() + + Public Function GetUniqueFilename() As String + Dim oExtension = GetExtension() + If oExtension Is Nothing Then + Return Nothing + End If + + Return ExtDocId & oExtension + End Function + + Private Function GetExtension() As String + If Path.HasExtension(DocOriginalFilename) Then + Return Path.GetExtension(DocOriginalFilename) + ElseIf Path.HasExtension(Name) Then + Return Path.GetExtension(Name) + Else + Return Nothing + End If + End Function End Class End Namespace diff --git a/Connectors.Common/Modules/slt/sltSync.vb b/Connectors.Common/Modules/slt/sltSync.vb index c911ea5..14bc1d1 100644 --- a/Connectors.Common/Modules/slt/sltSync.vb +++ b/Connectors.Common/Modules/slt/sltSync.vb @@ -8,6 +8,7 @@ Imports Connectors.Common.slt.Constants Imports Connectors.Common.slt.Responses Imports Connectors.Common.slt.Entities Imports System.Threading.Tasks +Imports System.IO Namespace slt Public Class sltSync @@ -46,15 +47,14 @@ Namespace slt For Each oDocId As String In oExtDocIds Try Logger.Debug("Fetching document from API..") - Dim oDocument = Await GetDocumentContent(oDocId) + Dim oDocument As sltDocument = Await GetDocumentContent(oDocId) + oDocument = Await GetDocumentDetails(oDocument) Logger.Debug("Document fetched!") AddInfoEntry("Document: [{0}]", oDocument.Name) Logger.Info("ExtDocId: [{0}]", oDocument.ExtDocId) - Dim oDocumentName = GetFilenameWithExtension(oDocument.ExtDocId, oDocument.DocMimeType) - Dim oFileName = ConvertFilenameToSlug(oDocumentName) - Dim oTempFileName = GetFinalFileName(oFileName, oDocument.ExtDocId) + Dim oFileName = oDocument.GetUniqueFilename() Dim oFilePath = GetFinalFilePath(oFileName) If CopyFileToOutputPath(oDocument.Data, oFilePath) = False Then @@ -121,10 +121,15 @@ Namespace slt oComplete = False End If + If Config.sltConfiguration.sltDatabase = String.Empty Then + AddWarnEntry("Configuration for 'sltDatabase' is empty.") + oComplete = False + End If + Return oComplete End Function - Private Function GetFilenameWithExtension(pFilename As String, pMimetype As String) As String + Private Function GetFilenameWithExtensionFromMimeType(pFilename As String, pMimetype As String) As String Try If pMimetype = "application/outlook" Then pMimetype = "application/vnd.ms-outlook" @@ -133,9 +138,14 @@ Namespace slt Dim oExtension = MimeEx.GetExtension(pMimetype) Return StringEx.ConvertTextToSlug(pFilename) & oExtension + Catch ex As ArgumentException + Logger.Error(ex) + Logger.Warn("File [{0}] does not have a valid mimetype [{1}]. Returning null.", pFilename, pMimetype) + Return Nothing + Catch ex As Exception Logger.Error(ex) - AddWarnEntry("File [{0}] does not have a valid mimetype [{1}]. Returning original filename.", pFilename, pMimetype) + AddWarnEntry("Unexpected error while getting extension. Returning original filename.", pFilename, pMimetype) Return pFilename End Try @@ -207,8 +217,8 @@ Namespace slt Try Dim oUrl = "/slt/External/System/Authentication/Json/Logout" Dim oParams = New Dictionary(Of String, String) From { - {"SessionId", SessionId} - } + {"SessionId", SessionId} + } Dim oJson As String = Await SendRequest(oUrl, oParams) Dim oResponse = JsonConvert.DeserializeObject(Of sltLogoutResponse)(oJson) @@ -225,6 +235,32 @@ Namespace slt End Try End Function + Private Async Function GetDocumentDetails(pDocument As sltDocument) As Task(Of sltDocument) + If Not IsLoggedIn Then + Throw New sltException(ErrorType.NotLoggedInError, "No session found") + End If + + Try + Logger.Debug("Fetching document details with ExtDocId [{0}]", pDocument.ExtDocId) + + Dim oSQL = $"SELECT DOCORIGINALNAME + FROM {Config.sltConfiguration.sltDatabase} + WHERE EXT_DOC_ID = {pDocument.ExtDocId}" + Dim oResult As Object = Await Database.GetScalarValueAsync(oSQL) + Dim oFileNameOriginal As String = ObjectEx.NotNull(Of String)(oResult.ToString, "") + + If Not String.IsNullOrEmpty(oFileNameOriginal) Then + pDocument.DocOriginalFilename = oFileNameOriginal + End If + + Return pDocument + + Catch ex As Exception + Logger.Error(ex) + Return pDocument + End Try + End Function + Private Async Function GetDocumentContent(pExternalDocumentId As String) As Threading.Tasks.Task(Of slt.Entities.sltDocument) If Not IsLoggedIn Then Throw New sltException(ErrorType.NotLoggedInError, "No session found") diff --git a/Connectors.Test/sltTest.vb b/Connectors.Test/sltTest.vb new file mode 100644 index 0000000..3c4a9f7 --- /dev/null +++ b/Connectors.Test/sltTest.vb @@ -0,0 +1,43 @@ +Imports Connectors.Common.slt.Entities +Imports Microsoft.VisualStudio.TestTools.UnitTesting + +Namespace Connectors.Test.slt + + Public Class ExtensionTest + Public oDocument1 As New sltDocument With { + .ExtDocId = "DOCID", + .DocOriginalFilename = "Rindenmoos Am Mittelfeld 15 msh alb 2022-05-20.dwg", + .Name = "Rindenmoos Am Mittelfeld 15 msh alb 2022-05-20" + } + + Public oDocument2 As New sltDocument With { + .ExtDocId = "DOCID", + .DocOriginalFilename = "Anhörung städt. Tiefbauamt", + .Name = "Anhörung städt" + } + + Public oDocument3 As New sltDocument With { + .ExtDocId = "DOCID", + .DocOriginalFilename = Nothing, + .Name = "Gutschrift-[TITEL].pdf" + } + + + Sub GetExtensionTest1() + Assert.AreEqual("DOCID.dwg", oDocument1.GetUniqueFilename()) + End Sub + + + Sub GetExtensionTest2() + Assert.AreEqual("", oDocument2.GetUniqueFilename()) + End Sub + + + Sub GetExtensionTest3() + Assert.AreEqual("DOCID.pdf", oDocument3.GetUniqueFilename()) + End Sub + + End Class + +End Namespace +