slt: Get Document Details from Database
This commit is contained in:
parent
134a4642b2
commit
74a9dbc486
@ -4,8 +4,8 @@
|
|||||||
''' <summary>
|
''' <summary>
|
||||||
''' This connection string needs to point to the database where the document data will be written to
|
''' This connection string needs to point to the database where the document data will be written to
|
||||||
''' </summary>
|
''' </summary>
|
||||||
''' <returns></returns>
|
|
||||||
Public Property ConnectionString As String = ""
|
Public Property ConnectionString As String = ""
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
''' This query must contain two placeholders:
|
''' This query must contain two placeholders:
|
||||||
'''
|
'''
|
||||||
@ -18,6 +18,7 @@
|
|||||||
''' - not used in slt Module
|
''' - not used in slt Module
|
||||||
''' </summary>
|
''' </summary>
|
||||||
Public Property SQLQueryExport As String = ""
|
Public Property SQLQueryExport As String = ""
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
''' This query needs to return a table with exactly one column, which represents the ExtDocId
|
''' This query needs to return a table with exactly one column, which represents the ExtDocId
|
||||||
''' </summary>
|
''' </summary>
|
||||||
@ -37,7 +38,7 @@
|
|||||||
|
|
||||||
Public Class SharepointConfig
|
Public Class SharepointConfig
|
||||||
''' <summary>
|
''' <summary>
|
||||||
''' 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
|
||||||
''' </summary>
|
''' </summary>
|
||||||
Public Property SharepointDatabase As String = ""
|
Public Property SharepointDatabase As String = ""
|
||||||
End Class
|
End Class
|
||||||
@ -63,5 +64,11 @@
|
|||||||
''' System or Mandator to use in slt
|
''' System or Mandator to use in slt
|
||||||
''' </summary>
|
''' </summary>
|
||||||
Public Property SystemId As String = ""
|
Public Property SystemId As String = ""
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Then name of the slt Database. Should contain the tables EXDOCS.
|
||||||
|
''' Used for fetching the file extension if mime-type matching failed
|
||||||
|
''' </summary>
|
||||||
|
Public Property sltDatabase As String = ""
|
||||||
End Class
|
End Class
|
||||||
End Class
|
End Class
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
Namespace slt.Entities
|
Imports System.IO
|
||||||
|
Imports DigitalData.Modules.Base
|
||||||
|
|
||||||
|
Namespace slt.Entities
|
||||||
Public Class sltDocument
|
Public Class sltDocument
|
||||||
Public Property ExtDocId As String
|
Public Property ExtDocId As String
|
||||||
Public Property Name As String
|
Public Property Name As String
|
||||||
@ -11,5 +14,24 @@
|
|||||||
Public Property StorPlaceId As String
|
Public Property StorPlaceId As String
|
||||||
Public Property ExternalId As String
|
Public Property ExternalId As String
|
||||||
Public Property Data As Byte()
|
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 Class
|
||||||
End Namespace
|
End Namespace
|
||||||
|
|||||||
@ -8,6 +8,7 @@ Imports Connectors.Common.slt.Constants
|
|||||||
Imports Connectors.Common.slt.Responses
|
Imports Connectors.Common.slt.Responses
|
||||||
Imports Connectors.Common.slt.Entities
|
Imports Connectors.Common.slt.Entities
|
||||||
Imports System.Threading.Tasks
|
Imports System.Threading.Tasks
|
||||||
|
Imports System.IO
|
||||||
|
|
||||||
Namespace slt
|
Namespace slt
|
||||||
Public Class sltSync
|
Public Class sltSync
|
||||||
@ -46,15 +47,14 @@ Namespace slt
|
|||||||
For Each oDocId As String In oExtDocIds
|
For Each oDocId As String In oExtDocIds
|
||||||
Try
|
Try
|
||||||
Logger.Debug("Fetching document from API..")
|
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!")
|
Logger.Debug("Document fetched!")
|
||||||
|
|
||||||
AddInfoEntry("Document: [{0}]", oDocument.Name)
|
AddInfoEntry("Document: [{0}]", oDocument.Name)
|
||||||
Logger.Info("ExtDocId: [{0}]", oDocument.ExtDocId)
|
Logger.Info("ExtDocId: [{0}]", oDocument.ExtDocId)
|
||||||
|
|
||||||
Dim oDocumentName = GetFilenameWithExtension(oDocument.ExtDocId, oDocument.DocMimeType)
|
Dim oFileName = oDocument.GetUniqueFilename()
|
||||||
Dim oFileName = ConvertFilenameToSlug(oDocumentName)
|
|
||||||
Dim oTempFileName = GetFinalFileName(oFileName, oDocument.ExtDocId)
|
|
||||||
Dim oFilePath = GetFinalFilePath(oFileName)
|
Dim oFilePath = GetFinalFilePath(oFileName)
|
||||||
|
|
||||||
If CopyFileToOutputPath(oDocument.Data, oFilePath) = False Then
|
If CopyFileToOutputPath(oDocument.Data, oFilePath) = False Then
|
||||||
@ -121,10 +121,15 @@ Namespace slt
|
|||||||
oComplete = False
|
oComplete = False
|
||||||
End If
|
End If
|
||||||
|
|
||||||
|
If Config.sltConfiguration.sltDatabase = String.Empty Then
|
||||||
|
AddWarnEntry("Configuration for 'sltDatabase' is empty.")
|
||||||
|
oComplete = False
|
||||||
|
End If
|
||||||
|
|
||||||
Return oComplete
|
Return oComplete
|
||||||
End Function
|
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
|
Try
|
||||||
If pMimetype = "application/outlook" Then
|
If pMimetype = "application/outlook" Then
|
||||||
pMimetype = "application/vnd.ms-outlook"
|
pMimetype = "application/vnd.ms-outlook"
|
||||||
@ -133,9 +138,14 @@ Namespace slt
|
|||||||
Dim oExtension = MimeEx.GetExtension(pMimetype)
|
Dim oExtension = MimeEx.GetExtension(pMimetype)
|
||||||
Return StringEx.ConvertTextToSlug(pFilename) & oExtension
|
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
|
Catch ex As Exception
|
||||||
Logger.Error(ex)
|
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
|
Return pFilename
|
||||||
End Try
|
End Try
|
||||||
@ -207,8 +217,8 @@ Namespace slt
|
|||||||
Try
|
Try
|
||||||
Dim oUrl = "/slt/External/System/Authentication/Json/Logout"
|
Dim oUrl = "/slt/External/System/Authentication/Json/Logout"
|
||||||
Dim oParams = New Dictionary(Of String, String) From {
|
Dim oParams = New Dictionary(Of String, String) From {
|
||||||
{"SessionId", SessionId}
|
{"SessionId", SessionId}
|
||||||
}
|
}
|
||||||
Dim oJson As String = Await SendRequest(oUrl, oParams)
|
Dim oJson As String = Await SendRequest(oUrl, oParams)
|
||||||
Dim oResponse = JsonConvert.DeserializeObject(Of sltLogoutResponse)(oJson)
|
Dim oResponse = JsonConvert.DeserializeObject(Of sltLogoutResponse)(oJson)
|
||||||
|
|
||||||
@ -225,6 +235,32 @@ Namespace slt
|
|||||||
End Try
|
End Try
|
||||||
End Function
|
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)
|
Private Async Function GetDocumentContent(pExternalDocumentId As String) As Threading.Tasks.Task(Of slt.Entities.sltDocument)
|
||||||
If Not IsLoggedIn Then
|
If Not IsLoggedIn Then
|
||||||
Throw New sltException(ErrorType.NotLoggedInError, "No session found")
|
Throw New sltException(ErrorType.NotLoggedInError, "No session found")
|
||||||
|
|||||||
43
Connectors.Test/sltTest.vb
Normal file
43
Connectors.Test/sltTest.vb
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
Imports Connectors.Common.slt.Entities
|
||||||
|
Imports Microsoft.VisualStudio.TestTools.UnitTesting
|
||||||
|
|
||||||
|
Namespace Connectors.Test.slt
|
||||||
|
<TestClass>
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
<TestMethod>
|
||||||
|
Sub GetExtensionTest1()
|
||||||
|
Assert.AreEqual("DOCID.dwg", oDocument1.GetUniqueFilename())
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
<TestMethod>
|
||||||
|
Sub GetExtensionTest2()
|
||||||
|
Assert.AreEqual("", oDocument2.GetUniqueFilename())
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
<TestMethod>
|
||||||
|
Sub GetExtensionTest3()
|
||||||
|
Assert.AreEqual("DOCID.pdf", oDocument3.GetUniqueFilename())
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
End Class
|
||||||
|
|
||||||
|
End Namespace
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user