1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.util;
17
18 import org.apache.commons.codec.EncoderException;
19 import org.apache.commons.codec.net.URLCodec;
20 import org.apache.commons.lang.StringUtils;
21 import org.kuali.rice.core.api.CoreApiServiceLocator;
22 import org.kuali.rice.core.api.encryption.EncryptionService;
23 import org.kuali.rice.core.api.util.Truth;
24 import org.kuali.rice.core.api.util.type.KualiDecimal;
25 import org.kuali.rice.core.web.format.BooleanFormatter;
26 import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
27 import org.kuali.rice.coreservice.framework.parameter.ParameterConstants;
28 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
29 import org.kuali.rice.krad.UserSession;
30 import org.kuali.rice.krad.messages.Message;
31 import org.kuali.rice.krad.messages.MessageService;
32 import org.kuali.rice.krad.service.DataObjectMetaDataService;
33 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
34 import org.kuali.rice.krad.service.KualiModuleService;
35 import org.kuali.rice.krad.service.ModuleService;
36 import org.kuali.rice.krad.uif.UifConstants;
37 import org.kuali.rice.krad.uif.UifParameters;
38 import org.kuali.rice.krad.uif.component.Component;
39 import org.kuali.rice.krad.uif.element.Action;
40 import org.kuali.rice.krad.uif.element.Image;
41 import org.kuali.rice.krad.uif.element.Link;
42 import org.kuali.rice.krad.uif.field.ActionField;
43 import org.kuali.rice.krad.uif.field.DataField;
44 import org.kuali.rice.krad.uif.field.Field;
45 import org.kuali.rice.krad.uif.field.FieldGroup;
46 import org.kuali.rice.krad.uif.field.ImageField;
47 import org.kuali.rice.krad.uif.field.LinkField;
48 import org.kuali.rice.krad.uif.field.MessageField;
49 import org.kuali.rice.krad.uif.field.SpaceField;
50 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
51 import org.kuali.rice.krad.uif.util.ViewModelUtils;
52 import org.kuali.rice.krad.uif.view.ExpressionEvaluator;
53 import org.kuali.rice.krad.uif.view.View;
54 import org.kuali.rice.krad.web.form.UifFormBase;
55 import org.springframework.util.Assert;
56
57 import javax.servlet.ServletRequest;
58 import javax.servlet.http.HttpServletRequest;
59 import java.lang.reflect.Constructor;
60 import java.lang.reflect.Method;
61 import java.security.GeneralSecurityException;
62 import java.text.MessageFormat;
63 import java.text.NumberFormat;
64 import java.util.ArrayList;
65 import java.util.Arrays;
66 import java.util.Collection;
67 import java.util.Collections;
68 import java.util.HashMap;
69 import java.util.Iterator;
70 import java.util.List;
71 import java.util.Map;
72 import java.util.Properties;
73 import java.util.regex.Pattern;
74
75
76
77
78
79
80 public final class KRADUtils {
81 private static KualiModuleService kualiModuleService;
82
83 private static final KualiDecimal ONE_HUNDRED = new KualiDecimal("100.00");
84
85
86
87
88 private KRADUtils() {
89 throw new UnsupportedOperationException("do not call");
90 }
91
92
93
94
95
96
97
98
99
100
101
102 public final static String getBusinessTitleForClass(Class<? extends Object> clazz) {
103 if (clazz == null) {
104 throw new IllegalArgumentException(
105 "The getBusinessTitleForClass method of KRADUtils requires a non-null class");
106 }
107 String className = clazz.getSimpleName();
108
109 StringBuffer label = new StringBuffer(className.substring(0, 1));
110 for (int i = 1; i < className.length(); i++) {
111 if (Character.isLowerCase(className.charAt(i))) {
112 label.append(className.charAt(i));
113 } else {
114 label.append(" ").append(className.charAt(i));
115 }
116 }
117 return label.toString().trim();
118 }
119
120
121
122
123
124
125
126
127
128
129
130 public final static List<String> getFileNameFromPath(List<String> fullFileNames) {
131 List<String> fileNameList = new ArrayList<String>();
132
133 for (String fullFileName : fullFileNames) {
134 if (StringUtils.contains(fullFileName, "/")) {
135 fileNameList.add(StringUtils.substringAfterLast(fullFileName, "/"));
136 } else {
137 fileNameList.add(StringUtils.substringAfterLast(fullFileName, "\\"));
138 }
139 }
140
141 return fileNameList;
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156 public final static String convertDecimalIntoInteger(KualiDecimal decimalNumber) {
157 KualiDecimal decimalAmount = decimalNumber.multiply(ONE_HUNDRED);
158 NumberFormat formatter = NumberFormat.getIntegerInstance();
159 String formattedAmount = formatter.format(decimalAmount);
160
161 return StringUtils.replace(formattedAmount, ",", "");
162 }
163
164
165
166
167
168
169
170
171
172
173
174 public static Integer getIntegerValue(String numberStr) {
175 Integer numberInt = null;
176 try {
177 numberInt = new Integer(numberStr);
178 } catch (NumberFormatException nfe) {
179 Double numberDbl = new Double(numberStr);
180 numberInt = new Integer(numberDbl.intValue());
181 }
182 return numberInt;
183 }
184
185
186
187
188
189
190
191
192
193
194 public static Object hydrateAttributeValue(Class<?> propertyType, String attributeValue) {
195 Object attributeValueObject = null;
196 if (propertyType != null && attributeValue != null) {
197 if (String.class.equals(propertyType)) {
198
199 attributeValueObject = attributeValue;
200 }
201 else if (Boolean.class.equals(propertyType) || Boolean.TYPE.equals(propertyType)) {
202 attributeValueObject = Truth.strToBooleanIgnoreCase(attributeValue);
203 } else {
204
205 attributeValueObject = KRADUtils.createObject(propertyType, new Class[]{String.class},
206 new Object[]{attributeValue});
207
208 }
209 }
210 return attributeValueObject;
211 }
212
213 public static Object createObject(Class<?> clazz, Class<?>[] argumentClasses, Object[] argumentValues) {
214 if (clazz == null) {
215 return null;
216 }
217 if (argumentClasses.length == 1 && argumentClasses[0] == String.class) {
218 if (argumentValues.length == 1 && argumentValues[0] != null) {
219 if (clazz == String.class) {
220
221
222 return argumentValues[0];
223 } else {
224
225 Method valueOfMethod = null;
226 try {
227 valueOfMethod = clazz.getMethod("valueOf", String.class);
228 } catch (NoSuchMethodException e) {
229
230 }
231 if (valueOfMethod != null) {
232 try {
233 return valueOfMethod.invoke(null, argumentValues[0]);
234 } catch (Exception e) {
235
236 }
237 }
238 }
239 }
240 }
241 try {
242 Constructor<?> constructor = clazz.getConstructor(argumentClasses);
243 return constructor.newInstance(argumentValues);
244 } catch (Exception e) {
245
246 }
247 return null;
248 }
249
250
251
252
253
254
255
256
257
258
259
260 public static String joinWithQuotes(List<String> list) {
261 if (list == null || list.size() == 0) {
262 return "";
263 }
264
265 return KRADConstants.SINGLE_QUOTE +
266 StringUtils.join(list.iterator(), KRADConstants.SINGLE_QUOTE + "," + KRADConstants.SINGLE_QUOTE) +
267 KRADConstants.SINGLE_QUOTE;
268 }
269
270 private static KualiModuleService getKualiModuleService() {
271 if (kualiModuleService == null) {
272 kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
273 }
274 return kualiModuleService;
275 }
276
277
278
279
280
281
282 public static String getNamespaceCode(Class<? extends Object> clazz) {
283 ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
284 if (moduleService == null) {
285 return KRADConstants.DEFAULT_NAMESPACE;
286 }
287 return moduleService.getModuleConfiguration().getNamespaceCode();
288 }
289
290 public static Map<String, String> getNamespaceAndComponentSimpleName(Class<? extends Object> clazz) {
291 Map<String, String> map = new HashMap<String, String>();
292 map.put(KRADConstants.NAMESPACE_CODE, getNamespaceCode(clazz));
293 map.put(KRADConstants.COMPONENT_NAME, getComponentSimpleName(clazz));
294 return map;
295 }
296
297 public static Map<String, String> getNamespaceAndComponentFullName(Class<? extends Object> clazz) {
298 Map<String, String> map = new HashMap<String, String>();
299 map.put(KRADConstants.NAMESPACE_CODE, getNamespaceCode(clazz));
300 map.put(KRADConstants.COMPONENT_NAME, getComponentFullName(clazz));
301 return map;
302 }
303
304 public static Map<String, String> getNamespaceAndActionClass(Class<? extends Object> clazz) {
305 Map<String, String> map = new HashMap<String, String>();
306 map.put(KRADConstants.NAMESPACE_CODE, getNamespaceCode(clazz));
307 map.put(KRADConstants.ACTION_CLASS, clazz.getName());
308 return map;
309 }
310
311 private static String getComponentSimpleName(Class<? extends Object> clazz) {
312 return clazz.getSimpleName();
313 }
314
315 private static String getComponentFullName(Class<? extends Object> clazz) {
316 return clazz.getName();
317 }
318
319
320
321
322
323
324
325
326 public static Map<String, String> convertStringParameterToMap(String parameter) {
327 Map<String, String> map = new HashMap<String, String>();
328
329 if (StringUtils.isNotBlank(parameter)) {
330 if (StringUtils.contains(parameter, ",")) {
331 String[] fieldConversions = StringUtils.split(parameter, ",");
332
333 for (int i = 0; i < fieldConversions.length; i++) {
334 String fieldConversionStr = fieldConversions[i];
335 if (StringUtils.isNotBlank(fieldConversionStr)) {
336 if (StringUtils.contains(fieldConversionStr, ":")) {
337 String[] fieldConversion = StringUtils.split(fieldConversionStr, ":");
338 map.put(fieldConversion[0], fieldConversion[1]);
339 } else {
340 map.put(fieldConversionStr, fieldConversionStr);
341 }
342 }
343 }
344 } else if (StringUtils.contains(parameter, ":")) {
345 String[] fieldConversion = StringUtils.split(parameter, ":");
346 map.put(fieldConversion[0], fieldConversion[1]);
347 } else {
348 map.put(parameter, parameter);
349 }
350 }
351
352 return map;
353 }
354
355
356
357
358
359
360
361 public static List<String> convertStringParameterToList(String parameter) {
362 List<String> list = new ArrayList<String>();
363
364 if (StringUtils.isNotBlank(parameter)) {
365 if (StringUtils.contains(parameter, ",")) {
366 String[] parameters = StringUtils.split(parameter, ",");
367 List arraysList = Arrays.asList(parameters);
368 list.addAll(arraysList);
369 } else {
370 list.add(parameter);
371 }
372 }
373
374 return list;
375 }
376
377
378
379
380
381
382
383
384 public static String translateToMapSafeKey(String key) {
385 String safeKey = key;
386
387 safeKey = StringUtils.replace(safeKey, "[", "_");
388 safeKey = StringUtils.replace(safeKey, "]", "_");
389
390 return safeKey;
391 }
392
393
394
395
396
397
398
399
400 public static String buildMapParameterString(Map<String, String> map) {
401 String parameterString = "";
402
403 for (Map.Entry<String, String> entry : map.entrySet()) {
404 if (StringUtils.isNotBlank(parameterString)) {
405 parameterString += ",";
406 }
407
408 parameterString += entry.getKey() + ":" + entry.getValue();
409 }
410
411 return parameterString;
412 }
413
414
415
416
417
418
419
420
421
422 public static Map<String, String> getMapFromParameterString(String parameterString) {
423 Map<String, String> map = new HashMap<String, String>();
424
425 String[] entries = parameterString.split(",");
426 for (int i = 0; i < entries.length; i++) {
427 String[] keyValue = entries[i].split(":");
428 if (keyValue.length != 2) {
429 throw new RuntimeException("malformed field conversion pair: " + Arrays.toString(keyValue));
430 }
431
432 map.put(keyValue[0], keyValue[1]);
433 }
434
435 return map;
436 }
437
438
439
440
441
442
443
444
445
446 public static Boolean getRequestParameterAsBoolean(ServletRequest request, String parameterName) {
447 Boolean parameterValue = null;
448
449 String parameterValueStr = request.getParameter(parameterName);
450 if (StringUtils.isNotBlank(parameterValueStr)) {
451 parameterValue = (Boolean) new BooleanFormatter().convertFromPresentationFormat(parameterValueStr);
452 }
453
454 return parameterValue;
455 }
456
457
458
459
460
461
462
463
464
465
466 public static Map<String, String> translateRequestParameterMap(Map<String, String[]> requestParameters) {
467 Map<String, String> parameters = new HashMap<String, String>();
468
469 for (Map.Entry<String, String[]> parameter : requestParameters.entrySet()) {
470 String parameterValue = "";
471 if (parameter.getValue().length > 1) {
472 parameterValue = StringUtils.join(parameter.getValue(), "|");
473 } else {
474 parameterValue = parameter.getValue()[0];
475 }
476 parameters.put(parameter.getKey(), parameterValue);
477 }
478
479 return parameters;
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496 public static Map<String, String> getParametersFromRequest(List<String> parameterNames, Class<?> parentObjectClass,
497 Map<String, String> requestParameters) {
498 Map<String, String> parameterValues = new HashMap<String, String>();
499
500 for (Iterator<String> iter = parameterNames.iterator(); iter.hasNext(); ) {
501 String keyPropertyName = iter.next();
502
503 if (requestParameters.get(keyPropertyName) != null) {
504 String keyValue = requestParameters.get(keyPropertyName);
505
506
507 if (KRADServiceLocatorWeb.getDataObjectAuthorizationService()
508 .attributeValueNeedsToBeEncryptedOnFormsAndLinks(parentObjectClass, keyPropertyName)) {
509 try {
510 keyValue = StringUtils.removeEnd(keyValue, EncryptionService.ENCRYPTION_POST_PREFIX);
511 keyValue = CoreApiServiceLocator.getEncryptionService().decrypt(keyValue);
512 } catch (GeneralSecurityException e) {
513 throw new RuntimeException(e);
514 }
515 }
516
517 parameterValues.put(keyPropertyName, keyValue);
518 }
519 }
520
521 return parameterValues;
522 }
523
524
525
526
527
528
529
530
531
532
533
534 public static Map<String, String> getPropertyKeyValuesFromDataObject(List<String> propertyNames,
535 Object dataObject) {
536 return getPropertyKeyValuesFromDataObject(propertyNames, Collections.<String>emptyList(), dataObject);
537 }
538
539
540
541
542
543
544
545
546
547
548
549
550 public static Map<String, String> getPropertyKeyValuesFromDataObject(List<String> propertyNames,
551 List<String> securePropertyNames, Object dataObject) {
552 Map<String, String> propertyKeyValues = new HashMap<String, String>();
553
554 if (dataObject == null) {
555 return propertyKeyValues;
556 }
557
558
559 for (String propertyName : propertyNames) {
560 Object propertyValue = ObjectPropertyUtils.getPropertyValue(dataObject, propertyName);
561 if (propertyValue == null) {
562 propertyValue = StringUtils.EMPTY;
563 }
564
565
566 if (!isSecure(propertyName, securePropertyNames, dataObject, propertyValue)) {
567 propertyKeyValues.put(propertyName, propertyValue.toString());
568 }
569
570 }
571
572 return propertyKeyValues;
573 }
574
575
576
577
578
579
580
581
582
583
584
585 private static boolean isSecure(String propertyName, List<String> securePropertyNames, Object dataObject, Object propertyValue) {
586 if (propertyValue instanceof String && containsSensitiveDataPatternMatch((String) propertyValue)) {
587 return true;
588 }
589
590 if (containsSecurePropertyName(propertyName, securePropertyNames)) {
591 return true;
592 }
593
594 return KRADServiceLocatorWeb.getDataObjectAuthorizationService()
595 .attributeValueNeedsToBeEncryptedOnFormsAndLinks(dataObject.getClass(), propertyName);
596 }
597
598
599
600
601
602
603
604
605
606 private static boolean containsSecurePropertyName(String propertyName, List<String> securePropertyNames) {
607 if (securePropertyNames == null) {
608 return false;
609 }
610
611 for (String securePropertyName : securePropertyNames) {
612
613 if (Pattern.compile("(?:\\.|^)" + Pattern.quote(securePropertyName) + "(?:\\.|\\[|$)").matcher(propertyName)
614 .find()) {
615 return true;
616 }
617 }
618
619 return false;
620 }
621
622
623
624
625
626
627
628 public static Properties convertMapToProperties(Map<String, String> parameters) {
629 Properties properties = new Properties();
630
631 if (parameters != null) {
632 for (Map.Entry<String, String> parameter : parameters.entrySet()) {
633 properties.put(parameter.getKey(), parameter.getValue());
634 }
635 }
636
637 return properties;
638 }
639
640
641
642
643
644
645
646
647
648
649
650 public static Properties convertRequestMapToProperties(Map<String, String[]> requestParameters) {
651 Properties properties = new Properties();
652
653 if (requestParameters != null) {
654 for (Map.Entry<String, String[]> parameter : requestParameters.entrySet()) {
655 String[] parameterValue = parameter.getValue();
656 String parameterValueString = StringUtils.join(parameterValue, ",");
657
658 properties.put(parameter.getKey(), parameterValueString);
659 }
660 }
661
662 return properties;
663 }
664
665
666
667
668
669
670
671
672
673
674
675
676 public static boolean containsSensitiveDataPatternMatch(String fieldValue) {
677 if (StringUtils.isBlank(fieldValue)) {
678 return false;
679 }
680
681 ParameterService parameterService = CoreFrameworkServiceLocator.getParameterService();
682 Collection<String> sensitiveDataPatterns = parameterService.getParameterValuesAsString(
683 KRADConstants.KNS_NAMESPACE, ParameterConstants.ALL_COMPONENT,
684 KRADConstants.SystemGroupParameterNames.SENSITIVE_DATA_PATTERNS);
685
686 for (String pattern : sensitiveDataPatterns) {
687 if (Pattern.compile(pattern).matcher(fieldValue).find()) {
688 return true;
689 }
690 }
691
692 return false;
693 }
694
695
696
697
698
699
700
701 public static String stripXSSPatterns(String value) {
702 if (value == null) {
703 return null;
704 }
705
706
707 value = value.replaceAll("", "");
708
709
710 Pattern scriptPattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
711 value = scriptPattern.matcher(value).replaceAll("");
712
713
714 scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'",
715 Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
716 value = scriptPattern.matcher(value).replaceAll("");
717
718 scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"",
719 Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
720 value = scriptPattern.matcher(value).replaceAll("");
721
722
723 scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
724 value = scriptPattern.matcher(value).replaceAll("");
725
726
727 scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
728 value = scriptPattern.matcher(value).replaceAll("");
729
730
731 scriptPattern = Pattern.compile("eval\\((.*?)\\)",
732 Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
733 value = scriptPattern.matcher(value).replaceAll("");
734
735
736 scriptPattern = Pattern.compile("expression\\((.*?)\\)",
737 Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
738 value = scriptPattern.matcher(value).replaceAll("");
739
740
741 scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
742 value = scriptPattern.matcher(value).replaceAll("");
743
744
745 scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
746 value = scriptPattern.matcher(value).replaceAll("");
747
748
749 scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
750 value = scriptPattern.matcher(value).replaceAll("");
751
752 return value;
753 }
754
755
756
757
758
759
760
761
762
763
764 public static final UserSession getUserSessionFromRequest(HttpServletRequest request) {
765 return (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
766 }
767
768
769
770
771
772
773 public static boolean isProductionEnvironment() {
774 return CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
775 KRADConstants.PROD_ENVIRONMENT_CODE_KEY).equalsIgnoreCase(
776 CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
777 KRADConstants.ENVIRONMENT_KEY));
778 }
779
780
781
782
783
784
785
786
787
788
789 public static String getMessageText(ErrorMessage errorMessage, boolean processPrefixSuffix) {
790 String message = "";
791 if (errorMessage != null && errorMessage.getErrorKey() != null) {
792 MessageService messageService = KRADServiceLocatorWeb.getMessageService();
793
794
795 message = messageService.getMessageText(errorMessage.getNamespaceCode(), errorMessage.getComponentCode(),
796 errorMessage.getErrorKey());
797 if (message == null) {
798 message = "Intended message with key: " + errorMessage.getErrorKey() + " not found.";
799 }
800
801 if (errorMessage.getMessageParameters() != null && StringUtils.isNotBlank(message)) {
802 message = message.replace("'", "''");
803 message = MessageFormat.format(message, (Object[]) errorMessage.getMessageParameters());
804 }
805
806
807 if (StringUtils.isNotBlank(errorMessage.getMessagePrefixKey()) && processPrefixSuffix) {
808 String prefix = messageService.getMessageText(errorMessage.getNamespaceCode(),
809 errorMessage.getComponentCode(), errorMessage.getMessagePrefixKey());
810
811 if (errorMessage.getMessagePrefixParameters() != null && StringUtils.isNotBlank(prefix)) {
812 prefix = prefix.replace("'", "''");
813 prefix = MessageFormat.format(prefix, (Object[]) errorMessage.getMessagePrefixParameters());
814 }
815
816 if (StringUtils.isNotBlank(prefix)) {
817 message = prefix + " " + message;
818 }
819 }
820
821
822 if (StringUtils.isNotBlank(errorMessage.getMessageSuffixKey()) && processPrefixSuffix) {
823 String suffix = messageService.getMessageText(errorMessage.getNamespaceCode(),
824 errorMessage.getComponentCode(), errorMessage.getMessageSuffixKey());
825
826 if (errorMessage.getMessageSuffixParameters() != null && StringUtils.isNotBlank(suffix)) {
827 suffix = suffix.replace("'", "''");
828 suffix = MessageFormat.format(suffix, (Object[]) errorMessage.getMessageSuffixParameters());
829 }
830
831 if (StringUtils.isNotBlank(suffix)) {
832 message = message + " " + suffix;
833 }
834 }
835 }
836
837 return message;
838 }
839
840
841
842
843
844
845
846
847 public static String getRequestStringFromMap(Map<String, String> requestParameters) {
848 String requestString = "";
849
850 if (requestParameters.isEmpty()) {
851 return requestString;
852 }
853
854 URLCodec urlCodec = new URLCodec(KRADConstants.DEFAULT_ENCODING);
855
856 for (String key : requestParameters.keySet()) {
857 String value = null;
858 try {
859 value = urlCodec.encode(requestParameters.get(key));
860 } catch (EncoderException e) {
861 throw new RuntimeException("Unable to encode parameter name or value: " + key + "=" + value, e);
862 }
863
864 if (StringUtils.isNotBlank(requestString)) {
865 requestString = requestString + "&";
866 }
867
868 requestString = requestString + key + "=" + value;
869 }
870
871 return "?" + requestString;
872 }
873
874
875
876
877
878
879
880
881
882
883 public static String buildViewUrl(String baseUrl, String controllerMapping, String viewId) {
884 Assert.hasLength(baseUrl, "base url is null or empty");
885 Assert.hasLength(controllerMapping, "controller mapping is null or empty");
886 Assert.hasLength(viewId, "view id is null or empty");
887
888 StringBuffer url = new StringBuffer();
889
890 url.append(baseUrl);
891
892 if (!baseUrl.endsWith("/")) {
893 url.append("/");
894 }
895
896 url.append(controllerMapping);
897
898 url.append("?");
899 url.append(UifParameters.VIEW_ID);
900 url.append("=");
901 url.append(viewId);
902
903 return url.toString();
904 }
905
906
907
908
909
910
911
912 public static void cleanRequestParameters(Properties requestParameters) {
913 requestParameters.remove(UifParameters.SESSION_ID);
914 requestParameters.remove(UifParameters.AJAX_REQUEST);
915 requestParameters.remove(UifParameters.AJAX_RETURN_TYPE);
916 requestParameters.remove(UifParameters.FORM_KEY);
917 requestParameters.remove(UifParameters.JUMP_TO_ID);
918 requestParameters.remove(UifParameters.FOCUS_ID);
919 }
920
921
922
923
924
925
926
927 public static String getFullURL(HttpServletRequest request) {
928 if (request == null) {
929 return null;
930 }
931
932 StringBuffer requestURL = request.getRequestURL();
933 String queryString = request.getQueryString();
934
935 if (queryString == null) {
936 return requestURL.toString();
937 } else {
938 return requestURL.append('?').append(queryString).toString();
939 }
940 }
941
942
943
944
945
946
947
948
949
950
951
952 public static String generateUniqueViewTitle(UifFormBase form, View view) {
953 String title = view.getHeader().getHeaderText();
954
955 String viewLabelPropertyName = "";
956
957 Class<?> dataObjectClass;
958 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
959 dataObjectClass = ObjectPropertyUtils.getPropertyType(form, view.getDefaultBindingObjectPath());
960 } else {
961 dataObjectClass = view.getFormClass();
962 }
963
964 DataObjectMetaDataService mds = KRADServiceLocatorWeb.getDataObjectMetaDataService();
965 if (dataObjectClass != null) {
966 viewLabelPropertyName = mds.getTitleAttribute(dataObjectClass);
967 }
968
969 String viewLabelPropertyPath = "";
970 if (StringUtils.isNotBlank(viewLabelPropertyName)) {
971
972 if (!viewLabelPropertyName.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) {
973 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
974 viewLabelPropertyPath = view.getDefaultBindingObjectPath() + "." + viewLabelPropertyName;
975 }
976 } else {
977 viewLabelPropertyPath = StringUtils.removeStart(viewLabelPropertyName,
978 UifConstants.NO_BIND_ADJUST_PREFIX);
979 }
980 } else {
981
982 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
983 dataObjectClass = ViewModelUtils.getObjectClassForMetadata(view, form,
984 view.getDefaultBindingObjectPath());
985 } else {
986 dataObjectClass = view.getFormClass();
987 }
988
989 if (dataObjectClass != null) {
990 String titleAttribute = mds.getTitleAttribute(dataObjectClass);
991 if (StringUtils.isNotBlank(titleAttribute)) {
992 viewLabelPropertyPath = view.getDefaultBindingObjectPath() + "." + titleAttribute;
993 }
994 }
995 }
996
997 Object viewLabelPropertyValue = null;
998 if (StringUtils.isNotBlank(viewLabelPropertyPath) && ObjectPropertyUtils.isReadableProperty(form,
999 viewLabelPropertyPath)) {
1000 viewLabelPropertyValue = ObjectPropertyUtils.getPropertyValue(form, viewLabelPropertyPath);
1001 }
1002
1003 if (viewLabelPropertyValue != null && StringUtils.isNotBlank(viewLabelPropertyValue.toString()) && StringUtils
1004 .isNotBlank(title)) {
1005 return title + " (" + viewLabelPropertyValue.toString() + ")";
1006 } else {
1007 return title;
1008 }
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022 public static String getSimpleFieldValue(Object model, Field field) {
1023 if (field == null) {
1024 return null;
1025 }
1026
1027 String value = null;
1028
1029 if (field instanceof DataField) {
1030 String propertyPath = ((DataField) field).getBindingInfo().getBindingPath();
1031 Object valueObject = null;
1032
1033 if (field.isHidden()) {
1034 return "";
1035 }
1036
1037
1038 if (ObjectPropertyUtils.isReadableProperty(model, propertyPath)) {
1039 valueObject = ObjectPropertyUtils.getPropertyValue(model, propertyPath);
1040 }
1041
1042
1043 if (valueObject != null && !((DataField) field).isApplyMask()) {
1044 value = valueObject.toString();
1045 } else if (valueObject != null && ((DataField) field).isApplyMask()) {
1046 value = ((DataField) field).getMaskFormatter().maskValue(valueObject);
1047 }
1048 } else if (field instanceof ActionField) {
1049 value = ((ActionField) field).getActionLabel();
1050
1051
1052 if (StringUtils.isBlank(value) && ((ActionField) field).getActionImage() != null) {
1053 value = ((ActionField) field).getActionImage().getAltText();
1054 }
1055 } else if (field instanceof LinkField) {
1056 value = ((LinkField) field).getLinkText();
1057 } else if (field instanceof ImageField) {
1058 value = ((ImageField) field).getAltText();
1059 } else if (field instanceof MessageField && ((MessageField) field).getMessage() != null) {
1060 value = ((MessageField) field).getMessage().getMessageText();
1061 } else if (field instanceof SpaceField) {
1062 value = "";
1063 } else if (field instanceof FieldGroup
1064 && ((FieldGroup) field).getGroup() != null
1065 && ((FieldGroup) field).getGroup().getItems() != null
1066 && !((FieldGroup) field).getGroup().getItems().isEmpty()) {
1067
1068 Component firstComponent = ((FieldGroup) field).getGroup().getItems().get(0);
1069
1070
1071 if (firstComponent != null && firstComponent instanceof Field) {
1072 value = getSimpleFieldValue(model, field);
1073 } else if (firstComponent instanceof Action && StringUtils.isNotBlank(
1074 ((Action) firstComponent).getActionLabel())) {
1075 value = ((Action) firstComponent).getActionLabel();
1076 } else if (firstComponent instanceof Action && ((Action) firstComponent).getActionImage() != null) {
1077 value = ((Action) firstComponent).getActionImage().getAltText();
1078 } else if (firstComponent instanceof Link) {
1079 value = ((Link) firstComponent).getLinkText();
1080 } else if (firstComponent instanceof Image) {
1081 value = ((Image) firstComponent).getAltText();
1082 } else if (firstComponent instanceof org.kuali.rice.krad.uif.element.Message) {
1083 value = ((org.kuali.rice.krad.uif.element.Message) firstComponent).getMessageText();
1084 } else {
1085 value = null;
1086 }
1087 }
1088
1089 return value;
1090 }
1091
1092
1093
1094
1095
1096
1097
1098 public static String convertToHTMLAttributeSafeString(String message) {
1099 if (StringUtils.isBlank(message)) {
1100 return message;
1101 }
1102
1103 if (message.contains("\"")) {
1104 message = message.replace("\"", """);
1105 }
1106 if (message.contains("'")) {
1107 message = message.replace("'", "'");
1108 }
1109 if (message.contains("\\")) {
1110 message = message.replace("\\", "\");
1111 }
1112
1113 return message;
1114 }
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127 public static String generateRowCssClassString(Map<String, String> conditionalRowCssClasses, int lineIndex,
1128 boolean isOdd, Map<String, Object> lineContext, ExpressionEvaluator expressionEvaluator) {
1129 String rowCss = "";
1130 if (conditionalRowCssClasses == null || conditionalRowCssClasses.isEmpty()) {
1131 return rowCss;
1132 }
1133
1134 for (String cssRule : conditionalRowCssClasses.keySet()) {
1135 if (cssRule.startsWith(UifConstants.EL_PLACEHOLDER_PREFIX) && lineContext != null &&
1136 expressionEvaluator != null) {
1137 String outcome = expressionEvaluator.evaluateExpressionTemplate(lineContext, cssRule);
1138 if (outcome != null && Boolean.parseBoolean(outcome)) {
1139 rowCss = rowCss + " " + conditionalRowCssClasses.get(cssRule);
1140 }
1141 } else if (cssRule.equals(UifConstants.RowSelection.ALL)) {
1142 rowCss = rowCss + " " + conditionalRowCssClasses.get(cssRule);
1143 } else if (cssRule.equals(UifConstants.RowSelection.EVEN) && !isOdd) {
1144 rowCss = rowCss + " " + conditionalRowCssClasses.get(cssRule);
1145 } else if (cssRule.equals(UifConstants.RowSelection.ODD) && isOdd) {
1146 rowCss = rowCss + " " + conditionalRowCssClasses.get(cssRule);
1147 } else if (StringUtils.isNumeric(cssRule) && (lineIndex + 1) == Integer.parseInt(cssRule)) {
1148 rowCss = rowCss + " " + conditionalRowCssClasses.get(cssRule);
1149 }
1150 }
1151
1152 rowCss = StringUtils.removeStart(rowCss, " ");
1153
1154 return rowCss;
1155 }
1156 }