/// <summary>
        /// Delete a debt holder
        /// </summary>
        /// <returns>True for sucess</returns>
        public override ErrorCode Delete(ConsumerTrust record)
        {
            DataModel            dataModel   = new DataModel();
            DataModelTransaction transaction = DataModelTransaction.Current;

            if (record.RowId == null || DataModel.ConsumerTrust.ConsumerTrustKey.Find(record.RowId) == null)
            {
                return(ErrorCode.RecordNotFound);
            }

            ConsumerTrustRow consumerTrust = DataModel.ConsumerTrust.ConsumerTrustKey.Find(record.RowId);

            consumerTrust.AcquireReaderLock(transaction);
            Guid        debtId   = consumerTrust.ConsumerTrustId;
            ConsumerRow consumer = consumerTrust.ConsumerRow;
            DebtRuleRow debtRule = consumerTrust.DebtRuleRow;

            consumerTrust.ReleaseReaderLock(transaction.TransactionId);

#if false   // If we switch from explicitly deleting the working order to explicitly deleting the security, then we need this.
            if (!TradingSupport.HasAccess(transaction, debtId, AccessRight.Write))
            {
                return(ErrorCode.AccessDenied);
            }
#endif

            consumerTrust.AcquireWriterLock(transaction);
            if (consumerTrust.RowState != DataRowState.Deleted && consumerTrust.RowState != DataRowState.Detached)
            {
                dataModel.DestroyConsumerTrust(new object[] { consumerTrust.ConsumerTrustId }, record.RowVersion);
            }
            consumerTrust.ReleaseWriterLock(transaction.TransactionId);

            consumer.AcquireWriterLock(transaction);
            if (consumer.RowState != DataRowState.Deleted && consumer.RowState != DataRowState.Detached)
            {
                dataModel.DestroyConsumer(new object[] { consumer.ConsumerId }, consumer.RowVersion);
            }
            consumer.ReleaseWriterLock(transaction.TransactionId);

            if (debtRule != null)
            {
                debtRule.AcquireReaderLock(transaction);
                if (debtRule.RowState != DataRowState.Deleted && debtRule.RowState != DataRowState.Detached && debtRule.GetDebtRuleMapRows().Length == 0)
                {
                    DebtRulePersistence debtRulePersistence = new DebtRulePersistence();
                    Guid debtRuleId = debtRule.DebtRuleId;
                    long rowVersion = debtRule.RowVersion;
                    debtRule.ReleaseReaderLock(transaction.TransactionId);
                    debtRulePersistence.Delete(new Records.DebtRule {
                        RowId = debtRuleId, RowVersion = rowVersion
                    });
                }
                else
                {
                    debtRule.ReleaseReaderLock(transaction.TransactionId);
                }
            }

            return(ErrorCode.Success);
        }
        /// <summary>
        /// Delete a WorkingOrderRow.
        /// </summary>
        /// <param name="dataModel">The data model.</param>
        /// <param name="transaction">The current transaction.</param>
        /// <param name="workingOrderRow">The working order row to delete.</param>
        /// <returns>Error code of any failure, or Success.</returns>
        public ErrorCode DeleteRow(DataModel dataModel, DataModelTransaction transaction, WorkingOrderRow workingOrderRow)
        {
            SecurityRow securityRow       = null;
            EntityRow   securityEntityRow = null;

            MatchRow[]         matchRows;
            ConsumerDebtRow[]  consumerDebtRows;
            ConsumerTrustRow[] consumerTrustRows;
            CreditCardRow      creditCardRow = null;
            ConsumerRow        consumerRow   = null;
            Guid    blotterId;
            Guid    securityEntityId         = Guid.Empty;
            Int64   securityEntityRowVersion = 0;
            Guid    consumerId           = Guid.Empty;
            Int64   consumerRowVersion   = 0;
            Guid    creditCardId         = Guid.Empty;
            Int64   creditCardRowVersion = 0;
            Boolean consumerStillInUse   = false;

            workingOrderRow.AcquireWriterLock(transaction.TransactionId, DataModel.LockTimeout);
            if (workingOrderRow.RowState == DataRowState.Deleted ||
                workingOrderRow.RowState == DataRowState.Detached)
            {
                workingOrderRow.ReleaseLock(transaction.TransactionId);
                return(ErrorCode.RecordNotFound);
            }
            else
            {
                transaction.AddLock(workingOrderRow);
            }
            blotterId   = workingOrderRow.BlotterId;
            securityRow = workingOrderRow.SecurityRowByFK_Security_WorkingOrder_SecurityId;
            matchRows   = workingOrderRow.GetMatchRows();

            if (matchRows != null)
            {
                foreach (MatchRow matchRow in matchRows)
                {
                    if (IsSettled(transaction, matchRow))
                    {
                        return(ErrorCode.RecordExists);
                    }
                }
            }

            if (!DataModelFilters.HasAccess(transaction, TradingSupport.UserId, blotterId, AccessRight.Write))
            {
                workingOrderRow.ReleaseLock(transaction.TransactionId);
                return(ErrorCode.AccessDenied);
            }
            securityRow.AcquireWriterLock(transaction.TransactionId, DataModel.LockTimeout);
            if (securityRow.RowState == DataRowState.Deleted ||
                securityRow.RowState == DataRowState.Detached)
            {
                workingOrderRow.ReleaseLock(transaction.TransactionId);
                securityRow.ReleaseWriterLock(transaction.TransactionId);
                return(ErrorCode.RecordNotFound);
            }
            securityEntityRow = securityRow.EntityRow;
            consumerDebtRows  = securityRow.GetConsumerDebtRows();
            consumerTrustRows = securityRow.GetConsumerTrustRows();
            securityRow.ReleaseWriterLock(transaction.TransactionId);

            securityEntityRow.AcquireWriterLock(transaction);
            if (securityEntityRow.RowState == DataRowState.Deleted ||
                securityEntityRow.RowState == DataRowState.Detached)
            {
                workingOrderRow.ReleaseLock(transaction.TransactionId);
                securityEntityRow.ReleaseLock(transaction.TransactionId);
                return(ErrorCode.RecordNotFound);
            }
            securityEntityId         = securityEntityRow.EntityId;
            securityEntityRowVersion = securityEntityRow.RowVersion;
            securityEntityRow.ReleaseLock(transaction.TransactionId);

            if (consumerTrustRows.Length > 0 && consumerDebtRows.Length > 0)
            {
                EventLog.Warning("Deleting a working order associated with both ConsumerDebt and ConsumerTrust rows");
            }
            else if (consumerDebtRows.Length > 1)
            {
                EventLog.Warning("Deleting a working order associated with more than one ConsumerDebt row");
            }
            else if (consumerTrustRows.Length > 1)
            {
                EventLog.Warning("Deleting a working order associated with more than one ConsumerTrust row");
            }

            if (consumerDebtRows.Length == 1)
            {
                ConsumerDebtRow consumerDebtRow = consumerDebtRows[0];

                consumerDebtRow.AcquireWriterLock(transaction);
                if (consumerDebtRow.RowState == DataRowState.Deleted ||
                    consumerDebtRow.RowState == DataRowState.Detached)
                {
                }
                else
                {
                    creditCardRow = consumerDebtRow.CreditCardRow;
                    consumerRow   = consumerDebtRow.ConsumerRow;
                }
                consumerDebtRow.ReleaseLock(transaction.TransactionId);
            }
            else if (consumerTrustRows.Length == 1)
            {
                ConsumerTrustRow consumerTrustRow = consumerTrustRows[0];

                consumerTrustRow.AcquireWriterLock(transaction);
                if (consumerTrustRow.RowState == DataRowState.Deleted ||
                    consumerTrustRow.RowState == DataRowState.Detached)
                {
                }
                else
                {
                    consumerRow = consumerTrustRow.ConsumerRow;
                }
                consumerTrustRow.ReleaseLock(transaction.TransactionId);
            }

            if (consumerRow != null)
            {
                consumerRow.AcquireWriterLock(transaction);
                if (consumerRow.RowState == DataRowState.Deleted ||
                    consumerRow.RowState == DataRowState.Detached)
                {
                    consumerRow = null;
                }
                else
                {
                    consumerStillInUse = consumerRow.GetConsumerDebtRows().Length > 1;
                    consumerId         = consumerRow.ConsumerId;
                    consumerRowVersion = consumerRow.RowVersion;
                }
                consumerRow.ReleaseLock(transaction.TransactionId);
            }

            if (creditCardRow != null)
            {
                creditCardRow.AcquireWriterLock(transaction);
                if (creditCardRow.RowState == DataRowState.Deleted ||
                    creditCardRow.RowState == DataRowState.Detached)
                {
                    creditCardRow = null;
                }
                else
                {
                    creditCardId         = creditCardRow.ConsumerId;
                    creditCardRowVersion = creditCardRow.RowVersion;
                }
                creditCardRow.ReleaseLock(transaction.TransactionId);
            }

            //gonna get the lock on the workingOrder and let the txn commit/rollback get rid of it
            //this will basically wrap the delete row
            //action in a critical section because the first
            //reader lock in the method is on the workingOrder row
            //workingOrderRow.AcquireWriterLock(transaction.TransactionId, DataModel.LockTimeout);
            if (workingOrderRow.RowState == DataRowState.Deleted ||
                workingOrderRow.RowState == DataRowState.Detached)
            {
                workingOrderRow.ReleaseLock(transaction.TransactionId);
                return(ErrorCode.RecordNotFound);
            }
            //securityRow.AcquireWriterLock(transaction.TransactionId, DataModel.LockTimeout);
            //if(securityRow.RowState == DataRowState.Deleted ||
            //        securityRow.RowState == DataRowState.Detached)
            //{
            //    workingOrderRow.ReleaseLock(transaction.TransactionId);
            //    return ErrorCode.RecordNotFound;
            //}
            if (creditCardRow != null && consumerStillInUse)
            {
                dataModel.DestroyCreditCard(new object[] { creditCardId }, creditCardRowVersion);
            }
            if (consumerRow != null && !consumerStillInUse)
            {
                dataModel.DestroyConsumer(new object[] { consumerId }, consumerRowVersion);
            }

            dataModel.DestroyEntity(new object[] { securityEntityId }, securityEntityRowVersion);

            return(ErrorCode.Success);
        }