EDMI: Add CheckInOutFile Method
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
Imports System.Data.SqlClient
|
||||
Imports DigitalData.Modules.Base.IDB
|
||||
Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Database.MSSQLServer.TransactionMode
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Namespace Methods.IDB.CheckInOutFile
|
||||
Public Class CheckInOutFileMethod
|
||||
Inherits BaseMethod
|
||||
|
||||
Private ReadOnly Connection As SqlConnection
|
||||
Private ReadOnly Transaction As SqlTransaction
|
||||
|
||||
Public Sub New(pLogConfig As LogConfig, pDatabaseIDB As MSSQLServer, pDatabaseECM As MSSQLServer, pGlobalState As GlobalState)
|
||||
MyBase.New(pLogConfig, pDatabaseIDB, pDatabaseECM, pGlobalState)
|
||||
|
||||
Connection = DatabaseIDB.GetConnection()
|
||||
Transaction = Connection.BeginTransaction()
|
||||
End Sub
|
||||
|
||||
Public Function Run(pData As CheckInOutFileRequest) As CheckInOutFileResponse
|
||||
Try
|
||||
Dim oResult = False
|
||||
|
||||
If pData.Action = CheckInOutFileAction.CheckOut Then
|
||||
oResult = CheckOutFile(pData.ObjectId, pData.User.UserName, pData.Comment)
|
||||
|
||||
ElseIf pData.Action = CheckInOutFileAction.CheckIn Then
|
||||
oResult = CheckInFile(pData.ObjectId, pData.User.UserName)
|
||||
|
||||
Else
|
||||
LogAndThrow("Invalid action supplied!")
|
||||
|
||||
End If
|
||||
|
||||
If oResult = False Then
|
||||
LogAndThrow($"Could not Check In/Out file [{pData.ObjectId}]!")
|
||||
End If
|
||||
|
||||
Return New CheckInOutFileResponse(pData.ObjectId)
|
||||
Catch ex As Exception
|
||||
Logger.Warn("Error occurred while checkin in/out file!")
|
||||
Logger.Error(ex)
|
||||
|
||||
Logger.Info("Rolling back transaction.")
|
||||
Transaction?.Rollback()
|
||||
|
||||
Return New CheckInOutFileResponse(ex)
|
||||
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function CheckOutFile(pObjectId As Long, pUsername As String, pComment As String) As Boolean
|
||||
Try
|
||||
Dim oTable = TestFileIsCheckedOut(pObjectId)
|
||||
If oTable Is Nothing Then
|
||||
Return False
|
||||
End If
|
||||
|
||||
' If there are rows, the file is already checked out (either by the calling user or somebody else)
|
||||
If oTable.Rows.Count > 0 Then
|
||||
' TODO: Return the person who has this file checked out
|
||||
Return False
|
||||
End If
|
||||
|
||||
Dim oSQL = $"INSERT INTO TBIDB_OBJECT_CHECK_IN_OUT (IDB_OBJ_ID, CHECKED_OUT_WHEN, COMMENT, ADDED_WHO)
|
||||
VALUES ({pObjectId}, GETDATE(), '{pComment}', '{pUsername}')"
|
||||
Return DatabaseIDB.ExecuteNonQuery(oSQL)
|
||||
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return False
|
||||
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function CheckInFile(pObjectId As Long, pUsername As String) As Boolean
|
||||
Try
|
||||
Dim oTable = TestFileIsCheckedOut(pObjectId)
|
||||
If oTable Is Nothing Then
|
||||
Return False
|
||||
End If
|
||||
|
||||
' If there are no rows, the file is not checked out
|
||||
If oTable.Rows.Count > 0 Then
|
||||
Return True
|
||||
End If
|
||||
|
||||
Dim oSQL = $"UPDATE TBIDB_OBJECT_CHECK_IN_OUT
|
||||
SET CHECKED_IN_WHEN = GETDATE(), CHANGED_WHO = '{pUsername}'
|
||||
WHERE IDB_OBJ_ID = {pObjectId} AND ADDED_WHO = '{pUsername}' AND CHECKED_IN_WHEN IS NULL"
|
||||
Return DatabaseIDB.ExecuteNonQuery(oSQL)
|
||||
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return False
|
||||
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function TestFileIsCheckedOut(pObjectId As Long) As DataTable
|
||||
Try
|
||||
Dim oSqlCheck = $"SELECT * FROM TBIDB_OBJECT_CHECK_IN_OUT WHERE IDB_OBJ_ID = {pObjectId} AND CHECKED_IN_WHEN IS NULL"
|
||||
Dim oTable As DataTable = DatabaseIDB.GetDatatable(oSqlCheck)
|
||||
|
||||
Return oTable
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return Nothing
|
||||
|
||||
End Try
|
||||
End Function
|
||||
|
||||
|
||||
End Class
|
||||
|
||||
End Namespace
|
||||
@@ -0,0 +1,31 @@
|
||||
Imports System.Runtime.Serialization
|
||||
Imports DigitalData.Modules.ZooFlow.State
|
||||
|
||||
Namespace Methods.IDB.CheckInOutFile
|
||||
<Serializable>
|
||||
<DataContract>
|
||||
Public Class CheckInOutFileRequest
|
||||
<DataMember>
|
||||
Public Property ObjectId As Long
|
||||
|
||||
<DataMember>
|
||||
Public Property Comment As Long
|
||||
|
||||
<DataMember>
|
||||
Public Property Action As CheckInOutFileAction
|
||||
|
||||
''' <summary>
|
||||
''' User Importing the file
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
<DataMember>
|
||||
Public Property User As UserState
|
||||
End Class
|
||||
|
||||
Public Enum CheckInOutFileAction
|
||||
CheckIn
|
||||
CheckOut
|
||||
End Enum
|
||||
|
||||
End Namespace
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
Imports System.Runtime.Serialization
|
||||
|
||||
Namespace Methods.IDB.CheckInOutFile
|
||||
<Serializable>
|
||||
<DataContract>
|
||||
Public Class CheckInOutFileResponse
|
||||
Inherits Messages.BaseResponse
|
||||
|
||||
<DataMember>
|
||||
Public Property ObjectId As Long
|
||||
|
||||
Public Sub New(pObjectId As Long)
|
||||
MyBase.New()
|
||||
ObjectId = pObjectId
|
||||
End Sub
|
||||
|
||||
Public Sub New(pException As Exception, Optional pDetails As String = "")
|
||||
MyBase.New(pException, pDetails)
|
||||
End Sub
|
||||
End Class
|
||||
End Namespace
|
||||
Reference in New Issue
Block a user