Imports System.Data.Common Imports System.Data.SqlClient Imports System.IO Imports System.Runtime.Remoting.Messaging Imports DigitalData.Modules.Base Imports DigitalData.Modules.Database Imports DigitalData.Modules.Logging Imports EnvelopeGenerator.Form.My.Resources Public Class EnvelopeEditorController Inherits BaseClass Private ReadOnly Database As MSSQLServer Private ReadOnly State As State = Nothing Private EnvelopeModel As EnvelopeModel Private DocumentModel As DocumentModel Private ReceiverModel As ReceiverModel Private ElementModel As ElementModel Public ReadOnly Envelope As Envelope = Nothing Public Sub New(pState As State) MyBase.New(pState.LogConfig) Database = pState.Database State = pState InitializeModels(pState) Envelope = CreateEnvelope() End Sub Public Sub New(pState As State, pEnvelope As Envelope) MyBase.New(pState.LogConfig) Database = pState.Database State = 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) ElementModel = New ElementModel(pState) End Sub #Region "Public" Public Function CreateEnvelope() As Envelope Dim oEnvelope As New Envelope() With {.UserId = State.UserId} If EnvelopeModel.Insert(oEnvelope) Then Return oEnvelope Else Return Nothing End If End Function Public Function SaveEnvelope(pEnvelope As Envelope) As Boolean Dim oConnection = Database.GetConnection() Dim oTransaction = oConnection.BeginTransaction(IsolationLevel.ReadUncommitted) 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 If SaveEnvelopeReceivers(pEnvelope, oTransaction) = False Then Throw New ApplicationException End If If SaveEnvelopeDocuments(pEnvelope, 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.Status = Constants.EnvelopeStatus.Created Then 'TODO: Delete Documents and Receivers and elements Return EnvelopeModel.Delete(Envelope) Else Return True 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 Try Return pEnvelope.Documents. Where(Function(d) d.Id = 0). 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 EnvelopeDocument, 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 If Database.ExecuteNonQuery(oCommand, pTransaction) Then pDocument.EnvelopeId = pEnvelope.Id pDocument.Id = GetDocumentId(pDocument.Filename, pEnvelope, pTransaction) Return True Else Return False End If 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 InsertReceivers(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 DeleteReceiver(pReceiver As EnvelopeReceiver) As Boolean Try Catch ex As Exception End Try End Function Public Function ElementsExist(pDocumentId As Integer, pReceiverId As Integer) As Boolean Return ElementModel.ElementsExist(pDocumentId, 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 ReceiverModel.TestReceiverExists(pReceiver) Then Return True End If If pReceiver.HasId = False Then Return ReceiverModel.Insert(pReceiver, pTransaction) Else Return True End If 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 Private Function GetDocumentId(pFilename As String, pEnvelope As Envelope, pTransaction As SqlTransaction) As Integer Try Return Database.GetScalarValue($"SELECT MAX(GUID) FROM TBSIG_ENVELOPE_DOCUMENT WHERE FILENAME = '{pFilename}' AND ENVELOPE_ID = {pEnvelope.Id}", pTransaction) Catch ex As Exception Logger.Error(ex) Return Nothing End Try End Function End Class