View Javadoc
1   /**
2    * Copyright 2005-2014 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.lifecycle.finalize;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.krad.uif.component.Component;
23  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
24  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.springframework.util.MethodInvoker;
28  
29  /**
30   * Invoke finalizer methods on the component.
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public class InvokeFinalizerTask extends ViewLifecycleTaskBase<Component> {
35      
36      private final Logger LOG = LoggerFactory.getLogger(InvokeFinalizerTask.class);
37  
38      /**
39       * Default constructor.
40       */
41      public InvokeFinalizerTask() {
42          super(Component.class);
43      }
44  
45      /**
46       * Invokes the finalize method for the component (if configured) and sets the render output for
47       * the component to the returned method string (if method is not a void type)
48       */
49      @Override
50      protected void performLifecycleTask() {
51          Component component = (Component) getElementState().getElement();
52          String finalizeMethodToCall = component.getFinalizeMethodToCall();
53          MethodInvoker finalizeMethodInvoker = component.getFinalizeMethodInvoker();
54  
55          if (StringUtils.isBlank(finalizeMethodToCall) && (finalizeMethodInvoker == null)) {
56              return;
57          }
58  
59          if (finalizeMethodInvoker == null) {
60              finalizeMethodInvoker = new MethodInvoker();
61          }
62  
63          // if method not set on invoker, use finalizeMethodToCall, note staticMethod could be set(don't know since
64          // there is not a getter), if so it will override the target method in prepare
65          if (StringUtils.isBlank(finalizeMethodInvoker.getTargetMethod())) {
66              finalizeMethodInvoker.setTargetMethod(finalizeMethodToCall);
67          }
68  
69          // if target class or object not set, use view helper service
70          if ((finalizeMethodInvoker.getTargetClass() == null) && (finalizeMethodInvoker.getTargetObject() == null)) {
71              finalizeMethodInvoker.setTargetObject(ViewLifecycle.getHelper());
72          }
73  
74          // setup arguments for method
75          List<Object> additionalArguments = component.getFinalizeMethodAdditionalArguments();
76          if (additionalArguments == null) {
77              additionalArguments = new ArrayList<Object>();
78          }
79  
80          Object[] arguments = new Object[2 + additionalArguments.size()];
81          arguments[0] = component;
82          arguments[1] = ViewLifecycle.getModel();
83  
84          int argumentIndex = 1;
85          for (Object argument : additionalArguments) {
86              argumentIndex++;
87              arguments[argumentIndex] = argument;
88          }
89          finalizeMethodInvoker.setArguments(arguments);
90  
91          // invoke finalize method
92          try {
93              LOG.debug("Invoking finalize method: "
94                      + finalizeMethodInvoker.getTargetMethod()
95                      + " for component: "
96                      + component.getId());
97              finalizeMethodInvoker.prepare();
98  
99              Class<?> methodReturnType = finalizeMethodInvoker.getPreparedMethod().getReturnType();
100             if (StringUtils.equals("void", methodReturnType.getName())) {
101                 finalizeMethodInvoker.invoke();
102             } else {
103                 String renderOutput = (String) finalizeMethodInvoker.invoke();
104 
105                 component.setSelfRendered(true);
106                 component.setRenderedHtmlOutput(renderOutput);
107             }
108         } catch (Exception e) {
109             LOG.error("Error invoking finalize method for component: " + component.getId(), e);
110             throw new RuntimeException("Error invoking finalize method for component: " + component.getId(), e);
111         }
112     }
113 
114 }