001/**
002 * Copyright 2005-2018 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.krad.uif.layout.collections;
017
018import org.kuali.rice.krad.uif.UifConstants;
019import org.kuali.rice.krad.uif.container.CollectionGroup;
020import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
021import org.kuali.rice.krad.uif.view.View;
022import org.kuali.rice.krad.web.form.UifFormBase;
023
024import java.util.List;
025
026/**
027 * StackedPagingHelper contains method(s) to help determine the correct page display information during
028 * a request.
029 *
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class CollectionPagingHelper {
033
034    /**
035     * Process the paging request by determining the displayStart value based on the page requested.
036     *
037     * @param view the current view
038     * @param form the form
039     * @param page the page requested (can be a number, prev, next, first, last)
040     */
041    public void processPagingRequest(View view, CollectionGroup collectionGroup, UifFormBase form, String page) {
042        // allow only one concurrent request per view
043        synchronized (view) {
044            List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(form,
045                    collectionGroup.getBindingInfo().getBindingPath());
046
047            int displayStart = collectionGroup.getDisplayStart();
048            int displayLength = collectionGroup.getDisplayLength();
049
050            // adjust displayStart based on the page requested
051            if (page.equals(UifConstants.PageRequest.FIRST)) {
052                displayStart = 0;
053            } else if (page.equals(UifConstants.PageRequest.PREV)) {
054                displayStart = displayStart - displayLength;
055            } else if (page.equals(UifConstants.PageRequest.NEXT)) {
056                displayStart = displayStart + displayLength;
057            } else if (page.equals(UifConstants.PageRequest.LAST)) {
058                int lastPageSize = modelCollection.size() % displayLength;
059
060                if (lastPageSize != 0) {
061                    displayStart = modelCollection.size() - lastPageSize;
062                } else {
063                    displayStart = modelCollection.size() - displayLength;
064                }
065            } else {
066                displayStart = ((Integer.parseInt(page.trim()) - 1) * displayLength);
067            }
068
069            collectionGroup.setDisplayStart(displayStart);
070        }
071    }
072}