124 lines
4.9 KiB
VB.net
124 lines
4.9 KiB
VB.net
Imports System.Threading.Tasks
|
|
Imports DigitalData.Modules.Base
|
|
Imports DigitalData.Modules.Database
|
|
Imports DigitalData.Modules.Logging
|
|
Namespace Sharepoint
|
|
Public Class SharepointSync
|
|
Inherits BaseModule
|
|
Implements ISync
|
|
|
|
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()
|
|
RefreshDirectoryGuid()
|
|
|
|
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 oTempFileName = GetFinalFileName(oDocument.Name, oDocument.Id)
|
|
Dim oFilePath = GetFinalFilePath(oTempFileName)
|
|
|
|
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 Await Database.ExecuteNonQueryAsync(oSQL) = True Then
|
|
RaiseFileProcessed(oFilePath)
|
|
Else
|
|
Throw New ApplicationException("Database entry could not be written!")
|
|
End If
|
|
|
|
Catch ex As Exception
|
|
RaiseFileError(oDocId)
|
|
|
|
Logger.Error(ex)
|
|
AddWarnEntry("Error while running Sync: " & ex.Message)
|
|
End Try
|
|
Next
|
|
|
|
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 = Await Database.GetDatatableAsync(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 |