Wednesday, October 01, 2014

View centric lazy data loading using Apache Commons LazyMap in JSF with Primefaces.

 Facelets:
   <p:dataTable id="europe" value="#{bean.dataMap.europe}" var="data">
    <p:column>
     <h:outputText value="#{data.displayName}" />
    </p:column>
   </p:dataTable>

   <p:dataTable id="asia" value="#{bean.dataMap.asia}" var="data">
    <p:column>
     <h:outputText value="#{data.displayName}" />
    </p:column>
   </p:dataTable>


 Backed Bean:

 import org.apache.commons.collections.Transformer;
 import org.apache.commons.collections.map.LazyMap;
 import org.primefaces.model.LazyDataModel;
 ...
 private Map dataMap = LazyMap.decorate(new HashMap<String, List>(), new Transformer() {
  public List transform(Object o) {
   if ("asia".equalsIgnoreCase(o.toString())) {
    return Arrays.asList(Locale.TAIWAN, Locale.CHINA, Locale.KOREA);
   } else {
    return Arrays.asList(Locale.GERMAN, Locale.FRENCH, Locale.ENGLISH);
   }
  }
 });