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