/// <summary>
        /// Delegate for Settlement row change event
        /// </summary>
        /// <param name="key"></param>
        /// <param name="parameters"></param>
        private void SettlementRowAddProc(Object[] key, params Object[] parameters)
        {
            Guid settlementId = (Guid)key[0];
            ConsumerTrustSettlementRow rawSettlementRow = ((ConsumerTrustSettlementRowChangeEventArgs)key[1]).Row;

            ProcessSettlement(rawSettlementRow);
        }
        /// <summary>
        /// Initialize the GUI.
        /// </summary>
        /// <param name="parameter"></param>
        protected override void InitializeData(object parameter)
        {
            settlementLetter = null;
            Guid consumerTrustSettlementId = (Guid)parameter;

            if ((consumerTrustSettlementId != null) && (consumerTrustSettlementId != Guid.Empty))
            {
                // Lock the data model to extract information from the settlementRow.
                lock (DataModel.SyncRoot)
                {
                    ConsumerTrustSettlementRow consumerTrustSettlementRow = DataModel.ConsumerTrustSettlement.ConsumerTrustSettlementKey.Find(consumerTrustSettlementId);
                    if (!consumerTrustSettlementRow.IsSettlementLetterNull())
                    {
                        settlementLetter = Convert.FromBase64String(consumerTrustSettlementRow.SettlementLetter);
                    }
                }

                // Dispatch to foreground thread for updating the GUI.
                if (settlementLetter != null)
                {
                    this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SettlementLetterHandler(UpdateConsole), settlementLetter);
                }

                currentConsumerTrustSettlementId = consumerTrustSettlementId;
            }
            else
            {
                //Clear out when switching between Debt Classes.
                if ((IsLetterLoaded) && (consumerTrustSettlementId == Guid.Empty))
                {
                    // Sending null to clear out the settlement from the viewer.
                    this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SettlementLetterHandler(UpdateConsole), null);
                    settlementLetter = null;
                }
                else if ((currentConsumerTrustSettlementId != null) && (currentConsumerTrustSettlementId != Guid.Empty))
                {
                    // Lock the data model to extract information from the settlementRow.
                    lock (DataModel.SyncRoot)
                    {
                        ConsumerTrustSettlementRow consumerTrustSettlementRow = DataModel.ConsumerTrustSettlement.ConsumerTrustSettlementKey.Find(currentConsumerTrustSettlementId);
                        if (!consumerTrustSettlementRow.IsSettlementLetterNull())
                        {
                            settlementLetter = Convert.FromBase64String(consumerTrustSettlementRow.SettlementLetter);
                        }
                    }

                    // Dispatch to foreground thread for updating the GUI.
                    if (settlementLetter != null)
                    {
                        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SettlementLetterHandler(UpdateConsole), settlementLetter);
                    }
                }
            }
        }
        /// <summary>
        /// Extract information from the settlementRow to send to GCS
        /// </summary>
        /// <param name="rawSettlementRow"></param>
        private void ProcessSettlement(ConsumerTrustSettlementRow rawSettlementRow)
        {
            List <PaymentInfo> paymentInfoList  = new List <PaymentInfo>();
            String             gcsAccountNumber = String.Empty;

            try
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    // This provides a context for any transactions.
                    DataModelTransaction dataModelTransaction = DataModelTransaction.Current;

                    if (rawSettlementRow != null)
                    {
                        rawSettlementRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                        bool settlementDeletedOrDetached = false;
                        try
                        {
                            settlementDeletedOrDetached = rawSettlementRow.RowState == DataRowState.Deleted ||
                                                          rawSettlementRow.RowState == DataRowState.Detached;
                        }
                        finally
                        {
                            rawSettlementRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                        }

                        //If the settlement was removed by the time we got to this, then there is no need to continue
                        if (settlementDeletedOrDetached == true)
                        {
                            return;
                        }
                    }

                    RowLockingWrapper <ConsumerTrustSettlementRow> settlementRow = new RowLockingWrapper <ConsumerTrustSettlementRow>(rawSettlementRow, dataModelTransaction);
                    settlementRow.AcquireReaderLock();
                    try
                    {
                        //We need GCS account id for this payment.  We cannot find a GCS account number then we cannot process this payment.
                        Guid creditCardId = Guid.Empty;
                        ConsumerTrustNegotiationRow negotiationRow = settlementRow.TypedRow.ConsumerTrustNegotiationRow;
                        negotiationRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                        try
                        {
                            creditCardId = negotiationRow.CreditCardId;
                        }
                        finally
                        {
                            negotiationRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                            //no longer usable
                            negotiationRow = null;
                        }

                        //HACK - add error reporting
                        if (creditCardId == Guid.Empty)
                        {
                            return;
                        }

                        //Determine the consumer
                        Guid          consumerId    = Guid.Empty;
                        CreditCardRow creditcardRow = DataModel.CreditCard.CreditCardKey.Find(creditCardId);
                        creditcardRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                        try
                        {
                            consumerId = creditcardRow.ConsumerId;
                        }
                        finally
                        {
                            creditcardRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                            //no longer usable
                            creditcardRow = null;
                        }

                        //HACK - add error reporting
                        if (consumerId == Guid.Empty)
                        {
                            return;
                        }

                        //Determine the consumerTrust
                        ConsumerTrustRow consumerTrustRow = null;
                        ConsumerRow      consumerRow      = DataModel.Consumer.ConsumerKey.Find(consumerId);
                        consumerRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                        try
                        {
                            consumerTrustRow = consumerRow.GetConsumerTrustRows_NoLockCheck().First();
                        }
                        finally
                        {
                            consumerRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                            //no longer usable
                            consumerRow = null;
                        }

                        //HACK - add error reporting
                        if (consumerTrustRow == null)
                        {
                            return;
                        }

                        consumerTrustRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                        try
                        {
                            gcsAccountNumber = (consumerTrustRow.IsSavingsAccountNull()) ? String.Empty : consumerTrustRow.SavingsAccount;
                        }
                        finally
                        {
                            consumerTrustRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                            //no longer usable
                            consumerTrustRow = null;
                        }

                        //HACK - add error reporting
                        if (String.IsNullOrEmpty(gcsAccountNumber))
                        {
                            return;
                        }


                        foreach (ConsumerTrustPaymentRow paymentRow in settlementRow.TypedRow.GetConsumerTrustPaymentRows())
                        {
                            paymentRow.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                            try
                            {
                                if (paymentRow.RowState != DataRowState.Deleted &&
                                    paymentRow.RowState != DataRowState.Detached)
                                {
                                    paymentInfoList.Add(new PaymentInfo(paymentRow));
                                }
                            }
                            finally
                            {
                                paymentRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
                            }
                        }
                    }
                    finally
                    {
                        settlementRow.ReleaseReaderLock();
                    }
                }
            }
            catch (Exception ex)
            {
                EventLog.Error(ex);
            }

            if (String.IsNullOrEmpty(gcsAccountNumber) == false && paymentInfoList.Count != 0)
            {
                SendPaymentsToGCS(gcsAccountNumber, paymentInfoList);
            }
        }
 /// <summary>
 /// TODO: Review and determine if this method is used?
 /// </summary>
 /// <param name="settlementRow"></param>
 /// <returns></returns>
 private bool FilterBlotters(ConsumerTrustSettlementRow settlementRow)
 {
     return(this.blotterList.BinarySearch(settlementRow.BlotterId) >= 0);
 }
 private Schema.DebtNegotiatorSettlement.Settlement SettlementSelector(ConsumerTrustSettlementRow consumerTrustSettlementRow)
 {
     Schema.DebtNegotiatorSettlement.Settlement settlement = new Schema.DebtNegotiatorSettlement.Settlement();
     return(settlement.Select(consumerTrustSettlementRow));
 }