I have three tables in MySQL database.
- category (excluded from this question)
- sub_category
- product
The relationship between these tables is intuitive - one-to-many in the order in which they appear.
I'm iterating through a list of SubCategory
, List<SubCategory>
using <p:dataGrid>
as follows.
<p:dataGrid var="row" value="#{featuredProductManagedBean}" rows="4" first="0" columns="1" rowIndexVar="rowIndex" paginator="true" paginatorAlwaysVisible="false" pageLinks="10" lazy="true" rowsPerPageTemplate="5,10,15">
<h:panelGrid columns="1" style="width:100%;">
<p:carousel value="#{featuredProductManagedBean.getProducts(row.subCatId)}" var="prodRow" numVisible="4" pageLinks="5" headerText="#{row.subCatName}" style="text-align: left;">
<!--Display product image in <p:graphicImage>-->
</p:carousel>
<h:outputLink rendered="#{featuredProductManagedBean.count gt 4}" value="xxx">View More</h:outputLink>
</h:panelGrid>
<p:ajax event="page" onstart="PF('blockDataPanelUIWidget').block()" oncomplete="PF('blockDataPanelUIWidget').unblock()"/>
</p:dataGrid>
There is a parameterized getter method associated with the value attribute of </p:carousel>
featuredProductManagedBean.getProducts(row.subCatId)
.
The method is called several times causing the expensive business service(s) to be invoked several times.
The managed bean:
@ManagedBean
@ViewScoped
public final class FeaturedProductManagedBean extends LazyDataModel<SubCategory> implements Serializable
{
@EJB
private final FeaturedProductBeanLocal service=null;
private Product selectedProduct; //Getter and setter.
private Long count; //Getter and setter.
private static final long serialVersionUID = 1L;
public FeaturedProductManagedBean() {}
public List<Product>getProducts(Long subCatId)
{
List<Product>products=null;
if(FacesContext.getCurrentInstance().getCurrentPhaseId().getOrdinal()==6)
{
setCount(service.countProducts(subCatId));
products=service.getProductList(subCatId);
}
return products;
}
@Override
public List<SubCategory> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters)
{
int rowCount = service.rowCount().intValue();
setRowCount(rowCount);
if(pageSize<=0)
{
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_FATAL, Utility.getMessage("faces.message.error"), Utility.getMessage("pageSize.error.message"));
FacesContext.getCurrentInstance().addMessage(null, message);
return Collections.emptyList();
}
else if(first>=pageSize&&rowCount<=Utility.currentPage(first, pageSize)*pageSize-pageSize) {
first-=pageSize;
}
setPageSize(pageSize);
return service.getList(first, pageSize);
}
}
Neither @PostConstruct
not lazy loading can be used here. Currently I have put a conditional check if(FacesContext.getCurrentInstance().getCurrentPhaseId().getOrdinal()==6)
to prevent the service methods from being called multiple times. Does this conditional check perform the right thing? Does it have some side effects?
This answer maintains a Map
but maintaining a Map
for large data sources is expensive.
Is there a precise way to prevent such business logic from being executed multiple times?