com.bstek.dorado.data.provider.PagingList.java Source code

Java tutorial

Introduction

Here is the source code for com.bstek.dorado.data.provider.PagingList.java

Source

/*
 * This file is part of Dorado 7.x (http://dorado7.bsdn.org).
 * 
 * Copyright (c) 2002-2012 BSTEK Corp. All rights reserved.
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * and BSDN commercial (http://www.bsdn.org/licenses) licenses.
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at http://www.bstek.com/contact.
 */

package com.bstek.dorado.data.provider;

import java.io.InvalidObjectException;
import java.io.ObjectStreamException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bstek.dorado.data.type.DataType;
import com.bstek.dorado.util.proxy.ListProxySupport;

/**
 * ??List
 * 
 * @author Benny Bao (mailto:benny.bao@bstek.com)
 * @since Apr 3, 2008
 */
public class PagingList<E> extends ListProxySupport<E> {
    private static final long serialVersionUID = -2663699029002561283L;
    private static final Log logger = LogFactory.getLog(PagingList.class);

    private DataProvider dataProvider;
    private DataType dataType;
    private Object parameter;
    private int pageSize;
    private int pageNo;
    private int entityCount;
    private int pageCount;

    private Map<Integer, Page<E>> pageMap = new HashMap<Integer, Page<E>>();

    @SuppressWarnings("unchecked")
    public PagingList(DataProvider dataProvider, DataType dataType, Object parameter, int pageSize, int pageNo)
            throws Exception {
        super((List<E>) Collections.emptyList());
        this.dataProvider = dataProvider;
        this.dataType = dataType;
        this.parameter = parameter;
        this.pageSize = pageSize;
        this.gotoPage(pageNo);
    }

    public PagingList(DataProvider dataProvider, DataType dataType, Object parameter, int pageSize)
            throws Exception {
        this(dataProvider, dataType, parameter, pageSize, 1);
    }

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

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

    /**
     * 
     * <p>
     * ?????
     * </p>
     */
    public int getEntityCount() {
        return entityCount;
    }

    /**
     * 
     * <p>
     * ?? ???
     * </p>
     */
    public void setEntityCount(int entityCount) {
        if (entityCount < 0) {
            throw new IllegalArgumentException("Illegal entityCount arguments. [entityCount=" + entityCount + "]");
        }

        this.entityCount = entityCount;
        pageCount = ((entityCount - 1) / pageSize) + 1;
    }

    /**
     * 
     */
    public int getPageCount() {
        return pageCount;
    }

    /**
     * <br>
     * ??List????
     * 
     * @throws Exception
     */
    public void gotoFirstPage() throws Exception {
        gotoPage(1);
    }

    /**
     * <br>
     * ??List????
     * 
     * @throws Exception
     */
    public void gotoPreviousPage() throws Exception {
        gotoPage(pageNo + 1);
    }

    /**
     * <br>
     * ??List????
     * 
     * @throws Exception
     */
    public void gotoNextPage() throws Exception {
        gotoPage(pageNo - 1);
    }

    /**
     * ?<br>
     * ??List?????
     * 
     * @throws Exception
     */
    public void gotoLastPage() throws Exception {
        gotoPage(pageCount);
    }

    protected Page<E> getPage(int pageNo) throws Exception {
        Page<E> page = pageMap.get(pageNo);
        if (page == null) {
            page = new Page<E>(pageSize, pageNo);
            dataProvider.getPagingResult(parameter, page, dataType);
            pageMap.put(pageNo, page);
        }
        return page;
    }

    /**
     * <br>
     * ??List????
     * 
     * @param pageNo
     * @throws Exception
     */
    public void gotoPage(int pageNo) throws Exception {
        if (pageNo > pageCount) {
            pageNo = pageCount;
        }
        if (pageNo < 1) {
            pageNo = 1;
        }

        if (pageNo != this.pageNo) {
            Page<E> page = getPage(pageNo);
            setTarget(page.getEntities());

            this.pageNo = pageNo;
            entityCount = page.getEntityCount();
            pageCount = page.getPageCount();
        }
    }

    @Override
    public Object writeReplace() throws ObjectStreamException {
        try {
            List<E> list = new ArrayList<E>();
            for (int i = 1; i <= pageCount; i++) {
                Page<E> page = getPage(pageNo);
                list.addAll(page.getEntities());
            }
            return list;
        } catch (Exception e) {
            logger.error(e, e);
            throw new InvalidObjectException(e.getMessage());
        }
    }
}