com.jdy.ddj.common.orm.PageRequest.java Source code

Java tutorial

Introduction

Here is the source code for com.jdy.ddj.common.orm.PageRequest.java

Source

/**
 * Copyright (c) 2005-2011 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *
 * $Id: PageRequest.java 1661 2011-11-17 20:35:10Z calvinxiu $
 */
package com.jdy.ddj.common.orm;

import com.google.common.collect.Lists;
import com.jdy.ddj.common.dao.Criteria;
import com.jdy.ddj.common.dao.Criterion;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

import java.io.Serializable;
import java.util.List;

/**
 * ??.
 */
public class PageRequest implements Serializable {
    protected int pageNo = 1;
    protected int pageSize = 10;

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

    protected boolean countTotal = true;

    private String orderByClause;

    protected List<Criterion> criteria = Lists.newArrayList();

    protected List<Criteria> oredCriteria = Lists.newArrayList();

    public String getOrderByClause() {
        return orderByClause;
    }

    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }

    public List<Criteria> getOredCriteria() {
        return oredCriteria;
    }

    public void setOredCriteria(List<Criteria> oredCriteria) {
        this.oredCriteria = oredCriteria;
    }

    public List<Criterion> getCriteria() {
        return criteria;
    }

    public void setCriteria(List<Criterion> criteria) {
        this.criteria = criteria;
    }

    public PageRequest() {
    }

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

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

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

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

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

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

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

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

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

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

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

        //order?
        String[] orderDirs = StringUtils.split(lowcaseOrderDir, ',');
        for (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() {
        String[] orderBys = StringUtils.split(orderBy, ',');
        String[] orderDirs = StringUtils.split(orderDir, ',');
        Validate.isTrue(orderBys.length == orderDirs.length,
                "???,????");

        List<Sort> orders = Lists.newArrayList();
        for (int i = 0; i < orderBys.length; i++) {
            orders.add(new Sort(orderBys[i], orderDirs[i]));
        }

        return orders;
    }

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

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

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

    /**
     * ?pageNopageSize???, ??0.
     */
    public int getOffset() {
        return ((pageNo - 1) * 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 property;
        }

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