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 com.google.common.collect.Lists;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
21 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
22 import org.kuali.rice.krad.uif.component.BindingInfo;
23 import org.kuali.rice.krad.uif.component.Component;
24 import org.kuali.rice.krad.uif.component.MethodInvokerConfig;
25 import org.kuali.rice.krad.uif.field.AttributeQuery;
26 import org.kuali.rice.krad.uif.field.InputField;
27 import org.kuali.rice.krad.uif.util.ScriptUtils;
28 import org.kuali.rice.krad.uif.view.View;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @BeanTag(name = "suggest-bean", parent = "Uif-Suggest")
46 public class Suggest extends WidgetBase {
47 private static final long serialVersionUID = 7373706855319347225L;
48
49 private AttributeQuery suggestQuery;
50
51 private String valuePropertyName;
52 private String labelPropertyName;
53 private List<String> additionalPropertiesToReturn;
54
55 private boolean returnFullQueryObject;
56
57 private boolean retrieveAllSuggestions;
58 private List<Object> suggestOptions;
59
60 private String suggestOptionsJsString;
61
62 public Suggest() {
63 super();
64 }
65
66
67
68
69
70
71
72
73 public void performApplyModel(View view, Object model, Component parent) {
74 super.performApplyModel(view, model, parent);
75
76 if (suggestQuery != null) {
77 view.getViewHelperService().getExpressionEvaluator().evaluateExpressionsOnConfigurable(view,
78 suggestQuery, getContext());
79 }
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93 @Override
94 public void performFinalize(View view, Object model, Component parent) {
95 super.performFinalize(view, model, parent);
96
97
98 if (!isSuggestConfigured()) {
99 setRender(false);
100 }
101
102 if (!isRender()) {
103 return;
104 }
105
106 if (retrieveAllSuggestions) {
107 if (suggestOptions == null || suggestOptions.isEmpty()) {
108
109 if (suggestQuery.hasConfiguredMethod()) {
110 retrieveSuggestOptions(view);
111 }
112 } else {
113 suggestOptionsJsString = ScriptUtils.translateValue(suggestOptions);
114 }
115 } else {
116
117 InputField field = (InputField) parent;
118
119 BindingInfo bindingInfo = field.getBindingInfo();
120 suggestQuery.updateQueryFieldMapping(bindingInfo);
121 }
122 }
123
124
125
126
127
128
129 public boolean isSuggestConfigured() {
130 if (StringUtils.isNotBlank(valuePropertyName) ||
131 suggestQuery.hasConfiguredMethod() ||
132 (suggestOptions != null && !suggestOptions.isEmpty())) {
133 return true;
134 }
135
136 return false;
137 }
138
139
140
141
142
143
144
145 protected void retrieveSuggestOptions(View view) {
146 String queryMethodToCall = suggestQuery.getQueryMethodToCall();
147 MethodInvokerConfig queryMethodInvoker = suggestQuery.getQueryMethodInvokerConfig();
148
149 if (queryMethodInvoker == null) {
150 queryMethodInvoker = new MethodInvokerConfig();
151 }
152
153
154
155 if (StringUtils.isBlank(queryMethodInvoker.getTargetMethod())) {
156 queryMethodInvoker.setTargetMethod(queryMethodToCall);
157 }
158
159
160 if ((queryMethodInvoker.getTargetClass() == null) && (queryMethodInvoker.getTargetObject() == null)) {
161 queryMethodInvoker.setTargetObject(view.getViewHelperService());
162 }
163
164 try {
165 queryMethodInvoker.prepare();
166
167 Object methodResult = queryMethodInvoker.invoke();
168 if (methodResult instanceof String) {
169 suggestOptionsJsString = (String) methodResult;
170 } else if (methodResult instanceof List) {
171 suggestOptions = (List<Object>) methodResult;
172 suggestOptionsJsString = ScriptUtils.translateValue(suggestOptions);
173 } else {
174 throw new RuntimeException("Suggest query method did not return List<String> for suggestions");
175 }
176 } catch (Exception e) {
177 throw new RuntimeException("Unable to invoke query method: " + queryMethodInvoker.getTargetMethod(), e);
178 }
179 }
180
181
182
183
184
185
186
187 @BeanTagAttribute(name = "suggestQuery", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
188 public AttributeQuery getSuggestQuery() {
189 return suggestQuery;
190 }
191
192
193
194
195
196
197 public void setSuggestQuery(AttributeQuery suggestQuery) {
198 this.suggestQuery = suggestQuery;
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214 @BeanTagAttribute(name = "valuePropertyName")
215 public String getValuePropertyName() {
216 return valuePropertyName;
217 }
218
219
220
221
222
223
224 public void setValuePropertyName(String valuePropertyName) {
225 this.valuePropertyName = valuePropertyName;
226 }
227
228
229
230
231
232
233
234
235
236
237
238
239 @BeanTagAttribute(name = "labelPropertyName")
240 public String getLabelPropertyName() {
241 return labelPropertyName;
242 }
243
244
245
246
247
248
249 public void setLabelPropertyName(String labelPropertyName) {
250 this.labelPropertyName = labelPropertyName;
251 }
252
253
254
255
256
257
258
259
260
261
262
263 @BeanTagAttribute(name = "additionalPropertiesToReturn", type = BeanTagAttribute.AttributeType.LISTVALUE)
264 public List<String> getAdditionalPropertiesToReturn() {
265 return additionalPropertiesToReturn;
266 }
267
268
269
270
271
272
273 public void setAdditionalPropertiesToReturn(List<String> additionalPropertiesToReturn) {
274 this.additionalPropertiesToReturn = additionalPropertiesToReturn;
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294 @BeanTagAttribute(name = "returnFullQueryObject")
295 public boolean isReturnFullQueryObject() {
296 return returnFullQueryObject;
297 }
298
299
300
301
302
303
304 public void setReturnFullQueryObject(boolean returnFullQueryObject) {
305 this.returnFullQueryObject = returnFullQueryObject;
306 }
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327 @BeanTagAttribute(name = "retrieveAllSuggestions")
328 public boolean isRetrieveAllSuggestions() {
329 return retrieveAllSuggestions;
330 }
331
332
333
334
335
336
337 public void setRetrieveAllSuggestions(boolean retrieveAllSuggestions) {
338 this.retrieveAllSuggestions = retrieveAllSuggestions;
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357 @BeanTagAttribute(name = "suggestOptions", type = BeanTagAttribute.AttributeType.LISTBEAN)
358 public List<Object> getSuggestOptions() {
359 return suggestOptions;
360 }
361
362
363
364
365
366
367 public void setSuggestOptions(List<Object> suggestOptions) {
368 this.suggestOptions = suggestOptions;
369 }
370
371
372
373
374
375
376 public String getSuggestOptionsJsString() {
377 if (StringUtils.isNotBlank(suggestOptionsJsString)) {
378 return this.suggestOptionsJsString;
379 }
380
381 return "null";
382 }
383
384
385
386
387
388
389 public void setSuggestOptionsJsString(String suggestOptionsJsString) {
390 this.suggestOptionsJsString = suggestOptionsJsString;
391 }
392
393
394
395
396 @Override
397 protected <T> void copyProperties(T component) {
398 super.copyProperties(component);
399 Suggest suggestCopy = (Suggest) component;
400 suggestCopy.setValuePropertyName(this.getValuePropertyName());
401 suggestCopy.setLabelPropertyName(this.getLabelPropertyName());
402
403 if(additionalPropertiesToReturn != null) {
404 suggestCopy.setAdditionalPropertiesToReturn(new ArrayList<String> (additionalPropertiesToReturn));
405 }
406
407 suggestCopy.setReturnFullQueryObject(this.isReturnFullQueryObject());
408 suggestCopy.setRetrieveAllSuggestions(this.isRetrieveAllSuggestions());
409
410 if (this.suggestQuery != null) {
411 suggestCopy.setSuggestQuery((AttributeQuery)this.suggestQuery.copy());
412 }
413
414 suggestCopy.setSuggestOptions(this.getSuggestOptions());
415 suggestCopy.setSuggestOptionsJsString(this.suggestOptionsJsString);
416 }
417 }