Imports C1.Data.Transactions
Imports C1.LiveLinq
Imports C1.Silverlight.Data.RiaServices

Partial Public Class OrderWindow
    Inherits ChildWindow

    ' All changes in this form are made in the scope of this client transaction.
    Private _transaction As ClientTransaction
    ' The order being edited.
    Private _order As Web.Order
    Private _customerToAddOrderTo As Web.Customer

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub New(order As Web.Order, cache As RiaClientCache, transaction As ClientTransaction, customerToAddOrderTo As Web.Customer)
        InitializeComponent()
        _order = order
        _customerToAddOrderTo = customerToAddOrderTo

        ' Create a child transaction. Rolling back the child transaction back will not roll back the parent transaction.
        _transaction = New ClientTransaction(transaction)

        ' Create a proxy for the order that automatically opens the transaction scope
        ' each time a value is assigned to a property.
        DataContext = transaction.ScopeDataContext(order)

        ' Define a live view of order details.
        Dim orderDetailsView = From od In order.Order_Details.AsLive(cache)
                               Select New OrderDetailsInfo With
                               {
                                   .OrderID = od.OrderID,
                                   .ProductID = od.ProductID,
                                   .UnitPrice = od.UnitPrice,
                                   .Discount = od.Discount,
                                   .Quantity = od.Quantity
                               }
        ' All changes in this view are made in the transaction scope.
        orderDetailsView.SetTransaction(_transaction)
        orderDetailsGrid.ItemsSource = orderDetailsView
    End Sub

    Private Sub ChildWindow_Closed(sender As System.Object, e As System.EventArgs)
        If _transaction.State = TransactionState.Open Then
            _transaction.Rollback() ' this is required because otherwise _transaction would not be immediately disposed if the window is closed by other means than PK/Cancel buttons
        End If
    End Sub

    Private Sub ok_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        ' Add the order in the transaction scope and commit the transaction.
        If Not (_customerToAddOrderTo Is Nothing) Then
            Using scope As Object = _transaction.Scope()
                _customerToAddOrderTo.Orders.Add(_order)
            End Using
        End If
        _transaction.Commit()
        DialogResult = True
    End Sub

    Private Sub cancel_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        ' Cancel the changes made by this window.
        _transaction.Rollback()
        DialogResult = False
    End Sub
End Class

' This class is used as the result item type of an order detail view defined above.
Public Class OrderDetailsInfo
    Public Overridable Property OrderID As Integer
    Public Overridable Property ProductID As Integer
    Public Overridable Property UnitPrice As Decimal
    Public Overridable Property Discount As Single
    Public Overridable Property Quantity As Decimal
End Class
