2 Replies Latest reply on May 19, 2011 4:22 AM by couse1

    Dynamic tree from database

    couse1 Newbie

      Hi,

       

      I'm trying to generate a dynamic tree from database oracle. I'm using richfaces 4.0, jsf 2.0, jboss 6 and eclipse helios.

       

      My table (building) :

      BAR_ID number(5)

      BAR_NOM varchar2(50)

      BAR_NIVEAU number(5)

      BAR_PERE number(5)  (auto-join with BAR_ID)

       

       

      My entity bean :

       

      package com.galhauban.patrimoine.batiment;

       

      import javax.persistence.Column;

      import javax.persistence.Entity;

      import javax.persistence.GeneratedValue;

      import javax.persistence.GenerationType;

      import javax.persistence.Id;

      import javax.persistence.SequenceGenerator;

      import javax.persistence.Table;

       

      @Entity

      @Table(name="BATI_REF")

      @SequenceGenerator(name = "seqbatiref", sequenceName = "SEQ_BATI_REF")

      public class RefItemEntity {

          @Id

          @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqbatiref")

          @Column(name = "bar_id")

          private Integer id;

          @Column(name = "bar_name")

          private String nom;

          @Column(name = "bar_niveau")

          private Integer niveau;

          @Column(name = "bar_pere")

          private Integer pere;

       

          public RefItemEntity(){}

       

          public Integer getId() {

              return id;

          }

       

          public void setId(Integer id) {

              this.id = id;

          }

       

          public String getNom() {

              return nom;

          }

       

          public void setNom(String nom) {

              this.nom = nom;

          }

       

          public Integer getNiveau() {

              return niveau;

          }

       

          public void setNiveau(Integer niveau) {

              this.niveau = niveau;

          }

       

          public Integer getPere() {

              return pere;

          }

       

          public void setPere(Integer pere) {

              this.pere = pere;

          }

      }

       

      My stateless bean :

       

      package com.galhauban.patrimoine.batiment;

      import java.io.Serializable;

      import java.util.ArrayList;

      import java.util.List;

      import javax.ejb.Local;

      import javax.ejb.Stateless;

      import javax.persistence.EntityManager;

      import javax.persistence.PersistenceContext;

      import javax.persistence.Query;

       

      @Stateless(name="refItemBean")

      @Local(com.galhauban.patrimoine.batiment.RefItemLocal.class)

      public class RefItemBean implements RefItemLocal,Serializable {

       

          @PersistenceContext (unitName = "EJB-Patrimoine", name = "EJB-Patrimoine")

          private EntityManager em;

          private RefItemEntity item;

          private static final long serialVersionUID = 1L;

          private List<RefItemEntity> items;

       

          public RefItemBean(){};

       

          public List<RefItemEntity> refItemList(){

              try {

                  System.out.println("*** Entre dans RefItemBean ");

                  Query query = em.createQuery("select b1 from RefItemEntity b1");

       

                  List<RefItemEntity> items = (List<RefItemEntity>) query.getResultList();

       

                  System.out.println("*** Nb element : "+items.size());

                  return items;

              }

              catch (Exception e) {

                  System.out.println("Voir pb: " + e.getMessage());

                  return null;

              }

          }

       

          public List<RefItemEntity> getItems() {

              return items;

          }

       

          public void setItems(List<RefItemEntity> items) {

              this.items = items;

          }

       

          public RefItemEntity getItem() {

              return item;

          }

       

          public void setItem(RefItemEntity item) {

              this.item = item;

          }

       

          public EntityManager getEm() {

              return em;

          }

       

          public void setEm(EntityManager em) {

              this.em = em;

          }

       

      }

       

      The interface :

      package com.galhauban.patrimoine.batiment;

       

      import java.util.List;

       

      import javax.ejb.Local;

       

      @Local

      public interface RefItemLocal {

          public List<RefItemEntity> refItemList();

          public RefItemEntity getItem();

          public void setItem(RefItemEntity item);

      }

       

       

      The managed bean :

      package com.galhauban.patrimoine.batiment;

      import java.io.Serializable;

      import java.util.ArrayList;

      import java.util.List;

      import javax.ejb.EJB;

      import javax.faces.bean.ManagedBean;

      import javax.faces.bean.SessionScoped;

      import javax.inject.Named;

       

      import org.richfaces.component.UITree;

      import org.richfaces.event.TreeSelectionChangeEvent;

      import org.richfaces.model.TreeNode;

      import org.richfaces.model.TreeNodeImpl;

       

      @ManagedBean(name="refItemController")

      @SessionScoped

      public class RefItemController implements Serializable {

       

          private static final long serialVersionUID = 1L;

          @EJB

          private RefItemLocal items;

          private List<RefItemEntity>  liste;

          private Tree myTree = new Tree();

          private TreeNode currentSelection = null;

       

          public RefItemController(){};

       

          public List<RefItemEntity> getListe() {

              return liste;

          }

       

          public void setListe(List<RefItemEntity> liste) {

              this.liste = liste;

          }

       

          public RefItemLocal getItems() {

              return items;

          }

       

          public void setItems(RefItemLocal items) {

              this.items = items;

          }

       

          public Tree getMyTree() {

              return myTree;

          }

       

          public void setMyTree(Tree myTree) {

              this.myTree = myTree;

          }

       

       

          public String doSearch() {

              try {

                  liste = items.refItemList();

                  System.out.println("*** Liste taille : " + liste.size());

       

                  for (RefItemEntity objet : liste){

                      Tree pere = new Tree();

                      if (objet.getPere() == null) {

                          pere.setName(objet.getNom());

                          pere.setType("arbo");

                          myTree.addChild(objet.getId(), pere);

                          System.out.println("**************************** sommet : " + pere.getName());

                      }

                      else{

                          pere.setName(objet.getNom());

                          pere.setType("arbo");

                          pere.addChild(objet.getId(), pere);

                          System.out.println("**************************** Pere en cours : " + pere.getName());

                      }

       

                      for (RefItemEntity objet2 : liste){

                          if(objet2.getPere()==objet.getId())

                          {

                              System.out.println("boucle2 : " + pere.getName());

                              Tree fils = new Tree();

                              fils.setType("arbo");

                              fils.setName(objet2.getNom());

                              pere.addChild(objet2.getId(), fils);

                              System.out.println("*** Pere : " + pere.getName());

                              System.out.println("*** Fils : " + fils.getName());

                          }

       

                      }

                  }

                  return "referentiel.found";

              }

              catch (Exception ex){

                  System.out.println("pb : " + ex.getMessage());

                  ex.printStackTrace();

                  return null;

              }

       

          }

       

          public void selectionChanged(TreeSelectionChangeEvent selectionChangeEvent) {

              // considering only single selection

              List<Object> selection = new ArrayList<Object>(selectionChangeEvent.getNewSelection());

              Object currentSelectionKey = selection.get(0);

              UITree tree = (UITree) selectionChangeEvent.getSource();

       

              Object storedKey = tree.getRowKey();

              tree.setRowKey(currentSelectionKey);

              currentSelection = (TreeNode) tree.getRowData();

              tree.setRowKey(storedKey);

          }

       

          public TreeNode getCurrentSelection() {

              return currentSelection;

          }

       

          public void setCurrentSelection(TreeNode currentSelection) {

              this.currentSelection = currentSelection;

          }

      }

       

       

       

      My tree.class

      package com.galhauban.patrimoine.batiment;

       

      import org.richfaces.model.TreeNodeImpl;

       

      public class Tree extends TreeNodeImpl{

          private String name;

          private String type;

       

          public String getName() {

              return name;

          }

       

          public void setName(String name) {

              this.name = name;

          }

       

          public String getType() {

              return type;

          }

       

          public void setType(String type) {

              this.type = type;

          }

       

          public String toString() {

              return this.name;

          }

      }

       

       

      My xhtml file :

      <!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:a4j="http://richfaces.org/a4j"

            xmlns:rich="http://richfaces.org/rich"

            xmlns:f="http://java.sun.com/jsf/core"

            xmlns:ui="http://java.sun.com/jsf/facelets">

       

      <h:head></h:head>

      <body>

          <rich:panel>

              <f:facet name="header">

                   Referentiel Batiment

              </f:facet>

          <h:form>

              <rich:tree id="tree" nodeType="#{node.type}" var="node"

                              value="#{refItemController.myTree}" toggleType="client"

                              selectionType="ajax"

                              selectionChangeListener="#{refItemController.selectionChanged}">

                 <rich:treeNode type="arbo">

                      #{node.name}

                  </rich:treeNode>

              </rich:tree>

         </h:form>

          </rich:panel>

      </body>

      </html>

       

       

      This the result :

       

      batiment

           ecole

           sport

           administratif

           ferry

           culte

           hotel de ville

       

       

      But i would like something like that :

      batiment

           ecole

                ferry

           sport

           administratif

                hotel de ville

           culte

       

      Does anyone have an idea or an example?

       

      Thanks in advance

      kind regards

      couse1