Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You do not really need a generic tree data structure for this. Consider the following example:</p> <pre><code>private static final class Menu { private Menu parent; private List&lt;Menu&gt; children; private String moduleName; private String moduleControllerName; private String moduleActionName; private String alias; } </code></pre> <p>(getters/setters ommited for conciseness)</p> <p>The <code>parent</code> field helps you set the parent menu and can be null for the root menu.</p> <p>Add the Children menu to the <code>children</code> field.</p> <p>With this you will be able to capture the needs of your hierarchical menu I hope.</p> <p>Now to build the query supposing you have a result set <code>rs</code>:</p> <pre><code>Menu menu; while (rs.hasNext()) { if (rs.get("parent_id") == null) { // it s the root menu = new Menu(rs.get("id"), /* etc... */); } else { menu = findMenuById(menu, rs.get("parent_id")); menu.addChild(new Menu(rs.get("id"), /* etc... */)); } </code></pre> <p>as for find <code>findMenuById</code> it could be something like:</p> <pre><code>private Menu findMenuById(Menu menu, Long id) { if (menu.getId() == id) return menu; for (Menu childMenu : menu.getChildren()) { Menu found = findMenuById(childMenu, id); if (found != null) return found; } return null; } </code></pre> <h3>edit</h3> <p>Here is a custom working implementation I made. I inserted your menu in the database and use result set. I should be almost the same with the custom abstraction of yours. </p> <pre><code>Menu root = null; Map&lt;Integer, Menu&gt; menus = new HashMap&lt;Integer, Menu&gt;(); final Database databaseConnection = Database.createConnection("test", "root", ""); final ResultSet rs = databaseConnection.executeQuery("SELECT * FROM test.menus;"); while ( rs.next() ) { final Menu menu = new Menu(rs.getInt("id")) .setAlias(rs.getString("alias")) .setModuleName(rs.getString("module_name")); final Integer parentId = rs.getInt("parent_id"); if (root == null &amp;&amp; parentId == 0) { root = menu; } else { menus.get(parentId).addSubMenu(menu); } menus.put(menu.getId(), menu); } rootMenu = root; databaseConnection.closeConnection(); </code></pre> <p><code>Note 1</code>: I used an <code>HashMap</code> to store the menus that are not attached to the root yet.</p> <p><code>Note 2</code>: This implementation wont work if there are more than one root menu.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload