1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35 public class InvokeFinalizerTask extends ViewLifecycleTaskBase<Component> {
36
37 private final Logger LOG = LoggerFactory.getLogger(InvokeFinalizerTask.class);
38
39
40
41
42
43
44 public InvokeFinalizerTask(ViewLifecyclePhase phase) {
45 super(phase, Component.class);
46 }
47
48
49
50
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
67
68 if (StringUtils.isBlank(finalizeMethodInvoker.getTargetMethod())) {
69 finalizeMethodInvoker.setTargetMethod(finalizeMethodToCall);
70 }
71
72
73 if ((finalizeMethodInvoker.getTargetClass() == null) && (finalizeMethodInvoker.getTargetObject() == null)) {
74 finalizeMethodInvoker.setTargetObject(ViewLifecycle.getHelper());
75 }
76
77
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
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 }