Imports DigitalData.Modules.Database Imports DigitalData.Modules.Base Imports EnvelopeGenerator.CommonServices Imports System.Data.SqlClient Imports EnvelopeGenerator.Domain.Entities Public MustInherit Class BaseController Inherits BaseClass Public EnvelopeModel As EnvelopeModel Public DocumentModel As DocumentModel Public ReceiverModel As ReceiverModel Public ElementModel As ElementModel Public HistoryModel As HistoryModel Public UserModel As UserModel Public EmailModel As EmailModel Public ChartModel As ChartModel Public ActionService As ActionService Public ReadOnly Property Database As MSSQLServer Public ReadOnly Property State As State Public Sub New(pState As State) MyBase.New(pState.LogConfig) State = pState Database = pState.Database InitializeModels(pState) ActionService = New ActionService(pState, Database) End Sub Private Sub InitializeModels(pState As State) EnvelopeModel = New EnvelopeModel(pState) DocumentModel = New DocumentModel(pState) ReceiverModel = New ReceiverModel(pState) ElementModel = New ElementModel(pState) HistoryModel = New HistoryModel(pState) UserModel = New UserModel(pState) EmailModel = New EmailModel(pState) ChartModel = New ChartModel(pState) End Sub Public Function DeleteEnvelope(pEnvelope As Envelope, pReason As String) As Boolean If pEnvelope Is Nothing Then Return True End If If pEnvelope.IsAlreadySent Then If DeleteDocumentsFromDisk(pEnvelope) = False Then Return False End If Return ActionService.DeleteEnvelope(pEnvelope, pReason) Else Return DeleteEnvelopeFromDisk(pEnvelope) End If End Function Public Function DeleteEnvelopeFromDisk(pEnvelope As Envelope) As Boolean If pEnvelope Is Nothing Then Return True End If Dim oConnection As SqlConnection = Database.GetConnection() Dim oTransaction As SqlTransaction = oConnection.BeginTransaction() Try Dim oResult = pEnvelope.Documents. Select(Function(d) DeleteDocument(d, oTransaction)). All(Function(r) r = True) If oResult = False Then Throw New ApplicationException("could not delete documents") End If Dim oResult2 = pEnvelope.EnvelopeReceivers. Select(Function(r) ReceiverModel.Delete(r.Receiver.Id, pEnvelope.Id, oTransaction)). All(Function(r) r = True) If oResult2 = False Then Throw New ApplicationException("could not delete receivers") End If Dim oResult3 = EnvelopeModel.Delete(pEnvelope, oTransaction) If oResult3 = False Then Throw New ApplicationException("could not delete envelope") End If oTransaction.Commit() Return True Catch ex As Exception Logger.Error(ex) oTransaction.Rollback() Return False End Try End Function Public Function DeleteDocument(pDocument As Document, pTransaction As SqlTransaction) As Boolean Try If DocumentModel.Delete(pDocument.Id, pTransaction) = True Then Dim oResult = ElementModel.DeleteElements(pDocument.Id, pTransaction) If oResult = False Then Throw New ApplicationException("Could not delete elements!") End If Else Throw New ApplicationException("Could not delete document!") End If Catch ex As Exception Logger.Error(ex) Return False End Try If DeleteDocumentFromDisk(pDocument) = False Then Return False End If Return True End Function Public Function DeleteDocumentsFromDisk(pEnvelope As Envelope) As Boolean If Not IsNothing(pEnvelope.Documents) Then Return pEnvelope.Documents. Select(Function(d) DeleteDocumentFromDisk(d)). All(Function(r) r = True) Else Return True End If End Function Public Function DeleteDocumentFromDisk(pDocument As Document) As Boolean Try If IO.File.Exists(pDocument.Filepath) Then IO.File.Delete(pDocument.Filepath) End If Return True Catch ex As Exception Logger.Error(ex) Return False End Try End Function End Class