Mount recursive function for TreeTable

0

I am trying to mount a folder tree and ask questions with Java, Jsf and Primefaces. I have two lists, one is the one that writes the folders and subfolders, and another list is the one that assembles the questions of these folders or subfolders. Follow the image of how it is mounted so far.

Inthefolderthatisoutthere,ifitisselected,thefolderiswrittentotheroot,asshownintheimage.IntheactionscolumnIhaveanothericonfolder,andaninterrogationiconthatifclickedisaddedaquestioninsidethelinethatwasclicked.TherearemethodsthatIwasabletoassemblesofartomounttherecursion.Ifanyonecangivealighthowtomountitwouldbegrateful.

publicTreeNodemontaTreeNode(){TreeNodepasta=null;TreeNoderoot=newDefaultTreeNode(newBancoPerguntaView(null,null,null),null);if(this.listaBancoPasta!=null||!this.listaBancoPasta.isEmpty()){try{for(BancoPastasbdPasta:this.listaBancoPasta){pasta=newDefaultTreeNode(newBancoPerguntaView(bdPasta.getIdBancoPasta(),bdPasta.getNomePasta(),bdPasta.getTipoPasta()),root);this.adicionarFilhos(pasta,bdPasta,null);}}catch(Exceptione){System.out.println("Erro treeNode: " + e.getMessage());
        }
    }

    return root;
}

public void adicionarFilhos(TreeNode nodePai, BancoPastas bdPasta, BancoPerguntas pergunta) {

    if(this.listaBancoPerguntas!= null || !this.listaBancoPerguntas.isEmpty()){

        try {

            for (BancoPerguntas bdPergunta : this.listaBancoPerguntas) {

                TreeNode nodeFilho = new DefaultTreeNode(new BancoPerguntaView(bdPergunta.getFkIdPasta(), bdPergunta.getNomePergunta(), bdPergunta.getTipoPergunta()), nodePai);

                this.adicionarFilhos(nodeFilho, null , bdPergunta);
            }

        } catch (Exception e) {
            System.out.println("Erro adicionarFilhos: " + e.getMessage());
        }
    }
}

But when I click to add a subfolder or a question, it loops in the line that calls recursion

this.adicionarFilhos(nodeFilho, null , bdPergunta);

Subfolders going to the root, instead of entering the folder

FollowrefactoredmethodsasWeslleysuggests

publicTreeNodemontaTreeNode(){TreeNodepasta=null;TreeNoderoot=newDefaultTreeNode(newBancoPerguntaView(null,null,null),null);if(this.listaBancoPasta!=null||!this.listaBancoPasta.isEmpty()){try{for(BancoPastasbdPasta:this.listaBancoPasta){pasta=newDefaultTreeNode(newBancoPerguntaView(bdPasta.getIdBancoPasta(),bdPasta.getNomePasta(),bdPasta.getTipoPasta()),root);this.adicionarFilhos(pasta,bdPasta,null);}}catch(Exceptione){System.out.println("Erro treeNode: " + e.getMessage());
        }
    }

    return root;

}

public void adicionarFilhos(TreeNode nodePai, BancoPastas bdPasta, List<BancoPerguntas> perguntas) {

    if (Objects.nonNull(perguntas) && !perguntas.isEmpty()) {

        BancoPerguntas bdPergunta = perguntas.get(0);

        perguntas.remove(bdPergunta); 

        for (BancoPerguntas pergunta : perguntas) {

            TreeNode nodeFilho = new DefaultTreeNode(new BancoPerguntaView(pergunta.getFkIdPasta(), pergunta.getNomePergunta(), pergunta.getTipoPergunta()), nodePai);

            this.adicionarFilhos(nodeFilho, null, perguntas);
        }
    }
}
    
asked by anonymous 05.04.2018 / 15:57

1 answer

1

Your problem is occurring because of the use of the listaBancoPerguntas parameter in your for .

Notice that every time you enter the call of the excerpt:

for (BancoPerguntas bdPergunta : this.listaBancoPerguntas) {
    TreeNode nodeFilho = new DefaultTreeNode(new BancoPerguntaView(bdPergunta.getFkIdPasta(), bdPergunta.getNomePergunta(), bdPergunta.getTipoPergunta()), nodePai);
    this.adicionarFilhos(nodeFilho, null , bdPergunta);
}

Your code will return to the same previous state, since the list has not changed yet.

One of the possible solutions would be to pass the list itself as a parameter to the call of your adicionarFilhos() method and to perform validation (removal of the current record perhaps?) required to eliminate the infinite loop in your code.

Correction proposal

One possibility would be to refactor your method to:

public void adicionarFilhos(TreeNode nodePai, BancoPastas bdPasta, List<BancoPerguntas> perguntas) {
    if (Objects.nonNull(perguntas) && !perguntas.isEmpty()) {
        BancoPerguntas bdPergunta = perguntas.get(0);
        perguntas.remove(bdPergunta); // Assumindo que a classe BancoPerguntas implementa o equals(). Caso não implemente, utilize o índice, que será o 0
        for (BancoPerguntas bdPergunta : perguntas) {
            TreeNode nodeFilho = new DefaultTreeNode(new BancoPerguntaView(bdPergunta.getFkIdPasta(), bdPergunta.getNomePergunta(), bdPergunta.getTipoPergunta()), nodePai);
            this.adicionarFilhos(nodeFilho, null , perguntas);
        }
    }
}
    
05.04.2018 / 16:57