Question

I want to create a treeTable using this primefaceExemple.

I'm looping into object to set it dynamically. Here is my bean code :

public class ModuleMB extends GenericMB<Module,ModuleService, Long> {

    @Autowired
    ModuleRepository moduleRepository;

    private Document selectedDocument;

    public ModuleMB() {
        super(Module.class);
    }

    @Autowired
    @Override
    public void setService(ModuleService service) {
        this.service = service;
    }

    public TreeNode getRoot() {
        TreeNode root = new DefaultTreeNode("root", null);
        Iterable<Module> moduleList = moduleRepository.findAll();
        for (Module m : moduleList) {
            TreeNode modules = new DefaultTreeNode(new Document(m.getLabel(), Long.toString(m.getLastModified()), m.getClass().getName()), root);
        }
        for (TreeNode curModuleNode : root.getChildren()) {
            System.out.println("ROWKEY : " + curModuleNode.getRowKey());
            for (Module m : moduleList) {
                System.out.println("MODULE LABEL : " + m.getLabel());
                for (ModuleVersion mv : m.getVersions()) {
                        System.out.println("MV LABEL : " + mv.getLabel());
                        TreeNode moduleVersions = new DefaultTreeNode(new Document(mv.getLabel(), Long.toString(mv.getLastModified()), mv.getClass().getName()), curModuleNode);
                    }
                }
        }
        return root;
    }

    public Document getSelectedDocument() {
        return selectedDocument;
    }

    public void setSelectedDocument(Document selectedDocument) {
        this.selectedDocument = selectedDocument;
    }
}

Here is my index.xhtml template :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:sec="http://www.springframework.org/security/tags">

    <h:body>
        <ui:composition template="/pages/theme/back/backOfficeLayout.xhtml">
            <ui:define name="bo-header">
                <p class="lead blog-description">Module administration.</p>
            </ui:define>
            <ui:define name="content">
                <h:form id="form">
                    <h:button outcome="add" id="addModule" value="Create new module" styleClass="btn btn-primary" /><br /><br />
                    <p:treeTable id="treetable" value="#{moduleMB.root}" var="document">
                <f:facet name="header">Document Viewer</f:facet>

                <p:column style="width:32%">
                    <f:facet name="header">Name</f:facet>
                    <h:outputText value="#{document.name}" />
                </p:column>

                <p:column style="width:32%">
                    <f:facet name="header">Update date</f:facet>
                    <h:outputText value="#{document.size}" />
                </p:column>

                <p:column style="width:32%">
                    <f:facet name="header">Type</f:facet>
                    <h:outputText value="#{document.type}" />
                </p:column>

                <p:column style="width:4%">
                    <p:commandLink update=":form:documentPanel" oncomplete="PF('documentDialog').show()" title="View Detail" styleClass="ui-icon ui-icon-search">
                        <f:setPropertyActionListener value="#{document}"
                            target="#{moduleMB.selectedDocument}" />
                    </p:commandLink>
                </p:column>
            </p:treeTable>

            <p:dialog id="dialog" header="Document Detail" showEffect="fade" widgetVar="documentDialog" modal="true">
                <p:outputPanel id="documentPanel">
                    <h:panelGrid  columns="2" cellpadding="5" rendered="#{not empty moduleMB.selectedDocument}">
                        <h:outputLabel for="name" value="Name: " />
                        <h:outputText id="name" value="#{moduleMB.selectedDocument.name}" style="font-weight:bold" />

                        <h:outputLabel for="size" value="Size: " />
                        <h:outputText id="size" value="#{moduleMB.selectedDocument.size}" style="font-weight:bold" />

                        <h:outputLabel for="type" value="Type " />
                        <h:outputText id="type" value="#{moduleMB.selectedDocument.type}" style="font-weight:bold" />
                    </h:panelGrid>
                </p:outputPanel>
            </p:dialog>
                </h:form>
            </ui:define>
        </ui:composition>
    </h:body>
</html>

The view result is as below :

  • First Module of the IDF market
    • V1 (correct)
    • V2 (correct)
  • Second market of the IDF market
    • V1 (wrong, second module dont have any children)
    • V2 (wrong, second module dont have any children)
  • Test A
    • V1 (wrong, Test A dont have any children)
    • V2 (wrong, Test A module dont have any children)

Here the console output (I don't put all, because lot of repetition) :

ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 1
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 2
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
ROWKEY : 0
MODULE LABEL : First module of the IDF market
MV LABEL : V1
MV LABEL : V2
MODULE LABEL : Second module of the IDF market
MODULE LABEL : test A
...

I don't understand why my module version are attached to each module, because only the first module get module version, not the two others !

Plus, when clicking on the loopt at the right, the Module information are writen instead of the module version information when clicking on a moduleVersion loop :

Thanks for reading,

Snite

Was it helpful?

Solution

You are adding all Module's ModuleVersions to each module due to the first two nested for-loops. I moved the inner most for-loop (for(ModuleVersion mv:) into the original for-loop:

public TreeNode getRoot() {
    TreeNode root = new DefaultTreeNode("root", null);
    Iterable<Module> moduleList = moduleRepository.findAll();
    for (Module m : moduleList) {
        TreeNode moduleNode = new DefaultTreeNode(new Document(m.getLabel(), Long.toString(m.getLastModified()), m.getClass().getName()), root);
        System.out.println("ROWKEY : " + moduleNode.getRowKey());
        System.out.println("MODULE LABEL : " + m.getLabel());
        for (ModuleVersion mv : m.getVersions()) {
            System.out.println("MV LABEL : " + mv.getLabel());
            TreeNode moduleVersions = new DefaultTreeNode(new Document(mv.getLabel(), Long.toString(mv.getLastModified()), mv.getClass().getName()), curModuleNode);
        }
    }
    return root;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top