diff --git a/Modules.EDMIAPI/Client.vb b/Modules.EDMIAPI/Client.vb index 85362502..a92b0adb 100644 --- a/Modules.EDMIAPI/Client.vb +++ b/Modules.EDMIAPI/Client.vb @@ -14,7 +14,6 @@ Public Class Client Private ReadOnly _channelFactory As ChannelFactory(Of IEDMIServiceChannel) Private ReadOnly _IPAddressServer As String - Private _dummy_table_attributes As DataTable Private _channel As IEDMIServiceChannel @@ -32,6 +31,29 @@ Public Class Client 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 + Public Shared Function ParseServiceAddress(AddressWithOptionalPort As String) As Tuple(Of String, Integer) Dim oSplit() As String = AddressWithOptionalPort.Split(":"c) Dim oAppServerAddress As String = oSplit(0) @@ -108,6 +130,10 @@ Public Class Client End Try End Function + Private Function PreloadAttributes() + + End Function + ''' ''' TODO: Creates a new object ''' @@ -245,8 +271,7 @@ Public Class Client If oType = GetType(DataTable).Name Then Dim oValueTable As DataTable = pValue - Dim oCurrentValue As Object - Dim oCurrentValueType As String + Dim oCurrentValue As VariableValue If pOptions.CheckDeleted = True Then Dim oOptions As New GetVariableValueOptions With { @@ -257,19 +282,16 @@ Public Class Client ' Get current value oCurrentValue = GetVariableValue(pObjectId, pAttributeName, pAttributeType, oOptions) - ' Get current type - oCurrentValueType = oCurrentValue.GetType.Name - ' If current value is datatable - If oCurrentValueType = GetType(DataTable).Name Then + If oCurrentValue.Type = GetType(DataTable) Then ' 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 - For Each oRow As DataRow In oCurrentTable.Rows + For Each oRow As DataRow In oTable.Rows Dim oExists As Boolean = False For Each oNewValueRow As DataRow In oValueTable.Rows Dim oInfo = $"Checking oldValue[{oRow.Item(0)}] vs NewValue [{oNewValueRow.Item(1)}]" @@ -301,12 +323,12 @@ Public Class Client Next If oExists = False Then _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 Else _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 @@ -342,18 +364,17 @@ Public Class Client End Function Public Class GetVariableValueOptions - Public FromIDB As Boolean = False Public Username As String = String.Empty Public Language As String = String.Empty 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 pOptions = New GetVariableValueOptions() End If - Dim oLanguage = NotNull(pOptions.Language, GetUserLanguage()) - Dim oUsername = NotNull(pOptions.Username, Environment.UserName) + pOptions.Language = NotNull(pOptions.Language, GetUserLanguage()) + pOptions.Username = NotNull(pOptions.Username, Environment.UserName) ' Check if ObjectId exists Try @@ -368,29 +389,9 @@ Public Class Client End Try ' Get Attributes and Values from Database - Dim oTable As DataTable - - 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 + Dim oAttributes As DataTable = GetAttributesForObject(pObjectId, pOptions.Language) ' TODO: Check if Attribute exists & REfactor - - - Try Dim oVectorAttribute As Boolean = False Select Case pAttributeType @@ -402,44 +403,112 @@ Public Class Client oVectorAttribute = False 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 = True And _dummy_table_attributes.Rows.Count = 1 And pOptions.FromIDB = False 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 + If oVectorAttribute = False Then + Dim oRow As DataRow = oRows.FirstOrDefault() + Dim oType As String = oRow.Item("AttributeType") - 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 + Dim oValue = GetValueByType(oRow, oType) + 'oValues.Add(oValue) - End If - End If - - If Not IsNothing(oAttributeValue) Then - Return oAttributeValue + Return New VariableValue(oValue) 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}','{oLanguage}',CONVERT(BIT,'0'))" - Dim oDatatable As TableResult = _channel.ReturnDatatable_MSSQL_IDB(oFNSQL) + For Each oRow As DataRow In oRows + Dim oType As String = oRow.Item("AttributeType") - If oDatatable.OK = False Then - Throw New ApplicationException(oDatatable.ErrorMessage) + Dim oValue = GetValueByType(oRow, oType) + oValues.Add(oValue) + Next + + Return New VariableValue(oValues) End If - If oDatatable.Table.Rows.Count = 1 Then - oAttributeValue = oDatatable.Table.Rows.Item(0).Item(0) - End If + 'If Not IsNothing(oAttributes) Then + ' If oVectorAttribute = True And _dummy_table_attributes.Rows.Count = 1 And pOptions.FromIDB = False 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 + 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 Catch ex As Exception @@ -449,6 +518,29 @@ Public Class Client End Try 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 Try Dim oLanguage = NotNull(pLanguage, GetUserLanguage()) @@ -516,8 +608,6 @@ Public Class Client End Try End Function - - ''' ''' Return infos about a file object ''' diff --git a/Modules.EDMIAPI/Constants.vb b/Modules.EDMIAPI/Constants.vb index 753e0bad..cc2dcacb 100644 --- a/Modules.EDMIAPI/Constants.vb +++ b/Modules.EDMIAPI/Constants.vb @@ -17,4 +17,16 @@ VectorString = 8 VectorInteger = 9 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