3 Replies Latest reply on Jul 22, 2010 5:59 AM by ilya_shaikovsky

    rich:tree implementation problem

    hakanm

      hello i am trying to create a hierarchical tree of

       

      document types

                     -> document types2

                           -> document years

       

      and the types, types2 and years of the documents are taken from the tables in a db.

       

      i followed the samples given in the richfaces 3.3.3 final but i can see only the root element in the tree and tree does not respond to clicking for expanding. I debugged and can see the Treenodeimpl structure correctly but i could not be able to show it up in the view. the code is below:

       

      DocTreeManagerBean.java

      @Name("docTreeManager")
      @Scope(ScopeType.PAGE)
      public class DocTreeManagerBean implements DocTreeManager, Serializable
      {
         
          /**
           *
           */
          private static final long serialVersionUID = 3390747870940320057L;
          private static final String PDOCTYPELISTSQL = "SELECT doctypes from Pdoctype doctypes";
          private static final String DOCUMENTLISTSQL = "SELECT docsoffirm from VDocsOfFirm docsoffirm WHERE docsoffirm.firmtaxnum= :firmtaxnum ORDER BY firmtaxnum, docid, ddate";

       


          public enum doctypes {FIRM, PDOCTYPE, DOCYEAR, FAIR, FAIRNAME, DOCUMENT};
         
          @Logger private Log log;

       


          @In StatusMessages statusMessages;

       


          @In EntityManager entityManager;
         
          @In (value="firmFound", scope=ScopeType.CONVERSATION, required=false)
          private Firm firm;
         
          private String value;

       


          private TreeNodeImpl<DocTreeNode> treeRootNode = null;
         
          private TreeNodeImpl<DocTreeNode> treeNodeImpl=null;
         
          private Long firmTaxNo;

       


          private List<Document> listOfDocs;
         
          private List<Pdoctype> docTypes;
         
          private List<VDocsOfFirm> docsOfFirm;
         
          //kullanici secim olaylari:
          private DocTreeNode nodeTitle;
         
          private List<DocTreeNode> selectedNodeChildren = new ArrayList<DocTreeNode>();   

       


         
          public void selectionListener(NodeSelectedEvent event){
              UITree tree = (UITree) event.getComponent();
              nodeTitle = (DocTreeNode) tree.getData();
          }
         
          public void expansionListener(NodeExpandedEvent event){
              UITree tree =(UITree) event.getComponent();
              nodeTitle=(DocTreeNode) tree.getData();
          }
          public void docTreeManager()
          {
              // implement your business logic here
              log.info("docTreeManager.docTreeManager() action called with: #{docTreeManager.value}");
              statusMessages.add("docTreeManager #{docTreeManager.value}");
          }

       


         
          ///root: documan turleri ekleniyor:
         
          @Observer("firmOutjected")
          public void addDocNodes(){
              treeRootNode = new TreeNodeImpl<DocTreeNode>();
              treeNodeImpl = new TreeNodeImpl<DocTreeNode>();
              DocTreeNode anyNode = null;
              //TODO duzeltilecek cok daha duzgun hale getirilmesi gerekiyor, tekrarli kod var.
              if(null != firm) {
                 
                  firmTaxNo = firm.getFtaxnum();
                  anyNode = new DocTreeNode();
                  anyNode.setTitle(firm.getFname());
                  anyNode.setObj(firm);
                  anyNode.setDoctype(doctypes.FIRM);
                  anyNode.setType("firm");
                  //dokuman turleri:
                  Query query = entityManager.createQuery(PDOCTYPELISTSQL);
                  setDocTypes(query.getResultList());
                 
                  //firmanin sistemdeki dokumanlari
                  query = entityManager.createQuery(DOCUMENTLISTSQL).setParameter("firmtaxnum", firmTaxNo);
                  setDocsOfFirm(query.getResultList());
                 
                  //firma vergi nosu root
                  treeNodeImpl.setData(anyNode);
                  //treeRootNode = treeNodeImpl;
                 
                  //treeRootNode.addChild(0, treeNodeImpl);
             
                  //dokuman turlerini gir:
                  int count=0;
                  for(Pdoctype doctype : docTypes){
                      String nodeStr = doctype.getPdoctype();
                      TreeNodeImpl<DocTreeNode> nodeDoctype = new TreeNodeImpl<DocTreeNode>();
                      anyNode = new DocTreeNode();
                      anyNode.setTitle(doctype.getPdoctype());
                      anyNode.setObj(doctype);
                      anyNode.setDoctype(doctypes.PDOCTYPE);
                      anyNode.setType("doctype");
                      nodeDoctype.setData(anyNode);
                      treeNodeImpl.addChild(count++, nodeDoctype);
                      addDateNodes(nodeDoctype,nodeStr);
                  }
                  treeRootNode.addChild(0, treeNodeImpl);
              } else {
                 
                  treeRootNode.addChild(1, treeNodeImpl);
              }
              
             
          }

       


         
          //firmanin dokumanlarindan nodeStr olanlarini tarihe gore ekleyecek.
          private void addDateNodes(TreeNode<DocTreeNode> node, String nodeStr){
              TreeNode<DocTreeNode> dateNodes = new TreeNodeImpl<DocTreeNode>();
              int dateCounter=1;
              DocTreeNode anyNode = null;
             
              for(VDocsOfFirm doc : docsOfFirm){
                  if(doc.getPdoctype().equals(((DocTreeNode)node.getData()).getTitle())){
                      TreeNodeImpl<DocTreeNode> treenode =  new TreeNodeImpl<DocTreeNode>();
                      anyNode = new DocTreeNode();
                      anyNode.setTitle(doc.getDdate().toString());
                      anyNode.setObj(doc);
                      anyNode.setDoctype(doctypes.DOCYEAR);
                      anyNode.setType("date");
                      treenode.setData(anyNode);
                      dateNodes.addChild(dateCounter++, treenode);
                      if(doc.getFuaradi()!=null && doc.getFuaradi().length()>0) {
                          addFuarNode(treenode, nodeStr, doc);
                      }
                  }
              }
              node.addChild(nodeStr, dateNodes);
          }
         
          //firmanin eklenen dokumanlarindan verilen tarihe ait olan eger
          //destek ise (fuaradi bos degilse) ekleyecek: gelen node a fuaradini ekleyecek.
          private void addFuarNode(TreeNode<DocTreeNode> node, String nodeStr, VDocsOfFirm doc){
              TreeNodeImpl<DocTreeNode> treeNode = new TreeNodeImpl<DocTreeNode>();
              DocTreeNode anyNode = new DocTreeNode();
              anyNode.setTitle(doc.getFuaradi());
              anyNode.setDoctype(doctypes.FAIR);
              anyNode.setObj(doc);
              anyNode.setType("fair");
              treeNode.setData(anyNode);
              node.addChild(0, treeNode);
          }

       


          public void processSelection(NodeSelectedEvent event) {
              HtmlTree tree = (HtmlTree) event.getComponent();
              nodeTitle = (DocTreeNode) tree.getRowData();
              selectedNodeChildren.clear();
              TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
              if (currentNode.isLeaf()){
                  selectedNodeChildren.add((DocTreeNode)currentNode.getData());
              }else
              {
                  Iterator<Map.Entry<Object, TreeNode>> it = currentNode.getChildren();
                  while (it!=null &&it.hasNext()) {
                      Map.Entry<Object, TreeNode> entry = it.next();
                      selectedNodeChildren.add((DocTreeNode)entry.getValue().getData());
                  }
              }
          }
         

          @Length(max = 10)
          public String getValue()
          {
              return value;
          }

       


          public void setValue(String value)
          {
              this.value = value;
          }

       


          @Remove @Destroy
          public void destroy() {}

       


          public void setTreeRootNode(TreeNodeImpl<DocTreeNode> treeRootNode) {
              this.treeRootNode = treeRootNode;
          }

       


          public TreeNode getTreeRootNode() {
              if(null == treeRootNode)
                  addDocNodes();
              return treeRootNode;
          }

       


          public void setFirmTaxNo(Long firmTaxNo) {
              this.firmTaxNo = firmTaxNo;
          }

       


          public Long getFirmTaxNo() {
              return firmTaxNo;
          }

       


          public void setListOfDocs(List<Document> listOfDocs) {
              this.listOfDocs = listOfDocs;
          }

       


          public List<Document> getListOfDocs() {
              return listOfDocs;
          }

       


          public void setDocTypes(List<Pdoctype> docTypes) {
              this.docTypes = docTypes;
          }

       


          public List<Pdoctype> getDocTypes() {
              return docTypes;
          }

       


          public void setDocsOfFirm(List<VDocsOfFirm> docsOfFirm) {
              this.docsOfFirm = docsOfFirm;
          }

       


          public List<VDocsOfFirm> getDocsOfFirm() {
              return docsOfFirm;
          }

       



          public void setNodeTitle(DocTreeNode nodeTitle) {
              this.nodeTitle = nodeTitle;
          }

       



          public DocTreeNode getNodeTitle() {
              return nodeTitle;
          }
         
      }

       

      DocTreeNode - holds the data shown in the tree:

       

      public class DocTreeNode implements Serializable{


          private static final long serialVersionUID = -2014941341643021587L;

          private String title;

          private doctypes doctype;
         
          private String type;
         
          private Object obj;
         
         
          public void setTitle(String title) {
              this.title = title;
          }
          public String getTitle() {
              return title;
          }
          public void setDoctype(doctypes value) {
              this.doctype = value;
          }
          public doctypes getDoctype() {
              return doctype;
          }
          public void setObj(Object obj) {
              this.obj = obj;
          }
          public Object getObj() {
              return obj;
          }

          public void setType(String type) {
              this.type = type;
          }
          public String getType() {
              return type;
          }
          @Override
          public String toString(){
              return title;
          }
         
         
      }

       

      ---------------

      view to show in the browser:

       

           <h:form>   
       
                  <rich:tree style="width:300px" nodeSelectListener="#{docTreeManager.processSelection}"
                      ajaxSubmitSelection="true"  switchType="ajax"
                      value="#{docTreeManager.treeRootNode}" var="item" ajaxKeys="#{null}">
                  </rich:tree>
          
              </h:form>

       

      any suggestions will be appreciated.

       

      thanks,

        • 1. Re: rich:tree implementation problem
          ilya_shaikovsky

          1) check please removeing the ajaxKeys="#{null}"

          2) please try to create simple localized sample if not helps.

          1 of 1 people found this helpful
          • 2. Re: rich:tree implementation problem
            hakanm

            hello ilya,

             

            i have seen your suggestion and tried removing the ajaxKeys null,

            however it didnot help.

             

            >>I will try localized one.

            i have tried commenting out the

            //addDateNodes(nodeDoctype,nodeStr);

            line so that the tree has only 1 depth. but the result is the same i can only see the root node, no other, however as before it makes an ajax call when i click on the arrow or the text in the root node.

             

            thanx a lot for having a look, and do you have any other suggestion,

             

             

            hakan

            • 3. Re: rich:tree implementation problem
              ilya_shaikovsky

              I do not even see

              i have tried commenting out the

              //addDateNodes(nodeDoctype,nodeStr);

              in your snippets. so please create sample to check locally.

              1 of 1 people found this helpful