Add attachment subfolder for success and error directories

This commit is contained in:
Jonathan Jenne 2019-06-28 14:42:53 +02:00
parent a57f0c6b9b
commit 8a30f645f8
3 changed files with 41 additions and 8 deletions

View File

@ -20,6 +20,7 @@ Public Class ThreadRunner
Private _successDirectory As String Private _successDirectory As String
Private _errorDirectory As String Private _errorDirectory As String
Private _originalEmailDirectory As String Private _originalEmailDirectory As String
Private _attachmentDirectory As String
Private _zugferd As ZUGFeRDInterface Private _zugferd As ZUGFeRDInterface
Private _jobArguments As WorkerArgs Private _jobArguments As WorkerArgs
Private _mssql As MSSQLServer Private _mssql As MSSQLServer
@ -148,6 +149,9 @@ Public Class ThreadRunner
Case ZUGFERD_REJECTED_EML Case ZUGFERD_REJECTED_EML
args.RejectedEmailDirectory = row.Item("FOLDER_PATH") args.RejectedEmailDirectory = row.Item("FOLDER_PATH")
Case ZUGFERD_ATTACHMENTS
args.AttachmentsSubDirectory = row.Item("FOLDER_PATH")
End Select End Select
Next Next

View File

@ -20,6 +20,7 @@ Public Class ImportZUGFeRDFiles
Public Const ZUGFERD_SUCCESS = "ZUGFeRD Success" Public Const ZUGFERD_SUCCESS = "ZUGFeRD Success"
Public Const ZUGFERD_EML = "ZUGFeRD Eml" Public Const ZUGFERD_EML = "ZUGFeRD Eml"
Public Const ZUGFERD_REJECTED_EML = "ZUGFeRD Eml Rejected" Public Const ZUGFERD_REJECTED_EML = "ZUGFeRD Eml Rejected"
Public Const ZUGFERD_ATTACHMENTS = "ZUGFeRD Attachments"
Private _logger As Logger Private _logger As Logger
Private _logConfig As LogConfig Private _logConfig As LogConfig
@ -226,7 +227,10 @@ Public Class ImportZUGFeRDFiles
Dim oZUGFeRDCount As Integer = 0 Dim oZUGFeRDCount As Integer = 0
' Set the default Move Directory ' Set the default Move Directory
Dim oMoveDirectory As String = args.SuccessDirectory Dim oMoveDirectory As String = args.SuccessDirectory
' Create file lists
Dim oFileGroupFiles As List(Of FileInfo) = oFileGroup.Value Dim oFileGroupFiles As List(Of FileInfo) = oFileGroup.Value
Dim oFileAttachmentFiles As New List(Of FileInfo)
Dim oFileGroupId As String = oFileGroup.Key Dim oFileGroupId As String = oFileGroup.Key
Dim oMissingProperties As New List(Of String) Dim oMissingProperties As New List(Of String)
Dim oMD5CheckSum As String = String.Empty Dim oMD5CheckSum As String = String.Empty
@ -248,6 +252,7 @@ Public Class ImportZUGFeRDFiles
' Only pdf files are allowed from here on ' Only pdf files are allowed from here on
If Not oFile.Name.EndsWith(".pdf") Then If Not oFile.Name.EndsWith(".pdf") Then
_logger.Debug("Skipping non-pdf file {0}", oFile.Name) _logger.Debug("Skipping non-pdf file {0}", oFile.Name)
oFileAttachmentFiles.Add(oFile)
Continue For Continue For
End If End If
@ -257,6 +262,7 @@ Public Class ImportZUGFeRDFiles
oDocument = _zugferd.ExtractZUGFeRDFile(oFile.FullName) oDocument = _zugferd.ExtractZUGFeRDFile(oFile.FullName)
Catch ex As Exception Catch ex As Exception
_logger.Warn("File is not a valid ZUGFeRD document! Skipping.") _logger.Warn("File is not a valid ZUGFeRD document! Skipping.")
oFileAttachmentFiles.Add(oFile)
Continue For Continue For
End Try End Try
@ -509,14 +515,16 @@ Public Class ImportZUGFeRDFiles
oConnection.Close() oConnection.Close()
' Move all files of the current group ' Move all files of the current group
For Each oFile In oFileGroupFiles Try
_filesystem.MoveTo(oFile.FullName, oMoveDirectory) MoveFiles(args, oFileGroupFiles, oFileAttachmentFiles, oMoveDirectory)
_logger.Info("Finished processing file {0}", oFile.Name) _logger.Info("Finished processing file group {0}", oFileGroupId)
_logger.Info("File moved to {0}", oMoveDirectory) Catch ex As Exception
Next _logger.Warn("Could not move files!")
_logger.Error(ex)
_logger.Info("Finished processing file group {0}", oFileGroupId) Throw ex
_logger.EndBlock() Finally
_logger.EndBlock()
End Try
End Try End Try
Next Next
End If End If
@ -529,6 +537,25 @@ Public Class ImportZUGFeRDFiles
End Try End Try
End Sub End Sub
Private Sub MoveFiles(Args As WorkerArgs, Files As List(Of FileInfo), AttachmentFiles As List(Of FileInfo), MoveDirectory As String)
For Each oFile In Files
Dim oFinalMoveDirectory As String = MoveDirectory
If AttachmentFiles.Contains(oFile) Then
oFinalMoveDirectory = Path.Combine(MoveDirectory, Args.AttachmentsSubDirectory)
If Not Directory.Exists(oFinalMoveDirectory) Then
Directory.CreateDirectory(oFinalMoveDirectory)
End If
End If
_filesystem.MoveTo(oFile.FullName, oFinalMoveDirectory)
_logger.Info("Finished processing file {0}", oFile.Name)
_logger.Info("File moved to {0}", oFinalMoveDirectory)
Next
End Sub
Private Function CreateBodyForMissingProperties(OriginalFilename As String, MissingProperties As List(Of String)) Private Function CreateBodyForMissingProperties(OriginalFilename As String, MissingProperties As List(Of String))
Dim oBody = $"<p>The following file is not ZUGFeRD-compliant: {OriginalFilename}</p>" Dim oBody = $"<p>The following file is not ZUGFeRD-compliant: {OriginalFilename}</p>"

View File

@ -6,6 +6,7 @@ Public Class WorkerArgs
Public ErrorDirectory As String Public ErrorDirectory As String
Public OriginalEmailDirectory As String Public OriginalEmailDirectory As String
Public RejectedEmailDirectory As String Public RejectedEmailDirectory As String
Public AttachmentsSubDirectory As String
Public PropertyMap As Dictionary(Of String, XmlItemProperty) Public PropertyMap As Dictionary(Of String, XmlItemProperty)
Public InsertIntoSQLServer As Boolean Public InsertIntoSQLServer As Boolean
@ -15,6 +16,7 @@ Public Class WorkerArgs
ErrorDirectory = Nothing ErrorDirectory = Nothing
OriginalEmailDirectory = Nothing OriginalEmailDirectory = Nothing
RejectedEmailDirectory = Nothing RejectedEmailDirectory = Nothing
AttachmentsSubDirectory = Nothing
PropertyMap = New Dictionary(Of String, XmlItemProperty) PropertyMap = New Dictionary(Of String, XmlItemProperty)
InsertIntoSQLServer = False InsertIntoSQLServer = False
End Sub End Sub