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