All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.luues.util.datawrap.Page Maven / Gradle / Ivy

There is a newer version: 1.3.0.5.RELEASE
Show newest version
package com.luues.util.datawrap;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Page extends QueryParamers implements Serializable {
    private static final int PAGE_SIZE = 20;
    private static final String MAP_KEY_START = "start";
    private static final String MAP_KEY_LIMIT = "limit";
    private int currPage = 1;
    private int pageSize = 20;
    private long total;
    private int pageCount;
    private List data;
    private String json;

    public Page() {
    }

    public Page(int currPage, int pageSize){
        this.currPage = currPage;
        this.pageSize = pageSize;
    }

    public Page(Map extGridParams) {
        Object obj = extGridParams.get("limit");
        if (obj != null) {
            this.setPageSize(Integer.parseInt(obj.toString()));
        }

        obj = extGridParams.get("start");
        if (obj != null) {
            this.setCurrPage(Integer.parseInt(obj.toString()) / this.getPageSize() + 1);
        }

        this.setQueryFilter(extGridParams);
        extGridParams.remove("start");
        extGridParams.remove("limit");
    }

    public int getCurrPage() {
        return this.currPage;
    }

    public void setCurrPage(int currPage) {
        this.currPage = currPage;
        if (currPage < 1) {
            this.currPage = 1;
        }
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
        if (pageSize < 1) {
            this.pageSize = 20;
        }
    }

    public int getFirst() {
        return (this.currPage - 1) * this.pageSize + 1;
    }

    public long getTotal() {
        return this.total;
    }

    public void setTotal(long total) {
        this.total = total;
        if (this.pageCount == 0) {
            this.pageCount = (int)total / this.pageSize;
            this.pageCount += total % (long)this.pageSize > 0L ? 1 : 0;
        }

    }

    public int getPageCount() {
        return this.pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public boolean isHasNext() {
        return this.currPage + 1 <= this.pageCount;
    }

    public int getNextPage() {
        return this.isHasNext() ? this.currPage + 1 : this.currPage;
    }

    public boolean isHasPre() {
        return this.currPage - 1 >= 1;
    }

    public int getPrePage() {
        return this.isHasPre() ? this.currPage - 1 : this.currPage;
    }

    public List getData() {
        return this.data;
    }

    public void setData(List data) {
        this.data = data;
    }

    public static Page buildFromJson(String json) {
        Page page = null;
        if (StringUtils.isEmpty(json)) {
            page = new Page();
        } else {
            page = JSON.parseObject(json, Page.class);
        }

        return page;
    }

    public String pageToJson(boolean includeData) {
        Map pageMap = new HashMap();
        pageMap.put("pageNo", this.getCurrPage());
        pageMap.put("queryFilter", this.getQueryFilter());
        pageMap.put("sortExpression", this.getSortExpression());
        this.json = JSON.parseObject(pageMap.toString()).toString();
        pageMap = null;
        return this.json;
    }
}

class QueryParamers implements Serializable {
    public static final String MAP_KEY_SORT = "sort";
    private Map queryFilter;
    private Map queryNotFilter;
    private Map queryLikeFilter;
    private Map queryMoreLikeFilter;
    private Map queryMoreFilter;
    private String select = null;
    private String from = null;
    private String sortExpression;
    private String groupExpression;
    private String json;

    public QueryParamers() {
        this.queryFilter = new HashMap();
        this.queryNotFilter = new HashMap();
        this.queryLikeFilter = new HashMap<>();
        this.queryMoreLikeFilter = new HashMap<>();
        this.queryMoreFilter = new HashMap<>();
    }

    public QueryParamers addQueryFilter(Object key, Object value) {
        this.queryFilter.put(key.toString(), value);
        return this;
    }

    public QueryParamers addQueryNotFilter(Object key, Object value) {
        this.queryNotFilter.put(key.toString(), value);
        return this;
    }

    public QueryParamers addQueryLikeFilter(Object key, Object value) {
        this.queryLikeFilter.put(key.toString(), value);
        return this;
    }

    public QueryParamers setSelect(String select){
        this.select = select;
        return this;
    }

    public QueryParamers setFrom(String from){
        this.from = from;
        return this;
    }

    public QueryParamers addQueryMoreLikeFilter(Object key, ArrayList value) {
        if(null == value){
            this.queryMoreLikeFilter.put(key.toString(), new ArrayList());
            return this;
        }
        this.queryMoreLikeFilter.put(key.toString(), value);
        return this;
    }

    public QueryParamers addQueryMoreFilter(Object key, ArrayList value) {
        if(null == value){
            this.queryMoreFilter.put(key.toString(), new ArrayList());
            return this;
        }
        this.queryMoreFilter.put(key.toString(), value);
        return this;
    }

    /*public QueryParamers addQueryTypeFilter(Object key, Object value, SelectType.Type type) {
        this.queryFilter.put(key + " " + type.getRemark() + " ", value);
        return this;
    }*/

    public QueryParamers removeQueryFilter(String key) {
        this.queryFilter.remove(key);
        return this;
    }

    public QueryParamers removeQueryNotFilter(String key) {
        this.queryNotFilter.remove(key);
        return this;
    }

    public QueryParamers removeQueryMoreLikeFilter(String key) {
        this.queryMoreLikeFilter.remove(key);
        return this;
    }

    public QueryParamers removeQueryMoreFilter(String key) {
        this.queryMoreFilter.remove(key);
        return this;
    }

    public QueryParamers removeQueryLikeFilter(String key) {
        this.queryLikeFilter.remove(key);
        return this;
    }

    public QueryParamers addQueryFilters(Map map) {
        this.queryFilter.putAll(map);
        return this;
    }

    public QueryParamers addQueryLikeFilters(Map map) {
        this.queryLikeFilter.putAll(map);
        return this;
    }

    public Map getQueryFilter() {
        return this.queryFilter;
    }

    public Map getQueryNotFilter() {
        return this.queryNotFilter;
    }

    public Map getQueryLikeFilter() {
        return this.queryLikeFilter;
    }

    public Map getQueryMoreLikeFilter() {
        return this.queryMoreLikeFilter;
    }

    public Map getQueryMoreFilter() {
        return this.queryMoreFilter;
    }

    public String getSelect(){return this.select;};

    public String getFrom(){return this.from;};

    public void setQueryFilter(Map queryFilter) {
        this.queryFilter = queryFilter;
    }

    public void setQueryLikeFilter(Map queryLikeFilter) {
        this.queryLikeFilter = queryLikeFilter;
    }

    public String getSortExpression() {
        return this.sortExpression;
    }

    public void setSortExpression(String sortExpression) {
        this.sortExpression = sortExpression;
        this.queryFilter.remove("sort");
    }

    public String queryToJson() {
        if (this.json == null) {
            this.json = JSON.toJSONString(this);
        }

        return this.json;
    }

    public static QueryParamers buildFromJson(String json) {
        QueryParamers queryParamers = null;
        if (StringUtils.isEmpty(json)) {
            queryParamers = new QueryParamers();
        } else {
            Map map = JSON.parseObject(json, HashMap.class);
            queryParamers = new QueryParamers();
            queryParamers.setQueryFilter(map);
        }

        return queryParamers;
    }

    public String getGroupExpression() {
        return groupExpression;
    }

    public void setGroupExpression(String groupExpression) {
        this.groupExpression = groupExpression;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy