Example usage for org.springframework.beans BeanWrapperImpl getPropertyTypeDescriptor

List of usage examples for org.springframework.beans BeanWrapperImpl getPropertyTypeDescriptor

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapperImpl getPropertyTypeDescriptor.

Prototype

@Override
    @Nullable
    public TypeDescriptor getPropertyTypeDescriptor(String propertyName) throws BeansException 

Source Link

Usage

From source file:org.gvnix.web.datatables.util.DatatablesUtils.java

/**
 * Convert a field value to string/*from w  ww.j  a  v a  2 s . c o  m*/
 * 
 * @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 "";
    }
}

From source file:org.gvnix.web.datatables.util.impl.DatatablesUtilsBeanImpl.java

/**
 * Convert a field value to string/*  ww w  . jav  a  2  s . c  o m*/
 * 
 * @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 "";
    }
}