diff --git a/Service.EDMIService/EDMIService.vbproj b/Service.EDMIService/EDMIService.vbproj index e6bb97f9..00656f80 100644 --- a/Service.EDMIService/EDMIService.vbproj +++ b/Service.EDMIService/EDMIService.vbproj @@ -131,6 +131,7 @@ + diff --git a/Service.EDMIService/IDB/Helpers.vb b/Service.EDMIService/IDB/Helpers.vb index 9fdff60a..c677630d 100644 --- a/Service.EDMIService/IDB/Helpers.vb +++ b/Service.EDMIService/IDB/Helpers.vb @@ -1,4 +1,5 @@ Imports DigitalData.Modules.Database +Imports DigitalData.Modules.Database.MSSQLServer Imports DigitalData.Modules.Language Imports DigitalData.Modules.Logging Imports DigitalData.Modules.EDMI.API.Client @@ -72,6 +73,87 @@ Namespace IDB End Try End Function + Public Function TestFileChecksumExists(pChecksum As String) As Long + Try + Dim oChecksumSQL = $"SELECT IDB_OBJ_ID FROM TBIDB_FILE_OBJECT WHERE FILE_HASH = '{pChecksum}'" + Dim oExistingObjectId As Long = Database.GetScalarValue(oChecksumSQL) + + If oExistingObjectId > 0 Then + Logger.Info("Returning early with ObjectId [{0}] because Checksum [{1}] already exists.", oExistingObjectId, pChecksum) + Return oExistingObjectId + End If + + Return Nothing + + Catch ex As Exception + Logger.Error(ex) + Return Nothing + + End Try + End Function + + Public Function NewObjectId(pKindType As String, pBusinessEntity As String, pWho As String) As Long + Try + Dim oNewObjectIdSQL = $"DECLARE @NEW_IDB_OBJ_ID BIGINT + EXEC PRIDB_NEW_OBJECT '{pKindType}','{pWho}','{pBusinessEntity}',0, @IDB_OBJ_ID = @NEW_IDB_OBJ_ID OUTPUT; + SELECT @NEW_IDB_OBJ_ID" + Dim oObjectId As Long = Database.GetScalarValue(oNewObjectIdSQL) + + Logger.Info("New Object with Id [{0}] created!", oObjectId) + + If IsNothing(oObjectId) Then + LogAndThrow("Could not create new ObjectId!") + End If + + Return oObjectId + Catch ex As Exception + Logger.Error(ex) + Return Nothing + End Try + End Function + + Public Function NewObjectIdWithTransaction(pKindType As String, pBusinessEntity As String, pWho As String, pConnection As SqlConnection, pTransaction As SqlTransaction) As Long + Try + Dim oNewObjectIdSQL = $"DECLARE @NEW_IDB_OBJ_ID BIGINT + EXEC PRIDB_NEW_OBJECT '{pKindType}','{pWho}','{pBusinessEntity}',0, @IDB_OBJ_ID = @NEW_IDB_OBJ_ID OUTPUT; + SELECT @NEW_IDB_OBJ_ID" + Dim oObjectId As Long = Database.GetScalarValueWithConnectionObject(oNewObjectIdSQL, + pConnection, + TransactionMode.ExternalTransaction, + pTransaction) + + Logger.Info("New Object with Id [{0}] created!", oObjectId) + + If IsNothing(oObjectId) Then + LogAndThrow("Could not create new ObjectId!") + End If + + Return oObjectId + Catch ex As Exception + Logger.Error(ex) + Return Nothing + End Try + End Function + + Public Function GetObject(pObjectId As Long) As IDBObject + Try + Dim oGetObjectSQL = $"SELECT * FROM VWIDB_OBJECT WHERE IDB_OBJ_ID = {pObjectId}" + Dim oRow As DataTable = Database.GetDatatable(oGetObjectSQL) + + If oRow Is Nothing Then + Logger.Warn("Object with Id [{0}] not found.", pObjectId) + Return Nothing + + End If + + 'TODO: Return object data + + Catch ex As Exception + Logger.Error(ex) + Return Nothing + End Try + End Function + 'Public Function GetAttributesForObject(pObjectId As Long, pLanguage As String) As List(Of ObjectAttribute) ' Dim oAttributes As New List(Of ObjectAttribute) diff --git a/Service.EDMIService/IDB/IDBObject.vb b/Service.EDMIService/IDB/IDBObject.vb new file mode 100644 index 00000000..ba79a508 --- /dev/null +++ b/Service.EDMIService/IDB/IDBObject.vb @@ -0,0 +1,7 @@ +Namespace IDB + Public Class IDBObject + Public Property ObjectId As Long + Public Property ObjectKind As Long + Public Property BusinessEntity As Long + End Class +End Namespace \ No newline at end of file diff --git a/Service.EDMIService/Methods/IDB/NewFile/NewFileMethod.vb b/Service.EDMIService/Methods/IDB/NewFile/NewFileMethod.vb index 88240d8e..28cadaf8 100644 --- a/Service.EDMIService/Methods/IDB/NewFile/NewFileMethod.vb +++ b/Service.EDMIService/Methods/IDB/NewFile/NewFileMethod.vb @@ -20,14 +20,14 @@ Namespace Methods.IDB.NewFile Public Function Run(pData As NewFile.NewFileRequest) As NewFile.NewFileResponse Dim oFilePath As String = Nothing - Dim oExistingObjectId = TestFileChecksumExists(pData.File.FileChecksum) + Dim oExistingObjectId = Helpers.TestFileChecksumExists(pData.File.FileChecksum) If oExistingObjectId > 0 Then Return New NewFile.NewFileResponse(oExistingObjectId) End If Try - Dim oObjectId = NewObjectId(pData.KindType, pData.BusinessEntity, pData.User.UserName) + Dim oObjectId = Helpers.NewObjectIdWithTransaction(pData.KindType, pData.BusinessEntity, pData.User.UserName, Connection, Transaction) If oObjectId = 0 Then LogAndThrow("Could not create new ObjectId!") End If @@ -176,44 +176,6 @@ Namespace Methods.IDB.NewFile End Try End Function - Private Function TestFileChecksumExists(pChecksum As String) As Long - Try - Dim oChecksumSQL = $"SELECT IDB_OBJ_ID FROM TBIDB_FILE_OBJECT WHERE FILE_HASH = '{pChecksum}'" - Dim oExistingObjectId As Long = DatabaseIDB.GetScalarValue(oChecksumSQL) - - If oExistingObjectId > 0 Then - Logger.Info("Returning early with ObjectId [{0}] because Checksum [{1}] already exists.", oExistingObjectId, pChecksum) - Return oExistingObjectId - End If - - Return Nothing - - Catch ex As Exception - Logger.Error(ex) - Return Nothing - - End Try - End Function - - Private Function NewObjectId(pKindType As String, pBusinessEntity As String, pWho As String) As Long - Try - Dim oNewObjectIdSQL = $"DECLARE @NEW_IDB_OBJ_ID BIGINT - EXEC PRIDB_NEW_OBJECT '{pKindType}','{pWho}','{pBusinessEntity}',0, @IDB_OBJ_ID = @NEW_IDB_OBJ_ID OUTPUT; - SELECT @NEW_IDB_OBJ_ID" - Dim oObjectId As Long = DatabaseIDB.GetScalarValueWithConnectionObject(oNewObjectIdSQL, Connection, ExternalTransaction, Transaction) - Logger.Info("New Object with Id [{0}] created!", oObjectId) - - If IsNothing(oObjectId) Then - LogAndThrow("Could not create new ObjectId!") - End If - - Return oObjectId - Catch ex As Exception - Logger.Error(ex) - Return Nothing - End Try - End Function - Private Function GetFileObjectFileName(IDB_OBJ_ID As Long, pFilename As String, pKeepFilename As Boolean) As String ' TODO: save actual extensions If pKeepFilename Then