001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.util;
017
018import org.apache.commons.beanutils.PropertyUtils;
019import org.kuali.rice.krad.bo.BusinessObject;
020
021import java.beans.PropertyDescriptor;
022
023/**
024 * Pulled this logic out of the org.kuali.rice.krad.workflow.service.impl.WorkflowAttributePropertyResolutionServiceImpl
025 * since it wasn't really service logic at all, just util logic.
026 * 
027 * @author James Renfro
028 *
029 */
030public class DataTypeUtil {
031
032        public static String determineFieldDataType(Class<? extends BusinessObject> businessObjectClass, String attributeName) {
033                final Class<?> attributeClass = thieveAttributeClassFromBusinessObjectClass(businessObjectClass, attributeName);
034                return determineDataType(attributeClass);
035        }
036        
037        public static String determineDataType(Class<?> attributeClass) {
038        if (isStringy(attributeClass)) return KRADConstants.DATA_TYPE_STRING; // our most common case should go first
039        if (isDecimaltastic(attributeClass)) return KRADConstants.DATA_TYPE_FLOAT;
040        if (isDateLike(attributeClass)) return KRADConstants.DATA_TYPE_DATE;
041        if (isIntsy(attributeClass)) return KRADConstants.DATA_TYPE_LONG;
042        if (isBooleanable(attributeClass)) return KRADConstants.DATA_TYPE_BOOLEAN;
043        return KRADConstants.DATA_TYPE_STRING; // default to String
044    }
045        
046    /**
047     * Determines if the given Class is a String
048     * @param clazz the class to check for Stringiness
049     * @return true if the Class is a String, false otherwise
050     */
051        public static boolean isStringy(Class clazz) {
052        return java.lang.String.class.isAssignableFrom(clazz);
053    }
054
055    /**
056     * Determines if the given class is enough like a date to store values of it as a SearchableAttributeDateTimeValue
057     * @param clazz the class to determine the type of
058     * @return true if it is like a date, false otherwise
059     */
060        public static boolean isDateLike(Class clazz) {
061        return java.util.Date.class.isAssignableFrom(clazz);
062    }
063    
064    /**
065     * Determines if the given class is enough like a Float to store values of it as a SearchableAttributeFloatValue
066     * @param clazz the class to determine of the type of
067     * @return true if it is like a "float", false otherwise
068     */
069        public static boolean isDecimaltastic(Class clazz) {
070        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);
071    }
072    
073    /**
074     * Determines if the given class is enough like a "long" to store values of it as a SearchableAttributeLongValue
075     * @param clazz the class to determine the type of
076     * @return true if it is like a "long", false otherwise
077     */
078        public static boolean isIntsy(Class clazz) {
079        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);
080    }
081
082    /**
083     * Determines if the given class is enough like a boolean, to index it as a String "Y" or "N"
084     * @param clazz the class to determine the type of
085     * @return true if it is like a boolean, false otherwise
086     */
087        public static boolean isBooleanable(Class clazz) {
088        return java.lang.Boolean.class.isAssignableFrom(clazz) || clazz.equals(Boolean.TYPE);
089    }
090    
091    /**
092     * Given a BusinessObject class and an attribute name, determines the class of that attribute on the BusinessObject class
093     * @param boClass a class extending BusinessObject
094     * @param attributeKey the name of a field on that class
095     * @return the Class of the given attribute
096     */
097    private static Class thieveAttributeClassFromBusinessObjectClass(Class<? extends BusinessObject> boClass, String attributeKey) {
098        for (PropertyDescriptor prop : PropertyUtils.getPropertyDescriptors(boClass)) {
099            if (prop.getName().equals(attributeKey)) {
100                return prop.getPropertyType();
101            }
102        }
103        return null;
104    }
105        
106}