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