private static void Execution_ExecutionRowDeleted(object sender, MarkThree.Guardian.DataSetMarket.ExecutionRowChangeEvent e) { int destinationOrderId = (int)e.Row[ServerMarketData.Execution.DestinationOrderIdColumn, e.Row.RowState == DataRowState.Deleted ? DataRowVersion.Original : DataRowVersion.Current]; ServerMarketData.DestinationOrderRow destinationOrderRow = ServerMarketData.DestinationOrder.FindByDestinationOrderId(destinationOrderId); if (destinationOrderRow == null) { return; } decimal orderQuantity = destinationOrderRow.OrderedQuantity - destinationOrderRow.CanceledQuantity; decimal executionQuantity = 0.0m; foreach (ServerMarketData.ExecutionRow executionRow in destinationOrderRow.GetExecutionRows()) { executionQuantity += executionRow.ExecutionQuantity; } if (executionQuantity < orderQuantity && executionQuantity != 0.0m && destinationOrderRow.StatusCode != Status.PartiallyFilled) { BusinessRules.businessRuleQueue.Enqueue(new ObjectAction(new ObjectHandler(SetDestinationOrderStatus), new object[] { destinationOrderId }, null, Status.PartiallyFilled)); } if (executionQuantity == 0.0m && destinationOrderRow.StatusCode != Status.New) { BusinessRules.businessRuleQueue.Enqueue(new ObjectAction(new ObjectHandler(SetDestinationOrderStatus), new object[] { destinationOrderId }, null, Status.New)); } }
/// <summary>Archives a DestinationOrder record.</summary> /// <param name="transaction">Commits or rejects a set of commands as a unit</param> /// <param name="RowVersion">The version number of this row.</param> /// <param name="destinationOrderId">The value for the DestinationOrderId column.</param> /// <param name="archive">true to archive the object, false to unarchive it.</param> public static void Archive(AdoTransaction adoTransaction, SqlTransaction sqlTransaction, long rowVersion, int destinationOrderId) { // Accessor for the DestinationOrder Table. ServerMarketData.DestinationOrderDataTable destinationOrderTable = ServerMarketData.DestinationOrder; // Rule #1: Make sure the record exists before updating it. ServerMarketData.DestinationOrderRow destinationOrderRow = destinationOrderTable.FindByDestinationOrderId(destinationOrderId); if ((destinationOrderRow == null)) { throw new Exception(string.Format("The DestinationOrder table does not have an element identified by {0}", destinationOrderId)); } // Rule #2: Optimistic Concurrency Check if ((destinationOrderRow.RowVersion != rowVersion)) { throw new System.Exception("This record is busy. Please try again later."); } // Archive the child records. for (int index = 0; (index < destinationOrderRow.GetExecutionRows().Length); index = (index + 1)) { ServerMarketData.ExecutionRow childExecutionRow = destinationOrderRow.GetExecutionRows()[index]; Execution.Archive(adoTransaction, sqlTransaction, childExecutionRow.RowVersion, childExecutionRow.ExecutionId); } // Increment the row version rowVersion = ServerMarketData.RowVersion.Increment(); // Delete the record in the ADO database. destinationOrderRow[destinationOrderTable.RowVersionColumn] = rowVersion; adoTransaction.DataRows.Add(destinationOrderRow); destinationOrderRow.Delete(); // Archive the record in the SQL database. SqlCommand sqlCommand = new SqlCommand("update \"DestinationOrder\" set \"IsArchived\" = 1 where \"DestinationOrderId\"=@destin" + "ationOrderId"); sqlCommand.Connection = sqlTransaction.Connection; sqlCommand.Transaction = sqlTransaction; sqlCommand.Parameters.Add(new SqlParameter("@destinationOrderId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, destinationOrderId)); sqlCommand.ExecuteNonQuery(); }