Something like:

ResultSet rs = ...;
Map<String, TreeNodeBase> nodes = new HashMap<String, TreeNodeBase>();
Map<String, Set<String>> hier = new HashMap<String, Set<String>>();
TreeNodeBase rootNode = new TreeNodeBase();

while (rs.next()) {
TreeNodeBase node = new TreeNodeBase();
node.setIdentifier(rs.getString(1));
node.setDescription(rs.getString(2));
String parentId = rs.getString(3);
node.setLeaf(true);
if (parentId != null) {
Set<String> ids = hier.get(parentId);
if (ids == null) {
ids = new HashSet<String>();
hier.put(parentId, ids);
} else {
rootNode.getChildren().add(node);
}
ids.add(node.getIdentifier());
nodes.put(node.getIdentifier(), node);
}

for (String parentId : hier) {
Set<String> ids = hier.get(parentId);
if (ids == null) { continue; }
TreeNodeBase parentNode = nodes.get(parentId);
parentNode.setLeaf(false);
for (String nodeId : ids) {
TreeNodeBase childNode = nodes.get(nodeId);
parentNode.getChildren().add(childNode);
}
}

return new TreeModelBase(rootNode);




On 7/18/07, kewldude <[EMAIL PROTECTED]> wrote:

something like loading the tree2 component with data from database...


kewldude wrote:
>
> What if I want to build the tree model all at once? I just need a
> tip/advice on how to do the fetching from the database and load it up into
> the TreeNodeBase object.
>
>
> Andrew Robinson-5 wrote:
>>
>> Do you want to lazy load them or build the tree model all at once?
>>
>> On 7/17/07, kewldude <[EMAIL PROTECTED]> wrote:
>>>
>>> Hi guys,
>>>
>>> I just need your advice though this is not really about the tree2
>>> component
>>> entirely. I just want to know how did you retrieve the data that
>>> represents
>>> all the folders and nodes for the tree2 component. I know most likely
>>> that
>>> data comes from a db that has a table that has a column mapping like
>>> this:
>>> ID
>>> NAME
>>> PARENT_ID
>>>
>>> Basically, I just need a guide or a tip on how to retrieve those values
>>> effectively and load it into the TreeNodeBase object according to the
>>> order
>>> (subfolders or nodes appearing in the right folder hierarchy)by which
>>> they
>>> should be rendered by the tree2 component.Calling those people who had
>>> used
>>> the tree2 component extensively, I hope you can share a piece of advice.
>>> Thanks.
>>> --
>>> View this message in context:
>>> 
http://www.nabble.com/Getting-contents-for-Tree2-component-tf4095761.html#a11645970
>>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>>>
>>>
>>
>>
>
>

--
View this message in context: 
http://www.nabble.com/Getting-contents-for-Tree2-component-tf4095761.html#a11667486
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Reply via email to