Jonathan Jenne 2579d7abdd 11-08-2023
2023-08-11 09:45:34 +02:00

135 lines
5.2 KiB
VB.net

Imports System.ComponentModel
Imports System.IO
Imports System.Threading.Tasks
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Namespace Sharepoint
Public Class SharepointSync
Inherits BaseModule
Implements ISync
Private Const STRING1_PLACEHOLDER = "[String1]"
Public Overrides Property Name As String = "Sharepoint Sync"
Public Overrides Property IsLoggedIn As Boolean
Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer, pConfig As Config)
MyBase.New(pLogConfig, pDatabase, pConfig)
End Sub
Public Overrides Async Function Run() As Task
Try
AddInfoEntry("Starting SharepointSync.")
AddDivider()
EnsureOutputDirectoryExists()
Dim oExtDocIds = Await FetchDocIds()
If oExtDocIds Is Nothing Then
Throw New ApplicationException($"Document Ids could not be fetched!")
End If
For Each oDocId As String In oExtDocIds
Try
Logger.Debug("Fetching document from Database..")
Dim oDocument = Await GetDocumentContent(oDocId)
If oDocument Is Nothing Then
Throw New SharepointException(Constants.ErrorType.GetDocumentError, "Document was not found!")
End If
Logger.Debug("Document fetched!")
AddInfoEntry("Document: [{0}]", oDocument.Name)
Logger.Info("ExtDocId: [{0}]", oDocument.Id)
Dim oFileName = ConvertFilenameToSlug(oDocument.Name)
Dim oSubPath = FileEx.CreateDateDirectory(Config.OutputDirectory)
If oSubPath Is Nothing Then
Throw New ApplicationException("Output sub path could not be created!")
End If
Dim oFilePath = Path.Combine(oSubPath, oFileName)
Logger.Debug("Subdirectory [{0}] created.", oSubPath)
If CopyFileToOutputPath(oDocument.Data, oFilePath) = False Then
Throw New ApplicationException("File could not be created in output path!")
End If
Dim oSQL = String.Format(Config.SQLQueryExport, oDocument.Id, oFileName)
If oSQL.Contains(STRING1_PLACEHOLDER) Then
oSQL.Replace(STRING1_PLACEHOLDER, oDocument.Path)
End If
Await Database.ExecuteNonQueryAsync(oSQL)
Catch ex As Exception
Logger.Error(ex)
AddWarnEntry("Error while running Sync: " & ex.Message)
End Try
Next
'TODO
AddInfoEntry("Finished Sync.")
Catch ex As Exception
Logger.Error(ex)
AddWarnEntry("Error while running Sync: " & ex.Message)
End Try
End Function
Public Overrides Function Cleanup() As Task
Return Task.CompletedTask
End Function
Public Overrides Function TestConfigIsComplete() As Boolean
Dim oComplete = TestConfigIsCompleteBase()
If Config.SharepointConfiguration.SharepointDatabase = String.Empty Then
AddWarnEntry("Configuration for 'SharepointDatabase' is empty.")
oComplete = False
End If
Return oComplete
End Function
Private Async Function GetDocumentContent(pDocumentId As String) As Task(Of Entities.SharepointDocument)
Try
Dim oSql As String = $"SELECT T.LeafName, T.DirName, T2.Content
FROM {Config.SharepointConfiguration.SharepointDatabase}.[AllDocs] T
INNER JOIN {Config.SharepointConfiguration.SharepointDatabase}.[AllDocStreams] T2
ON T.Id = T2.Id AND T.InternalVersion = T2.InternalVersion AND T.SiteId = T2.SiteId
WHERE T.Id = '{pDocumentId}'"
Dim oTable As DataTable = Database.GetDatatable(oSql)
If oTable Is Nothing OrElse oTable.Rows.Count = 0 Then
Logger.Warn("Document with Id [{0}] was not found in SharePoint Database!", pDocumentId)
Return Nothing
End If
Dim oRow = oTable.Rows.Item(0)
Dim oFilename As String = oRow.ItemEx("LeafName", "")
Dim oPath As String = oRow.ItemEx("DirName", "")
Dim oContent As Byte() = CType(oRow.Item("Content"), Byte())
Return New Entities.SharepointDocument() With {
.Id = pDocumentId,
.Name = oFilename,
.Path = oPath,
.Data = oContent
}
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
End Class
End Namespace