Example usage for org.springframework.beans BeanWrapper getPropertyValue

List of usage examples for org.springframework.beans BeanWrapper getPropertyValue

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper getPropertyValue.

Prototype

@Nullable
Object getPropertyValue(String propertyName) throws BeansException;

Source Link

Document

Get the current value of the specified property.

Usage

From source file:Main.java

public static <T> List<T> sortCollection(List<T> collection, String sortCol, boolean isAsc) {
    for (int i = 0; i < collection.size(); i++) {
        for (int j = i + 1; j < collection.size(); j++) {
            BeanWrapper bwi = new BeanWrapperImpl(collection.get(i));
            BeanWrapper bwj = new BeanWrapperImpl(collection.get(j));
            int leftI = (Integer) bwi.getPropertyValue(sortCol);
            int leftJ = (Integer) bwj.getPropertyValue(sortCol);
            if (isAsc) {
                if (leftI > leftJ) {
                    T obj = collection.get(j);
                    collection.set(j, collection.get(i));
                    collection.set(i, obj);
                }// w ww  .  ja va2 s .c  o m
            } else {
                if (leftI < leftJ) {
                    T obj = collection.get(j);
                    collection.set(j, collection.get(i));
                    collection.set(i, obj);
                }
            }
        }
    }
    return collection;
}

From source file:org.jdal.util.BeanUtils.java

/**
 * Copy a property, avoid Execeptions//w  w w  .ja  v a  2s. c om
 * @param source source bean
 * @param dest destination bean
 * @param propertyName the propertyName
 * 
 */
public static void copyProperty(Object source, Object dest, String propertyName) {
    BeanWrapper wrapper = new BeanWrapperImpl(source);
    PropertyValue pv = new PropertyValue(propertyName, wrapper.getPropertyValue(propertyName));

    wrapper.setPropertyValue(pv);
}

From source file:hu.petabyte.redflags.engine.util.MappingUtils.java

public static synchronized Object getDeepPropertyIfExists(BeanWrapper wrapper, String property) {
    if (wrapper.isReadableProperty(property)) {
        return wrapper.getPropertyValue(property);
    } else {//from  w  w  w  .jav  a  2  s . c o m
        return null;
    }
}

From source file:org.lightadmin.core.config.domain.configuration.support.ExceptionAwareTransformer.java

public static Transformer<Object, String> exceptionAwareNameExtractor(
        final EntityNameExtractor<Object> entityNameExtractor,
        final DomainTypeBasicConfiguration domainTypeBasicConfiguration) {
    return new ExceptionAwareTransformer() {
        @Override/*  w  w  w .j ava2 s .  c  o m*/
        public String apply(final Object instance) {
            try {
                return entityNameExtractor.apply(instance);
            } catch (Exception ex) {
                BeanWrapper beanWrapper = new DirectFieldAccessFallbackBeanWrapper(instance);

                String domainTypeName = domainTypeBasicConfiguration.getDomainTypeName();
                Object id = beanWrapper.getPropertyValue(
                        domainTypeBasicConfiguration.getPersistentEntity().getIdProperty().getName());

                return format("%s #%s", domainTypeName, String.valueOf(id));
            }
        }
    };
}

From source file:com.aw.support.beans.BeanUtils.java

public static Object getPropertyValueNullSafe(BeanWrapper beanWrapper, String fieldName) {
    while (fieldName.indexOf('.') != -1) {
        String beanName = fieldName.substring(0, fieldName.indexOf('.'));
        fieldName = fieldName.substring(fieldName.indexOf('.') + 1);
        Object beanRef = beanWrapper.getPropertyValue(beanName);
        if (beanRef == null) {
            logger.debug("[" + fieldName + "] Bean-->Cmpt:<NULL> inner path is null");
            return null;
        }// ww  w .  ja  va  2 s .  c o m
        beanWrapper = new BeanWrapperImpl(beanRef);
    }
    Object beanValue = beanWrapper.getPropertyValue(fieldName);
    ;
    //        if (fieldName.indexOf('.') == -1 ||
    //                beanWrapper.getPropertyValue(fieldName.substring(0, fieldName.indexOf('.'))) != null) {
    //
    //            beanValue = beanWrapper.getPropertyValue(fieldName);
    //        } else {
    //            logger.debug("[" + fieldName + "] Bean-->Cmpt:<NULL> inner path is null");
    //        }
    return beanValue;
}

