The problem occurs generating the corresponding menus. It should display: Home, Purchases, Sales... but instead what is displayed is: Sales, Sales, Sales...
In this image you can see the multiplicity of the Sales menus:
The corresponding code is:
public void establecerPermisos(){
for(SubMenu m:lista){
if(m.getMenu().getTipo().equals("S")){
DefaultSubMenu firstSubMenu=new DefaultSubMenu(m.getMenu().getNomMenu());
for(SubMenu i:lista){
Menu menu=i.getMenu();
if(menu!=null){
if(menu.getIdMenu()==m.getIdsubmenu()){
DefaultMenuItem item=new DefaultMenuItem(i.getNombresubmenu());
firstSubMenu.addElement(item);
}
}
}
model.addElement(firstSubMenu);
}else{
if(m.getIdsubmenu()==0){
DefaultMenuItem item=new DefaultMenuItem(m.getNombresubmenu());
model.addElement(item);
}
}
}
}
public List<SubMenu> listar() throws Exception{
List<SubMenu> lista;
ResultSet rs;
Menu menu=new Menu();
SubMenu submenu=new SubMenu();
EmpleadoDao dao=new EmpleadoDao();
dao.listar();
try{
this.Conectar();
PreparedStatement st=this.getCn().prepareCall("select
m.idMenu,m.codMenu,m.nomMenu,
m.link,m.tipo,m.estadoMenu,
s.idSubMenu,s.itemSubMenu,
s.nombreSubMenu,s.linkSubMenu,
s.estadoSubMenu,s.tipo,
s.idMenu from menu m \n" +
"inner join submenu s on m.idmenu=s.idmenu group by m.nomMenu");
rs=st.executeQuery();
lista=new ArrayList();
while(rs.next()){
menu.setIdMenu(rs.getInt("m.idMenu"));
menu.setCodMenu(rs.getString("m.codMenu"));
menu.setNomMenu(rs.getString("nomMenu"));
menu.setLink(rs.getString("m.link"));
menu.setTipo(rs.getString("m.tipo"));
menu.setEstadomenu(rs.getString("m.estadoMenu"));
submenu.setIdsubmenu(rs.getInt("s.idSubMenu"));
submenu.setItemsubmenu(rs.getString("s.itemSubMenu"));
submenu.setNombresubmenu(rs.getString("s.nombreSubMenu"));
submenu.setLinksubmenu(rs.getString("s.linkSubMenu"));
submenu.setEstadosubmenu(rs.getString("s.estadoSubMenu"));
submenu.setTipo(rs.getString("s.tipo"));
menu.setIdMenu(rs.getInt("s.idMenu"));
submenu.setMenu(menu);
lista.add(submenu);
//lista3.add(menu);
}
}catch(Exception e){
throw e;
}finally{
this.Cerrar();
}
return lista;
}
Ok, certainly the way you have the data is correct in what happens to you. Currently your menu creation algorithm loops through the list itself for each iteration. The problem is that by looping through your list, you're obviously duplicating instances and that causes your menu to look the way it does.
The idea is the following:
for
you must indicate which is the current record (with its idMenu)for
internal, you must process only those that match that current id (right now you process them all), that isif (actual == i.getmenu().getIdMenu())
Your inconvenience is to identify who has already been processed so that they are not processed again. I propose the following:
In your classes
SubMenu
andMenu
create an attributeboolean procesado
(with itsgetter
andsetter
), the processed attribute ofSubMenu
will be given a valuetrue
inside thefor
inner and the processed attribute ofMenu
will be given a valuetrue
when leaving thefor
inner.For each iteration of the
for
main one, you will only enter to process the record if it has the attributeprocesado
with valuefalse
. Modify your methodestablecerPermisos()
like so:Try this approach for your rendering and let us know. Cheers,
The response after debugging. You had to initialize the menu and submenu on each iteration of the loop
while
:But at the moment of executing the menus are duplicated with their corresponding submenu, if someone of you can give me ideas in the solution, thank you very much.
Well I hope someone helps me
well in my opinion the problem is
for(SubMenu m:list){
I am going to add an if to control the repeated menus but I don't know how I can control it. I have made comparisons with codigodesubmenu vs codmenu but it does not work. How do I compare the menu codes and if it is repeated, show it only once.
I hope someone can help me with another approach. Cheers
I leave the image of the query the database is in MYSQL you will notice that they are two related tables