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.field;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.uif.UifConstants;
20  import org.kuali.rice.krad.uif.component.Component;
21  import org.kuali.rice.krad.uif.view.View;
22  
23  /**
24   * Action field that performs an Ajax request and will result in updating of the page or a component
25   *
26   * @author Kuali Rice Team (rice.collab@kuali.org)
27   */
28  public class AjaxActionField extends ActionField {
29      private static final long serialVersionUID = -2831173647391138870L;
30  
31      private String refreshId;
32      private String refreshPropertyName;
33  
34      public AjaxActionField() {
35          super();
36      }
37  
38      /**
39       * The following finalization is performed:
40       *
41       * <ul>
42       * <li>Add methodToCall action parameter if set and setup event code for
43       * setting action parameters</li>
44       * </ul>
45       *
46       * @see org.kuali.rice.krad.uif.component.ComponentBase#performFinalize(org.kuali.rice.krad.uif.view.View,
47       *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
48       */
49      @Override
50      public void performFinalize(View view, Object model, Component parent) {
51          Component refreshComponent = null;
52  
53          // if refresh property name is given, adjust the binding and then attempt to find the
54          // component in the view index
55          if (StringUtils.isNotBlank(refreshPropertyName)) {
56              // TODO: does this support all binding prefixes?
57              if (refreshPropertyName.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) {
58                  refreshPropertyName = StringUtils.removeStart(refreshPropertyName, UifConstants.NO_BIND_ADJUST_PREFIX);
59              } else if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
60                  refreshPropertyName = view.getDefaultBindingObjectPath() + "." + refreshPropertyName;
61              }
62  
63              DataField dataField = view.getViewIndex().getDataFieldByPath(refreshPropertyName);
64              if (dataField != null) {
65                 refreshComponent = dataField;
66              }
67          }
68          else if (StringUtils.isNotBlank(refreshId)) {
69              Component component = view.getViewIndex().getComponentById(refreshId);
70              if (component != null) {
71                  refreshComponent = component;
72              }
73          }
74  
75          String actionScript = "";
76          if (refreshComponent != null) {
77              refreshComponent.setRefreshedByAction(true);
78              // update initial state
79              Component initialComponent = view.getViewIndex().getInitialComponentStates().get(
80                      refreshComponent.getFactoryId());
81              if (initialComponent != null) {
82                  initialComponent.setRefreshedByAction(true);
83                  view.getViewIndex().getInitialComponentStates().put(refreshComponent.getFactoryId(), initialComponent);
84              }
85  
86              // refresh component for action
87              actionScript = "retrieveComponent('" + refreshComponent.getId() + "','" + refreshComponent.getFactoryId()
88                      + "','" + getMethodToCall() + "');";
89          }
90          else {
91              // refresh page
92              actionScript = "submitForm();";
93          }
94  
95          // add action script to client JS
96          if (StringUtils.isNotBlank(getClientSideJs())) {
97              actionScript = getClientSideJs() + actionScript;
98          }
99          setClientSideJs(actionScript);
100 
101         super.performFinalize(view, model, parent);
102     }
103 
104     /**
105      * Id for the component that should be refreshed after the action completes
106      *
107      * <p>
108      * Either refresh id or refresh property name can be set to configure the component that should
109      * be refreshed after the action completes. If both are blank, the page will be refreshed
110      * </p>
111      *
112      * @return String valid component id
113      */
114     public String getRefreshId() {
115         return refreshId;
116     }
117 
118     /**
119      * Setter for the component refresh id
120      *
121      * @param refreshId
122      */
123     public void setRefreshId(String refreshId) {
124         this.refreshId = refreshId;
125     }
126 
127     /**
128      * Property name for the {@link DataField} that should be refreshed after the action completes
129      *
130      * <p>
131      * Either refresh id or refresh property name can be set to configure the component that should
132      * be refreshed after the action completes. If both are blank, the page will be refreshed
133      * </p>
134      *
135      * <p>
136      * Property name will be adjusted to use the default binding path unless it contains the form prefix
137      *
138      * @return String valid property name with an associated DataField
139      * @see org.kuali.rice.krad.uif.UifConstants#NO_BIND_ADJUST_PREFIX
140      *      </p>
141      */
142     public String getRefreshPropertyName() {
143         return refreshPropertyName;
144     }
145 
146     /**
147      * Setter for the property name of the DataField that should be refreshed
148      *
149      * @param refreshPropertyName
150      */
151     public void setRefreshPropertyName(String refreshPropertyName) {
152         this.refreshPropertyName = refreshPropertyName;
153     }
154 }