Basic CRUD Web Aplikasi Menggunakan SpringFramework-MVC, Freemarker, Hibernate dan JPA - Pagination Helper Model Class

Article Index

Pagination Helper Model Class

Helper class ini kita perlukan agar data table yang tampil pada halaman web memiliki pagination. Helper class ini kita tempatkan pada package org.fajarapps.jpacrud.domain. Selanjutnya buatlah 3 class helper yakni: AbstractDataGrid, DepartmentDataGrid dan EmployeeDataGrid.

Source code AbstractDataGrid.java:

package org.fajarapps.jpacrud.domain;

import org.springframework.data.domain.Sort;

public abstract class AbstractDataGrid
{
    public static final int PAGESIZE = 15;
    protected int numberOfItems;
    protected int page;
    protected String sortDir;
    protected String sortField;
    protected Sort sort;
    protected long totalItems;
    protected int totalPages;

    /**
     * Returns number of items in current page.
     */
    public int getNumberOfItems() {
        return numberOfItems;
    }

    /**
     * Sets number of items in current page.
     *
     * @param numberOfItems number of items
     * @return Fluent interface, returns itself.
     */
    public AbstractDataGrid setNumberOfItems(int numberOfItems) {
        this.numberOfItems = numberOfItems;
        return this;
    }

    /**
     * Returns the page number of current page.
     */
    public int getPage() {
        return page;
    }

    /**
     * Sets the page number for current page.
     *
     * @param page page number
     * @return Fluent interface, returns itself.
     */
    public AbstractDataGrid setPage(int page) {
        this.page = page;
        return this;
    }

    /**
     * Returns maximum items within a page.
     */
    public int getPageSize() {
        return AbstractDataGrid.PAGESIZE;
    }

    /**
     * Returns the sorting method.
     */
    public Sort getSort() {
        return sort;
    }

    /**
     * Sets the sorting method.
     *
     * @param sort The sorting method.
     * @return Fluent interface, returns itself.
     */
    public AbstractDataGrid setSort(Sort sort) {
        this.sort = sort;
        return this;
    }

    /**
     * Get sorting method specification.
     *
     * @param defaultField the default sort field
     * @return The sorting method
     */
    public Sort getSortSpec(String defaultField) {
        if (sort != null) {
            return sort;
        }

        if (sortDir == null || sortDir.isEmpty()) {
            sortDir = "asc";
        }
        if (sortField == null || sortField.isEmpty()) {
            sortField = defaultField;
        }

        sort = new Sort(Sort.Direction.fromString(sortDir), sortField);
        return sort;
    }

    /**
     * Returns the sort direction.
     */
    public String getSortDir() {
        return sortDir;
    }

    /**
     * Sets the sort direction.
     *
     * @param sortdir sort direction
     * @return Fluent interface, returns itself.
     */
    public AbstractDataGrid setSortDir(String sortdir) {
        this.sortDir = sortdir;
        return this;
    }

    /**
     * Returns the field to sort.
     */
    public String getSortField() {
        return sortField;
    }

    /**
     * Sets the field to sort.
     *
     * @param sortfield The field to sort
     * @return Fluent interface, returns itself.
     */
    public AbstractDataGrid setSortField(String sortfield) {
        this.sortField = sortfield;
        return this;
    }

    /**
     * Gets the number of total items returns from a query resultset.
     */
    public long getTotalItems() {
        return totalItems;
    }

    /**
     * Sets the number of total items returns from a query resultset.
     *
     * @param totalItems Number of total items
     * @return Fluent interface, returns itself.
     */
    public AbstractDataGrid setTotalItems(long totalItems) {
        this.totalItems = totalItems;
        return this;
    }

    /**
     * Returns the number of total pages.
     */
    public int getTotalPages() {
        return totalPages;
    }

    /**
     * Sets the number of total pages.
     *
     * @param totalPages Number of total pages
     * @return Fluent interface, returns itself.
     */
    public AbstractDataGrid setTotalPages(int totalPages) {
        this.totalPages = totalPages;
        return this;
    }
}

Class AbstractDataGrid diperlukan agar class DepartmentDataGrid dan EmployeeDataGrid lebih terstruktur dan kita tidak membuat code program yang sama berulang-ulang, atau biasa dikenal dengan istilah DRY (Don't Repeat Yourself).

Source code DepartmentDataGrid.java:

package org.fajarapps.jpacrud.domain;

/* import ... tidak ditampilkan */

public class DepartmentDataGrid extends AbstractDataGrid
{
    List entries;

    /**
     * Default class constructor.
     */
    public DepartmentDataGrid() {
    }

    /**
     * Class constructor.
     */
    public DepartmentDataGrid(Page<Department> pages) {
        setPageable(pages);
    }

    public List getEntries() {
        return entries;
    }

    public void setEntries(List<Department> entries) {
        this.entries = entries;
    }

    /**
     * Menempatkan data hasil query ke dalam datagrid buffer.
     *
     * @param pages Data hasil query
     * @return Fluent interface, returns itself.
     */
    public DepartmentDataGrid setPageable(Page<Department> pages) {
        setEntries(pages.getContent());
        setNumberOfItems(pages.getNumberOfElements());
        setPage(pages.getNumber() + 1);
        setTotalItems(pages.getTotalElements());
        setTotalPages(pages.getTotalPages());

        return this;
    }
}

Source code EmployeeDataGrid.java:

package org.fajarapps.jpacrud.domain;

/* import ... tidak ditampilkan */

public class EmployeeDataGrid extends AbstractDataGrid
{
    private List entries;
    private Integer departmentId;
    private String term;

    /**
     * Default class constructor.
     */
    public EmployeeDataGrid() {
    }

    /**
     * Class constructor.
     */
    public EmployeeDataGrid(Page<Person> pages) {
        setPageable(pages);
    }

    /* method: getter dan setter ... tidak ditampilkan */

    /**
     * Menempatkan data hasil query ke dalam datagrid buffer.
     *
     * @param pages Data hasil query
     * @return Fluent interface, returns itself.
     */
    public EmployeeDataGrid setPageable(Page<Person> pages) {
        setEntries(pages.getContent());
        setNumberOfItems(pages.getNumberOfElements());
        setPage(pages.getNumber() + 1);
        setTotalItems(pages.getTotalElements());
        setTotalPages(pages.getTotalPages());

        return this;
    }
}