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