6 Replies Latest reply on Oct 6, 2008 11:31 PM by Smitha Ramaswamy

    dynamic tree, unable to display child nodes

    Smitha Ramaswamy Newbie

      Hello,



      I am working on trying to get dynamic tree working, where I am reading the textnode from the database. I have the parent node rendered but it gives be an error that it cannot find the childnode in my code. I know I have incorrectly assigned value to node. Can anyone please explain how to assign child node that’s queried.


      One more, I have the database schema listed below. I want to query the nodeText for children and display at associated parentnode. Any pointers on this will be appreciated.


      Configuration.xhtml


      <rich:tree switchType="ajax" componentState="#{configurationTreeManager.treeState}">
                          <rich:recursiveTreeNodesAdaptor id="config" 
                           roots="#{configurationTreeManager.rootNodes}" 
                           var="config"
                           nodes="#{config.childNodes}" 
                           >
                               <rich:treeNode>
                                 <h:outputText value="#{config.nodeText}" />
                              </rich:treeNode>
                              
                          </rich:recursiveTreeNodesAdaptor>
                     </rich:tree>
      



      ConfigurationTreeManager.java


      @Scope(ScopeType.CONVERSATION)
      @Name("configurationTreeManager")
      public class ConfigurationTreeManager {
      
           @In
           protected FacesMessages facesMessages;
      
           @In
           private EntityManager entityManager;
      
           private HtmlTree tree1;
      
           private UITree tree;
      
           private TreeState treeState;
      
           @DataModel
           private List<Configuration> rNodes;
      
           @DataModel
           private List<Configuration> childNodes = null;
      
           public List<Configuration> getRootNodes() {
                
                if (rNodes == null) {
      
                     System.out.println("In configTreeManager getrootNodes.....");
      
                     rNodes = entityManager.createQuery(
                               "from Configuration c where c.parentNodeId is null")
                               .getResultList();
                }
                return rNodes;
           }
      
           public List<Configuration> getChildren() {
      
                System.out.println("In configTreeManager getChildNodes.....");
                
                if (childNodes == null) {
                     childNodes = entityManager.createQuery("from Configuration c")
                               .getResultList();
                }
                return childNodes;
           }
      
           private String name = "";
      
           public String getName() {
                return name;
           }
      
           public void setName(final String name) {
                this.name = name;
           }
           
      }





      *Data schema*
      
      Id     NodeText     ParentNodeId
      1     node1                    
      2     node2
      3     node3     
      4     node4
      5     node1a          1
      6     node1b          1
      7     node3a          3     


           

        • 1. Re: dynamic tree, unable to display child nodes
          Francisco Jose Peredo Noguez Master

          Hi!


          Well, you have a getChildren method in your ConfigurationTreeManager , that IMHO makes no sense... and will never be used... and you have nodes="#{config.childNodes}" that means you should have a getChildNodes method in your Configuration class, but you didn't post the code of that class so I do not know if you actually have that method.


          Regards,

          • 2. Re: dynamic tree, unable to display child nodes
            Smitha Ramaswamy Newbie

            Appreciate your very quick response. Yes, yesterday after my posting while I was debugging I did notice that it is looking for the method in Configuration entity bean class and the method getChildren() is not in there but in Manager class.


            Can you please explain me what the root node is expecting and what the child node expects.
            And in my situation here what is the best design to keep in mind when root node and their childrens are being retrieved from the database and they are all of type Configuration as you can see the schema that I have in my previous post.


            And here is the Configuration class,



            @Entity
            @Table(name = "Configuration")
            public class Configuration {
            
                 @Id
                 @GeneratedValue
                 @Column(name = "ConfigId")
                 private Long configId;
            
                 @ManyToOne
                 @JoinColumn(name = "ParentNodeId")
                 @ForeignKey(name = "FK1_Configuration_Configuration")
                 private Configuration parentNodeId;
            
                 @Column(name = "NodeText", length = 100)
                 private String nodeText;
            
                 public Configuration() {
                 }
                 
                 public Long getConfigId() {
                      return configId;
                 }
            
                 public void setConfigId(final Long configId) {
                      this.configId = configId;
                 }
                 
                 public Configuration getParentNodeId() {
                      return parentNodeId;
                 }
            
                 
                 public void setParentNodeId(final Configuration parentNodeId) {
                      this.parentNodeId = parentNodeId;
                 }
            
                 public String getNodeText() {
                      return nodeText;
                 }
            
                 
                 public void setNodeText(final String nodeText) {
                      this.nodeText = nodeText;
                 }
            
            }
            





            Thanks again.

            • 3. Re: dynamic tree, unable to display child nodes
              Francisco Jose Peredo Noguez Master

              Just add a getChildNodes configured as a @OneToMany mapped by parentNodeId, BWT that is not good name for that variable, it should be named just parent ( parentConfiguration is also wrong because it is redundant)  to you configuration class and that should be it.

              • 4. Re: dynamic tree, unable to display child nodes
                Jeronimo Azevedo Newbie

                Hi,


                I have the same problem


                I manged to get it working by adding getChildNodes in the entity Central and in the entity Switch.


                Here are the Entities


                @Entity
                @Table(name="CENTRALS")
                public class Central {
                          
                     private Integer id;
                     private City city;
                     private List<Switch> switches;
                     
                     public Central() {
                          
                     }
                     
                     public Central(Integer id, City city) {
                          this.id = id;
                          this.city = city;
                     }
                     
                     @Id
                     public Integer getId() {
                          return id;
                     }
                
                     public void setId(Integer id) {
                          this.id = id;
                     }
                     
                     @OneToOne
                     @JoinColumn(name="CITY_ID")
                     @NotNull(message="CT has to associated to a city")
                     public City getCity() {
                          return city;
                     }
                
                     public void setCity(City city) {
                          this.city = city;
                     }
                
                     @OneToMany(mappedBy="central")
                     public List<Switch> getChildNodes() {
                          return switches;
                     }
                
                     public void setChildNodes(List<Switch> switches) {
                          this.switches = switches;
                     }
                
                     public String getNodeText() {
                          return city.getName();
                     }
                     
                     public void setNodeText(String nodeText) {
                     }
                



                @Entity
                @Table(name="SWITCHES")
                public class Switch {
                     
                     private Long id;
                     private String swType;
                     private Central central;
                     private City city;
                     private List<Point> points;
                     
                     public Switch() {
                          
                     }
                     
                     public Switch(String type, Central central, City city) {
                          this.swType = type;
                          this.central = central;
                          this.city = city;
                     }
                     
                     @Id @GeneratedValue
                     public Long getId() {
                          return id;
                     }
                     
                     public void setId(Long id) {
                          this.id = id;
                     }
                     
                     @NotNull(message="Switch type is missing (i.e. DSX, DSL, DS2,...)")
                     public String getSwType() {
                          return swType;
                     }
                     
                     public void setSwType(String type) {
                          this.swType = type;
                     }
                     
                     @ManyToOne
                     @JoinColumn(name="CENTRAL_ID")
                     @NotNull(message="Central has to be specified.")
                     public Central getCentral() {
                          return central;
                     }
                     
                     public void setCentral(Central central) {
                          this.central = central;
                     }
                     
                     @ManyToOne
                     @JoinColumn(name="CITY_ID")
                     public City getCity() {
                          return city;
                     }
                     
                     public void setCity(City city) {
                          this.city = city;
                     }
                     
                     @OneToMany(mappedBy="dwitch")
                     public List<Point> getChildNodes() {
                          return points;
                     }
                
                     public void setChildNodes(List<Point> points) {
                          this.points = points;
                     }
                
                     public String getNodeText() {
                          return this.swType;
                     }
                     
                     public void setNodeText(String nodeText) {
                          
                     }
                



                The thing is that I would like to go down tree from switch and have a @OneToMay association to entity Point.


                Here the code



                @Entity
                @Table(name="POINTS")
                public class Point {
                     
                          
                     private Long id;
                     private String name;
                     private Switch dwitch;
                     
                     
                     @Id @GeneratedValue
                     public Long getId() {
                          return id;
                     }
                     
                     public void setId(Long id) {
                          this.id = id;
                     }
                     
                     public String getName() {
                          return name;
                     }
                
                     public void setName(String name) {
                          this.name = name;
                     }
                
                     @ManyToOne
                     @JoinColumn(name="SWITCH_ID")
                     public Switch getDwitch() {
                          return dwitch;
                     }
                
                     public void setDwitch(Switch switch1) {
                          dwitch = switch1;
                     }
                     
                     public String getNodeText() {
                          return this.name;
                     }
                     
                     public void setNodeText(String nodeText) {
                     }
                



                How can I list all the points under switch and that Points have no childrens


                My.xhtml


                <rich:tree switchType="ajax">
                   <rich:recursiveTreeNodesAdaptor id="tree" roots="#{simpleDslamTree.treeNode}" 
                                        var="tree" nodes="#{tree.childNodes}">
                     <rich:treeNode>
                        <h:outputText value="#{tree.nodeText}"/>
                     </rich:treeNode>
                   </rich:recursiveTreeNodesAdaptor>
                </rich:tree>
                



                Thanks,


                Jeronimo

                • 5. Re: dynamic tree, unable to display child nodes
                  Jeronimo Azevedo Newbie

                  I got it, it is working the way I want I added



                  public String getChildNodes() {
                     return null;
                  }
                       
                  public void setChildNodes(String nodes) {
                  
                  }
                  



                  to my Point Entity and it works now


                  Thanks Francisco to have pointed me to this Thread



                  • 6. Re: dynamic tree, unable to display child nodes
                    Smitha Ramaswamy Newbie

                    To add getChildNodes configured as a @OneToMany mapped by parentNodeId I don't have any column as childNode. The database schema is as below.



                    ConfigId  NodeText  ParentNodeId
                    1          node1      null
                    2          node2      null
                    3          node3      null 
                    4          node1a     1 
                    5          node1aa    4




                    Hence any other suggestion.


                    Thanks,