Using custom hierarchical datasource

Hello, I need to use a treeTable with an entity, which has no parent property, but has children list… Maybe there is a common way to solve this problem? I guess it could be solved by a CustomHierarchicalDatasource, then could you, please, give a working sample of using custom hierarchical datasource? the one hosted on github is not working in local network without internet connection (i left an issue there)

Hello,

Your case doesn’t feel like a huge programming problem.
Just copy the code of existing datasource (e.g. HierarchicalDatasourceImpl or CustomHierarchicalDatasource).
And replace all usages of:

Entity<K> parentItem = item.getValue(hierarchyPropertyName);

by this pseudo code:

Entity<K> parentItem = items.stream()
          .findFirst(p -> p.getChildren().contains(item))
          .orElse(null);

Example above is of course inefficient for large collections, so you might want to maintain an auxiliary data
structure to speed up parent lookup.

Regards,
Alexander.

1 Like

let’s say i’m not so good at handling datasources to understand this pseudocode. could you, please, provide some more info? Also i didnt find any information about how to include a custom datasource to a screen, can you help me with that, please?

A custom datasource is defined in the screen XML using the datasourceClass attribute of the datasource XML element. See the docs.

thanks, maybe you could provide a sample with parent-to-child hierarchy? or better a sample for this one Showing a complex hierarchy - CUBA.Platform?

You could at least try to save our time which you want us to spend for solving your problem by providing a test project with data model and screens.

Parent-Children.zip (92.8 KB)

I created a tree, but can’t properly handle datasources in it… it asks for a view in a treenode datasource, i guess… anyhow it can’t handle different amounts fo kids in a parent((

I tried to understand the data model in your sample but I failed.
You wrote " I need to use a treeTable with an entity, which has no parent property, but has children list… " but in your project the entity has two lists: parents and children. What does it mean and how do you want to present it?

sorry for bothering, i did it by myself, creating a virtual tree. my question was an attemt to understand - is there a known easy way to implement something like complex hierarchy with a many-to-many connection through another entity. Once again sorry