Java Code Examples for org.springframework.beans.BeanWrapperImpl#getPropertyTypeDescriptor()

The following examples show how to use org.springframework.beans.BeanWrapperImpl#getPropertyTypeDescriptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatatablesUtils.java    From gvnix with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convert a field value to string
 * 
 * @param datePatterns
 * @param dateFormatters
 * @param conversionService
 * @param entityBean
 * @param entity
 * @param fieldName
 * @param unescapedFieldName
 * @return
 */
private static <T> String convertFieldValueToString(
        Map<String, Object> datePatterns,
        Map<String, SimpleDateFormat> dateFormatters,
        ConversionService conversionService, BeanWrapperImpl entityBean,
        T entity, String fieldName, String unescapedFieldName) {
    try {
        Object value = null;
        TypeDescriptor fieldDesc = entityBean
                .getPropertyTypeDescriptor(unescapedFieldName);
        TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
        value = entityBean.getPropertyValue(unescapedFieldName);
        if (value == null) {
            return "";
        }

        // For dates
        if (Date.class.isAssignableFrom(value.getClass())
                || Calendar.class.isAssignableFrom(value.getClass())) {
            SimpleDateFormat formatter = getDateFormatter(datePatterns,
                    dateFormatters, entityBean.getWrappedClass(),
                    unescapedFieldName);
            if (formatter != null) {
                if (Calendar.class.isAssignableFrom(value.getClass())) {
                    // Gets Date instance as SimpleDateFormat
                    // doesn't works with Calendar
                    value = ((Calendar) value).getTime();
                }
                return formatter.format(value);
            }
        }
        String stringValue;
        // Try to use conversion service (uses field descrition
        // to handle field format annotations)
        if (conversionService.canConvert(fieldDesc, strDesc)) {
            stringValue = (String) conversionService.convert(value,
                    fieldDesc, strDesc);
            if (stringValue == null) {
                stringValue = "";
            }
        }
        else {
            stringValue = ObjectUtils.getDisplayString(value);
        }
        return stringValue;
    }
    catch (Exception ex) {
        LOGGER.error(String.format(
                "Error getting value of property [%s] in bean %s [%s]",
                unescapedFieldName,
                entity.getClass().getSimpleName(),
                org.apache.commons.lang3.ObjectUtils.firstNonNull(
                        entity.toString(), "{unknow}")), ex);
        return "";
    }
}
 
Example 2
Source File: DatatablesUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convert a field value to string
 * 
 * @param datePatterns
 * @param dateFormatters
 * @param conversionService
 * @param entityBean
 * @param entity
 * @param fieldName
 * @param unescapedFieldName
 * @return
 */
private <T> String convertFieldValueToString(
        Map<String, Object> datePatterns,
        Map<String, SimpleDateFormat> dateFormatters,
        BeanWrapperImpl entityBean, T entity, String fieldName,
        String unescapedFieldName) {
    try {
        Object value = null;
        TypeDescriptor fieldDesc = entityBean
                .getPropertyTypeDescriptor(unescapedFieldName);
        TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
        value = entityBean.getPropertyValue(unescapedFieldName);
        if (value == null) {
            return "";
        }

        // For dates
        if (Date.class.isAssignableFrom(value.getClass())
                || Calendar.class.isAssignableFrom(value.getClass())) {
            SimpleDateFormat formatter = getDateFormatter(datePatterns,
                    dateFormatters, entityBean.getWrappedClass(),
                    unescapedFieldName);
            if (formatter != null) {
                if (Calendar.class.isAssignableFrom(value.getClass())) {
                    // Gets Date instance as SimpleDateFormat
                    // doesn't works with Calendar
                    value = ((Calendar) value).getTime();
                }
                return formatter.format(value);
            }
        }
        String stringValue;
        // Try to use conversion service (uses field descrition
        // to handle field format annotations)
        if (conversionService.canConvert(fieldDesc, strDesc)) {
            stringValue = (String) conversionService.convert(value,
                    fieldDesc, strDesc);
            if (stringValue == null) {
                stringValue = "";
            }
        }
        else {
            stringValue = ObjectUtils.getDisplayString(value);
        }
        return stringValue;
    }
    catch (Exception ex) {
        LOGGER.error(String.format(
                "Error getting value of property [%s] in bean %s [%s]",
                unescapedFieldName,
                entity.getClass().getSimpleName(),
                org.apache.commons.lang3.ObjectUtils.firstNonNull(
                        entity.toString(), "{unknow}")), ex);
        return "";
    }
}