001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kns.util;
017
018/**
019 * This class provides utilities to support the rendering of tables in Kuali without using display tag.
020 * 
021 * Normally, displaytag handles the rendering of Kuali tables on various screens, but
022 * there are situations where displaytag is inadequate for the task (e.g. multiple value lookups).
023 * In particular, display tag does not handle POSTing of forms when switching between pages and sorting.
024 *
025 * @deprecated Only used in KNS classes, use KRAD.
026 */
027@Deprecated
028public final class TableRenderUtil {
029        
030        private TableRenderUtil() {
031                throw new UnsupportedOperationException("do not call");
032        }
033        
034    /**
035     * Returns the minimum number of pages needed to display a result set of the given page
036     * 
037     * @param resultSize number of results
038     * @param maxRowsPerPage maximum number of rows 
039     * 
040     * @return
041     */
042    public static int computeTotalNumberOfPages(int resultSize, int maxRowsPerPage) {
043        int numPages = resultSize / maxRowsPerPage;
044        if (resultSize % maxRowsPerPage != 0) {
045            // partial page
046            numPages++;
047        }
048        return numPages;
049    }
050    
051    /**
052     * This method computes the list index of the first row of the given page
053     * 
054     * @param pageNumber first page is index 0
055     * @param resultSize the size of the list being rendered
056     * @param maxRowsPerPage max number of rows on a page
057     * @return the index in the result list of the first row of the given page 
058     */
059    public static int computeStartIndexForPage(int pageNumber, int resultSize, int maxRowsPerPage) {
060        if (pageNumber < 0 && pageNumber >= computeTotalNumberOfPages(resultSize, maxRowsPerPage)) {
061            return -1;
062        }
063        return pageNumber * maxRowsPerPage;
064    }
065    
066    /**
067     * This method computes the index of the last row of the given page
068     * 
069     * @param pageNumber first page is index 0
070     * @param resultSize the size of the list being rendered
071     * @param maxRowsPerPage max number of rows on a page
072     * @return the index in the result list of the last row of the given page 
073     */
074    public static int computeLastIndexForPage(int pageNumber, int resultSize, int maxRowsPerPage) {
075        int startIndex = computeStartIndexForPage(pageNumber, resultSize, maxRowsPerPage);
076        if (startIndex == -1) {
077            return -1;
078        }
079        if (startIndex + maxRowsPerPage - 1 < resultSize) {
080            return startIndex + maxRowsPerPage - 1;
081        }
082        // partial page
083        return resultSize - 1;
084   }
085}