How to change the parent of the element in hierarchy

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • How to change the parent of the element in hierarchy

    Hello, Friends,

    I have a task to organize an ability to change element's parent from one to another without using Modeller.
    I was trying PALO.EUPDATE function, but it seems, that it doesn't work for me.
    For Example I have the following heirarchy:
    -In progress
    ---task 1
    ---task 2
    -Completed
    ---task 3
    ---task 4
    And I need to move 'task 2' under 'Completed' without using Modeller.

    Do you have any ideas?

    Thank you for any help.
  • 1. PALO.ENABLE_LOOP
    2. Add the element to the new parent using PALO.EADD
    3. Remove the element from the old parent using PALO.EUPDATE. You can use the following VBA code:

    Source Code

    1. Private Sub PaloRemoveElementFromHierarchy(dimension As String, element As String, parent As String)
    2. Dim result As Variant
    3. result = Application.Run("PALO.ELEMENT_LIST_CHILDREN", DB_NAME, dimension, parent)
    4. If Not IsError(result) Then
    5. Dim children() As String, childIdx As Integer, resultIdx As Integer
    6. childIdx = 0
    7. ' If there is only one element in the result list, it has only one instead of two dimensions,
    8. ' fix that.
    9. EnsureTwoDimensionalResultArray result
    10. ' Create list of remaining children (alternating element name, consolidation factor)
    11. For resultIdx = LBound(result) To UBound(result)
    12. If result(resultIdx, 1) <> element Then
    13. ReDim Preserve children(childIdx + 1)
    14. children(childIdx) = result(resultIdx, 1)
    15. children(childIdx + 1) = result(resultIdx, 2)
    16. childIdx = childIdx + 2
    17. End If
    18. Next resultIdx
    19. If childIdx > 0 Then
    20. result = Application.Run("PALO.EUPDATE", DB_NAME, dimension, parent, "C", children)
    21. Else
    22. ' special case if last child is removed
    23. ReDim children(1)
    24. children(0) = 0
    25. children(1) = 0
    26. result = Application.Run("PALO.EUPDATE", DB_NAME, dimension, parent, "N", children)
    27. End If
    28. End If
    29. End Sub
    30. Private Sub EnsureTwoDimensionalResultArray(ByRef arr As Variant)
    31. Dim i As Integer
    32. On Error GoTo NotTwoDimensions
    33. i = LBound(arr, 2)
    34. Exit Sub
    35. NotTwoDimensions:
    36. Dim oneResultArray() As Variant
    37. ReDim oneResultArray(1 To 1, LBound(arr) To UBound(arr))
    38. For i = LBound(arr) To UBound(arr)
    39. oneResultArray(1, i) = arr(i)
    40. Next i
    41. arr = oneResultArray
    42. End Sub
    Display All


    4. PALO.DISABLE_LOOP

    Regards,
    Urs

    The post was edited 1 time, last by ufleisch ().

  • I found your code very useful, thank you (all the more since I did not found the PALO.EUPDATE function alone...); I added DB_NAME among the parameters (first one) and wrote an updatedim function that uses it this way (I am not sure this code is very clean, since I rather use Python than VBA but it seems to work OK for me, up to now at least ,and can perhaps be of use to others ...)

    Private Sub updatedim(DB_NAME, dimension, element, parent, weight, suppress)
    res = Application.Run("PALO.EISCHILD", DB_NAME, dimension, parent, element)
    If Not IsError(res) Then
    If res = True Then Exit Sub
    End If
    parent1 = Application.Run("PALO.EPARENT", DB_NAME, dimension, element, 1)
    If Not IsError(parent1) Then

    Call PaloRemoveElementFromHierarchy(DB_NAME, dimension, element, parent1)

    End If
    If parent = "" Then
    param = "N"
    Else
    param = "C"
    End If
    paloret_2 = Application.Run("PALO.EADD", DB_NAME, dimension, param, element, parent, weight,suppress)

    End Sub

    It is a pity such a function is not available in palo VBA from the start since it seems necessary (same think in the .Net or Java API by the way...at least in the versions I tested once)!

    Thank you again.
    LJ
  • Hi, thx for idea :)

    Here is completed code for element move with preserve of data:

    Public Function eElementMove(sServer As String, sDimension As String, sElement As String, sOldParent As String, sNewParent As String, iWeight As Double) As Boolean
    res = Application.Run("PALO.ENABLE_LOOP", True)
    sType = Application.Run("PALO.ETYPE", sServer, sDimension, sElement)
    Select Case sType
    Case "consolidated"
    sType = "C"
    Case "numeric"
    sType = "N"
    Case "string"
    sType = "S"
    End Select
    Add = Application.Run("PALO.EADD", sServer, sDimension, sType, sElement, sNewParent, iWeight, False)
    Call PaloRemoveElementFromHierarchy(sServer, sDimension, sElement, sOldParent)
    res = Application.Run("PALO.ENABLE_LOOP", False)
    End Function

    Private Sub PaloRemoveElementFromHierarchy(sServer As String, sDimension As String, sElement As String, sParent As String)
    Dim result As Variant
    result = Application.Run("PALO.ELEMENT_LIST_CHILDREN", sServer, sDimension, sParent)
    If Not IsError(result) Then
    Dim children() As String, childIdx As Integer, resultIdx As Integer
    childIdx = 0
    ' If there is only one element in the result list, it has only one instead of two dimensions,
    ' fix that.
    EnsureTwoDimensionalResultArray result
    ' Create list of remaining children (alternating element name, consolidation factor)
    For resultIdx = LBound(result) To UBound(result)
    If result(resultIdx, 1) <> sElement Then
    ReDim Preserve children(childIdx + 1)
    children(childIdx) = result(resultIdx, 1)
    children(childIdx + 1) = result(resultIdx, 2)
    childIdx = childIdx + 2
    End If
    Next resultIdx
    If childIdx > 0 Then
    result = Application.Run("PALO.EUPDATE", sServer, sDimension, sParent, "C", children)
    Else
    ' special case if last child is removed
    ReDim children(1)
    children(0) = 0
    children(1) = 0
    result = Application.Run("PALO.EUPDATE", sServer, sDimension, sParent, "N", children)
    End If
    End If
    End Sub

    Private Sub EnsureTwoDimensionalResultArray(ByRef arr As Variant)
    Dim i As Integer
    On Error GoTo NotTwoDimensions
    i = LBound(arr, 2)
    Exit Sub
    NotTwoDimensions:
    Dim oneResultArray() As Variant
    ReDim oneResultArray(1 To 1, LBound(arr) To UBound(arr))
    For i = LBound(arr) To UBound(arr)
    oneResultArray(1, i) = arr(i)
    Next i
    arr = oneResultArray
    End Sub

    Perhaps not mind if I add code to the section "All about VBA and PALO" ;)

    The post was edited 4 times, last by vesi ().