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.Maps;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.CoreApiServiceLocator;
21 import org.kuali.rice.core.web.format.Formatter;
22 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
23 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
24 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
25 import org.kuali.rice.krad.service.ModuleService;
26 import org.kuali.rice.krad.uif.UifConstants;
27 import org.kuali.rice.krad.uif.UifParameters;
28 import org.kuali.rice.krad.uif.component.BindingInfo;
29 import org.kuali.rice.krad.uif.component.Component;
30 import org.kuali.rice.krad.uif.element.Action;
31 import org.kuali.rice.krad.uif.element.Link;
32 import org.kuali.rice.krad.uif.field.DataField;
33 import org.kuali.rice.krad.uif.field.InputField;
34 import org.kuali.rice.krad.uif.util.LookupInquiryUtils;
35 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
36 import org.kuali.rice.krad.uif.util.ViewModelUtils;
37 import org.kuali.rice.krad.uif.view.View;
38 import org.kuali.rice.krad.util.UrlFactory;
39
40 import java.security.GeneralSecurityException;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Map.Entry;
45 import java.util.Properties;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 @BeanTag(name = "inquiry-bean", parent = "Uif-Inquiry")
61 public class Inquiry extends WidgetBase {
62 private static final long serialVersionUID = -2154388007867302901L;
63 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(Inquiry.class);
64
65 public static final String INQUIRY_TITLE_PREFIX = "title.inquiry.url.value.prependtext";
66
67 private String baseInquiryUrl;
68
69 private String dataObjectClassName;
70 private String viewName;
71
72 private Map<String, String> inquiryParameters;
73
74 private Link inquiryLink;
75
76 private Action directInquiryAction;
77 private boolean enableDirectInquiry;
78
79 private boolean adjustInquiryParameters;
80 private BindingInfo fieldBindingInfo;
81
82 private boolean parentReadOnly;
83
84 public Inquiry() {
85 super();
86
87 inquiryParameters = new HashMap<String, String>();
88 }
89
90
91
92
93
94 @Override
95 public void performFinalize(View view, Object model, Component parent) {
96 super.performFinalize(view, model, parent);
97
98 if (!isRender()) {
99 return;
100 }
101
102
103 setRender(false);
104
105
106 setParentReadOnly(parent.isReadOnly());
107
108
109 if (isParentReadOnly()) {
110 if (StringUtils.isBlank(((DataField) parent).getBindingInfo().getBindingPath()) || ((DataField) parent)
111 .getBindingInfo().getBindingPath().equals("null")) {
112 return;
113 }
114
115
116 try {
117 Object propertyValue = ObjectPropertyUtils.getPropertyValue(model,
118 ((DataField) parent).getBindingInfo().getBindingPath());
119
120 if ((propertyValue == null) || StringUtils.isBlank(propertyValue.toString())) {
121 return;
122 }
123 } catch (Exception e) {
124
125 return;
126 }
127 }
128
129
130 if (!isParentReadOnly() && parent instanceof InputField) {
131 if (!enableDirectInquiry) {
132 return;
133 }
134
135
136 if (StringUtils.isBlank(getDataObjectClassName())
137 || (getInquiryParameters() == null)
138 || getInquiryParameters().isEmpty()) {
139
140 adjustInquiryParameters = true;
141 fieldBindingInfo = ((InputField) parent).getBindingInfo();
142 }
143 }
144
145 setupLink(view, model, (DataField) parent);
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160 public void setupLink(View view, Object model, DataField field) {
161 String propertyName = field.getBindingInfo().getBindingName();
162
163
164 if (StringUtils.isNotBlank(getDataObjectClassName()) && (getInquiryParameters() != null) &&
165 !getInquiryParameters().isEmpty()) {
166 Class<?> inquiryObjectClass;
167 try {
168 inquiryObjectClass = Class.forName(getDataObjectClassName());
169 } catch (ClassNotFoundException e) {
170 LOG.error("Unable to get class for: " + getDataObjectClassName());
171 throw new RuntimeException(e);
172 }
173
174 updateInquiryParameters(field.getBindingInfo());
175
176 buildInquiryLink(model, propertyName, inquiryObjectClass, getInquiryParameters());
177 }
178
179 else {
180
181 Object parentObject = ViewModelUtils.getParentObjectForMetadata(view, model, field);
182 view.getViewHelperService().buildInquiryLink(parentObject, propertyName, this);
183 }
184 }
185
186
187
188
189
190
191
192 public void updateInquiryParameters(BindingInfo bindingInfo) {
193 Map<String, String> adjustedInquiryParameters = new HashMap<String, String>();
194 for (Entry<String, String> stringEntry : inquiryParameters.entrySet()) {
195 String toField = stringEntry.getValue();
196 String adjustedFromFieldPath = bindingInfo.getPropertyAdjustedBindingPath(stringEntry.getKey());
197
198 adjustedInquiryParameters.put(adjustedFromFieldPath, toField);
199 }
200
201 this.inquiryParameters = adjustedInquiryParameters;
202 }
203
204
205
206
207
208
209
210
211
212
213 public void buildInquiryLink(Object dataObject, String propertyName, Class<?> inquiryObjectClass,
214 Map<String, String> inquiryParams) {
215
216 Properties urlParameters = new Properties();
217
218 urlParameters.setProperty(UifParameters.DATA_OBJECT_CLASS_NAME, inquiryObjectClass.getName());
219 urlParameters.setProperty(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.START);
220 if (StringUtils.isNotBlank(this.viewName)) {
221 urlParameters.setProperty(UifParameters.VIEW_NAME, this.viewName);
222 }
223
224
225 if (getInquiryLink().getLightBox() != null) {
226 getInquiryLink().getLightBox().setAddAppParms(true);
227 }
228
229
230 if (isParentReadOnly()) {
231 for (Entry<String, String> inquiryParameter : inquiryParams.entrySet()) {
232 String parameterName = inquiryParameter.getKey();
233
234 Object parameterValue = ObjectPropertyUtils.getPropertyValue(dataObject, parameterName);
235
236
237 if (parameterValue == null) {
238 parameterValue = "";
239 } else if (parameterValue instanceof java.sql.Date) {
240 if (Formatter.findFormatter(parameterValue.getClass()) != null) {
241 Formatter formatter = Formatter.getFormatter(parameterValue.getClass());
242 parameterValue = formatter.format(parameterValue);
243 }
244 } else {
245 parameterValue = parameterValue.toString();
246 }
247
248
249
250 if (KRADServiceLocatorWeb.getDataObjectAuthorizationService()
251 .attributeValueNeedsToBeEncryptedOnFormsAndLinks(inquiryObjectClass,
252 inquiryParameter.getValue())) {
253 try {
254 parameterValue = CoreApiServiceLocator.getEncryptionService().encrypt(parameterValue);
255 } catch (GeneralSecurityException e) {
256 throw new RuntimeException("Exception while trying to encrypted value for inquiry framework.",
257 e);
258 }
259 }
260
261
262 urlParameters.put(inquiryParameter.getValue(), parameterValue);
263 }
264
265
266 String inquiryUrl;
267
268
269 ModuleService responsibleModuleService =
270 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(inquiryObjectClass);
271 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(inquiryObjectClass)) {
272 inquiryUrl = responsibleModuleService.getExternalizableDataObjectInquiryUrl(inquiryObjectClass,
273 urlParameters);
274 } else {
275 inquiryUrl = UrlFactory.parameterizeUrl(getBaseInquiryUrl(), urlParameters);
276 }
277
278 getInquiryLink().setHref(inquiryUrl);
279
280
281 String linkTitle = createTitleText(inquiryObjectClass);
282 linkTitle = LookupInquiryUtils.getLinkTitleText(linkTitle, inquiryObjectClass, getInquiryParameters());
283 getInquiryLink().setTitle(linkTitle);
284
285 setRender(true);
286 }
287
288 else {
289
290 String inquiryUrl = UrlFactory.parameterizeUrl(getBaseInquiryUrl(), urlParameters);
291
292 StringBuilder paramMapString = new StringBuilder();
293
294
295 for (Entry<String, String> inquiryParameter : inquiryParams.entrySet()) {
296 String inquiryParameterFrom = inquiryParameter.getKey();
297
298 if (adjustInquiryParameters && (fieldBindingInfo != null)) {
299 inquiryParameterFrom = fieldBindingInfo.getPropertyAdjustedBindingPath(inquiryParameterFrom);
300 }
301
302 paramMapString.append(inquiryParameterFrom);
303 paramMapString.append(":");
304 paramMapString.append(inquiryParameter.getValue());
305 paramMapString.append(",");
306 }
307 paramMapString.deleteCharAt(paramMapString.length() - 1);
308
309
310 String lightBoxOptions = "";
311 boolean lightBoxShow = (getInquiryLink().getLightBox() != null);
312 if (lightBoxShow) {
313 lightBoxOptions = getInquiryLink().getLightBox().getTemplateOptionsJSString();
314 }
315
316
317
318 StringBuilder onClickScript = new StringBuilder("showDirectInquiry(\"");
319 onClickScript.append(inquiryUrl);
320 onClickScript.append("\", \"");
321 onClickScript.append(paramMapString);
322 onClickScript.append("\", ");
323 onClickScript.append(lightBoxShow);
324 onClickScript.append(", ");
325 onClickScript.append(lightBoxOptions);
326 onClickScript.append(");");
327
328 directInquiryAction.setPerformDirtyValidation(false);
329 directInquiryAction.setActionScript(onClickScript.toString());
330
331 setRender(true);
332 }
333 }
334
335
336
337
338
339
340
341 public String createTitleText(Class<?> dataObjectClass) {
342 String titleText = "";
343
344 String titlePrefixProp = CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
345 INQUIRY_TITLE_PREFIX);
346 if (StringUtils.isNotBlank(titlePrefixProp)) {
347 titleText += titlePrefixProp + " ";
348 }
349
350 String objectLabel = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDataObjectEntry(
351 dataObjectClass.getName()).getObjectLabel();
352 if (StringUtils.isNotBlank(objectLabel)) {
353 titleText += objectLabel + " ";
354 }
355
356 return titleText;
357 }
358
359
360
361
362 @Override
363 public List<Component> getComponentsForLifecycle() {
364 List<Component> components = super.getComponentsForLifecycle();
365
366 components.add(getInquiryLink());
367 components.add(getDirectInquiryAction());
368
369 return components;
370 }
371
372
373
374
375
376
377
378
379
380
381
382
383
384 @BeanTagAttribute(name = "baseInquiryUrl")
385 public String getBaseInquiryUrl() {
386 return this.baseInquiryUrl;
387 }
388
389
390
391
392
393
394 public void setBaseInquiryUrl(String baseInquiryUrl) {
395 this.baseInquiryUrl = baseInquiryUrl;
396 }
397
398
399
400
401
402
403
404
405
406
407
408
409 @BeanTagAttribute(name = "dataObjectClassName")
410 public String getDataObjectClassName() {
411 return this.dataObjectClassName;
412 }
413
414
415
416
417
418
419 public void setDataObjectClassName(String dataObjectClassName) {
420 this.dataObjectClassName = dataObjectClassName;
421 }
422
423
424
425
426
427
428
429
430
431
432
433
434 @BeanTagAttribute(name = "viewName")
435 public String getViewName() {
436 return this.viewName;
437 }
438
439
440
441
442
443
444 public void setViewName(String viewName) {
445 this.viewName = viewName;
446 }
447
448
449
450
451
452
453
454
455
456
457
458
459
460 @BeanTagAttribute(name = "inquiryParameters", type = BeanTagAttribute.AttributeType.MAPVALUE)
461 public Map<String, String> getInquiryParameters() {
462 return this.inquiryParameters;
463 }
464
465
466
467
468
469
470
471 public void setInquiryParameters(Map<String, String> inquiryParameters) {
472 this.inquiryParameters = inquiryParameters;
473 }
474
475
476
477
478
479
480 @BeanTagAttribute(name = "inquiryLink", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
481 public Link getInquiryLink() {
482 return this.inquiryLink;
483 }
484
485
486
487
488
489
490 public void setInquiryLink(Link inquiryLink) {
491 this.inquiryLink = inquiryLink;
492 }
493
494
495
496
497
498
499 @BeanTagAttribute(name = "directInquiryAction", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
500 public Action getDirectInquiryAction() {
501 return this.directInquiryAction;
502 }
503
504
505
506
507
508
509 public void setDirectInquiryAction(Action directInquiryAction) {
510 this.directInquiryAction = directInquiryAction;
511 }
512
513
514
515
516
517
518 @BeanTagAttribute(name = "enableDirectInquiry")
519 public boolean isEnableDirectInquiry() {
520 return enableDirectInquiry;
521 }
522
523
524
525
526
527
528 public void setEnableDirectInquiry(boolean enableDirectInquiry) {
529 this.enableDirectInquiry = enableDirectInquiry;
530 }
531
532
533
534
535
536
537 protected boolean isParentReadOnly() {
538 return parentReadOnly;
539 }
540
541
542
543
544
545
546
547
548
549
550 protected void setParentReadOnly(boolean parentReadOnly) {
551 this.parentReadOnly = parentReadOnly;
552 }
553
554
555
556
557
558
559 public boolean isAdjustInquiryParameters() {
560 return adjustInquiryParameters;
561 }
562
563
564
565
566
567
568
569
570
571
572 public void setAdjustInquiryParameters(boolean adjustInquiryParameters) {
573 this.adjustInquiryParameters = adjustInquiryParameters;
574 }
575
576
577
578
579
580
581
582
583
584
585 public void setFieldBindingInfo(BindingInfo fieldBindingInfo) {
586 this.fieldBindingInfo = fieldBindingInfo;
587 }
588
589
590
591
592 @Override
593 protected <T> void copyProperties(T component) {
594 super.copyProperties(component);
595 Inquiry inquiryCopy = (Inquiry) component;
596 inquiryCopy.setBaseInquiryUrl(this.getBaseInquiryUrl());
597 inquiryCopy.setDataObjectClassName(this.getDataObjectClassName());
598 inquiryCopy.setViewName(this.getViewName());
599 inquiryCopy.setInquiryLink((Link) this.getInquiryLink().copy());
600 inquiryCopy.setDirectInquiryAction((Action) this.getDirectInquiryAction().copy());
601 inquiryCopy.setEnableDirectInquiry(this.isEnableDirectInquiry());
602 inquiryCopy.setAdjustInquiryParameters(this.isAdjustInquiryParameters());
603 inquiryCopy.setParentReadOnly(this.isParentReadOnly());
604
605 if (inquiryParameters != null) {
606 Map<String, String> inquiryParametersCopy = Maps.newHashMapWithExpectedSize(inquiryParameters.size());
607 for (Map.Entry inquiryParameter : inquiryParameters.entrySet()) {
608 inquiryParametersCopy.put(inquiryParameter.getKey().toString(), inquiryParameter.getValue().toString());
609 }
610 inquiryCopy.setInquiryParameters(inquiryParametersCopy);
611 }
612
613 if (fieldBindingInfo != null) {
614 inquiryCopy.setFieldBindingInfo((BindingInfo) fieldBindingInfo.copy());
615 }
616 }
617 }