หน้าเว็บ

วันอังคารที่ 27 พฤศจิกายน พ.ศ. 2555

Lazy load primefaces

xml
         
<p:dataTable value="#{startupForumMB.categorys}"
             var="category"
             rows="25"
             lazy="true"
             sortBy="#{category.id}"
             sortOrder="ASCENDING"
             rowIndexVar="counter"
             paginator="true"
             paginatorPosition="bottom"
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
             rowsPerPageTemplate="5,10,15,25,50">
    <p:column headerText="No.">
        <h:outputText value="#{counter + 1}"/>
    </p:column>
    <p:column headerText="category">
        <h:outputText value="#{category.categoryName}"/>
    </p:column>
</p:dataTable>
managed bean
package com.blogspot.na5cent.controller;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.blogspot.na5cent.lazy.CategoryLazy;

/**
 *
 * @author Redcrow
 */
@ManagedBean
@RequestScoped
public class StartupForumMB {

    private static final Logger LOG = LoggerFactory.getLogger(StartupForumMB.class);
    //information
    private CategoryLazy categorys;

    @PostConstruct
    public void postContruct() {
        reset();
    }

    private void reset() {
        categorys = new CategoryLazy();
    }

    public CategoryLazy getCategorys() {
        return categorys;
    }

    public void setCategorys(CategoryLazy categorys) {
        this.categorys = categorys;
    }
}

lazy class
package com.blogspot.na5cent.lazy;

import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import com.blogspot.na5cent.model.Category;
import com.blogspot.na5cent.service.CategoryService;
import com.blogspot.na5cent.util.JSFSpringUtils;

/**
 *
 * @author Redcrow
 */
public class CategoryLazy extends LazyDataModel<Category> {
    //log

    static final Logger LOG = LoggerFactory.getLogger(CategoryLazy.class);
    private List<Category> categorys;
    private long totalElements;
    //services
    private CategoryService categoryService = JSFSpringUtils.getBean(CategoryService.class);


    public CategoryLazy() {
    }

    @Override
    public Category getRowData(String id) {
        for (Category cate : categorys) {
            if (cate.getId().toString().equals(id)) {
                return cate;
            }
        }

        return null;
    }

    @Override
    public Object getRowKey(Category cate) {
        return cate.getId();
    }

    @Override
    public void setRowIndex(final int rowIndex) {
        if (rowIndex == -1 || getPageSize() == 0) {
            super.setRowIndex(-1);
        } else {
            super.setRowIndex(rowIndex % getPageSize());
        }
    }

    @Override
    public List<Category> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        Sort.Direction direction;
        if (sortOrder == SortOrder.ASCENDING) {
            direction = Sort.Direction.ASC;
        } else {
            direction = Sort.Direction.DESC;
        }

        Page page = categoryService.findAllPaging(new PageRequest(first / pageSize, pageSize, direction, sortField));
        categorys = page.getContent();
        totalElements = page.getTotalElements();
        this.setRowCount((int) page.getTotalElements());
        return categorys;
    }

    public List<Category> getContents() {
        return categorys;
    }

    public long getTotalElements() {
        return totalElements;
    }
}