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