Jonathan Jenne b24fbf58fc 19.07.2023
2023-07-31 09:52:49 +02:00

280 lines
10 KiB
VB.net

Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.Runtime.Remoting.Messaging
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports EnvelopeGenerator.Form.My.Resources
Public Class EnvelopeEditorController
Inherits BaseClass
Private ReadOnly Database As MSSQLServer
Private ReadOnly State As State = Nothing
Private ReadOnly EnvelopeModel As EnvelopeModel
Private ReadOnly DocumentModel As DocumentModel
Private ReadOnly ReceiverModel As ReceiverModel
Public ReadOnly Envelope As Envelope = Nothing
Public Sub New(pState As State)
MyBase.New(pState.LogConfig)
Database = pState.Database
State = pState
EnvelopeModel = New EnvelopeModel(pState)
Envelope = CreateEnvelope()
End Sub
Public Sub New(pState As State, pEnvelope As Envelope)
MyBase.New(pState.LogConfig)
Database = pState.Database
State = pState
EnvelopeModel = New EnvelopeModel(pState)
DocumentModel = New DocumentModel(pState)
ReceiverModel = New ReceiverModel(pState)
Envelope = pEnvelope
Envelope.Documents = DocumentModel.List(pEnvelope.Id)
Envelope.Receivers = ReceiverModel.List(pEnvelope.Id)
End Sub
#Region "Public"
Public Function CreateEnvelope() As Envelope
Dim oEnvelope As New Envelope() With {.UserId = State.UserId}
If EnvelopeModel.Insert(oEnvelope) Then
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 = Constants.EnvelopeStatus.Saved
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 = Constants.EnvelopeStatus.Created Then
'TODO: Delete Documents and Receivers and elements
Return EnvelopeModel.Delete(Envelope)
Else
Return True
End If
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
Return Database.ExecuteNonQuery(oCommand)
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) InsertDocument(pEnvelope, d, pTransaction)).
All(Function(pResult) pResult = True)
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function
Private Function InsertDocument(pEnvelope As Envelope, pDocument As EnvelopeDocument, pTransaction As SqlTransaction) As Boolean
Dim oSql = "INSERT INTO [dbo].[TBSIG_ENVELOPE_DOCUMENT]
([FILENAME]
,[FILEPATH]
,[ENVELOPE_ID])
VALUES
(@FILENAME
,@FILEPATH
,@ENVELOPE_ID)"
Dim oCommand As New SqlCommand(oSql)
oCommand.Parameters.Add("FILENAME", SqlDbType.NVarChar).Value = pDocument.Filename
oCommand.Parameters.Add("FILEPATH", SqlDbType.NVarChar).Value = pDocument.Filepath
oCommand.Parameters.Add("ENVELOPE_ID", SqlDbType.Int).Value = pEnvelope.Id
If Database.ExecuteNonQuery(oCommand, pTransaction) Then
pDocument.EnvelopeId = pEnvelope.Id
pDocument.Id = GetDocumentId(pDocument.Filename, pEnvelope, pTransaction)
Return True
Else
Return False
End If
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 UpdateReceivers(pEnvelope.Receivers, pTransaction) = False Then
Return False
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 UpdateReceivers(pReceivers As List(Of EnvelopeReceiver), pTransaction As SqlTransaction) As Boolean
Try
Return pReceivers.
Select(Function(r) UpdateReceiver(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 Database.ExecuteNonQuery($"DELETE FROM [dbo].[TBSIG_ENVELOPE_RECEIVER] WHERE [ENVELOPE_ID] = {pEnvelope.Id}") = False Then
Return False
End If
Return pEnvelope.Receivers.
Select(Function(r) AssignReceiver(pEnvelope, r, pTransaction)).
All(Function(pResult) pResult = True)
End Function
Private Function AssignReceiver(pEnvelope As Envelope, pReceiver As EnvelopeReceiver, pTransaction As SqlTransaction) As Boolean
Dim oSql = "INSERT INTO [dbo].[TBSIG_ENVELOPE_RECEIVER]
([ENVELOPE_ID]
,[RECEIVER_ID]
,[PRIVATE_MESSAGE]
,[ACCESS_CODE]
,[SEQUENCE])
VALUES
(@ENVELOPE_ID
,@RECEIVER_ID
,@MESSAGE
,@ACCESS_CODE
,@SEQUENCE)"
Dim oCommand As New SqlCommand(oSql)
oCommand.Parameters.Add("ENVELOPE_ID", SqlDbType.NVarChar).Value = pEnvelope.Id
oCommand.Parameters.Add("RECEIVER_ID", SqlDbType.NVarChar).Value = pReceiver.Id
oCommand.Parameters.Add("MESSAGE", SqlDbType.NVarChar).Value = pReceiver.PrivateMessage
oCommand.Parameters.Add("ACCESS_CODE", SqlDbType.NVarChar).Value = pReceiver.AccessCode
oCommand.Parameters.Add("SEQUENCE", SqlDbType.NVarChar).Value = pReceiver.Sequence
Return Database.ExecuteNonQuery(oCommand, pTransaction)
End Function
Private Function UpdateReceiver(pReceiver As EnvelopeReceiver, pTransaction As SqlTransaction) As Boolean
If pReceiver.Id = 0 Then
Dim oSql As String = "INSERT INTO [dbo].[TBSIG_RECEIVER]
([NAME]
,[EMAIL_ADDRESS]
,[SIGNATURE]
,[COMPANY_NAME]
,[JOB_TITLE])
VALUES
(@NAME
,@EMAIL
,@SIGNATURE
,@COMPANY
,@JOB)"
Dim oCommand = New SqlCommand(oSql)
oCommand.Parameters.Add("NAME", SqlDbType.NVarChar).Value = pReceiver.Name
oCommand.Parameters.Add("EMAIL", SqlDbType.NVarChar).Value = pReceiver.Email
oCommand.Parameters.Add("SIGNATURE", SqlDbType.NVarChar).Value = pReceiver.Signature
oCommand.Parameters.Add("COMPANY", SqlDbType.NVarChar).Value = pReceiver.Company
oCommand.Parameters.Add("JOB", SqlDbType.NVarChar).Value = pReceiver.JobTitle
Dim oResult = Database.ExecuteNonQuery(oCommand, pTransaction)
If oResult = True Then
pReceiver.Id = GetReceiverId(pReceiver.Email, pTransaction)
Else
Return False
End If
Return True
Else
Dim oSql As String = "UPDATE [dbo].[TBSIG_RECEIVER]
SET [NAME] = @NAME
,[COMPANY_NAME] = @COMPANY
,[JOB_TITLE] = @JOB
WHERE GUID = @GUID"
Dim oCommand = New SqlCommand(oSql)
oCommand.Parameters.Add("NAME", SqlDbType.NVarChar).Value = pReceiver.Name
oCommand.Parameters.Add("COMPANY", SqlDbType.NVarChar).Value = pReceiver.Company
oCommand.Parameters.Add("JOB", SqlDbType.NVarChar).Value = pReceiver.JobTitle
oCommand.Parameters.Add("GUID", SqlDbType.Int).Value = pReceiver.Id
Return Database.ExecuteNonQuery(oCommand, pTransaction)
End If
End Function
Private Function GetReceiverId(pEmailAddress As String, pTransaction As SqlTransaction) As Integer
Try
Return Database.GetScalarValue($"SELECT GUID FROM TBSIG_RECEIVER WHERE EMAIL_ADDRESS = '{pEmailAddress}'", pTransaction)
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function GetDocumentId(pFilename As String, pEnvelope As Envelope, pTransaction As SqlTransaction) As Integer
Try
Return Database.GetScalarValue($"SELECT MAX(GUID) FROM TBSIG_ENVELOPE_DOCUMENT WHERE FILENAME = '{pFilename}' AND ENVELOPE_ID = {pEnvelope.Id}", pTransaction)
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
End Class