31.07.2023

This commit is contained in:
Jonathan Jenne
2023-07-31 11:51:33 +02:00
parent b24fbf58fc
commit 13cf091d8e
15 changed files with 349 additions and 106 deletions

View File

@@ -1,5 +1,6 @@
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.IO
Imports System.Runtime.Remoting.Messaging
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Database
@@ -12,9 +13,9 @@ Public Class EnvelopeEditorController
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
Private EnvelopeModel As EnvelopeModel
Private DocumentModel As DocumentModel
Private ReceiverModel As ReceiverModel
Public ReadOnly Envelope As Envelope = Nothing
@@ -24,7 +25,7 @@ Public Class EnvelopeEditorController
Database = pState.Database
State = pState
EnvelopeModel = New EnvelopeModel(pState)
InitializeModels(pState)
Envelope = CreateEnvelope()
End Sub
@@ -35,15 +36,19 @@ Public Class EnvelopeEditorController
Database = pState.Database
State = pState
EnvelopeModel = New EnvelopeModel(pState)
DocumentModel = New DocumentModel(pState)
ReceiverModel = New ReceiverModel(pState)
InitializeModels(pState)
Envelope = pEnvelope
Envelope.Documents = DocumentModel.List(pEnvelope.Id)
Envelope.Receivers = ReceiverModel.List(pEnvelope.Id)
End Sub
Private Sub InitializeModels(pState As State)
EnvelopeModel = New EnvelopeModel(pState)
DocumentModel = New DocumentModel(pState)
ReceiverModel = New ReceiverModel(pState)
End Sub
#Region "Public"
Public Function CreateEnvelope() As Envelope
Dim oEnvelope As New Envelope() With {.UserId = State.UserId}
@@ -60,6 +65,11 @@ Public Class EnvelopeEditorController
Try
pEnvelope.Status = Constants.EnvelopeStatus.Saved
If SaveEnvelopeDocumentsToFilesystem(pEnvelope) = False Then
Throw New ApplicationException
End If
If EnvelopeModel.Update(pEnvelope, oTransaction) = False Then
Throw New ApplicationException
End If
@@ -94,14 +104,75 @@ Public Class EnvelopeEditorController
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
@@ -175,85 +246,25 @@ Public Class EnvelopeEditorController
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
If ReceiverModel.Unassign(pEnvelope, pTransaction) = False Then
Return False
End If
Return pEnvelope.Receivers.
Select(Function(r) AssignReceiver(pEnvelope, r, pTransaction)).
Select(Function(r) ReceiverModel.Assign(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
If ReceiverModel.TestReceiverExists(pReceiver) Then
Return True
End If
If pReceiver.HasId Then
Return ReceiverModel.Update(pReceiver, pTransaction)
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)
Return ReceiverModel.Insert(pReceiver, pTransaction)
End If
End Function