Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DataTypeUtil |
|
| 4.875;4.875 |
1 | /* | |
2 | * Copyright 2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 1.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl1.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.kns.util; | |
17 | ||
18 | import java.beans.BeanInfo; | |
19 | import java.beans.IntrospectionException; | |
20 | import java.beans.Introspector; | |
21 | import java.beans.PropertyDescriptor; | |
22 | ||
23 | import org.kuali.rice.kns.bo.BusinessObject; | |
24 | ||
25 | /** | |
26 | * Pulled this logic out of the org.kuali.rice.kns.workflow.service.impl.WorkflowAttributePropertyResolutionServiceImpl | |
27 | * since it wasn't really service logic at all, just util logic. | |
28 | * | |
29 | * @author James Renfro | |
30 | * | |
31 | */ | |
32 | 0 | public class DataTypeUtil { |
33 | ||
34 | public static String determineFieldDataType(Class<? extends BusinessObject> businessObjectClass, String attributeName) { | |
35 | 0 | final Class<?> attributeClass = thieveAttributeClassFromBusinessObjectClass(businessObjectClass, attributeName); |
36 | 0 | return determineDataType(attributeClass); |
37 | } | |
38 | ||
39 | public static String determineDataType(Class<?> attributeClass) { | |
40 | 0 | if (isStringy(attributeClass)) return KNSConstants.DATA_TYPE_STRING; // our most common case should go first |
41 | 0 | if (isDecimaltastic(attributeClass)) return KNSConstants.DATA_TYPE_FLOAT; |
42 | 0 | if (isDateLike(attributeClass)) return KNSConstants.DATA_TYPE_DATE; |
43 | 0 | if (isIntsy(attributeClass)) return KNSConstants.DATA_TYPE_LONG; |
44 | 0 | if (isBooleanable(attributeClass)) return KNSConstants.DATA_TYPE_BOOLEAN; |
45 | 0 | return KNSConstants.DATA_TYPE_STRING; // default to String |
46 | } | |
47 | ||
48 | /** | |
49 | * Determines if the given Class is a String | |
50 | * @param clazz the class to check for Stringiness | |
51 | * @return true if the Class is a String, false otherwise | |
52 | */ | |
53 | public static boolean isStringy(Class clazz) { | |
54 | 0 | return java.lang.String.class.isAssignableFrom(clazz); |
55 | } | |
56 | ||
57 | /** | |
58 | * Determines if the given class is enough like a date to store values of it as a SearchableAttributeDateTimeValue | |
59 | * @param class the class to determine the type of | |
60 | * @return true if it is like a date, false otherwise | |
61 | */ | |
62 | public static boolean isDateLike(Class clazz) { | |
63 | 0 | return java.util.Date.class.isAssignableFrom(clazz); |
64 | } | |
65 | ||
66 | /** | |
67 | * Determines if the given class is enough like a Float to store values of it as a SearchableAttributeFloatValue | |
68 | * @param value the class to determine of the type of | |
69 | * @return true if it is like a "float", false otherwise | |
70 | */ | |
71 | public static boolean isDecimaltastic(Class clazz) { | |
72 | 0 | return java.lang.Double.class.isAssignableFrom(clazz) || java.lang.Float.class.isAssignableFrom(clazz) || clazz.equals(Double.TYPE) || clazz.equals(Float.TYPE) || java.math.BigDecimal.class.isAssignableFrom(clazz) || org.kuali.rice.core.util.type.KualiDecimal.class.isAssignableFrom(clazz); |
73 | } | |
74 | ||
75 | /** | |
76 | * Determines if the given class is enough like a "long" to store values of it as a SearchableAttributeLongValue | |
77 | * @param value the class to determine the type of | |
78 | * @return true if it is like a "long", false otherwise | |
79 | */ | |
80 | public static boolean isIntsy(Class clazz) { | |
81 | 0 | return java.lang.Integer.class.isAssignableFrom(clazz) || java.lang.Long.class.isAssignableFrom(clazz) || java.lang.Short.class.isAssignableFrom(clazz) || java.lang.Byte.class.isAssignableFrom(clazz) || java.math.BigInteger.class.isAssignableFrom(clazz) || clazz.equals(Integer.TYPE) || clazz.equals(Long.TYPE) || clazz.equals(Short.TYPE) || clazz.equals(Byte.TYPE); |
82 | } | |
83 | ||
84 | /** | |
85 | * Determines if the given class is enough like a boolean, to index it as a String "Y" or "N" | |
86 | * @param value the class to determine the type of | |
87 | * @return true if it is like a boolean, false otherwise | |
88 | */ | |
89 | public static boolean isBooleanable(Class clazz) { | |
90 | 0 | return java.lang.Boolean.class.isAssignableFrom(clazz) || clazz.equals(Boolean.TYPE); |
91 | } | |
92 | ||
93 | /** | |
94 | * Given a BusinessObject class and an attribute name, determines the class of that attribute on the BusinessObject class | |
95 | * @param boClass a class extending BusinessObject | |
96 | * @param attributeKey the name of a field on that class | |
97 | * @return the Class of the given attribute | |
98 | */ | |
99 | private static Class thieveAttributeClassFromBusinessObjectClass(Class<? extends BusinessObject> boClass, String attributeKey) { | |
100 | 0 | Class attributeFieldClass = null; |
101 | try { | |
102 | 0 | final BeanInfo beanInfo = Introspector.getBeanInfo(boClass); |
103 | 0 | int i = 0; |
104 | 0 | while (attributeFieldClass == null && i < beanInfo.getPropertyDescriptors().length) { |
105 | 0 | final PropertyDescriptor prop = beanInfo.getPropertyDescriptors()[i]; |
106 | 0 | if (prop.getName().equals(attributeKey)) { |
107 | 0 | attributeFieldClass = prop.getPropertyType(); |
108 | } | |
109 | 0 | i += 1; |
110 | 0 | } |
111 | } | |
112 | 0 | catch (SecurityException se) { |
113 | 0 | throw new RuntimeException("Could not determine type of attribute "+attributeKey+" of BusinessObject class "+boClass.getName(), se); |
114 | } | |
115 | 0 | catch (IntrospectionException ie) { |
116 | 0 | throw new RuntimeException("Could not determine type of attribute "+attributeKey+" of BusinessObject class "+boClass.getName(), ie); |
117 | 0 | } |
118 | 0 | return attributeFieldClass; |
119 | } | |
120 | ||
121 | } |