001    /**
002     * Copyright 2005-2012 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     */
016    package 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     */
026    public final class TableRenderUtil {
027            
028            private TableRenderUtil() {
029                    throw new UnsupportedOperationException("do not call");
030            }
031            
032        /**
033         * Returns the minimum number of pages needed to display a result set of the given page
034         * 
035         * @param resultSize number of results
036         * @param maxRowsPerPage maximum number of rows 
037         * 
038         * @return
039         */
040        public static int computeTotalNumberOfPages(int resultSize, int maxRowsPerPage) {
041            int numPages = resultSize / maxRowsPerPage;
042            if (resultSize % maxRowsPerPage != 0) {
043                // partial page
044                numPages++;
045            }
046            return numPages;
047        }
048        
049        /**
050         * This method computes the list index of the first row of the given page
051         * 
052         * @param pageNumber first page is index 0
053         * @param resultSize the size of the list being rendered
054         * @param maxRowsPerPage max number of rows on a page
055         * @return the index in the result list of the first row of the given page 
056         */
057        public static int computeStartIndexForPage(int pageNumber, int resultSize, int maxRowsPerPage) {
058            if (pageNumber < 0 && pageNumber >= computeTotalNumberOfPages(resultSize, maxRowsPerPage)) {
059                return -1;
060            }
061            return pageNumber * maxRowsPerPage;
062        }
063        
064        /**
065         * This method computes the index of the last row of the given page
066         * 
067         * @param pageNumber first page is index 0
068         * @param resultSize the size of the list being rendered
069         * @param maxRowsPerPage max number of rows on a page
070         * @return the index in the result list of the last row of the given page 
071         */
072        public static int computeLastIndexForPage(int pageNumber, int resultSize, int maxRowsPerPage) {
073            int startIndex = computeStartIndexForPage(pageNumber, resultSize, maxRowsPerPage);
074            if (startIndex == -1) {
075                return -1;
076            }
077            if (startIndex + maxRowsPerPage - 1 < resultSize) {
078                return startIndex + maxRowsPerPage - 1;
079            }
080            // partial page
081            return resultSize - 1;
082       }
083    }