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