Imports DigitalData.Modules.EDMI.API.EDMIServiceReference
Imports DigitalData.Modules.Logging

Namespace Modules.IDB
    Public Class CheckOutFile
        Inherits BaseMethod

        Public Sub New(pLogConfig As LogConfig, pChannel As IEDMIServiceChannel)
            MyBase.New(pLogConfig, pChannel)
        End Sub

        Public Async Function RunAsync(pObjectId As Long, Optional pComment As String = "", Optional pOptions As Options.CheckOutInOptions = Nothing) As Task(Of Long)
            ' Set default options
            If pOptions Is Nothing Then
                pOptions = New Options.CheckOutInOptions()
            End If

            Dim oArgs = New CheckInOutFileRequest With {
                .User = New UserState With {
                    .Language = pOptions.Language,
                    .UserName = pOptions.Username
                },
                .Action = CheckInOutFileAction.CheckOut,
                .Comment = pComment,
                .ObjectId = pObjectId
            }
            Dim oCheckOutFileResponse = Await Channel.CheckInOutFileAsync(oArgs)

            If oCheckOutFileResponse.OK = False Then
                Throw New ApplicationException(oCheckOutFileResponse.ErrorMessage)
            End If

            Return oCheckOutFileResponse.ObjectId
        End Function
    End Class

End Namespace
