/// <summary>
        ///
        /// </summary>
        /// <param name="dataModelTransaction"></param>
        /// <param name="moveToInfo"></param>
        /// <param name="consumerTrustNegotiationRow"></param>
        private static void CollectSettlementData(DataModelTransaction dataModelTransaction, MoveToInfo moveToInfo, ConsumerTrustNegotiationRow consumerTrustNegotiationRow)
        {
            foreach (ConsumerTrustSettlementRow settlementRow in consumerTrustNegotiationRow.GetConsumerTrustSettlementRows())
            {
                settlementRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                try
                {
                    moveToInfo.settlementRows.Add(new BaseRecord()
                    {
                        RowId = settlementRow.ConsumerTrustSettlementId, RowVersion = settlementRow.RowVersion
                    });

                    //Get  payment method rows to move
                    foreach (ConsumerTrustSettlementPaymentMethodRow consumerTrustSettlementPaymentMethodRow in settlementRow.GetConsumerTrustSettlementPaymentMethodRows())
                    {
                        consumerTrustSettlementPaymentMethodRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                        try
                        {
                            moveToInfo.consumerTrustSettlementPaymentMethodRows.Add(new BaseRecord()
                            {
                                RowId = consumerTrustSettlementPaymentMethodRow.ConsumerTrustSettlementPaymentMethodId, RowVersion = consumerTrustSettlementPaymentMethodRow.RowVersion
                            });
                        }
                        finally
                        {
                            consumerTrustSettlementPaymentMethodRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                        }
                    }

                    //Get payment rows to move
                    foreach (ConsumerTrustPaymentRow settlementRowPaymentMethodRow in settlementRow.GetConsumerTrustPaymentRows())
                    {
                        settlementRowPaymentMethodRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                        try
                        {
                            moveToInfo.consumerTrustPaymentRows.Add(new BaseRecord()
                            {
                                RowId = settlementRowPaymentMethodRow.ConsumerTrustPaymentId, RowVersion = settlementRowPaymentMethodRow.RowVersion
                            });
                        }
                        finally
                        {
                            settlementRowPaymentMethodRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                        }
                    }
                }
                finally
                {
                    settlementRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                }
            }
        }
        /// <summary>
        /// Lock rows and collect the data.
        /// </summary>
        /// <param name="record"></param>
        /// <param name="blotterId"></param>
        /// <param name="dataModelTransaction"></param>
        /// <returns></returns>
        private MoveToInfo CollectData(BaseRecord record, Guid blotterId, DataModelTransaction dataModelTransaction)
        {
            MoveToInfo      moveToInfo      = new MoveToInfo();
            WorkingOrderRow workingOrderRow = DataModel.WorkingOrder.WorkingOrderKey.Find(record.RowId);

            MatchRow[] workingOrderRowMatchRows = null;

            if (workingOrderRow == null || workingOrderRow.RowState == DataRowState.Detached)
            {
                throw new FaultException <RecordNotFoundFault>(new RecordNotFoundFault("WorkingOrder", new object[] { record }));
            }

            workingOrderRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
            try
            {
                if (workingOrderRow.RowVersion != record.RowVersion)
                {
                    throw new FaultException <OptimisticConcurrencyFault>(new OptimisticConcurrencyFault("WorkingOrder", new object[] { record.RowId }),
                                                                          new FaultReason("OptimisticConcurrencyFault:  Someone updated this before you.  Please try again!"));
                }

                //Nothing to do
                if (blotterId == workingOrderRow.BlotterId)
                {
                    return(null);
                }

                workingOrderRowMatchRows = workingOrderRow.GetMatchRows();
            }
            finally
            {
                workingOrderRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
            }

            //Get all matches.
            if (workingOrderRowMatchRows != null)
            {
                moveToInfo.matchRows = new List <BaseRecord>();
                foreach (MatchRow matchRow in workingOrderRowMatchRows)
                {
                    matchRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                    try
                    {
                        moveToInfo.matchRows.Add(new BaseRecord()
                        {
                            RowId = matchRow.MatchId, RowVersion = matchRow.RowVersion
                        });

                        //Grab all the chat rows associated with this match.
                        foreach (ChatRow chatRow in matchRow.GetChatRows())
                        {
                            chatRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                            try
                            {
                                moveToInfo.chatRows.Add(new BaseRecord()
                                {
                                    RowId = chatRow.ChatId, RowVersion = chatRow.RowVersion
                                });
                            }
                            finally
                            {
                                chatRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                            }
                        }

                        //Grab all the negotiation rows associated with this match.
                        CollectNegotiationData(dataModelTransaction, moveToInfo, matchRow);
                    }
                    finally
                    {
                        matchRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                    }
                }
            }

            return(moveToInfo);
        }
        /// <summary>
        /// Collect negotiation and settlement data to move
        /// </summary>
        /// <param name="dataModelTransaction"></param>
        /// <param name="moveToInfo"></param>
        /// <param name="matchRow"></param>
        private static void CollectNegotiationData(DataModelTransaction dataModelTransaction, MoveToInfo moveToInfo, MatchRow matchRow)
        {
            foreach (ConsumerTrustNegotiationRow consumerTrustNegotiationRow in matchRow.GetConsumerTrustNegotiationRows())
            {
                consumerTrustNegotiationRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                try
                {
                    moveToInfo.consumerTrustNegotiationRows.Add(new BaseRecord()
                    {
                        RowId = consumerTrustNegotiationRow.ConsumerTrustNegotiationId, RowVersion = consumerTrustNegotiationRow.RowVersion
                    });


                    //Get counter payment rows to move
                    foreach (ConsumerTrustNegotiationCounterPaymentMethodRow counterPaymentMethodRow in consumerTrustNegotiationRow.GetConsumerTrustNegotiationCounterPaymentMethodRows())
                    {
                        counterPaymentMethodRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                        try
                        {
                            moveToInfo.consumerTrustNegotiationCounterPaymentMethodRows.Add(new BaseRecord()
                            {
                                RowId = counterPaymentMethodRow.ConsumerTrustNegotiationCounterPaymentMethodId, RowVersion = counterPaymentMethodRow.RowVersion
                            });
                        }
                        finally
                        {
                            counterPaymentMethodRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                        }
                    }

                    //Get offer payment rows to move
                    foreach (ConsumerTrustNegotiationOfferPaymentMethodRow offerPaymentMethodRow in consumerTrustNegotiationRow.GetConsumerTrustNegotiationOfferPaymentMethodRows())
                    {
                        offerPaymentMethodRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                        try
                        {
                            moveToInfo.consumerTrustNegotiationOfferPaymentMethodRows.Add(new BaseRecord()
                            {
                                RowId = offerPaymentMethodRow.ConsumerTrustNegotiationOfferPaymentMethodId, RowVersion = offerPaymentMethodRow.RowVersion
                            });
                        }
                        finally
                        {
                            offerPaymentMethodRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                        }
                    }

                    //Get Settlement rows to move
                    CollectSettlementData(dataModelTransaction, moveToInfo, consumerTrustNegotiationRow);
                }
                finally
                {
                    consumerTrustNegotiationRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                }
            }
        }
        /// <summary>
        /// Move this record to a new blotter
        /// </summary>
        /// <param name="blotterId"></param>
        /// <param name="record"></param>
        /// <returns></returns>
        public void MoveToBlotter(Guid blotterId, BaseRecord record)
        {
            //All the operations need to be done in a transaction.
            DataModelTransaction dataModelTransaction = DataModelTransaction.Current;
            DataModel            dataModel            = new DataModel();
            Guid modifiedByUser = TradingSupport.DaemonUserId;

            //Let the errors propogate back
            MoveToInfo moveToInfo = CollectData(record, blotterId, dataModelTransaction);

            //This will be null if there is nothing to do.
            if (moveToInfo == null)
            {
                return;
            }

            dataModel.UpdateWorkingOrder(
                null,
                blotterId,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                DateTime.UtcNow,
                modifiedByUser,
                null,
                record.RowVersion,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                new object[] { record.RowId });

            if (moveToInfo.matchRows != null)
            {
                //Move Match data
                MoveMatchRowsToBlotter(blotterId, modifiedByUser, dataModel, moveToInfo.matchRows);

                //Move Negotiatiaon Data
                MoveNegotiationRowsToBlotter(blotterId, modifiedByUser, dataModel, moveToInfo.consumerTrustNegotiationRows);
                MoveNegotiationCounterPaymentMethodRowsToBlotter(blotterId, dataModel, moveToInfo.consumerTrustNegotiationCounterPaymentMethodRows);
                MoveNegotiationOfferPaymentMethodRowsToBlotter(blotterId, dataModel, moveToInfo.consumerTrustNegotiationOfferPaymentMethodRows);
                MoveChatRowsToBlotter(blotterId, dataModel, moveToInfo.chatRows);

                //Move Settlement data
                MoveSettlementRowsToBlotter(blotterId, dataModel, moveToInfo.settlementRows);
                MoveSettlementPaymentMethodRowsToBlotter(blotterId, dataModel, moveToInfo.consumerTrustSettlementPaymentMethodRows);
                MovePaymentRowsToBlotter(blotterId, dataModel, moveToInfo.consumerTrustPaymentRows);
            }
        }