Jonathan Jenne b63655b212 06-07-2023
2023-07-06 14:24:30 +02:00

257 lines
10 KiB
VB.net

Imports System.Data.SqlClient
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Public Class EnvelopeController
Inherits BaseClass
Private ReadOnly Database As MSSQLServer
Private Envelope As Envelope = Nothing
Public Sub New(pState As State)
MyBase.New(pState.LogConfig)
Database = pState.Database
End Sub
#Region "Public"
Public Function SaveEnvelope(pEnvelope As Envelope) As Boolean
Dim oConnection = Database.GetConnection()
Dim oTransaction = oConnection.BeginTransaction(IsolationLevel.ReadUncommitted)
Try
If pEnvelope.Id > 0 Then
Dim oSql = "UPDATE [dbo].[TBSIG_ENVELOPE] SET [SUBJECT] = @SUBJECT, [MESSAGE] = @MESSAGE, [ENVELOPE_UUID] = @UUID WHERE GUID = @ID AND USER_ID = @USER_ID"
Dim oCommand As New SqlCommand(oSql)
oCommand.Parameters.Add("SUBJECT", SqlDbType.NVarChar).Value = pEnvelope.Subject
oCommand.Parameters.Add("MESSAGE", SqlDbType.NVarChar).Value = pEnvelope.Message
oCommand.Parameters.Add("UUID", SqlDbType.NVarChar).Value = pEnvelope.Uuid
oCommand.Parameters.Add("ID", SqlDbType.Int).Value = pEnvelope.Id
oCommand.Parameters.Add("USER_ID", SqlDbType.Int).Value = pEnvelope.UserId
Dim oResult = Database.ExecuteNonQueryWithConnectionObject(oCommand, oConnection, MSSQLServer.TransactionMode.ExternalTransaction, oTransaction)
If oResult = False Then
Throw New ApplicationException
End If
Else
Dim oSql = "INSERT INTO [dbo].[TBSIG_ENVELOPE] (SUBJECT, MESSAGE, ENVELOPE_UUID, STATUS, USER_ID) VALUES (@SUBJECT, @MESSAGE, @UUID, @STATUS, @USER_ID)"
Dim oCommand As New SqlCommand(oSql)
oCommand.Parameters.Add("SUBJECT", SqlDbType.NVarChar).Value = pEnvelope.Subject
oCommand.Parameters.Add("MESSAGE", SqlDbType.NVarChar).Value = pEnvelope.Message
oCommand.Parameters.Add("UUID", SqlDbType.NVarChar).Value = pEnvelope.Uuid
oCommand.Parameters.Add("STATUS", SqlDbType.NVarChar).Value = pEnvelope.Status.ToString()
oCommand.Parameters.Add("USER_ID", SqlDbType.Int).Value = pEnvelope.UserId
If Database.ExecuteNonQuery(oCommand, oTransaction) = False Then
Throw New ApplicationException
End If
pEnvelope.Id = GetEnvelopeId(pEnvelope.UserId, oTransaction)
If SaveEnvelopeReceivers(pEnvelope, oTransaction) = False Then
Throw New ApplicationException
End If
If SaveEnvelopeDocuments(pEnvelope, oTransaction) = False Then
Throw New ApplicationException
End If
End If
oTransaction.Commit()
Envelope = pEnvelope
Return True
Catch ex As Exception
Logger.Error(ex)
oTransaction.Rollback()
Return False
End Try
End Function
Public Function DeleteDocument(pDocument As EnvelopeFile) As Boolean
If Envelope Is Nothing Then
Return False
End If
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.
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 EnvelopeFile, 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
pDocument.EnvelopeId = pEnvelope.Id
Return Database.ExecuteNonQuery(oCommand, pTransaction)
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 Receiver), 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 Receiver, 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 Receiver, 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 GetEnvelopeId(pUserId As Integer, pTransaction As SqlTransaction) As Integer
Try
Return Database.GetScalarValue($"SELECT MAX(GUID) FROM TBSIG_ENVELOPE WHERE USER_ID = {pUserId}", pTransaction)
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
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
End Class