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.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
31
32
33
34 public class InvokeFinalizerTask extends ViewLifecycleTaskBase<Component> {
35
36 private final Logger LOG = LoggerFactory.getLogger(InvokeFinalizerTask.class);
37
38
39
40
41 public InvokeFinalizerTask() {
42 super(Component.class);
43 }
44
45
46
47
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
64
65 if (StringUtils.isBlank(finalizeMethodInvoker.getTargetMethod())) {
66 finalizeMethodInvoker.setTargetMethod(finalizeMethodToCall);
67 }
68
69
70 if ((finalizeMethodInvoker.getTargetClass() == null) && (finalizeMethodInvoker.getTargetObject() == null)) {
71 finalizeMethodInvoker.setTargetObject(ViewLifecycle.getHelper());
72 }
73
74
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
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 }