From source file:com.aw.support.beans.BeanUtils.java

public static int countPropertyFilled(Object bean) {
    BeanWrapper wrap = new BeanWrapperImpl(bean);
    int count = 0;
    for (PropertyDescriptor descriptor : wrap.getPropertyDescriptors()) {
        if (descriptor.getReadMethod() == null || descriptor.getWriteMethod() == null)
            continue;
        Object value = wrap.getPropertyValue(descriptor.getName());
        if (value instanceof String) {
            if (StringUtils.hasText((String) value))
                count++;/*from   ww  w  .j  ava  2 s .  co  m*/
        } else if (value != null)
            count++;
    }
    return count;
}

From source file:org.ng200.openolympus.util.Beans.java

public static <T> void copy(T from, T to) {
    final BeanWrapper src = new BeanWrapperImpl(from);
    final BeanWrapper trg = new BeanWrapperImpl(to);

    for (final String propertyName : Stream.of(src.getPropertyDescriptors()).map(pd -> pd.getName())
            .collect(Collectors.toList())) {
        if (!trg.isWritableProperty(propertyName)) {
            continue;
        }//from ww w .  j av  a 2 s .  c o  m

        trg.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
    }
}

From source file:com.allinfinance.system.util.BeanUtils.java

/**
 * null??/*from w w  w  . j  a  va2  s.c  o  m*/
* @param source
* @return
*/
public static String[] getNullPropertyNames(Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

    Set<String> emptyNames = new HashSet<String>();
    for (java.beans.PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null)
            emptyNames.add(pd.getName());
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}

From source file:org.jdal.swing.form.FormUtils.java

/**
 * Add a link on primary and dependent JComboBoxes by property name. 
 * When selection changes on primary use propertyName to get a Collection and fill dependent JComboBox with it
 * @param primary JComboBox when selection changes
 * @param dependent JComboBox that are filled with collection   
 * @param propertyName the property name for get the collection from primary selected item
 * @param addNull if true, add a null as first combobox item
 *///from  ww  w .  ja v a 2s .  co m
@SuppressWarnings("rawtypes")
public static void link(final JComboBox primary, final JComboBox dependent, final String propertyName,
        final boolean addNull) {

    primary.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Object selected = primary.getSelectedItem();
            if (selected != null) {
                BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(selected);
                if (wrapper.isWritableProperty(propertyName)) {
                    Collection collection = (Collection) wrapper.getPropertyValue(propertyName);
                    Vector<Object> vector = new Vector<Object>(collection);
                    if (addNull)
                        vector.add(0, null);
                    DefaultComboBoxModel model = new DefaultComboBoxModel(vector);
                    dependent.setModel(model);
                } else {
                    log.error("Can't write on propety '" + propertyName + "' of class: '" + selected.getClass()
                            + "'");
                }
            }
        }
    });
}

From source file:gov.nih.nci.cabig.ctms.web.WebTools.java

public static SortedMap<String, Object> requestPropertiesToMap(HttpServletRequest request) {
    BeanWrapper wrapped = new BeanWrapperImpl(request);
    SortedMap<String, Object> map = new TreeMap<String, Object>();
    for (PropertyDescriptor descriptor : wrapped.getPropertyDescriptors()) {
        String name = descriptor.getName();
        if (!EXCLUDED_REQUEST_PROPERTIES.contains(name) && descriptor.getReadMethod() != null) {
            Object propertyValue;
            try {
                propertyValue = wrapped.getPropertyValue(name);
            } catch (InvalidPropertyException e) {
                log.debug("Exception reading request property " + name, e);
                propertyValue = e.getMostSpecificCause();
            }/*from w ww  .j a v a 2 s  . c  o  m*/
            map.put(name, propertyValue);
        }
    }
    return map;
}