2023-12-06 12:40:24 +01:00

363 lines
12 KiB
VB.net

Imports System.Data.SqlClient
Imports System.IO
Imports DigitalData.Modules.Logging
Imports EnvelopeGenerator.Common
Imports EnvelopeGenerator.Common.Constants
Imports EnvelopeGenerator.Common.My
Public Class EnvelopeEditorController
Inherits BaseController
Public ReadOnly Envelope As Envelope = Nothing
Public ReadOnly EmailService As EmailService
Public ReadOnly ActionService As ActionService
Public ReadOnly Thumbnail As Thumbnail
Public Sub New(pState As State)
MyBase.New(pState)
Envelope = CreateEnvelope()
Thumbnail = New Thumbnail(pState.LogConfig)
EmailService = New EmailService(pState)
ActionService = New ActionService(pState)
End Sub
Public Sub New(pState As State, pEnvelope As Envelope)
MyBase.New(pState)
Envelope = pEnvelope
Envelope.Documents = DocumentModel.List(pEnvelope.Id).ToList()
Envelope.Receivers = ReceiverModel.ListEnvelopeReceivers(pEnvelope.Id).ToList()
Thumbnail = New Thumbnail(pState.LogConfig)
ActionService = New ActionService(pState)
End Sub
#Region "Public"
Public Function SendEnvelope() As Boolean
Return ActionService.SendEnvelope(Envelope)
End Function
Public Function ValidateEnvelopeForSending(pErrors As List(Of String)) As List(Of String)
Dim oEnvelopeErrors = pErrors
If ElementModel.ElementsExist(Envelope.Id) = False Then
oEnvelopeErrors.Add(Resources.Envelope.Missing_Elements)
End If
If ElementModel.OneElementPerReceiverExist(Envelope.Id) = False Then
For Each receiverItem As EnvelopeReceiver In Envelope.Receivers
If ElementModel.ElementsExist(Envelope.Id, receiverItem.Id) = False Then
oEnvelopeErrors.Add(String.Format(Resources.Envelope.Missing_Elements_for_Receiver, receiverItem.Name))
End If
Next
End If
Return oEnvelopeErrors
End Function
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 = EnvelopeStatus.EnvelopeCreated,
.UserReference = oEnvelope.User.Email
}
HistoryModel.Insert(newHistoryEntry)
Return oEnvelope
Else
Return Nothing
End If
End Function
Public Function SaveReceivers(pEnvelope As Envelope, pReceiversFromGrid As List(Of EnvelopeReceiver)) As Boolean
Dim oExistingReceivers As List(Of EnvelopeReceiver) = ReceiverModel.ListReceivers(pReceiversFromGrid).ToList()
Dim oExistingAddresses = oExistingReceivers.Select(Function(r) r.Email)
Dim oNewReceivers = pReceiversFromGrid.Where(Function(r) Not oExistingAddresses.Contains(r.Email)).ToList()
If CreateNewReceivers(oNewReceivers) = False Then
Return False
End If
Dim oAllReceivers As List(Of EnvelopeReceiver) = ReceiverModel.ListReceivers(pReceiversFromGrid).ToList()
pEnvelope.Receivers.Clear()
For Each oReceiver In pReceiversFromGrid
Dim oDbReceiver = oAllReceivers.Where(Function(r) r.Email = oReceiver.Email).SingleOrDefault()
If oDbReceiver IsNot Nothing Then
oReceiver.Id = oDbReceiver.Id
oReceiver.Signature = oDbReceiver.Signature
End If
pEnvelope.Receivers.Add(oReceiver)
Next
Return True
End Function
Public Function SaveEnvelope() As Boolean
Dim oConnection = Database.GetConnection()
Dim oTransaction = oConnection.BeginTransaction(IsolationLevel.ReadUncommitted)
Try
Envelope.Status = EnvelopeStatus.EnvelopeSaved
If SaveEnvelopeDocumentsToFilesystem(Envelope) = False Then
Throw New ApplicationException
End If
If EnvelopeModel.Update(Envelope, oTransaction) = False Then
Throw New ApplicationException
End If
If SaveEnvelopeReceivers(Envelope, oTransaction) = False Then
Throw New ApplicationException
End If
If SaveEnvelopeDocuments(Envelope, 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 CreateDocument(pDocumentFilePath As String) As EnvelopeDocument
Try
Dim oFileInfo = New FileInfo(pDocumentFilePath)
Dim oTempFiles As New TempFiles(State.LogConfig)
Dim oTempFilePath = Path.Combine(oTempFiles.TempPath, Guid.NewGuid().ToString + oFileInfo.Extension)
File.Copy(oFileInfo.FullName, oTempFilePath, True)
Dim oFileInfoTemp = New FileInfo(oTempFilePath)
Dim oDocument = New EnvelopeDocument() With {
.Filename = oFileInfoTemp.Name,
.Filepath = oFileInfoTemp.FullName,
.FileNameOriginal = oFileInfo.Name,
.Thumbnail = Thumbnail.GetThumbnailFromPDFFile(oTempFilePath)
}
Return oDocument
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Public Function CreateThumbnail(pDocumentPath As String) As Bitmap
Try
Dim oThumbNail As Bitmap = Thumbnail.GetThumbnailFromPDFFile(pDocumentPath)
Return oThumbNail
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Public Overloads Function DeleteDocument(pDocument As EnvelopeDocument) As Boolean
Dim oConnection As SqlConnection = Database.GetConnection
Dim oTransaction As SqlTransaction = oConnection.BeginTransaction
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
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)
Dim oFileInfo = New FileInfo(oDocumentFilePath)
oDocument.IsTempFile = False
oDocument.Filename = oFileInfo.Name
oDocument.Filepath = oFileInfo.FullName
Next
Return True
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function
Public Function GetEnvelopeReceiverAddresses(pUserId As Integer) As List(Of String)
Return ReceiverModel.ListAllEnvelopeReceiverAddresses(pUserId)
End Function
Public Function CreateNewReceivers(pNewReceivers As List(Of EnvelopeReceiver)) As Boolean
If pNewReceivers.Count = 0 Then
Return True
End If
Dim oConnection = Database.GetConnection()
Dim oTransaction = oConnection.BeginTransaction()
Try
If InsertReceivers(pNewReceivers, 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
#End Region
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
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
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
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(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
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