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