EDMI.API/Client: WIP GetVariableValue

This commit is contained in:
Jonathan Jenne 2021-07-12 13:57:51 +02:00
parent 9f2cbc17e5
commit b81d6a1314
2 changed files with 172 additions and 70 deletions

View File

@ -14,7 +14,6 @@ Public Class Client
Private ReadOnly _channelFactory As ChannelFactory(Of IEDMIServiceChannel) Private ReadOnly _channelFactory As ChannelFactory(Of IEDMIServiceChannel)
Private ReadOnly _IPAddressServer As String Private ReadOnly _IPAddressServer As String
Private _dummy_table_attributes As DataTable Private _dummy_table_attributes As DataTable
Private _channel As IEDMIServiceChannel Private _channel As IEDMIServiceChannel
@ -32,6 +31,29 @@ Public Class Client
Public AccessRight As AccessRight Public AccessRight As AccessRight
End Class 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
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)
@ -108,6 +130,10 @@ 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>
@ -245,8 +271,7 @@ Public Class Client
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 Object Dim oCurrentValue As VariableValue
Dim oCurrentValueType As String
If pOptions.CheckDeleted = True Then If pOptions.CheckDeleted = True Then
Dim oOptions As New GetVariableValueOptions With { Dim oOptions As New GetVariableValueOptions With {
@ -257,19 +282,16 @@ Public Class Client
' Get current value ' Get current value
oCurrentValue = GetVariableValue(pObjectId, pAttributeName, pAttributeType, oOptions) oCurrentValue = GetVariableValue(pObjectId, pAttributeName, pAttributeType, oOptions)
' Get current type
oCurrentValueType = oCurrentValue.GetType.Name
' If current value is datatable ' If current value is datatable
If oCurrentValueType = GetType(DataTable).Name Then If oCurrentValue.Type = GetType(DataTable) Then
' Convert value to Datatable ' Convert value to Datatable
Dim oCurrentTable As DataTable = oCurrentValue Dim oTable As DataTable = oCurrentValue.Value
If oCurrentTable.Rows.Count > 1 Then If oTable.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 'now Checking whether the old row still remains in Vector? If not it will be deleted as it cannot be replaced in multivalues
For Each oRow As DataRow In oCurrentTable.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)}]" Dim oInfo = $"Checking oldValue[{oRow.Item(0)}] vs NewValue [{oNewValueRow.Item(1)}]"
@ -301,12 +323,12 @@ Public Class Client
Next Next
If oExists = False Then If oExists = False Then
_logger.Debug($"Value [{oCurrentValue}] no longer existing in Vector-Attribute [{pAttributeName}] - will be deleted!") _logger.Debug($"Value [{oCurrentValue}] no longer existing in Vector-Attribute [{pAttributeName}] - will be deleted!")
DeleteTermObjectFromMetadata(pObjectId, pAttributeName, oCurrentValue) DeleteTermObjectFromMetadata(pObjectId, pAttributeName, oCurrentValue.Value)
End If End If
Else Else
_logger.Debug($"Value [{oCurrentValue}] of Attribute [{pAttributeName}] obviously was updated during runtime - will be deleted!") _logger.Debug($"Value [{oCurrentValue}] of Attribute [{pAttributeName}] obviously was updated during runtime - will be deleted!")
DeleteTermObjectFromMetadata(pObjectId, pAttributeName, oCurrentValue) DeleteTermObjectFromMetadata(pObjectId, pAttributeName, oCurrentValue.Value)
End If End If
@ -342,18 +364,17 @@ Public Class Client
End Function End Function
Public Class GetVariableValueOptions Public Class GetVariableValueOptions
Public FromIDB As Boolean = False
Public Username As String = String.Empty Public Username As String = String.Empty
Public Language As String = String.Empty Public Language As String = String.Empty
End Class End Class
Public Function GetVariableValue(pObjectId As Long, pAttributeName As String, pAttributeType As AttributeType, Optional pOptions As GetVariableValueOptions = Nothing) As Object Public Function GetVariableValue(pObjectId As Long, pAttributeName As String, pAttributeType As AttributeType, 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
Dim oLanguage = NotNull(pOptions.Language, GetUserLanguage()) pOptions.Language = NotNull(pOptions.Language, GetUserLanguage())
Dim oUsername = NotNull(pOptions.Username, Environment.UserName) pOptions.Username = NotNull(pOptions.Username, Environment.UserName)
' Check if ObjectId exists ' Check if ObjectId exists
Try Try
@ -368,29 +389,9 @@ Public Class Client
End Try End Try
' Get Attributes and Values from Database ' Get Attributes and Values from Database
Dim oTable As DataTable Dim oAttributes As DataTable = GetAttributesForObject(pObjectId, pOptions.Language)
Try
Dim oResult As TableResult = _channel.ReturnDatatable_MSSQL_IDB($"EXEC [PRIDB_GET_VALUE_DT]({pObjectId}, '{oLanguage}')")
If oResult.OK = False Then
Throw New ApplicationException(oResult.ErrorMessage)
End If
If oResult.Table Is Nothing OrElse oResult.Table.Rows.Count = 0 Then
Return Nothing
End If
oTable = oResult.Table
Catch ex As Exception
_logger.Error(ex)
Return Nothing
End Try
' TODO: Check if Attribute exists & REfactor ' TODO: Check if Attribute exists & REfactor
Try Try
Dim oVectorAttribute As Boolean = False Dim oVectorAttribute As Boolean = False
Select Case pAttributeType Select Case pAttributeType
@ -402,44 +403,112 @@ Public Class Client
oVectorAttribute = False oVectorAttribute = False
End Select End Select
Dim oAttributeValue As Object = Nothing 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()
If Not IsNothing(_dummy_table_attributes) Then If oVectorAttribute = False Then
If oVectorAttribute = True And _dummy_table_attributes.Rows.Count = 1 And pOptions.FromIDB = False Then Dim oRow As DataRow = oRows.FirstOrDefault()
Try Dim oType As String = oRow.Item("AttributeType")
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) Dim oValue = GetValueByType(oRow, oType)
Catch ex As Exception 'oValues.Add(oValue)
_logger.Debug($"Error getting Attribute from IDB_DT_DOC_DATA: {ex.Message}")
End Try
End If Return New VariableValue(oValue)
End If
If Not IsNothing(oAttributeValue) Then
Return oAttributeValue
Else Else
_logger.Debug($"oAttributeValue for Attribute [{pAttributeName}] is so far nothing..Now trying FNIDB_PM_GET_VARIABLE_VALUE ") For Each oRow As DataRow In oRows
End If Dim oType As String = oRow.Item("AttributeType")
Dim oFNSQL = $"SELECT * FROM [dbo].[FNIDB_PM_GET_VARIABLE_VALUE] ({pObjectId},'{pAttributeName}','{oLanguage}',CONVERT(BIT,'0'))"
Dim oDatatable As TableResult = _channel.ReturnDatatable_MSSQL_IDB(oFNSQL)
If oDatatable.OK = False Then Dim oValue = GetValueByType(oRow, oType)
Throw New ApplicationException(oDatatable.ErrorMessage) oValues.Add(oValue)
Next
Return New VariableValue(oValues)
End If End If
If oDatatable.Table.Rows.Count = 1 Then 'If Not IsNothing(oAttributes) Then
oAttributeValue = oDatatable.Table.Rows.Item(0).Item(0) ' If oVectorAttribute = True And _dummy_table_attributes.Rows.Count = 1 And pOptions.FromIDB = False Then
End If ' 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
Catch ex As Exception
_logger.Error(ex)
Return Nothing
End Try
End Function
Private Function GetValueByType(pRow As DataRow, pTypeString As String) As Object
Try
Dim oAttributeValue As Object
Select Case pTypeString
Case Constants.AttributeTypeName.BIT
oAttributeValue = pRow.Item("ValueBigInt")
Case Constants.AttributeTypeName.BIG_INTEGER
oAttributeValue = pRow.Item("ValueBigInt")
Case Constants.AttributeTypeName.DATE
oAttributeValue = pRow.Item("ValueDate")
Case Constants.AttributeTypeName.DATETIME
oAttributeValue = pRow.Item("ValueDate")
Case Constants.AttributeTypeName.DECIMAL
oAttributeValue = pRow.Item("ValueDecimal")
Case Constants.AttributeTypeName.FLOAT
oAttributeValue = pRow.Item("ValueDecimal")
Case Constants.AttributeTypeName.VARCHAR
oAttributeValue = pRow.Item("ValueText")
Case Constants.AttributeTypeName.VECTOR_INTEGER
oAttributeValue = pRow.Item("ValueBigInt")
Case Constants.AttributeTypeName.VECTOR_STRING
oAttributeValue = pRow.Item("ValueText")
Case Else
oAttributeValue = Nothing
End Select
Return oAttributeValue Return oAttributeValue
Catch ex As Exception Catch ex As Exception
@ -449,6 +518,29 @@ Public Class Client
End Try End Try
End Function End Function
Private Function GetAttributesForObject(pObjectId As Long, pLanguage As String) As DataTable
Dim oTable As DataTable
Try
Dim oResult As TableResult = _channel.ReturnDatatable_MSSQL_IDB($"EXEC [PRIDB_GET_VALUE_DT]({pObjectId}, '{pLanguage}')")
If oResult.OK = False Then
Throw New ApplicationException(oResult.ErrorMessage)
End If
If oResult.Table Is Nothing OrElse oResult.Table.Rows.Count = 0 Then
Return Nothing
End If
oTable = oResult.Table
Return oTable
Catch ex As Exception
_logger.Error(ex)
Return Nothing
End Try
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 = NotNull(pLanguage, GetUserLanguage())
@ -516,8 +608,6 @@ Public Class Client
End Try End Try
End Function End Function
''' <summary> ''' <summary>
''' Return infos about a file object ''' Return infos about a file object
''' </summary> ''' </summary>

View File

@ -17,4 +17,16 @@
VectorString = 8 VectorString = 8
VectorInteger = 9 VectorInteger = 9
End Enum End Enum
Public Class AttributeTypeName
Public Const VARCHAR = "VARCHAR"
Public Const BIG_INTEGER = "BIG INTEGER"
Public Const FLOAT = "FLOAT"
Public Const [DECIMAL] = "DECIMAL"
Public Const [DATE] = "DATE"
Public Const [DATETIME] = "DATETIME"
Public Const BIT = "BIT"
Public Const VECTOR_STRING = "VECTOR STRING"
Public Const VECTOR_INTEGER = "VECTOR INTEGER"
End Class
End Class End Class