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