View Javadoc
1   /**
2    * Copyright 2005-2015 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;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
20  import org.kuali.rice.krad.uif.CssConstants;
21  import org.kuali.rice.krad.uif.UifConstants;
22  import org.kuali.rice.krad.uif.UifPropertyPaths;
23  import org.kuali.rice.krad.uif.component.DataBinding;
24  import org.kuali.rice.krad.uif.container.CollectionGroup;
25  import org.kuali.rice.krad.uif.control.Control;
26  import org.kuali.rice.krad.uif.control.ValueConfiguredControl;
27  import org.kuali.rice.krad.uif.field.InputField;
28  import org.kuali.rice.krad.uif.field.Field;
29  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
30  import org.kuali.rice.krad.uif.element.Pager;
31  import org.kuali.rice.krad.util.KRADUtils;
32  
33  import java.util.List;
34  
35  /**
36   * Utilities for collection layout managers
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class CollectionLayoutUtils {
41  
42      public static void prepareSelectFieldForLine(Field selectField, CollectionGroup collectionGroup, String lineBindingPath,
43              Object line) {
44          // if select property name set use as property name for select field
45          String selectPropertyName = collectionGroup.getLineSelectPropertyName();
46          if (StringUtils.isNotBlank(selectPropertyName)) {
47              // if select property contains form prefix, will bind to form and not each line
48              if (selectPropertyName.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) {
49                  selectPropertyName = StringUtils.removeStart(selectPropertyName, UifConstants.NO_BIND_ADJUST_PREFIX);
50                  ((DataBinding) selectField).getBindingInfo().setBindingName(selectPropertyName);
51                  ((DataBinding) selectField).getBindingInfo().setBindToForm(true);
52              } else {
53                  ((DataBinding) selectField).getBindingInfo().setBindingName(selectPropertyName);
54                  ((DataBinding) selectField).getBindingInfo().setBindByNamePrefix(lineBindingPath);
55              }
56          } else {
57              // select property name not given, use UifFormBase#selectedCollectionLines
58              String collectionLineKey = KRADUtils.translateToMapSafeKey(
59                      collectionGroup.getBindingInfo().getBindingPath());
60              String selectBindingPath = UifPropertyPaths.SELECTED_COLLECTION_LINES + "['" + collectionLineKey + "']";
61  
62              ((DataBinding) selectField).getBindingInfo().setBindingName(selectBindingPath);
63              ((DataBinding) selectField).getBindingInfo().setBindToForm(true);
64          }
65  
66          setControlValueToLineIdentifier(selectField, line, lineBindingPath);
67      }
68  
69      protected static void setControlValueToLineIdentifier(Field selectField, Object line, String lineBindingPath) {
70          if (selectField instanceof InputField) {
71              Control selectControl = ((InputField) selectField).getControl();
72  
73              if (selectControl != null) {
74                  selectControl.addStyleClass(CssConstants.Classes.SELECT_FIELD_STYLE_CLASS);
75  
76                  if (selectControl instanceof ValueConfiguredControl) {
77                      String lineIdentifier
78                              = KRADServiceLocatorWeb.getLegacyDataAdapter().getDataObjectIdentifierString(line);
79  
80                      if (StringUtils.isBlank(lineIdentifier)) {
81                          lineIdentifier = lineBindingPath;
82                      }
83  
84                      ((ValueConfiguredControl) selectControl).setValue(lineIdentifier);
85                  }
86              }
87          }
88      }
89  
90      /**
91       * Setup a pagerWidget's values for numberOfPages and currentPage, based on the collection size, displayStart,
92       * and displayLength
93       *
94       * @param pagerWidget the Pager to setup
95       * @param collectionGroup  the collectionGroup which uses this Pager
96       * @param model the current model
97       */
98      protected static void setupPagerWidget(Pager pagerWidget, CollectionGroup collectionGroup, Object model){
99          List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model,
100                         collectionGroup.getBindingInfo().getBindingPath());
101 
102         // The size of the collection divided by the pageLength is used to determine the number of pages for
103         // the pager component (ceiling is used if there is not a full page left over in the division)
104         if (modelCollection != null) {
105             double pages = (double) modelCollection.size() / (double) collectionGroup.getDisplayLength();
106             pagerWidget.setNumberOfPages((int) Math.ceil(pages));
107         } else {
108             pagerWidget.setNumberOfPages(1);
109         }
110 
111         // By using displayStart, currentPage can be determined, the displayLength is added here before division,
112         // because the pager is 1-based
113         int currentPage = (collectionGroup.getDisplayStart() + collectionGroup.getDisplayLength()) / collectionGroup
114                 .getDisplayLength();
115         pagerWidget.setCurrentPage(currentPage);
116 
117         if (StringUtils.isBlank(pagerWidget.getLinkScript())){
118             pagerWidget.setLinkScript("retrieveCollectionPage(this, '" + collectionGroup.getId() + "');");
119         }
120     }
121 }