View Javadoc

1   /**
2    * Copyright 2005-2011 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              if (refreshPropertyName.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) {
57                  refreshPropertyName = StringUtils.removeStart(refreshPropertyName, UifConstants.NO_BIND_ADJUST_PREFIX);
58              } else if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
59                  refreshPropertyName = view.getDefaultBindingObjectPath() + "." + refreshPropertyName;
60              }
61  
62              DataField dataField = view.getViewIndex().getDataFieldByPath(refreshPropertyName);
63              if (dataField != null) {
64                 refreshComponent = dataField;
65              }
66          }
67          else if (StringUtils.isNotBlank(refreshId)) {
68              Component component = view.getViewIndex().getComponentById(refreshId);
69              if (component != null) {
70                  refreshComponent = component;
71              }
72          }
73  
74          String actionScript = "";
75          if (refreshComponent != null) {
76              refreshComponent.setRefreshedByAction(true);
77              // update initial state
78              Component initialComponent = view.getViewIndex().getInitialComponentStates().get(
79                      refreshComponent.getFactoryId());
80              if (initialComponent != null) {
81                  initialComponent.setRefreshedByAction(true);
82                  view.getViewIndex().getInitialComponentStates().put(refreshComponent.getFactoryId(), initialComponent);
83              }
84  
85              // refresh component for action
86              actionScript = "retrieveComponent('" + refreshComponent.getId() + "','" + refreshComponent.getFactoryId()
87                      + "','" + getMethodToCall() + "');";
88          }
89          else {
90              // refresh page
91              actionScript = "submitForm();";
92          }
93  
94          // add action script to client JS
95          if (StringUtils.isNotBlank(getClientSideJs())) {
96              actionScript = getClientSideJs() + actionScript;
97          }
98          setClientSideJs(actionScript);
99  
100         super.performFinalize(view, model, parent);
101     }
102 
103     /**
104      * Id for the component that should be refreshed after the action completes
105      *
106      * <p>
107      * Either refresh id or refresh property name can be set to configure the component that should
108      * be refreshed after the action completes. If both are blank, the page will be refreshed
109      * </p>
110      *
111      * @return String valid component id
112      */
113     public String getRefreshId() {
114         return refreshId;
115     }
116 
117     /**
118      * Setter for the component refresh id
119      *
120      * @param refreshId
121      */
122     public void setRefreshId(String refreshId) {
123         this.refreshId = refreshId;
124     }
125 
126     /**
127      * Property name for the {@link DataField} that should be refreshed after the action completes
128      *
129      * <p>
130      * Either refresh id or refresh property name can be set to configure the component that should
131      * be refreshed after the action completes. If both are blank, the page will be refreshed
132      * </p>
133      *
134      * <p>
135      * Property name will be adjusted to use the default binding path unless it contains the form prefix
136      *
137      * @return String valid property name with an associated DataField
138      * @see org.kuali.rice.krad.uif.UifConstants#NO_BIND_ADJUST_PREFIX
139      *      </p>
140      */
141     public String getRefreshPropertyName() {
142         return refreshPropertyName;
143     }
144 
145     /**
146      * Setter for the property name of the DataField that should be refreshed
147      *
148      * @param refreshPropertyName
149      */
150     public void setRefreshPropertyName(String refreshPropertyName) {
151         this.refreshPropertyName = refreshPropertyName;
152     }
153 }