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