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).ToList() Envelope.Receivers = ReceiverModel.ListEnvelopeReceivers(pEnvelope.Id).ToList() Thumbnail = New Thumbnail(pState.LogConfig) End Sub #Region "Public" Public Function SendEnvelope() As Boolean For Each receiverItem As EnvelopeReceiver In Envelope.Receivers Dim oEmailData As 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) } Dim oTemplate As New EmailTemplate() oTemplate.FillDocumentReceivedEmailBody(oEmailData) If EmailModel.Insert(oEmailData) = False Then Logger.Error("EMail data could not be inserted.") Dim oResult As Boolean = False End If Next If EnvelopeModel.Send(Envelope) Then 'TODO: Send email to History 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, .ActionType = EnvelopeHistoryActionType.Created, .ActionDescription = "Envelope wurde neu erstellt", .UserReference = oEnvelope.User.Email } HistoryModel.Insert(newHistoryEntry) 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 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) ' Neue Empfänger Dim oNewReceivers = pCurrentReceivers. Where(Function(r) Not oExistingAddresses.Contains(r.Email)).ToList() ' Empfänger, die bereits einmal verwendet wurden For Each oCurrentReceiver In pCurrentReceivers If oCurrentReceiver.Id = 0 Then Dim oExistingReceiver As EnvelopeReceiver = oExistingReceivers.Where(Function(r) r.Email.Equals(oCurrentReceiver.Email, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault() If oExistingReceiver IsNot Nothing Then oCurrentReceiver.Id = oExistingReceiver.Id End If End If 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