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);
}
}
}