Search in sources :

Example 1 with QueryProperty

use of org.flowable.engine.common.api.query.QueryProperty in project plumdo-work by wengwh.

the class AbstractPaginateList method paginateList.

@SuppressWarnings("rawtypes")
public DataResponse paginateList(Map<String, String> requestParams, PaginateRequest paginateRequest, Query query, String defaultSort, Map<String, QueryProperty> properties) {
    if (paginateRequest == null) {
        paginateRequest = new PaginateRequest();
    }
    // URL if possible
    if (paginateRequest.getPageNum() == null) {
        paginateRequest.setPageNum(RequestUtil.getInteger(requestParams, "pageNum", 1));
    }
    if (paginateRequest.getPageSize() == null) {
        paginateRequest.setPageSize(RequestUtil.getInteger(requestParams, "pageSize", 10));
    }
    if (paginateRequest.getSortOrder() == null) {
        paginateRequest.setSortOrder(requestParams.get("sortOrder"));
    }
    if (paginateRequest.getSortName() == null) {
        paginateRequest.setSortName(requestParams.get("sortName"));
    }
    // Use defaults for paging, if not set in the PaginationRequest, nor in
    // the URL
    Integer pageNum = paginateRequest.getPageNum();
    if (pageNum == null || pageNum <= 0) {
        pageNum = 1;
    }
    Integer pageSize = paginateRequest.getPageSize();
    if (pageSize == null || (pageSize != -1 && pageSize < 0)) {
        pageSize = 10;
    }
    String sort = paginateRequest.getSortName();
    if (sort == null) {
        sort = defaultSort;
    }
    String order = paginateRequest.getSortOrder();
    if (order == null) {
        order = "asc";
    }
    // Sort order
    if (sort != null && !properties.isEmpty()) {
        QueryProperty qp = properties.get(sort);
        if (qp == null) {
            throw new FlowableIllegalArgumentException("Value for param 'sort' is not valid, '" + sort + "' is not a valid property");
        }
        ((AbstractQuery) query).orderBy(qp);
        if (order.equals("asc")) {
            query.asc();
        } else if (order.equals("desc")) {
            query.desc();
        } else {
            throw new FlowableIllegalArgumentException("Value for param 'order' is not valid : '" + order + "', must be 'asc' or 'desc'");
        }
    }
    // Get result and set pagination parameters
    List list = null;
    // size等于-1不做分页
    if (pageSize == -1) {
        list = processList(query.list());
    } else {
        list = processList(query.listPage((pageNum - 1) * pageSize, pageSize));
    }
    DataResponse response = new DataResponse();
    response.setData(list);
    response.setDataTotal(query.count());
    response.setPageSize(pageSize);
    response.setPageNum(pageNum);
    response.setStartNum((pageNum - 1) * pageSize + 1);
    if (response.getDataTotal() > pageNum * pageSize) {
        response.setEndNum(pageNum * pageSize);
    } else {
        response.setEndNum(response.getDataTotal());
    }
    if (response.getDataTotal() % pageSize == 0) {
        response.setPageTotal((int) (response.getDataTotal() / pageSize));
    } else {
        response.setPageTotal((int) (response.getDataTotal() / pageSize) + 1);
    }
    return response;
}
Also used : FlowableIllegalArgumentException(org.flowable.engine.common.api.FlowableIllegalArgumentException) QueryProperty(org.flowable.engine.common.api.query.QueryProperty) AbstractQuery(org.flowable.engine.impl.AbstractQuery) List(java.util.List)

Aggregations

List (java.util.List)1 FlowableIllegalArgumentException (org.flowable.engine.common.api.FlowableIllegalArgumentException)1 QueryProperty (org.flowable.engine.common.api.query.QueryProperty)1 AbstractQuery (org.flowable.engine.impl.AbstractQuery)1