Okay I tried to integrate this code and this is what I've come up:
List <Catalogue>list = CatalogueDAO.instance().getCatalogues(refid);
Map<String, TreeNodeBase> nodes = new HashMap<String, TreeNodeBase>();
Map<String, Set<String>> hier = new HashMap<String, Set<String>>();
TreeNodeBase rootNode = new TreeNodeBase();
for(Catalogue cat: list){
TreeNodeBase node = new TreeNodeBase();
node.setIdentifier( String.valueOf( cat.getId()));
node.setDescription(cat.getName());
int parentId = cat.getParent_id();
node.setLeaf(true);
if (parentId != 0) {//has a parent folder
Set<String> ids = hier.get(String.valueOf(parentId));
if (ids == null) {
ids = new HashSet<String>();
hier.put(String.valueOf(parentId), ids);
} else {//parent id is present in map hier
rootNode.getChildren().add(node);//add node to
rootNode
}
ids.add(node.getIdentifier());//add nodeIdentifier to
HashSet of ids
nodes.put(String.valueOf(parentId), node);
}
Iterator it = hier.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
log.debug("key:" + pairs.getKey() + " value:" +
pairs.getValue());
Set<String> ids = (Set<String>) pairs.getValue();
if (ids == null) { continue; }
TreeNodeBase parentNode = nodes.get(pairs.getKey());
parentNode.setLeaf(false);
for (String nodeId : ids) {
TreeNodeBase childNode =
nodes.get(nodeId);
parentNode.getChildren().add(childNode);
}
}
}
return new TreeModelBase(rootNode);
This gives me the nasty NullPointerException, I tried to looked into the
myfaces core codes to see what I missed but to no avail...any leads on how
to get pass this error.
2007-07-19 17:43:11,627 ERROR
[org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/pages].[jsp]]
Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException: key
at
javax.faces.component._ComponentFacetMap.checkKey(_ComponentFacetMap.java:126)
at
javax.faces.component._ComponentFacetMap.get(_ComponentFacetMap.java:93)
at
javax.faces.component.UIComponentBase.getFacet(UIComponentBase.java:446)
at
org.apache.myfaces.custom.tree2.HtmlTreeRenderer.encodeCurrentNode(HtmlTreeRenderer.java:321)
at
org.apache.myfaces.custom.tree2.HtmlTreeRenderer.encodeTree(HtmlTreeRenderer.java:258)
at
org.apache.myfaces.custom.tree2.HtmlTreeRenderer.encodeChildren(HtmlTreeRenderer.java:206)
at
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:527)
at
org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChild(RendererUtils.java:414)
at
org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChildren(RendererUtils.java:400)
at
org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChild(RendererUtils.java:417)
at
org.apache.myfaces.shared_impl.renderkit.html.HtmlGridRendererBase.renderChildren(HtmlGridRendererBase.java:229)
at
org.apache.myfaces.shared_impl.renderkit.html.HtmlGridRendererBase.encodeEnd(HtmlGridRendererBase.java:101)
at
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:539)
at
org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChild(RendererUtils.java:419)
at
org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChildren(RendererUtils.java:400)
at
org.apache.myfaces.shared_impl.renderkit.html.HtmlGroupRendererBase.encodeEnd(HtmlGroupRendererBase.java:78)
at
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:539)
at
org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils.renderChild(RendererUtils.java:419)
at
org.apache.myfaces.custom.tabbedpane.HtmlTabbedPaneRenderer.writeTabsContents(HtmlTabbedPaneRenderer.java:550)
at
org.apache.myfaces.custom.tabbedpane.HtmlTabbedPaneRenderer.encodeEnd(HtmlTabbedPaneRenderer.java:232)
at
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:539)
at
org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChild(RendererUtils.java:419)
at
org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChildren(RendererUtils.java:400)
at
org.apache.myfaces.shared_impl.renderkit.html.HtmlGroupRendererBase.encodeEnd(HtmlGroupRendererBase.java:78)
at
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:539)
at javax.faces.webapp.UIComponentTag.encodeEnd(UIComponentTag.java:498)
at javax.faces.webapp.UIComponentTag.doEndTag(UIComponentTag.java:366)
at
org.apache.myfaces.shared_impl.taglib.UIComponentBodyTagBase.doEndTag(UIComponentBodyTagBase.java:57)
Andrew Robinson-5 wrote:
>
> 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.
>>
>>
>
>
--
View this message in context:
http://www.nabble.com/Getting-contents-for-Tree2-component-tf4095761.html#a11685944
Sent from the MyFaces - Users mailing list archive at Nabble.com.