View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.layout.collections;
17  
18  import org.kuali.rice.krad.uif.UifConstants;
19  import org.kuali.rice.krad.uif.container.CollectionGroup;
20  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
21  import org.kuali.rice.krad.uif.view.View;
22  import org.kuali.rice.krad.web.form.UifFormBase;
23  
24  import java.util.List;
25  
26  /**
27   * StackedPagingHelper contains method(s) to help determine the correct page display information during
28   * a request.
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class CollectionPagingHelper {
33  
34      /**
35       * Process the paging request by determining the displayStart value based on the page requested.
36       *
37       * @param view the current view
38       * @param form the form
39       * @param page the page requested (can be a number, prev, next, first, last)
40       */
41      public void processPagingRequest(View view, CollectionGroup collectionGroup, UifFormBase form, String page) {
42          // allow only one concurrent request per view
43          synchronized (view) {
44              List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(form,
45                      collectionGroup.getBindingInfo().getBindingPath());
46  
47              int displayStart = collectionGroup.getDisplayStart();
48              int displayLength = collectionGroup.getDisplayLength();
49  
50              // adjust displayStart based on the page requested
51              if (page.equals(UifConstants.PageRequest.FIRST)) {
52                  displayStart = 0;
53              } else if (page.equals(UifConstants.PageRequest.PREV)) {
54                  displayStart = displayStart - displayLength;
55              } else if (page.equals(UifConstants.PageRequest.NEXT)) {
56                  displayStart = displayStart + displayLength;
57              } else if (page.equals(UifConstants.PageRequest.LAST)) {
58                  int lastPageSize = modelCollection.size() % displayLength;
59  
60                  if (lastPageSize != 0) {
61                      displayStart = modelCollection.size() - lastPageSize;
62                  } else {
63                      displayStart = modelCollection.size() - displayLength;
64                  }
65              } else {
66                  displayStart = ((Integer.parseInt(page.trim()) - 1) * displayLength);
67              }
68  
69              collectionGroup.setDisplayStart(displayStart);
70          }
71      }
72  }