EDMI.API: WIP Client

This commit is contained in:
Jonathan Jenne 2021-07-19 16:40:46 +02:00
parent 9168474530
commit 464dc1d4ad

View File

@ -18,43 +18,12 @@ Public Class Client
Private _dummy_table_attributes As DataTable Private _dummy_table_attributes As DataTable
Private _channel As IEDMIServiceChannel Private _channel As IEDMIServiceChannel
Public Class StreamedFile
Public Stream As MemoryStream
Public FileName As String
End Class
Public Class FileList
Public Datatable As DataTable
End Class
Public Class DocumentInfo
Public FullPath As String
Public AccessRight As AccessRight
End Class
Public Class VariableValue
Public ReadOnly Property IsVector As Boolean = False
Public Property Value As Object
Public Property Type As Type
Public Sub New(pValue As Object)
' Check if value is a collection
If TypeOf pValue Is IEnumerable Then
IsVector = True
End If
' Try to determine the type
If IsNothing(pValue) Then
Type = Nothing
Else
Type = pValue.GetType
End If
Value = pValue
End Sub
End Class
''' <summary>
''' Parse a IPAddress:Port String into its parts
''' </summary>
''' <param name="AddressWithOptionalPort"></param>
''' <returns></returns>
Public Shared Function ParseServiceAddress(AddressWithOptionalPort As String) As Tuple(Of String, Integer) Public Shared Function ParseServiceAddress(AddressWithOptionalPort As String) As Tuple(Of String, Integer)
Dim oSplit() As String = AddressWithOptionalPort.Split(":"c) Dim oSplit() As String = AddressWithOptionalPort.Split(":"c)
Dim oAppServerAddress As String = oSplit(0) Dim oAppServerAddress As String = oSplit(0)
@ -131,10 +100,6 @@ Public Class Client
End Try End Try
End Function End Function
Private Function PreloadAttributes()
End Function
''' <summary> ''' <summary>
''' TODO: Creates a new object ''' TODO: Creates a new object
''' </summary> ''' </summary>
@ -143,23 +108,7 @@ Public Class Client
Throw New NotImplementedException() Throw New NotImplementedException()
End Function End Function
''' <summary>
''' Import options for NewFileAsync.
''' </summary>
Public Class NewFileOptions
''' <summary>
''' Option to keep the original extension when importing. Defaults to false.
''' </summary>
Public KeepExtension As Boolean = False
''' <summary>
''' Windows username of the user responsible for the import. Defaults to the currently logged in user.
''' </summary>
Public Username As String = Environment.UserName
''' <summary>
''' Date when the file was imported. Can be in the past. Defaults to now.
''' </summary>
Public DateImported As Date = Date.Now
End Class
''' <summary> ''' <summary>
''' Imports a file from a filepath, creating a IDB ObjectId and Filesystem Object ''' Imports a file from a filepath, creating a IDB ObjectId and Filesystem Object
@ -236,30 +185,27 @@ Public Class Client
End Try End Try
End Function End Function
Public Class SetVariableValueOptions
Public CheckDeleted As Boolean = False
Public Username As String = String.Empty
Public Language As String = String.Empty
End Class
''' <summary> ''' <summary>
''' Sets a value to an attribute ''' Sets a value to an attribute
''' </summary> ''' </summary>
''' <param name="pObjectId">IDB ObjectId</param> ''' <param name="pObjectId">IDB ObjectId</param>
''' <param name="pAttributeName">Name of the attribute</param> ''' <param name="pAttributeName">Name of the attribute</param>
''' <param name="pAttributeType">The type of Attribute</param>
''' <param name="pValue"></param> ''' <param name="pValue"></param>
''' <param name="pOptions"></param> ''' <param name="pOptions"></param>
''' <returns></returns> ''' <returns></returns>
Public Function SetVariableValue(pObjectId As Long, pAttributeName As String, pAttributeType As AttributeType, pValue As Object, Optional pOptions As SetVariableValueOptions = Nothing) As Boolean Public Function SetVariableValue(pObjectId As Long, pAttributeName As String, pValue As Object, Optional pOptions As SetVariableValueOptions = Nothing) As Boolean
Try Try
' Set default options ' Set default options
If pOptions Is Nothing Then If pOptions Is Nothing Then
pOptions = New SetVariableValueOptions() pOptions = New SetVariableValueOptions()
End If End If
Dim oLanguage = NotNull(pOptions.Language, GetUserLanguage()) Dim oLanguage = GetUserLanguage(pOptions.UserLanguage)
Dim oUsername = NotNull(pOptions.Username, Environment.UserName) Dim oUsername = GetUserName(pOptions.UserName)
Dim oOptions As New GetVariableValueOptions With {
.UserLanguage = oLanguage,
.UserName = oUsername
}
Try Try
Dim oResponse = _channel.TestObjectIdExists(New TestObjectIdExistsRequest With {.ObjectId = pObjectId}) Dim oResponse = _channel.TestObjectIdExists(New TestObjectIdExistsRequest With {.ObjectId = pObjectId})
@ -272,22 +218,14 @@ Public Class Client
Return False Return False
End Try End Try
' TODO: Check if Attribute exists
Dim oType = pValue.GetType.Name Dim oType = pValue.GetType.Name
If oType = GetType(DataTable).Name Then If oType = GetType(DataTable).Name Then
Dim oValueTable As DataTable = pValue Dim oValueTable As DataTable = pValue
Dim oCurrentValue As VariableValue Dim oCurrentValue As VariableValue
If pOptions.CheckDeleted = True Then
Dim oOptions As New GetVariableValueOptions With {
.Language = oLanguage,
.Username = oUsername
}
' Get current value ' Get current value
oCurrentValue = GetVariableValue(pObjectId, pAttributeName, pAttributeType, oOptions) oCurrentValue = GetVariableValue(pObjectId, pAttributeName, oOptions)
' If current value is datatable ' If current value is datatable
If oCurrentValue.Type = GetType(DataTable) Then If oCurrentValue.Type = GetType(DataTable) Then
@ -301,7 +239,8 @@ Public Class Client
For Each oRow As DataRow In oTable.Rows For Each oRow As DataRow In oTable.Rows
Dim oExists As Boolean = False Dim oExists As Boolean = False
For Each oNewValueRow As DataRow In oValueTable.Rows For Each oNewValueRow As DataRow In oValueTable.Rows
Dim oInfo = $"Checking oldValue[{oRow.Item(0)}] vs NewValue [{oNewValueRow.Item(1)}]" _logger.Debug($"Checking oldValue[{oCurrentValue}] vs NewValue [{oNewValueRow.Item(1)}]")
If oNewValueRow.Item(1).ToString.ToUpper = oRow.Item(0).ToString.ToUpper Then If oNewValueRow.Item(1).ToString.ToUpper = oRow.Item(0).ToString.ToUpper Then
oExists = True oExists = True
Exit For Exit For
@ -317,6 +256,8 @@ Public Class Client
End If End If
Else Else
If oValueTable.Rows.Count > 1 Then If oValueTable.Rows.Count > 1 Then
'now Checking whether the old row still remains in Vector? If not it will be deleted as it cannot be replaced in multivalues
Dim oExists As Boolean = False Dim oExists As Boolean = False
For Each oNewValueRow As DataRow In oValueTable.Rows For Each oNewValueRow As DataRow In oValueTable.Rows
_logger.Debug($"Checking oldValue[{oCurrentValue}] vs NewValue [{oNewValueRow.Item(1)}]") _logger.Debug($"Checking oldValue[{oCurrentValue}] vs NewValue [{oNewValueRow.Item(1)}]")
@ -324,7 +265,6 @@ Public Class Client
If oNewValueRow.Item(1).ToString.ToUpper = oCurrentValue.ToString.ToUpper Then If oNewValueRow.Item(1).ToString.ToUpper = oCurrentValue.ToString.ToUpper Then
oExists = True oExists = True
Exit For Exit For
End If End If
Next Next
@ -341,27 +281,22 @@ Public Class Client
End If End If
End If
For Each oNewValueRow As DataRow In oValueTable.Rows For Each oNewValueRow As DataRow In oValueTable.Rows
Dim oSuccess As Boolean = False Dim oResult As Boolean = NewObjectData(pObjectId, pAttributeName, pValue, New NewObjectOptions With {
Dim oSQL = $"DECLARE @NEW_OBJ_MD_ID BIGINT .UserLanguage = oLanguage,
EXEC PRIDB_NEW_OBJ_DATA({pObjectId}, '{pAttributeName}', '{oUsername}', '{oNewValueRow.Item(1)}', '{oLanguage}', 0, @OMD_ID = @NEW_OBJ_MD_ID OUTPUT)" .UserName = oUsername
})
Dim oResult = _channel.ExecuteNonQuery_MSSQL_IDB(oSQL) If oResult = False Then
If Not oResult.OK Then
Return False Return False
End If End If
Next Next
Return True Return True
Else Else
Dim oSql = $"DECLARE @NEW_OBJ_MD_ID BIGINT Return NewObjectData(pObjectId, pAttributeName, pValue, New NewObjectOptions With {
EXEC PRIDB_NEW_OBJ_DATA({pObjectId}, '{pAttributeName}', '{oUsername}', '{pValue}', '{oLanguage}', 0, @OMD_ID = @NEW_OBJ_MD_ID OUTPUT)" .UserLanguage = oLanguage,
.UserName = oUsername
Dim oResult = _channel.ExecuteNonQuery_MSSQL_IDB(oSql) })
Return oResult.OK
End If End If
Catch ex As Exception Catch ex As Exception
@ -370,112 +305,60 @@ Public Class Client
End Try End Try
End Function End Function
Public Class GetVariableValueOptions ''' <summary>
Public Username As String = String.Empty '''
Public Language As String = String.Empty ''' </summary>
End Class ''' <param name="pObjectId"></param>
''' <param name="pAttributeName"></param>
Public Function GetVariableValue(pObjectId As Long, pAttributeName As String, pAttributeType As AttributeType, Optional pOptions As GetVariableValueOptions = Nothing) As VariableValue ''' <param name="pOptions"></param>
''' <returns></returns>
Public Function GetVariableValue(pObjectId As Long, pAttributeName As String, Optional pOptions As GetVariableValueOptions = Nothing) As VariableValue
If pOptions Is Nothing Then If pOptions Is Nothing Then
pOptions = New GetVariableValueOptions() pOptions = New GetVariableValueOptions()
End If End If
pOptions.Language = NotNull(pOptions.Language, GetUserLanguage()) pOptions.UserLanguage = GetUserLanguage(pOptions.UserLanguage)
pOptions.Username = NotNull(pOptions.Username, Environment.UserName) pOptions.UserName = GetUserName(pOptions.UserName)
' Check if ObjectId exists ' Check if ObjectId exists
Try Try
Dim oResponse = _channel.TestObjectIdExists(New TestObjectIdExistsRequest With {.ObjectId = pObjectId}) Dim oResponse = _channel.TestObjectIdExists(New TestObjectIdExistsRequest With {.ObjectId = pObjectId})
If oResponse.Exists = False Then If oResponse.Exists = False Then
Return Nothing Return New VariableValue()
End If End If
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _logger.Error(ex)
Return Nothing Return New VariableValue()
End Try End Try
' Get Attributes and Values from Database
Dim oAttributes As DataTable = GetAttributesForObject(pObjectId, pOptions.Language)
' TODO: Check if Attribute exists & REfactor
Try Try
Dim oVectorAttribute As Boolean = False ' Get Attributes and Values from Database
Select Case pAttributeType Dim oAttributes As List(Of ObjectAttribute) = GetAttributesForObject(pObjectId, pOptions.UserLanguage)
Case AttributeType.VectorInteger Dim oValues = oAttributes.AsEnumerable().
oVectorAttribute = True Where(Function(pAttr)
Case AttributeType.VectorString Return pAttr.Title.ToUpper = pAttributeName.ToUpper
oVectorAttribute = True End Function).
Case Else Select(Function(pAttr) pAttr.Value).
oVectorAttribute = False
End Select
Dim oValues As New List(Of Object)
Dim oRows As List(Of DataRow) = oAttributes.AsEnumerable().
Where(Function(pRow As DataRow) pRow.Item("AttributeTitle") = pAttributeName).
ToList() ToList()
'If Not IsNothing(oAttributes) Then ' Either return a list or a single value or nothing, always wrapped in VariableValue
' If oVectorAttribute = True And _dummy_table_attributes.Rows.Count = 1 And pOptions.FromIDB = False Then If oValues.Count > 1 Then
' Try
' If pAttributeName = "IDBCreatedWhen" Then
' pAttributeName = "ADDED_WHEN"
' ElseIf pAttributeName = "IDBCreatedWho" Then
' pAttributeName = "ADDED_WHO"
' ElseIf pAttributeName = "IDBChangedWhen" Then
' pAttributeName = "CHANGED_WHEN"
' ElseIf pAttributeName = "IDBChangedWho" Then
' pAttributeName = "CHANGED_WHO"
' End If
' oAttributeValue = _dummy_table_attributes.Rows(0).Item(pAttributeName)
' Catch ex As Exception
' _logger.Debug($"Error getting Attribute from IDB_DT_DOC_DATA: {ex.Message}")
' End Try
' End If
'Else
' Throw New ApplicationException($"Could not get Attributes for ObjectId [{pObjectId}]")
'End If
'TODO: BRAUCHEN SIE DAS ÜBERHAUPT??????11111?!!1!1
'If Not IsNothing(oAttributeValue) Then
' Return oAttributeValue
'Else
' _logger.Debug($"oAttributeValue for Attribute [{pAttributeName}] is so far nothing..Now trying FNIDB_PM_GET_VARIABLE_VALUE ")
'End If
'Dim oFNSQL = $"SELECT * FROM [dbo].[FNIDB_PM_GET_VARIABLE_VALUE] ({pObjectId},'{pAttributeName}','{pOptions.Language}',CONVERT(BIT,'0'))"
'Dim oDatatable As TableResult = _channel.ReturnDatatable_MSSQL_IDB(oFNSQL)
'If oDatatable.OK = False Then
' Throw New ApplicationException(oDatatable.ErrorMessage)
'End If
'If oDatatable.Table.Rows.Count = 1 Then
' oAttributeValue = oDatatable.Table.Rows.Item(0).Item(0)
'End If
If oVectorAttribute = False Then
Dim oRow As DataRow = oRows.FirstOrDefault()
Dim oType As String = oRow.Item("AttributeType")
Dim oValue = GetValueByType(oRow, oType)
'oValues.Add(oValue)
Return New VariableValue(oValue)
Else
For Each oRow As DataRow In oRows
Dim oType As String = oRow.Item("AttributeType")
Dim oValue = GetValueByType(oRow, oType)
oValues.Add(oValue)
Next
Return New VariableValue(oValues) Return New VariableValue(oValues)
ElseIf oValues.Count = 1 Then
Return New VariableValue(oValues.First())
Else
Return New VariableValue()
End If End If
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _logger.Error(ex)
Return Nothing Return New VariableValue()
End Try End Try
End Function End Function
@ -525,11 +408,62 @@ Public Class Client
End Try End Try
End Function End Function
Private Function GetAttributesForObject(pObjectId As Long, pLanguage As String) As DataTable Private Class ObjectAttribute
Dim oTable As DataTable Public Property Id As Long
Public Property Title As String
Public Property Type As String
Public Property ValueBigInt As Long
Public Property ValueText As String
Public Property ValueDecimal As Decimal
Public Property ValueDate As DateTime
Public ReadOnly Property Value As Object
Get
Return GetValue()
End Get
End Property
Private Function GetValue() As Object
Select Case Type
Case AttributeTypeName.VECTOR_INTEGER
Return ValueBigInt
Case AttributeTypeName.BIG_INTEGER
Return ValueBigInt
Case AttributeTypeName.VECTOR_STRING
Return ValueText
Case AttributeTypeName.VARCHAR
Return ValueText
Case AttributeTypeName.BIT
Return IIf(ValueBigInt = 1, True, False)
Case AttributeTypeName.DATE
Return ValueDate
Case AttributeTypeName.DATETIME
Return ValueDate
Case AttributeTypeName.DECIMAL
Return ValueDecimal
Case AttributeTypeName.FLOAT
Return ValueDecimal
Case Else
Return Nothing
End Select
End Function
End Class
Private Function GetAttributesForObject(pObjectId As Long, pLanguage As String) As List(Of ObjectAttribute)
Dim oAttributes As New List(Of ObjectAttribute)
Try Try
Dim oResult As TableResult = _channel.ReturnDatatable_MSSQL_IDB($"EXEC [PRIDB_GET_VALUE_DT]({pObjectId}, '{pLanguage}')") Dim oResult As TableResult = _channel.ReturnDatatable_MSSQL_IDB($"EXEC [PRIDB_GET_VALUE_DT] {pObjectId}, '{pLanguage}'")
If oResult.OK = False Then If oResult.OK = False Then
Throw New ApplicationException(oResult.ErrorMessage) Throw New ApplicationException(oResult.ErrorMessage)
@ -539,24 +473,61 @@ Public Class Client
Return Nothing Return Nothing
End If End If
oTable = oResult.Table For Each oRow As DataRow In oResult.Table.Rows
Dim oAttribute As New ObjectAttribute With {
.Id = oRow.Item("AttributeId"),
.Title = oRow.Item("AttributeTitle"),
.Type = oRow.Item("AttributeType"),
.ValueBigInt = NotNull(oRow.Item("ValueBigInt"), Nothing),
.ValueDate = NotNull(oRow.Item("ValueDate"), Nothing),
.ValueDecimal = NotNull(oRow.Item("ValueDecimal"), Nothing),
.ValueText = NotNull(oRow.Item("ValueText"), Nothing)
}
Return oTable oAttributes.Add(oAttribute)
Next
Return oAttributes
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _logger.Error(ex)
Return Nothing Return Nothing
End Try End Try
End Function End Function
Private Function NewObjectData(pObjectId As Long, pAttributeName As String, pValue As Object, pOptions As NewObjectOptions)
If pOptions Is Nothing Then
pOptions = New NewObjectOptions()
End If
Dim oLanguage = GetUserLanguage(pOptions.UserLanguage)
Dim oUsername = GetUserName(pOptions.UserName)
Dim oSql = $"DECLARE @NEW_OBJ_MD_ID BIGINT
EXEC PRIDB_NEW_OBJ_DATA({pObjectId}, '{pAttributeName}', '{oUsername}', '{pValue}', '{oLanguage}', 0, @OMD_ID = @NEW_OBJ_MD_ID OUTPUT)"
Dim oResult = _channel.ExecuteNonQuery_MSSQL_IDB(oSql)
If oResult.OK = False Then
_logger.Warn("Error while deleting Term object")
_logger.Error(oResult.ErrorMessage)
End If
Return oResult.OK
End Function
Private Function DeleteTermObjectFromMetadata(pObjectId As Long, pAttributeName As String, pTerm2Delete As String, Optional pUsername As String = "", Optional pLanguage As String = "") As Boolean Private Function DeleteTermObjectFromMetadata(pObjectId As Long, pAttributeName As String, pTerm2Delete As String, Optional pUsername As String = "", Optional pLanguage As String = "") As Boolean
Try Try
Dim oLanguage = NotNull(pLanguage, GetUserLanguage()) Dim oLanguage = GetUserLanguage(pLanguage)
Dim oUsername = NotNull(pUsername, Environment.UserName) Dim oUsername = GetUserName(pUsername)
Dim oIdIsForeign As Integer = 1 Dim oIdIsForeign As Integer = 1
Dim oDELSQL = $"EXEC PRIDB_DELETE_TERM_OBJECT_METADATA {pObjectId},'{pAttributeName}','{pTerm2Delete}','{oUsername}','{oLanguage}',{oIdIsForeign}" Dim oDELSQL = $"EXEC PRIDB_DELETE_TERM_OBJECT_METADATA {pObjectId},'{pAttributeName}','{pTerm2Delete}','{oUsername}','{oLanguage}',{oIdIsForeign}"
Dim oResult = _channel.ExecuteNonQuery_MSSQL_IDB(oDELSQL) Dim oResult = _channel.ExecuteNonQuery_MSSQL_IDB(oDELSQL)
If oResult.OK = False Then
_logger.Warn("Error while deleting Term object")
_logger.Error(oResult.ErrorMessage)
End If
Return oResult.OK Return oResult.OK
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _logger.Error(ex)
@ -574,7 +545,6 @@ Public Class Client
End Try End Try
End Function End Function
Public Async Function GetDatatableFromIDBAsync(SQL As String) As Task(Of TableResult) Public Async Function GetDatatableFromIDBAsync(SQL As String) As Task(Of TableResult)
Try Try
Dim oResponse = Await _channel.ReturnDatatable_MSSQL_IDBAsync(SQL) Dim oResponse = Await _channel.ReturnDatatable_MSSQL_IDBAsync(SQL)
@ -699,8 +669,93 @@ Public Class Client
End Try End Try
End Function End Function
Private Function GetUserLanguage() As String Private Function GetUserLanguage(pOverrideLanguage As String) As String
Return Threading.Thread.CurrentThread.CurrentUICulture.Name Return NotNull(pOverrideLanguage, Threading.Thread.CurrentThread.CurrentUICulture.Name)
End Function End Function
Private Function GetUserName(pOverrideName) As String
Return NotNull(pOverrideName, Environment.UserName)
End Function
#Region "Option & Parameter Classes"
''' <summary>
''' Import options for NewFileAsync.
''' </summary>
Public Class NewFileOptions
''' <summary>
''' Option to keep the original extension when importing. Defaults to false.
''' </summary>
Public KeepExtension As Boolean = False
''' <summary>
''' Windows username of the user responsible for the import. Defaults to the currently logged in user.
''' </summary>
Public Username As String = Environment.UserName
''' <summary>
''' Date when the file was imported. Can be in the past. Defaults to now.
''' </summary>
Public DateImported As Date = Date.Now
End Class
Public Class NewObjectOptions
Public UserName As String
Public UserLanguage As String
End Class
Public Class VariableValueOptions
Public UserName As String
Public UserLanguage As String
End Class
Public Class GetVariableValueOptions
Inherits VariableValueOptions
End Class
Public Class SetVariableValueOptions
Inherits VariableValueOptions
End Class
#End Region
#Region "Response Classes"
Public Class StreamedFile
Public Stream As MemoryStream
Public FileName As String
End Class
Public Class FileList
Public Datatable As DataTable
End Class
Public Class DocumentInfo
Public FullPath As String
Public AccessRight As AccessRight
End Class
Public Class VariableValue
Public ReadOnly Property IsVector As Boolean = False
Public Property Value As Object
Public Property Type As Type
Public Sub New()
MyClass.New(Nothing)
End Sub
Public Sub New(pValue As Object)
' Check if value is a collection
If TypeOf pValue Is IEnumerable Then
IsVector = True
End If
' Try to determine the type
If IsNothing(pValue) Then
Type = Nothing
Else
Type = pValue.GetType
End If
Value = pValue
End Sub
End Class
#End Region
End Class End Class