com.goosby.virgo.utils.PageRequest.java Source code

Java tutorial

Introduction

Here is the source code for com.goosby.virgo.utils.PageRequest.java

Source

/**
 * Copyright (c) 2005-2011 springside.org.cn
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: Fixtures.java 1593 2011-05-11 10:37:12Z calvinxiu $
 */
package com.goosby.virgo.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

/**
 * ??.
 */
public class PageRequest implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected int pageNo = 1;
    protected int pageSize = 10;

    protected String orderBy = null;
    protected String orderDir = null;

    protected boolean countTotal = true;

    public PageRequest() {
    }

    public PageRequest(int pageNo, int pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    /**
     * ??, ??1, 1.
     */
    public int getPageNo() {
        return this.pageNo;
    }

    /**
     * ??, ??1, 11.
     */
    public void setPageNo(final int pageNo) {
        this.pageNo = pageNo;

        if (pageNo < 1) {
            this.pageNo = 1;
        }
    }

    /**
     * ??, 10.
     */
    public int getPageSize() {
        return this.pageSize;
    }

    /**
     * ??, 11.
     */
    public void setPageSize(final int pageSize) {
        this.pageSize = pageSize;

        if (pageSize < 1) {
            this.pageSize = 1;
        }
    }

    /**
     * ?, . ?','.
     */
    public String getOrderBy() {
        return this.orderBy;
    }

    /**
     * ?, ?','.
     */
    public void setOrderBy(final String orderBy) {
        this.orderBy = orderBy;
    }

    /**
     * ??, .
     */
    public String getOrderDir() {
        return this.orderDir;
    }

    /**
     * ???.
     * 
     * @param orderDir ?descasc,?','.
     */
    public void setOrderDir(final String orderDir) {
        final String lowcaseOrderDir = StringUtils.lowerCase(orderDir);

        // order?
        final String[] orderDirs = StringUtils.split(lowcaseOrderDir, ',');
        for (final String orderDirStr : orderDirs) {
            if (!StringUtils.equals(Sort.DESC, orderDirStr) && !StringUtils.equals(Sort.ASC, orderDirStr))
                throw new IllegalArgumentException("??" + orderDirStr + "??");
        }

        this.orderDir = lowcaseOrderDir;
    }

    /**
     * ??.
     */
    public List<Sort> getSort() {
        final String[] orderBys = StringUtils.split(this.orderBy, ',');
        final String[] orderDirs = StringUtils.split(this.orderDir, ',');
        final List<Sort> orders = new ArrayList<Sort>();
        for (int i = 0; i < orderBys.length; i++) {
            orders.add(new Sort(orderBys[i], orderDirs[i]));
        }
        return orders;
    }

    /**
     * ??,.
     */
    public boolean isOrderBySetted() {
        return (StringUtils.isNotBlank(this.orderBy) && StringUtils.isNotBlank(this.orderDir));
    }

    /**
     * ?.
     */
    public boolean isCountTotal() {
        return this.countTotal;
    }

    /**
     * ?.
     */
    public void setCountTotal(boolean countTotal) {
        this.countTotal = countTotal;
    }

    /**
     * ?pageNopageSize???, ??0.
     */
    public int getOffset() {
        return ((this.pageNo - 1) * this.pageSize);
    }

    public static class Sort {

        public static final String ASC = "asc";
        public static final String DESC = "desc";

        private final String property;
        private final String dir;

        public Sort(String property, String dir) {
            this.property = property;
            this.dir = dir;
        }

        public String getProperty() {
            return this.property;
        }

        public String getDir() {
            return this.dir;
        }
    }
}