Jonathan Jenne 7c31ccee1c 09-08-2023
2023-08-09 11:57:43 +02:00

308 lines
9.7 KiB
VB.net

Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.IO
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports EnvelopeGenerator.Common
Imports EnvelopeGenerator.Common.Constants
Public Class EnvelopeEditorController
Inherits BaseClass
Private ReadOnly Database As MSSQLServer
Private ReadOnly State As State = Nothing
Private EnvelopeModel As EnvelopeModel
Private DocumentModel As DocumentModel
Private ReceiverModel As ReceiverModel
Private ElementModel As ElementModel
Private HistoryModel As HistoryModel
Private UserModel As UserModel
Public ReadOnly Envelope As Envelope = Nothing
Public Sub New(pState As State)
MyBase.New(pState.LogConfig)
Database = pState.Database
State = pState
InitializeModels(pState)
Envelope = CreateEnvelope()
End Sub
Public Sub New(pState As State, pEnvelope As Envelope)
MyBase.New(pState.LogConfig)
Database = pState.Database
State = pState
InitializeModels(pState)
Envelope = pEnvelope
Envelope.Documents = DocumentModel.List(pEnvelope.Id)
Envelope.Receivers = ReceiverModel.ListEnvelopeReceivers(pEnvelope.Id)
End Sub
Private Sub InitializeModels(pState As State)
EnvelopeModel = New EnvelopeModel(pState)
DocumentModel = New DocumentModel(pState)
ReceiverModel = New ReceiverModel(pState)
ElementModel = New ElementModel(pState)
HistoryModel = New HistoryModel(pState)
UserModel = New UserModel(pState)
End Sub
#Region "Public"
Public Function CreateEnvelope() As Envelope
Dim oEnvelope As New Envelope() With {
.UserId = State.UserId,
.User = UserModel.SelectUser()
}
If EnvelopeModel.Insert(oEnvelope) Then
Dim newHistoryEntry As New EnvelopeHistoryEntry With {
.EnvelopeId = oEnvelope.Id,
.Status = HistoryStatus.Created,
.ActionTitle = "Envelope erzeugt",
.ActionDescription = "Envelope wurde erzeugt"
}
'TODO .UserEmailAddress = oEnvelope.User.Email ' TODO - fehlt noch
Return oEnvelope
Else
Return Nothing
End If
End Function
Public Function SaveEnvelope(pEnvelope As Envelope) As Boolean
Dim oConnection = Database.GetConnection()
Dim oTransaction = oConnection.BeginTransaction(IsolationLevel.ReadUncommitted)
Try
pEnvelope.Status = EnvelopeStatus.Saved
If SaveEnvelopeDocumentsToFilesystem(pEnvelope) = False Then
Throw New ApplicationException
End If
If EnvelopeModel.Update(pEnvelope, oTransaction) = False Then
Throw New ApplicationException
End If
If SaveEnvelopeReceivers(pEnvelope, oTransaction) = False Then
Throw New ApplicationException
End If
If SaveEnvelopeDocuments(pEnvelope, oTransaction) = False Then
Throw New ApplicationException
End If
oTransaction.Commit()
Return True
Catch ex As Exception
Logger.Error(ex)
oTransaction.Rollback()
Return False
End Try
End Function
Public Function CleanupEnvelope() As Boolean
If Envelope.Status = Common.Constants.EnvelopeStatus.Created Then
'TODO: Delete Documents and Receivers and elements
Return EnvelopeModel.Delete(Envelope)
Else
Return True
End If
End Function
Public Function CreateDocument(pDocumentFilePath As String) As EnvelopeDocument
Try
Dim oFileInfo = New FileInfo(pDocumentFilePath)
Dim oTempDirectoryPath = Path.GetTempPath()
Dim oTempFilePath = Path.Combine(oTempDirectoryPath, oFileInfo.Name)
File.Copy(oFileInfo.FullName, oTempFilePath)
Dim oDocument = New EnvelopeDocument() With {
.FileInfo = New FileInfo(oTempFilePath)
}
Return oDocument
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Public Function DeleteDocument(pDocument As EnvelopeDocument) As Boolean
Dim oSql = "DELETE FROM [dbo].[TBSIG_ENVELOPE_DOCUMENT] WHERE FILENAME = @FILENAME AND ENVELOPE_ID = @ENVELOPE_ID"
Dim oCommand As New SqlCommand(oSql)
oCommand.Parameters.Add("FILENAME", SqlDbType.NVarChar).Value = pDocument.Filename
oCommand.Parameters.Add("ENVELOPE_ID", SqlDbType.Int).Value = Envelope.Id
'TODO: delete document file, from temp and from documentpath
Return Database.ExecuteNonQuery(oCommand)
End Function
Public Function SaveEnvelopeDocumentsToFilesystem(pEnvelope As Envelope) As Boolean
Try
For Each oDocument In pEnvelope.Documents.Where(Function(d) d.IsTempFile)
Dim oEnvelopePath = GetEnvelopePath(pEnvelope)
If oEnvelopePath Is Nothing Then
Return False
End If
Dim oDocumentFilePath = Path.Combine(oEnvelopePath, oDocument.Filename)
File.Copy(oDocument.Filepath, oDocumentFilePath)
File.Delete(oDocument.Filepath)
oDocument.IsTempFile = False
oDocument.FileInfo = New FileInfo(oDocumentFilePath)
Next
Return True
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function
Private Function GetEnvelopePath(pEnvelope As Envelope) As String
Try
Dim oEnvelopePath As String = Path.Combine(State.DbConfig.DocumentPath, pEnvelope.Uuid)
If Not Directory.Exists(oEnvelopePath) Then
Directory.CreateDirectory(oEnvelopePath)
End If
Return oEnvelopePath
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
#End Region
Private Function SaveEnvelopeDocuments(pEnvelope As Envelope, pTransaction As SqlTransaction) As Boolean
Try
Return pEnvelope.Documents.
Where(Function(d) d.Id = 0).
Select(Function(d) DocumentModel.Insert(pEnvelope, d, pTransaction)).
All(Function(pResult) pResult = True)
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function
Public Function CreateEnvelopeReceivers(pReceivers As List(Of EnvelopeReceiver)) As Boolean
Dim oExistingReceivers As List(Of EnvelopeReceiver) = ReceiverModel.ListReceivers(pReceivers)
Dim oExistingAddresses = oExistingReceivers.Select(Function(r) r.Email)
Dim oNewReceivers = pReceivers.
Where(Function(r) Not oExistingAddresses.Contains(r.Email)).ToList()
Dim oOldReceivers = pReceivers.
Where(Function(r) oExistingAddresses.Contains(r.Email)).ToList()
For Each oReceiver In oOldReceivers
oReceiver.Id = oExistingReceivers.
Where(Function(r) r.Email = oReceiver.Email).
Select(Function(r) r.Id).
FirstOrDefault()
Next
Dim oConnection = Database.GetConnection()
Dim oTransaction = oConnection.BeginTransaction()
Try
If InsertReceivers(oNewReceivers, oTransaction) = False Then
Throw New ApplicationException("Could not insert receivers!")
End If
oTransaction.Commit()
Return True
Catch ex As Exception
Logger.Error(ex)
oTransaction.Rollback()
Return False
End Try
End Function
Private Function SaveEnvelopeReceivers(pEnvelope As Envelope, pTransaction As SqlTransaction) As Boolean
Try
If pEnvelope.Id = Nothing Then
Throw New ArgumentNullException("EnvelopeId")
End If
If AssignReceivers(pEnvelope, pTransaction) = False Then
Return False
End If
Return True
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function
Private Function DeleteReceiver(pReceiver As EnvelopeReceiver) As Boolean
Try
Catch ex As Exception
End Try
End Function
Public Function ElementsExist(pDocumentId As Integer, pReceiverId As Integer) As Boolean
Return ElementModel.ElementsExist(pDocumentId, pReceiverId)
End Function
Private Function InsertReceivers(pReceivers As List(Of EnvelopeReceiver), pTransaction As SqlTransaction) As Boolean
Try
Return pReceivers.
Select(Function(r) InsertReceiver(r, pTransaction)).
All(Function(pResult) pResult = True)
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function
Private Function AssignReceivers(pEnvelope As Envelope, pTransaction As SqlTransaction) As Boolean
If ReceiverModel.Unassign(pEnvelope, pTransaction) = False Then
Return False
End If
Return pEnvelope.Receivers.
Select(Function(r) ReceiverModel.Assign(pEnvelope, r, pTransaction)).
All(Function(pResult) pResult = True)
End Function
Private Function InsertReceiver(pReceiver As EnvelopeReceiver, pTransaction As SqlTransaction) As Boolean
If pReceiver.HasId = False Then
Return ReceiverModel.Insert(pReceiver, pTransaction)
Else
Return True
End If
End Function
End Class