1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.widget;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21 import org.kuali.rice.krad.uif.component.BindingInfo;
22 import org.kuali.rice.krad.uif.component.Component;
23 import org.kuali.rice.krad.uif.component.MethodInvokerConfig;
24 import org.kuali.rice.krad.uif.field.AttributeQuery;
25 import org.kuali.rice.krad.uif.field.InputField;
26 import org.kuali.rice.krad.uif.util.ScriptUtils;
27 import org.kuali.rice.krad.uif.view.View;
28
29 import java.util.List;
30
31
32
33
34
35
36
37
38
39
40
41
42
43 @BeanTag(name = "suggest")
44 public class Suggest extends WidgetBase {
45 private static final long serialVersionUID = 7373706855319347225L;
46
47 private AttributeQuery suggestQuery;
48
49 private String sourcePropertyName;
50 private boolean sourceQueryMethodResults;
51
52 private boolean retrieveAllSuggestions;
53 private List<Object> suggestOptions;
54
55 private String suggestOptionsJsString;
56
57 public Suggest() {
58 super();
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72 @Override
73 public void performFinalize(View view, Object model, Component parent) {
74 super.performFinalize(view, model, parent);
75
76
77 if (StringUtils.isBlank(sourcePropertyName) &&
78 !suggestQuery.hasConfiguredMethod() &&
79 (suggestOptions == null || suggestOptions.isEmpty())) {
80 setRender(false);
81 }
82
83 if (!isRender()) {
84 return;
85 }
86
87 if (retrieveAllSuggestions) {
88 if (suggestOptions == null || suggestOptions.isEmpty()) {
89
90 if (suggestQuery.hasConfiguredMethod()) {
91 retrieveSuggestOptions(view);
92 }
93 } else {
94 suggestOptionsJsString = ScriptUtils.translateValue(suggestOptions);
95 }
96 } else {
97
98 InputField field = (InputField) parent;
99
100 BindingInfo bindingInfo = field.getBindingInfo();
101 suggestQuery.updateQueryFieldMapping(bindingInfo);
102 }
103 }
104
105
106
107
108
109
110
111 protected void retrieveSuggestOptions(View view) {
112 String queryMethodToCall = suggestQuery.getQueryMethodToCall();
113 MethodInvokerConfig queryMethodInvoker = suggestQuery.getQueryMethodInvokerConfig();
114
115 if (queryMethodInvoker == null) {
116 queryMethodInvoker = new MethodInvokerConfig();
117 }
118
119
120
121 if (StringUtils.isBlank(queryMethodInvoker.getTargetMethod())) {
122 queryMethodInvoker.setTargetMethod(queryMethodToCall);
123 }
124
125
126 if ((queryMethodInvoker.getTargetClass() == null) && (queryMethodInvoker.getTargetObject() == null)) {
127 queryMethodInvoker.setTargetObject(view.getViewHelperService());
128 }
129
130 try {
131 queryMethodInvoker.prepare();
132
133 Object methodResult = queryMethodInvoker.invoke();
134 if (methodResult instanceof String) {
135 suggestOptionsJsString = (String) methodResult;
136 } else if (methodResult instanceof List) {
137 suggestOptions = (List<Object>) methodResult;
138 suggestOptionsJsString = ScriptUtils.translateValue(suggestOptions);
139 } else {
140 throw new RuntimeException("Suggest query method did not return List<String> for suggestions");
141 }
142 } catch (Exception e) {
143 throw new RuntimeException("Unable to invoke query method: " + queryMethodInvoker.getTargetMethod(), e);
144 }
145 }
146
147
148
149
150
151
152
153 @BeanTagAttribute(name = "suggestQuery", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
154 public AttributeQuery getSuggestQuery() {
155 return suggestQuery;
156 }
157
158
159
160
161
162
163 public void setSuggestQuery(AttributeQuery suggestQuery) {
164 this.suggestQuery = suggestQuery;
165 }
166
167
168
169
170
171
172
173
174 @BeanTagAttribute(name = "sourcePropertyName")
175 public String getSourcePropertyName() {
176 return sourcePropertyName;
177 }
178
179
180
181
182
183
184 public void setSourcePropertyName(String sourcePropertyName) {
185 this.sourcePropertyName = sourcePropertyName;
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 @BeanTagAttribute(name = "sourceQueryMethodResults")
203 public boolean isSourceQueryMethodResults() {
204 return sourceQueryMethodResults;
205 }
206
207
208
209
210
211
212 public void setSourceQueryMethodResults(boolean sourceQueryMethodResults) {
213 this.sourceQueryMethodResults = sourceQueryMethodResults;
214 }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235 @BeanTagAttribute(name = "retrieveAllSuggestions")
236 public boolean isRetrieveAllSuggestions() {
237 return retrieveAllSuggestions;
238 }
239
240
241
242
243
244
245 public void setRetrieveAllSuggestions(boolean retrieveAllSuggestions) {
246 this.retrieveAllSuggestions = retrieveAllSuggestions;
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 @BeanTagAttribute(name = "suggestOptions", type = BeanTagAttribute.AttributeType.LISTBEAN)
266 public List<Object> getSuggestOptions() {
267 return suggestOptions;
268 }
269
270
271
272
273
274
275 public void setSuggestOptions(List<Object> suggestOptions) {
276 this.suggestOptions = suggestOptions;
277 }
278
279
280
281
282
283
284 public String getSuggestOptionsJsString() {
285 if (StringUtils.isNotBlank(suggestOptionsJsString)) {
286 return this.suggestOptionsJsString;
287 }
288
289 return "null";
290 }
291 }