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.lang.StringUtils;
19 import org.kuali.rice.core.api.util.Truth;
20 import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
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.type.KualiDecimal;
24 import org.kuali.rice.coreservice.framework.parameter.ParameterConstants;
25 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
26 import org.kuali.rice.core.web.format.BooleanFormatter;
27 import org.kuali.rice.krad.UserSession;
28 import org.kuali.rice.krad.messages.MessageService;
29 import org.kuali.rice.krad.service.KRADServiceLocator;
30 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
31 import org.kuali.rice.krad.service.KualiModuleService;
32 import org.kuali.rice.krad.service.ModuleService;
33 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
34
35 import javax.servlet.ServletRequest;
36 import javax.servlet.http.HttpServletRequest;
37 import java.lang.reflect.Constructor;
38 import java.lang.reflect.Method;
39 import java.security.GeneralSecurityException;
40 import java.text.MessageFormat;
41 import java.text.NumberFormat;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collection;
45 import java.util.HashMap;
46 import java.util.Iterator;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.Properties;
50 import java.util.regex.Pattern;
51
52
53
54
55
56
57 public final class KRADUtils {
58 private static KualiModuleService kualiModuleService;
59
60 private static final KualiDecimal ONE_HUNDRED = new KualiDecimal("100.00");
61
62
63
64
65 private KRADUtils() {
66 throw new UnsupportedOperationException("do not call");
67 }
68
69
70
71
72
73
74
75
76
77
78
79 public final static String getBusinessTitleForClass(Class<? extends Object> clazz) {
80 if (clazz == null) {
81 throw new IllegalArgumentException(
82 "The getBusinessTitleForClass method of KRADUtils requires a non-null class");
83 }
84 String className = clazz.getSimpleName();
85
86 StringBuffer label = new StringBuffer(className.substring(0, 1));
87 for (int i = 1; i < className.length(); i++) {
88 if (Character.isLowerCase(className.charAt(i))) {
89 label.append(className.charAt(i));
90 } else {
91 label.append(" ").append(className.charAt(i));
92 }
93 }
94 return label.toString().trim();
95 }
96
97
98
99
100
101
102
103
104
105
106
107 public final static List<String> getFileNameFromPath(List<String> fullFileNames) {
108 List<String> fileNameList = new ArrayList<String>();
109
110 for (String fullFileName : fullFileNames) {
111 if (StringUtils.contains(fullFileName, "/")) {
112 fileNameList.add(StringUtils.substringAfterLast(fullFileName, "/"));
113 } else {
114 fileNameList.add(StringUtils.substringAfterLast(fullFileName, "\\"));
115 }
116 }
117
118 return fileNameList;
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public final static String convertDecimalIntoInteger(KualiDecimal decimalNumber) {
134 KualiDecimal decimalAmount = decimalNumber.multiply(ONE_HUNDRED);
135 NumberFormat formatter = NumberFormat.getIntegerInstance();
136 String formattedAmount = formatter.format(decimalAmount);
137
138 return StringUtils.replace(formattedAmount, ",", "");
139 }
140
141
142
143
144
145
146
147
148
149
150
151 public static Integer getIntegerValue(String numberStr) {
152 Integer numberInt = null;
153 try {
154 numberInt = new Integer(numberStr);
155 } catch (NumberFormatException nfe) {
156 Double numberDbl = new Double(numberStr);
157 numberInt = new Integer(numberDbl.intValue());
158 }
159 return numberInt;
160 }
161
162
163
164
165
166
167
168
169
170
171 public static Object hydrateAttributeValue(Class<?> propertyType, String attributeValue) {
172 Object attributeValueObject = null;
173 if (propertyType != null && attributeValue != null) {
174 if (String.class.equals(propertyType)) {
175
176 attributeValueObject = attributeValue;
177 }
178 else if (Boolean.class.equals(propertyType) || Boolean.TYPE.equals(propertyType)) {
179 attributeValueObject = Truth.strToBooleanIgnoreCase(attributeValue);
180 } else {
181
182 attributeValueObject = KRADUtils.createObject(propertyType, new Class[]{String.class},
183 new Object[]{attributeValue});
184
185 }
186 }
187 return attributeValueObject;
188 }
189
190 public static Object createObject(Class<?> clazz, Class<?>[] argumentClasses, Object[] argumentValues) {
191 if (clazz == null) {
192 return null;
193 }
194 if (argumentClasses.length == 1 && argumentClasses[0] == String.class) {
195 if (argumentValues.length == 1 && argumentValues[0] != null) {
196 if (clazz == String.class) {
197
198
199 return argumentValues[0];
200 } else {
201
202 Method valueOfMethod = null;
203 try {
204 valueOfMethod = clazz.getMethod("valueOf", String.class);
205 } catch (NoSuchMethodException e) {
206
207 }
208 if (valueOfMethod != null) {
209 try {
210 return valueOfMethod.invoke(null, argumentValues[0]);
211 } catch (Exception e) {
212
213 }
214 }
215 }
216 }
217 }
218 try {
219 Constructor<?> constructor = clazz.getConstructor(argumentClasses);
220 return constructor.newInstance(argumentValues);
221 } catch (Exception e) {
222
223 }
224 return null;
225 }
226
227
228
229
230
231
232
233
234
235
236
237 public static String joinWithQuotes(List<String> list) {
238 if (list == null || list.size() == 0) {
239 return "";
240 }
241
242 return KRADConstants.SINGLE_QUOTE +
243 StringUtils.join(list.iterator(), KRADConstants.SINGLE_QUOTE + "," + KRADConstants.SINGLE_QUOTE) +
244 KRADConstants.SINGLE_QUOTE;
245 }
246
247 private static KualiModuleService getKualiModuleService() {
248 if (kualiModuleService == null) {
249 kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
250 }
251 return kualiModuleService;
252 }
253
254
255
256
257
258
259 public static String getNamespaceCode(Class<? extends Object> clazz) {
260 ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
261 if (moduleService == null) {
262 return KRADConstants.DEFAULT_NAMESPACE;
263 }
264 return moduleService.getModuleConfiguration().getNamespaceCode();
265 }
266
267 public static Map<String, String> getNamespaceAndComponentSimpleName(Class<? extends Object> clazz) {
268 Map<String, String> map = new HashMap<String, String>();
269 map.put(KRADConstants.NAMESPACE_CODE, getNamespaceCode(clazz));
270 map.put(KRADConstants.COMPONENT_NAME, getComponentSimpleName(clazz));
271 return map;
272 }
273
274 public static Map<String, String> getNamespaceAndComponentFullName(Class<? extends Object> clazz) {
275 Map<String, String> map = new HashMap<String, String>();
276 map.put(KRADConstants.NAMESPACE_CODE, getNamespaceCode(clazz));
277 map.put(KRADConstants.COMPONENT_NAME, getComponentFullName(clazz));
278 return map;
279 }
280
281 public static Map<String, String> getNamespaceAndActionClass(Class<? extends Object> clazz) {
282 Map<String, String> map = new HashMap<String, String>();
283 map.put(KRADConstants.NAMESPACE_CODE, getNamespaceCode(clazz));
284 map.put(KRADConstants.ACTION_CLASS, clazz.getName());
285 return map;
286 }
287
288 private static String getComponentSimpleName(Class<? extends Object> clazz) {
289 return clazz.getSimpleName();
290 }
291
292 private static String getComponentFullName(Class<? extends Object> clazz) {
293 return clazz.getName();
294 }
295
296
297
298
299
300
301
302
303 public static Map<String, String> convertStringParameterToMap(String parameter) {
304 Map<String, String> map = new HashMap<String, String>();
305
306 if (StringUtils.isNotBlank(parameter)) {
307 if (StringUtils.contains(parameter, ",")) {
308 String[] fieldConversions = StringUtils.split(parameter, ",");
309
310 for (int i = 0; i < fieldConversions.length; i++) {
311 String fieldConversionStr = fieldConversions[i];
312 if (StringUtils.isNotBlank(fieldConversionStr)) {
313 if (StringUtils.contains(fieldConversionStr, ":")) {
314 String[] fieldConversion = StringUtils.split(fieldConversionStr, ":");
315 map.put(fieldConversion[0], fieldConversion[1]);
316 } else {
317 map.put(fieldConversionStr, fieldConversionStr);
318 }
319 }
320 }
321 } else if (StringUtils.contains(parameter, ":")) {
322 String[] fieldConversion = StringUtils.split(parameter, ":");
323 map.put(fieldConversion[0], fieldConversion[1]);
324 } else {
325 map.put(parameter, parameter);
326 }
327 }
328
329 return map;
330 }
331
332
333
334
335
336
337
338 public static List<String> convertStringParameterToList(String parameter) {
339 List<String> list = new ArrayList<String>();
340
341 if (StringUtils.isNotBlank(parameter)) {
342 if (StringUtils.contains(parameter, ",")) {
343 String[] parameters = StringUtils.split(parameter, ",");
344 List arraysList = Arrays.asList(parameters);
345 list.addAll(arraysList);
346 } else {
347 list.add(parameter);
348 }
349 }
350
351 return list;
352 }
353
354
355
356
357
358
359
360
361 public static String translateToMapSafeKey(String key) {
362 String safeKey = key;
363
364 safeKey = StringUtils.replace(safeKey, "[", "_");
365 safeKey = StringUtils.replace(safeKey, "]", "_");
366
367 return safeKey;
368 }
369
370
371
372
373
374
375
376
377 public static String buildMapParameterString(Map<String, String> map) {
378 String parameterString = "";
379
380 for (Map.Entry<String, String> entry : map.entrySet()) {
381 if (StringUtils.isNotBlank(parameterString)) {
382 parameterString += ",";
383 }
384
385 parameterString += entry.getKey() + ":" + entry.getValue();
386 }
387
388 return parameterString;
389 }
390
391
392
393
394
395
396
397
398
399 public static Map<String, String> getMapFromParameterString(String parameterString) {
400 Map<String, String> map = new HashMap<String, String>();
401
402 String[] entries = parameterString.split(",");
403 for (int i = 0; i < entries.length; i++) {
404 String[] keyValue = entries[i].split(":");
405 if (keyValue.length != 2) {
406 throw new RuntimeException("malformed field conversion pair: " + Arrays.toString(keyValue));
407 }
408
409 map.put(keyValue[0], keyValue[1]);
410 }
411
412 return map;
413 }
414
415
416
417
418
419
420
421
422
423 public static Boolean getRequestParameterAsBoolean(ServletRequest request, String parameterName) {
424 Boolean parameterValue = null;
425
426 String parameterValueStr = request.getParameter(parameterName);
427 if (StringUtils.isNotBlank(parameterValueStr)) {
428 parameterValue = (Boolean) new BooleanFormatter().convertFromPresentationFormat(parameterValueStr);
429 }
430
431 return parameterValue;
432 }
433
434
435
436
437
438
439
440
441
442
443 public static Map<String, String> translateRequestParameterMap(Map<String, String[]> requestParameters) {
444 Map<String, String> parameters = new HashMap<String, String>();
445
446 for (Map.Entry<String, String[]> parameter : requestParameters.entrySet()) {
447 String parameterValue = "";
448 if (parameter.getValue().length > 1) {
449 parameterValue = StringUtils.join(parameter.getValue(), "|");
450 } else {
451 parameterValue = parameter.getValue()[0];
452 }
453 parameters.put(parameter.getKey(), parameterValue);
454 }
455
456 return parameters;
457 }
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473 public static Map<String, String> getParametersFromRequest(List<String> parameterNames, Class<?> parentObjectClass,
474 Map<String, String> requestParameters) {
475 Map<String, String> parameterValues = new HashMap<String, String>();
476
477 for (Iterator<String> iter = parameterNames.iterator(); iter.hasNext(); ) {
478 String keyPropertyName = iter.next();
479
480 if (requestParameters.get(keyPropertyName) != null) {
481 String keyValue = requestParameters.get(keyPropertyName);
482
483
484 if (KRADServiceLocatorWeb.getDataObjectAuthorizationService()
485 .attributeValueNeedsToBeEncryptedOnFormsAndLinks(parentObjectClass, keyPropertyName)) {
486 try {
487 keyValue = StringUtils.removeEnd(keyValue, EncryptionService.ENCRYPTION_POST_PREFIX);
488 keyValue = CoreApiServiceLocator.getEncryptionService().decrypt(keyValue);
489 } catch (GeneralSecurityException e) {
490 throw new RuntimeException(e);
491 }
492 }
493
494 parameterValues.put(keyPropertyName, keyValue);
495 }
496 }
497
498 return parameterValues;
499 }
500
501
502
503
504
505
506
507
508
509
510
511 public static Map<String, String> getPropertyKeyValuesFromDataObject(List<String> propertyNames,
512 Object dataObject) {
513 Map<String, String> propertyKeyValues = new HashMap<String, String>();
514
515 if (dataObject == null) {
516 return propertyKeyValues;
517 }
518
519
520 for (String propertyName : propertyNames) {
521 Object propertyValue = ObjectPropertyUtils.getPropertyValue(dataObject, propertyName);
522 if (propertyValue == null) {
523 propertyValue = StringUtils.EMPTY;
524 }
525
526 if (KRADServiceLocatorWeb.getDataObjectAuthorizationService()
527 .attributeValueNeedsToBeEncryptedOnFormsAndLinks(dataObject.getClass(), propertyName)) {
528 try {
529 if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
530 propertyValue = CoreApiServiceLocator.getEncryptionService().encrypt(propertyValue) +
531 EncryptionService.ENCRYPTION_POST_PREFIX;
532 }
533 } catch (GeneralSecurityException e) {
534 throw new RuntimeException("Exception while trying to encrypt value for key/value map.", e);
535 }
536 }
537
538
539 propertyKeyValues.put(propertyName, propertyValue.toString());
540 }
541
542 return propertyKeyValues;
543 }
544
545
546
547
548
549
550
551 public static Properties convertMapToProperties(Map<String, String> parameters) {
552 Properties properties = new Properties();
553
554 if (parameters != null) {
555 for (Map.Entry<String, String> parameter : parameters.entrySet()) {
556 properties.put(parameter.getKey(), parameter.getValue());
557 }
558 }
559
560 return properties;
561 }
562
563
564
565
566
567
568
569
570
571
572
573 public static Properties convertRequestMapToProperties(Map<String, String[]> requestParameters) {
574 Properties properties = new Properties();
575
576 if (requestParameters != null) {
577 for (Map.Entry<String, String[]> parameter : requestParameters.entrySet()) {
578 String[] parameterValue = parameter.getValue();
579 String parameterValueString = StringUtils.join(parameterValue, ",");
580
581 properties.put(parameter.getKey(), parameterValueString);
582 }
583 }
584
585 return properties;
586 }
587
588
589
590
591
592
593
594
595
596
597
598
599 public static boolean containsSensitiveDataPatternMatch(String fieldValue) {
600 if (StringUtils.isBlank(fieldValue)) {
601 return false;
602 }
603 ParameterService parameterService = CoreFrameworkServiceLocator.getParameterService();
604 Collection<String> sensitiveDataPatterns = parameterService.getParameterValuesAsString(
605 KRADConstants.KNS_NAMESPACE, ParameterConstants.ALL_COMPONENT,
606 KRADConstants.SystemGroupParameterNames.SENSITIVE_DATA_PATTERNS);
607 for (String pattern : sensitiveDataPatterns) {
608 if (Pattern.compile(pattern).matcher(fieldValue).find()) {
609 return true;
610 }
611 }
612 return false;
613 }
614
615
616
617
618
619
620
621
622
623
624 public static final UserSession getUserSessionFromRequest(HttpServletRequest request) {
625 return (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
626 }
627
628
629
630
631
632
633 public static boolean isProductionEnvironment() {
634 return KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
635 KRADConstants.PROD_ENVIRONMENT_CODE_KEY).equalsIgnoreCase(
636 KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
637 KRADConstants.ENVIRONMENT_KEY));
638 }
639
640
641
642
643
644
645
646
647
648
649 public static String getMessageText(ErrorMessage errorMessage, boolean processPrefixSuffix) {
650 String message = "";
651 if (errorMessage != null && errorMessage.getErrorKey() != null) {
652 MessageService messageService = KRADServiceLocatorWeb.getMessageService();
653
654
655 message = messageService.getMessageText(errorMessage.getNamespaceCode(), errorMessage.getComponentCode(),
656 errorMessage.getErrorKey());
657 if (message == null) {
658 message = "Intended message with key: " + errorMessage.getErrorKey() + " not found.";
659 }
660
661 if (errorMessage.getMessageParameters() != null && StringUtils.isNotBlank(message)) {
662 message = message.replace("'", "''");
663 message = MessageFormat.format(message, (Object[]) errorMessage.getMessageParameters());
664 }
665
666
667 if (StringUtils.isNotBlank(errorMessage.getMessagePrefixKey()) && processPrefixSuffix) {
668 String prefix = messageService.getMessageText(errorMessage.getNamespaceCode(),
669 errorMessage.getComponentCode(), errorMessage.getMessagePrefixKey());
670
671 if (errorMessage.getMessagePrefixParameters() != null && StringUtils.isNotBlank(prefix)) {
672 prefix = prefix.replace("'", "''");
673 prefix = MessageFormat.format(prefix, (Object[]) errorMessage.getMessagePrefixParameters());
674 }
675
676 if (StringUtils.isNotBlank(prefix)) {
677 message = prefix + " " + message;
678 }
679 }
680
681
682 if (StringUtils.isNotBlank(errorMessage.getMessageSuffixKey()) && processPrefixSuffix) {
683 String suffix = messageService.getMessageText(errorMessage.getNamespaceCode(),
684 errorMessage.getComponentCode(), errorMessage.getMessageSuffixKey());
685
686 if (errorMessage.getMessageSuffixParameters() != null && StringUtils.isNotBlank(suffix)) {
687 suffix = suffix.replace("'", "''");
688 suffix = MessageFormat.format(suffix, (Object[]) errorMessage.getMessageSuffixParameters());
689 }
690
691 if (StringUtils.isNotBlank(suffix)) {
692 message = message + " " + suffix;
693 }
694 }
695 }
696
697 return message;
698 }
699 }