Example usage for org.springframework.beans BeanWrapperImpl getPropertyDescriptors

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

Introduction

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

Prototype

@Override
    public PropertyDescriptor[] getPropertyDescriptors() 

Source Link

Usage

From source file:org.wallride.web.support.ControllerUtils.java

public static MultiValueMap<String, String> convertBeanForQueryParams(Object target,
        ConversionService conversionService) {
    BeanWrapperImpl beanWrapper = new BeanWrapperImpl(target);
    beanWrapper.setConversionService(conversionService);
    MultiValueMap<String, String> queryParams = new LinkedMultiValueMap();
    for (PropertyDescriptor pd : beanWrapper.getPropertyDescriptors()) {
        if (beanWrapper.isWritableProperty(pd.getName())) {
            Object pv = beanWrapper.getPropertyValue(pd.getName());
            if (pv != null) {
                if (pv instanceof Collection) {
                    if (!CollectionUtils.isEmpty((Collection) pv)) {
                        for (Object element : (Collection) pv) {
                            queryParams.set(pd.getName(), convertPropertyValueForString(target, pd, element));
                        }/*from   ww  w  .  j  a  v  a  2 s.  c  o  m*/
                    }
                } else {
                    queryParams.set(pd.getName(), convertPropertyValueForString(target, pd, pv));
                }
            }
        }
    }
    return queryParams;
}

From source file:com.frank.search.solr.server.support.SolrClientUtils.java

/**
 * Solr property names do not match the getters/setters used for them. Check
 * on any write method, try to find the according property and set the value
 * for it. Will ignore all other, and nested properties
 * // w  w w  .  ja v a2 s  .  c  om
 * @param source
 * @param target
 */
private static void copyProperties(SolrClient source, SolrClient target) {
    BeanWrapperImpl wrapperImpl = new BeanWrapperImpl(source);
    for (PropertyDescriptor pd : wrapperImpl.getPropertyDescriptors()) {
        Method writer = pd.getWriteMethod();
        if (writer != null) {
            try {
                Field property = ReflectionUtils.findField(source.getClass(), pd.getName());
                if (property != null) {
                    ReflectionUtils.makeAccessible(property);
                    Object o = ReflectionUtils.getField(property, source);
                    if (o != null) {
                        writer.invoke(target, o);
                    }
                }
            } catch (Exception e) {
                logger.warn("Could not copy property value for: " + pd.getName(), e);
            }
        }
    }
}

From source file:io.spring.initializr.web.project.AbstractInitializrController.java

/**
 * Render the home page with the specified template.
 * @param model the model data//from   w w  w.  ja va  2 s.  c o  m
 */
protected void renderHome(Map<String, Object> model) {
    InitializrMetadata metadata = this.metadataProvider.get();

    model.put("serviceUrl", generateAppUrl());
    BeanWrapperImpl wrapper = new BeanWrapperImpl(metadata);
    for (PropertyDescriptor descriptor : wrapper.getPropertyDescriptors()) {
        if ("types".equals(descriptor.getName())) {
            model.put("types", removeTypes(metadata.getTypes()));
        } else {
            model.put(descriptor.getName(), wrapper.getPropertyValue(descriptor.getName()));
        }
    }

    // Google analytics support
    model.put("trackingCode", metadata.getConfiguration().getEnv().getGoogleAnalyticsTrackingCode());

}

From source file:org.archive.spring.ConfigPathConfigurer.java

/**
 * Find any ConfigPath properties in the passed bean; ensure that
 * if they have a null 'base', that is replaced with the job home
 * directory. Also, remember all ConfigPaths so fixed-up for later
 * reference. //from   w  ww. jav a2  s.c  om
 * 
 * @param bean
 * @param beanName
 * @return Same bean as passed in, fixed as necessary
 */
protected Object fixupPaths(Object bean, String beanName) {
    BeanWrapperImpl wrapper = new BeanWrapperImpl(bean);
    for (PropertyDescriptor d : wrapper.getPropertyDescriptors()) {
        if (d.getPropertyType().isAssignableFrom(ConfigPath.class)
                || d.getPropertyType().isAssignableFrom(ConfigFile.class)) {
            Object value = wrapper.getPropertyValue(d.getName());
            if (value != null && value instanceof ConfigPath) {
                String patchName = beanName + "." + d.getName();
                fixupConfigPath((ConfigPath) value, patchName);
            }
        } else if (Iterable.class.isAssignableFrom(d.getPropertyType())) {
            Iterable<?> iterable = (Iterable<?>) wrapper.getPropertyValue(d.getName());
            if (iterable != null) {
                int i = 0;
                for (Object candidate : iterable) {
                    if (candidate != null && candidate instanceof ConfigPath) {
                        String patchName = beanName + "." + d.getName() + "[" + i + "]";
                        fixupConfigPath((ConfigPath) candidate, patchName);
                    }
                    i++;
                }
            }
        }
    }
    return bean;
}

From source file:io.spring.initializr.generator.CommandLineHelpGenerator.java

