| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DataTypeUtil |
|
| 4.375;4.375 |
| 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.krad.util; | |
| 17 | ||
| 18 | import org.apache.commons.beanutils.PropertyUtils; | |
| 19 | import org.kuali.rice.krad.bo.BusinessObject; | |
| 20 | ||
| 21 | import java.beans.PropertyDescriptor; | |
| 22 | ||
| 23 | /** | |
| 24 | * Pulled this logic out of the org.kuali.rice.krad.workflow.service.impl.WorkflowAttributePropertyResolutionServiceImpl | |
| 25 | * since it wasn't really service logic at all, just util logic. | |
| 26 | * | |
| 27 | * @author James Renfro | |
| 28 | * | |
| 29 | */ | |
| 30 | 0 | public class DataTypeUtil { |
| 31 | ||
| 32 | public static String determineFieldDataType(Class<? extends BusinessObject> businessObjectClass, String attributeName) { | |
| 33 | 0 | final Class<?> attributeClass = thieveAttributeClassFromBusinessObjectClass(businessObjectClass, attributeName); |
| 34 | 0 | return determineDataType(attributeClass); |
| 35 | } | |
| 36 | ||
| 37 | public static String determineDataType(Class<?> attributeClass) { | |
| 38 | 34 | if (isStringy(attributeClass)) return KRADConstants.DATA_TYPE_STRING; // our most common case should go first |
| 39 | 0 | if (isDecimaltastic(attributeClass)) return KRADConstants.DATA_TYPE_FLOAT; |
| 40 | 0 | if (isDateLike(attributeClass)) return KRADConstants.DATA_TYPE_DATE; |
| 41 | 0 | if (isIntsy(attributeClass)) return KRADConstants.DATA_TYPE_LONG; |
| 42 | 0 | if (isBooleanable(attributeClass)) return KRADConstants.DATA_TYPE_BOOLEAN; |
| 43 | 0 | return KRADConstants.DATA_TYPE_STRING; // default to String |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * Determines if the given Class is a String | |
| 48 | * @param clazz the class to check for Stringiness | |
| 49 | * @return true if the Class is a String, false otherwise | |
| 50 | */ | |
| 51 | public static boolean isStringy(Class clazz) { | |
| 52 | 34 | return java.lang.String.class.isAssignableFrom(clazz); |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * Determines if the given class is enough like a date to store values of it as a SearchableAttributeDateTimeValue | |
| 57 | * @param clazz the class to determine the type of | |
| 58 | * @return true if it is like a date, false otherwise | |
| 59 | */ | |
| 60 | public static boolean isDateLike(Class clazz) { | |
| 61 | 0 | return java.util.Date.class.isAssignableFrom(clazz); |
| 62 | } | |
| 63 | ||
| 64 | /** | |
| 65 | * Determines if the given class is enough like a Float to store values of it as a SearchableAttributeFloatValue | |
| 66 | * @param clazz the class to determine of the type of | |
| 67 | * @return true if it is like a "float", false otherwise | |
| 68 | */ | |
| 69 | public static boolean isDecimaltastic(Class clazz) { | |
| 70 | 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.api.util.type.KualiDecimal.class.isAssignableFrom(clazz); |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * Determines if the given class is enough like a "long" to store values of it as a SearchableAttributeLongValue | |
| 75 | * @param clazz the class to determine the type of | |
| 76 | * @return true if it is like a "long", false otherwise | |
| 77 | */ | |
| 78 | public static boolean isIntsy(Class clazz) { | |
| 79 | 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); |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Determines if the given class is enough like a boolean, to index it as a String "Y" or "N" | |
| 84 | * @param clazz the class to determine the type of | |
| 85 | * @return true if it is like a boolean, false otherwise | |
| 86 | */ | |
| 87 | public static boolean isBooleanable(Class clazz) { | |
| 88 | 0 | return java.lang.Boolean.class.isAssignableFrom(clazz) || clazz.equals(Boolean.TYPE); |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * Given a BusinessObject class and an attribute name, determines the class of that attribute on the BusinessObject class | |
| 93 | * @param boClass a class extending BusinessObject | |
| 94 | * @param attributeKey the name of a field on that class | |
| 95 | * @return the Class of the given attribute | |
| 96 | */ | |
| 97 | private static Class thieveAttributeClassFromBusinessObjectClass(Class<? extends BusinessObject> boClass, String attributeKey) { | |
| 98 | 0 | for (PropertyDescriptor prop : PropertyUtils.getPropertyDescriptors(boClass)) { |
| 99 | 0 | if (prop.getName().equals(attributeKey)) { |
| 100 | 0 | return prop.getPropertyType(); |
| 101 | } | |
| 102 | } | |
| 103 | 0 | return null; |
| 104 | } | |
| 105 | ||
| 106 | } |