Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GeneratedField |
|
| 1.8571428571428572;1.857 |
1 | /* | |
2 | * Copyright 2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 1.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/ecl1.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.kns.uif.field; | |
17 | ||
18 | import org.apache.commons.lang.StringUtils; | |
19 | import org.kuali.rice.kns.uif.container.View; | |
20 | import org.kuali.rice.kns.uif.core.Component; | |
21 | import org.springframework.util.MethodInvoker; | |
22 | ||
23 | /** | |
24 | * Field instance whose output is produced by invoking a method | |
25 | * | |
26 | * <p> | |
27 | * Generated fields can be used to produce any kind of HTML output using code. | |
28 | * The properties configured for the <code>GeneratedField</code> configure the | |
29 | * class and method that should be invoked to render the component. The method | |
30 | * that will be invoked should take the <code>GeneratedField</code> instance as | |
31 | * a parameter and return a String (which can include state HTML) | |
32 | * </p> | |
33 | * | |
34 | * <p> | |
35 | * If the renderingMethodToCall is set, it is assumed to be a method on the | |
36 | * configured <code>ViewHelperService</code>. For invoking other class methods | |
37 | * the renderMethodInvoker must be configured | |
38 | * </p> | |
39 | * | |
40 | * <p> | |
41 | * e.g. public String sayHiInBold(GeneratedField field) { return "<b>HI!</b>"; } | |
42 | * </p> | |
43 | * | |
44 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
45 | * @deprecated | |
46 | */ | |
47 | public class GeneratedField extends FieldBase { | |
48 | private static final long serialVersionUID = 1575182633700024203L; | |
49 | ||
50 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GeneratedField.class); |
51 | ||
52 | private String renderingMethodToCall; | |
53 | private MethodInvoker renderingMethodInvoker; | |
54 | ||
55 | private String renderOutput; | |
56 | ||
57 | public GeneratedField() { | |
58 | 0 | super(); |
59 | 0 | } |
60 | ||
61 | /** | |
62 | * The following finalization is done here: | |
63 | * | |
64 | * <ul> | |
65 | * <li>Setup the method invoker and invoke method to get the render output</li> | |
66 | * </ul> | |
67 | * | |
68 | * @see org.kuali.rice.kns.uif.core.ComponentBase#performFinalize(org.kuali.rice.kns.uif.container.View, | |
69 | * java.lang.Object, org.kuali.rice.kns.uif.core.Component) | |
70 | */ | |
71 | @Override | |
72 | public void performFinalize(View view, Object model, Component parent) { | |
73 | 0 | super.performFinalize(view, model, parent); |
74 | ||
75 | 0 | if (renderingMethodInvoker == null) { |
76 | 0 | renderingMethodInvoker = new MethodInvoker(); |
77 | } | |
78 | ||
79 | // if method not set on invoker, use renderingMethodToCall | |
80 | 0 | if (StringUtils.isBlank(renderingMethodInvoker.getTargetMethod())) { |
81 | 0 | renderingMethodInvoker.setTargetMethod(renderingMethodToCall); |
82 | } | |
83 | ||
84 | // if target class or object not set, use view helper service | |
85 | 0 | if ((renderingMethodInvoker.getTargetClass() == null) && (renderingMethodInvoker.getTargetObject() == null)) { |
86 | 0 | renderingMethodInvoker.setTargetObject(view.getViewHelperService()); |
87 | } | |
88 | ||
89 | // add the component instance as an argument | |
90 | 0 | Object[] arguments = new Object[1]; |
91 | 0 | arguments[0] = this; |
92 | 0 | renderingMethodInvoker.setArguments(arguments); |
93 | ||
94 | // invoke method and get render output | |
95 | try { | |
96 | 0 | LOG.debug("Invoking render method: " + renderingMethodInvoker.getTargetMethod() + " for component: " |
97 | + getId()); | |
98 | 0 | renderingMethodInvoker.prepare(); |
99 | ||
100 | 0 | renderOutput = (String) renderingMethodInvoker.invoke(); |
101 | } | |
102 | 0 | catch (Exception e) { |
103 | 0 | LOG.error("Error invoking rendering method for component: " + getId(), e); |
104 | 0 | throw new RuntimeException("Error invoking rendering method for component: " + getId(), e); |
105 | 0 | } |
106 | 0 | } |
107 | ||
108 | /** | |
109 | * Name of the method that should be invoked for rendering the component | |
110 | * (full method name, without parameters or return type) | |
111 | * | |
112 | * <p> | |
113 | * Note the method can also be set with the renderingMethodInvoker | |
114 | * targetMethod property. If the method is on the configured | |
115 | * <code>ViewHelperService</code>, only this property needs to be configured | |
116 | * </p> | |
117 | * | |
118 | * @return String method name | |
119 | */ | |
120 | public String getRenderingMethodToCall() { | |
121 | 0 | return this.renderingMethodToCall; |
122 | } | |
123 | ||
124 | /** | |
125 | * Setter for the rendering method | |
126 | * | |
127 | * @param renderingMethodToCall | |
128 | */ | |
129 | public void setRenderingMethodToCall(String renderingMethodToCall) { | |
130 | 0 | this.renderingMethodToCall = renderingMethodToCall; |
131 | 0 | } |
132 | ||
133 | /** | |
134 | * <code>MethodInvoker</code> instance for the method that should be invoked | |
135 | * for rendering the component | |
136 | * | |
137 | * <p> | |
138 | * MethodInvoker can be configured to specify the class or object the method | |
139 | * should be called on. For static method invocations, the targetClass | |
140 | * property can be configured. For object invocations, that targetObject | |
141 | * property can be configured | |
142 | * </p> | |
143 | * | |
144 | * @return MethodInvoker instance | |
145 | */ | |
146 | public MethodInvoker getRenderingMethodInvoker() { | |
147 | 0 | return this.renderingMethodInvoker; |
148 | } | |
149 | ||
150 | /** | |
151 | * Setter for the method invoker instance | |
152 | * | |
153 | * @param renderingMethodInvoker | |
154 | */ | |
155 | public void setRenderingMethodInvoker(MethodInvoker renderingMethodInvoker) { | |
156 | 0 | this.renderingMethodInvoker = renderingMethodInvoker; |
157 | 0 | } |
158 | ||
159 | /** | |
160 | * Rendering output for the component that will be sent as part of the | |
161 | * response (can contain static text and HTML) | |
162 | * | |
163 | * @return String render output | |
164 | */ | |
165 | public String getRenderOutput() { | |
166 | 0 | return this.renderOutput; |
167 | } | |
168 | ||
169 | } |