protected Map<String, Object> buildParametersDescription(InitializrMetadata metadata) {
    Map<String, Object> result = new LinkedHashMap<>();
    BeanWrapperImpl wrapper = new BeanWrapperImpl(metadata);
    for (PropertyDescriptor descriptor : wrapper.getPropertyDescriptors()) {
        Object value = wrapper.getPropertyValue(descriptor.getName());
        BeanWrapperImpl nested = new BeanWrapperImpl(value);
        if (nested.isReadableProperty("description") && nested.isReadableProperty("id")) {
            result.put((String) nested.getPropertyValue("id"), nested.getPropertyValue("description"));
        }//  w w  w .j a v  a 2 s .c  o m
    }
    result.put("applicationName", "application name");
    result.put("baseDir", "base directory to create in the archive");
    return result;
}

From source file:org.archive.crawler.restlet.JobRelatedResource.java

/**
 * Get and modify the PropertyDescriptors associated with the BeanWrapper.
 * @param bwrap/*ww  w.java  2s.  c o m*/
 * @return
 */
protected PropertyDescriptor[] getPropertyDescriptors(BeanWrapperImpl bwrap) {
    PropertyDescriptor[] descriptors = bwrap.getPropertyDescriptors();
    for (PropertyDescriptor pd : descriptors) {
        if (DescriptorUpdater.class.isAssignableFrom(bwrap.getWrappedClass())) {
            ((DescriptorUpdater) bwrap.getWrappedInstance()).updateDescriptor(pd);
        } else {
            defaultUpdateDescriptor(pd);
        }
    }
    return descriptors;
}

From source file:com.fmguler.ven.QueryMapper.java

protected void mapRecursively(ResultSet rs, Set columns, String tableName, Class objectClass, List parentList) {
    try {/*from   w  ww  .j  a  va  2 s .co m*/
        if (!columns.contains(tableName + "_id"))
            return; //this object does not exist in the columns
        Object id = rs.getObject(tableName + "_id");
        if (id == null)
            return; //this object exists in the columns but null, probably because of left join

        //create bean wrapper for the object class
        BeanWrapperImpl wr = new BeanWrapperImpl(objectClass); //already caches class introspection (CachedIntrospectionResults.forClass())
        wr.setPropertyValue("id", id); //set the id property
        Object object = wr.getWrappedInstance();
        boolean map = true;

        //check if this object exists in the parent list (since SQL joins are cartesian products, do not create new object if this row is just the same as previous)
        for (Iterator it = parentList.iterator(); it.hasNext();) {
            Object objectInList = (Object) it.next();
            if (objectIdEquals(objectInList, id)) {
                wr.setWrappedInstance(objectInList); //already exists in the list, use that instance
                map = false; // and do not map again
                break;
            }
        }
        if (map)
            parentList.add(object); //could not find in the parent list, add the new object

        PropertyDescriptor[] pdArr = wr.getPropertyDescriptors();
        for (int i = 0; i < pdArr.length; i++) {
            PropertyDescriptor pd = pdArr[i];
            Class fieldClass = pd.getPropertyType(); //field class
            String fieldName = Convert.toDB(pd.getName()); //field name
            Object fieldValue = wr.getPropertyValue(pd.getName());
            String columnName = tableName + "_" + fieldName;

            //database class (primitive property)
            if (map && dbClasses.contains(fieldClass)) {
                if (columns.contains(columnName)) {
                    if (debug)
                        System.out.println(">>field is found: " + columnName);
                    wr.setPropertyValue(pd.getName(), rs.getObject(columnName));
                } else {
                    if (debug)
                        System.out.println("--field not found: " + columnName);
                }
                continue; //if this is a primitive property, it cannot be an object or list
            }

            //many to one association (object property)
            if (fieldClass.getPackage() != null && domainPackages.contains(fieldClass.getPackage().getName())) {
                if (columns.contains(columnName + "_id")) {
                    if (debug)
                        System.out.println(">>object is found " + columnName);
                    List list = new ArrayList(1); //we know there will be single result
                    if (!map)
                        list.add(fieldValue); //otherwise we cannot catch one to many assc. (lists) of many to one (object) assc.
                    mapRecursively(rs, columns, columnName, fieldClass, list);
                    if (list.size() > 0)
                        wr.setPropertyValue(pd.getName(), list.get(0));
                } else {
                    if (debug)
                        System.out.println("--object not found: " + columnName);
                }
            }

            //one to many association (list property)
            if (fieldValue instanceof List) { //Note: here recurring row's list property is mapped and add to parent's list
                if (columns.contains(columnName + "_id")) {
                    Class elementClass = VenList.findElementClass((List) fieldValue);
                    if (debug)
                        System.out.println(">>list is found " + columnName);
                    mapRecursively(rs, columns, columnName, elementClass, (List) fieldValue);
                } else {
                    if (debug)
                        System.out.println("--list not found: " + columnName);
                }
            }
        }
    } catch (Exception ex) {
        System.out.println("Ven - error while mapping row, table: " + tableName + " object class: "
                + objectClass + " error: " + ex.getMessage());
        if (debug) {
            ex.printStackTrace();
        }
    }
}