377 lines
12 KiB
VB.net
377 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 Thumbnail As Thumbnail
|
|
|
|
Public Sub New(pState As State)
|
|
MyBase.New(pState)
|
|
|
|
Envelope = CreateEnvelope()
|
|
Thumbnail = New Thumbnail(pState.LogConfig)
|
|
End Sub
|
|
|
|
Public Sub New(pState As State, pEnvelope As Envelope)
|
|
MyBase.New(pState)
|
|
|
|
Envelope = pEnvelope
|
|
Envelope.Documents = DocumentModel.List(pEnvelope.Id)
|
|
Envelope.Receivers = ReceiverModel.ListEnvelopeReceivers(pEnvelope.Id)
|
|
|
|
Thumbnail = New Thumbnail(pState.LogConfig)
|
|
End Sub
|
|
|
|
#Region "Public"
|
|
Public Function SendEnvelope() As Boolean
|
|
Dim oResult As Boolean = True
|
|
|
|
For Each receiverItem As EnvelopeReceiver In Envelope.Receivers
|
|
|
|
Dim oEmailData As EmailData = New EmailData With
|
|
{
|
|
.EmailAdress = receiverItem.Email,
|
|
.EmailSubject = Envelope.Subject,
|
|
.Message = Envelope.Message,
|
|
.ReferenceID = Envelope.Id,
|
|
.ReferenceString = Envelope.Uuid,
|
|
.ReceiverName = receiverItem.Name,
|
|
.SenderAdress = Envelope.User.Email,
|
|
.SenderName = Envelope.User.FullName,
|
|
.SignatureLink = Helpers.GetEnvelopeURL(State.DbConfig.SignatureHost, Envelope.Uuid, receiverItem.Signature)
|
|
}
|
|
|
|
' TODO Email-Template füllen
|
|
Dim oTemplate As EmailTemplate = New EmailTemplate(State)
|
|
oTemplate.SetEmailBody(oEmailData)
|
|
|
|
|
|
If EmailModel.Insert(oEmailData) = False Then
|
|
Logger.Error("EMail data could not be inserted.")
|
|
oResult = False
|
|
End If
|
|
|
|
Next
|
|
|
|
|
|
If EnvelopeModel.Send(Envelope) Then
|
|
'TODO: Send email
|
|
Return True
|
|
Else
|
|
Return False
|
|
End If
|
|
End Function
|
|
|
|
Public Function ValidateEnvelopeForSending() As List(Of String)
|
|
Dim oEnvelopeErrors = Envelope.Validate()
|
|
|
|
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 = HistoryStatus.Created,
|
|
.ActionTitle = "Envelope erzeugt",
|
|
.ActionDescription = "Envelope wurde erzeugt"
|
|
}
|
|
'TODO .UserEmailAddress = oEnvelope.User.Email ' TODO - fehlt noch
|
|
|
|
Return oEnvelope
|
|
Else
|
|
Return Nothing
|
|
End If
|
|
End Function
|
|
|
|
Public Function SaveEnvelope() As Boolean
|
|
Dim oConnection = Database.GetConnection()
|
|
Dim oTransaction = oConnection.BeginTransaction(IsolationLevel.ReadUncommitted)
|
|
|
|
Try
|
|
Envelope.Status = EnvelopeStatus.Saved
|
|
|
|
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 CleanupEnvelope() As Boolean
|
|
If Envelope Is Nothing Then
|
|
Return True
|
|
End If
|
|
|
|
If Envelope.Status = Common.Constants.EnvelopeStatus.Created Then
|
|
Return DeleteEnvelope(Envelope)
|
|
Else
|
|
Return True
|
|
End If
|
|
End Function
|
|
|
|
Public Function CreateDocument(pDocumentFilePath As String) As EnvelopeDocument
|
|
Try
|
|
Dim oFileInfo = New FileInfo(pDocumentFilePath)
|
|
Dim oTempFiles As TempFiles = 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
|
|
|
|
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
|
|
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
|
|
|
|
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 = pCurrentReceivers.
|
|
Where(Function(r) Not oExistingAddresses.Contains(r.Email)).ToList()
|
|
|
|
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
|
|
|
|
oTransaction.Commit()
|
|
|
|
Return True
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
oTransaction.Rollback()
|
|
|
|
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
|