This commit is contained in:
Jonathan Jenne
2023-08-16 12:06:10 +02:00
parent 069f5a6f88
commit b312396bb5
15 changed files with 354 additions and 155 deletions

View File

@@ -1,60 +1,28 @@
Imports System.Data.Common
Imports System.Data.SqlClient
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
Inherits BaseController
Public ReadOnly Envelope As Envelope = Nothing
Public Sub New(pState As State)
MyBase.New(pState.LogConfig)
Database = pState.Database
State = pState
InitializeModels(pState)
MyBase.New(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)
MyBase.New(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 {
@@ -114,9 +82,7 @@ Public Class EnvelopeEditorController
Public Function CleanupEnvelope() As Boolean
If Envelope.Status = Common.Constants.EnvelopeStatus.Created Then
'TODO: Delete Documents and Receivers and elements
Return EnvelopeModel.Delete(Envelope)
Return DeleteEnvelope(Envelope)
Else
Return True
End If
@@ -141,14 +107,17 @@ Public Class EnvelopeEditorController
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
Public Overloads Function DeleteDocument(pDocument As EnvelopeDocument) As Boolean
Dim oConnection As SqlConnection = Database.GetConnection
Dim oTransaction As SqlTransaction = oConnection.BeginTransaction
Return Database.ExecuteNonQuery(oCommand)
If DeleteDocument(pDocument, oTransaction) = True Then
oTransaction.Commit()
Return True
Else
oTransaction.Rollback()
Return False
End If
End Function
Public Function SaveEnvelopeDocumentsToFilesystem(pEnvelope As Envelope) As Boolean
@@ -206,28 +175,21 @@ Public Class EnvelopeEditorController
End Try
End Function
Public Function CreateEnvelopeReceivers(pReceivers As List(Of EnvelopeReceiver)) As Boolean
Dim oExistingReceivers As List(Of EnvelopeReceiver) = ReceiverModel.ListReceivers(pReceivers)
Public Function CreateEnvelopeReceivers(pCurrentReceivers As List(Of EnvelopeReceiver)) As Boolean
Dim oExistingReceivers As List(Of EnvelopeReceiver) = ReceiverModel.ListReceivers(pCurrentReceivers)
Dim oExistingAddresses = oExistingReceivers.Select(Function(r) r.Email)
Dim oNewReceivers = pReceivers.
Dim oNewReceivers = pCurrentReceivers.
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()
For Each oReceiver In oNewReceivers
oReceiver.Id = 0
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
@@ -261,16 +223,35 @@ Public Class EnvelopeEditorController
End Try
End Function
Private Function DeleteReceiver(pReceiver As EnvelopeReceiver) As Boolean
Public Function DeleteReceiver(pReceiver As EnvelopeReceiver) As Boolean
Dim oConnection As SqlConnection = Database.GetConnection()
Dim oTransaction As SqlTransaction = oConnection.BeginTransaction()
Try
If ReceiverModel.Delete(pReceiver.Id, Envelope.Id, oTransaction) = True Then
Dim oResult = Envelope.Documents.
Select(Function(d) ElementModel.DeleteElements(pReceiver.Id, d.Id, oTransaction)).
All(Function(pResult) pResult = True)
If oResult = False Then
Throw New ApplicationException("Could not delete elements!")
End If
Else
Throw New ApplicationException("Could not delete receiver!")
End If
oTransaction.Commit()
Return True
Catch ex As Exception
Logger.Error(ex)
oTransaction.Rollback()
Return False
End Try
End Function
Public Function ElementsExist(pDocumentId As Integer, pReceiverId As Integer) As Boolean
Return ElementModel.ElementsExist(pDocumentId, pReceiverId)
Public Function ElementsExist(pReceiverId As Integer) As Boolean
Return ElementModel.ElementsExist(Envelope.Id, pReceiverId)
End Function
Private Function InsertReceivers(pReceivers As List(Of EnvelopeReceiver), pTransaction As SqlTransaction) As Boolean