title: Using jsf session scoped managed bean to represent static hierarchical data
body: <p>It is probably a kind of architectural question, but still it has to have a 'best practice solution' or an accepted standard.</p>
  
  <p>I am talking about some kind of static data that needs to be displayed on site, like catalogue of products, list of menus and menu items, list of breadcrumb blocks etc. This option is available when using any standard CMS, I suppose.</p>
  
  <p>But I would like to use a pure JSF solution for this issue.</p>
  
  <p>So, to get back to the question, my elaborations stem upon the following principles:</p>
  
  <ol>
  <li><p>Data shouldn't be hardcoded in facelets, hence I use database to hold the values, like in the following db script (MYSQL in my case):</p>
  
  <pre><code>CREATE TABLE CatalogueGroup (
      CatalogueGroupName VARCHAR(100) NOT NULL PRIMARY KEY,
      URLPath VARCHAR(200) NOT NULL,
      ParentGroupName VARCHAR(100) DEFAULT NULL,
      FOREIGN KEY (ParentGroupName) REFERENCES CatalogueGroup(CatalogueGroupName) ON UPDATE CASCADE ON DELETE SET NULL
  )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  </code></pre></li>
  <li><p>I would then like to use the entity class to be held in a @ManagedBean and display it in a view, like</p>
  
  <pre><code>public class CatalogueGroup implements Serializable {
      private String catalogueGroupName;
      private List&lt;CatalogueGroup&gt; children = new ArrayList&lt;CatalogueGroup&gt;();
      private CatalogueGroup parentGroup;
      //other stuff of this bean
  }
  
  @ManagedBean
  @SessionScoped
  public class CatalogueBean implements Serializable {
      private CatalogueGroup catalogue;//loaded via CatalogueGroupDAO with condition parentGroup == null
      //other stuff of this bean
  }
  
  //snippet of xhtml view for a two-level catalogue
  &lt;ul&gt;&lt;h:outputText value="#{catalogueBean.catalogue.catalogueGroupName}" /&gt;
      &lt;ui:rereat value="#{catalogueBean.catalogue.children}" var="group"&gt;
          &lt;li&gt;&lt;h:outputText value="#{group.catalogueGroupName}" /&gt;&lt;/li&gt;
      &lt;/ui:repeat&gt;
  &lt;/ul&gt;
  </code></pre></li>
  </ol>
  
  <p>The abovementioned setup works, but it feels like it is an awkward one.
  So, I would like to raise the following open, 'best practice' questions to the jsf community:</p>
  
  <ol>
  <li>What is a proper way of setting up a catalogue bean like this:
  <ul>
  <li>A <code>@SessionScope</code> bean that will be loaded once and redisplayed on every view or</li>
  <li>A <code>@RequestScoped</code> bean that will access database on every page display.</li>
  </ul></li>
  <li>Is there a way to setup recursive functions in facelet view, or I should limit the catalogue nesting level to, say, 2 or 3.</li>
  <li>I would like to display some kind of modified catalogue, exposing more groups to the logged in users, depending on his role (column added in a database table) and display basic catalogue when no user logs in. Additionally, I would like to occasionally insert some new groups in the catalogue and not force users to re-log in, but redisplay proper data at once:
  <ul>
  <li>Shall I filter groups in business layer and expose a filtered CatalogueGroup to the bean, or I will load the whole catalogue and limit its children in views with rendered=false;</li>
  <li>Is exposure of a whole catalogue in session a proper way to go;</li>
  <li>Is it possible to send modification events on adding new database entries to all current CatalogueBean active on server forcing to refresh their properties (CatalogueGroup) or to achieve this functionality I need to use <code>@RequestScoped</code> bean only;</li>
  <li>In case using request scoped bean is the only alternative will it be wise to access database to get data so often that rarely change or there is a smarter way of doing things;</li>
  <li>When user logs in (and logs out) there is already an instance of session scoped catalogue, how to refresh it: do I need to manually do it in action/action listener or I need to invalidate session or do something more appropriate to the situation.</li>
  </ul></li>
  </ol>