Example usage for org.springframework.beans BeanWrapperImpl BeanWrapperImpl

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

Introduction

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

Prototype

public BeanWrapperImpl(Class<?> clazz) 

Source Link

Document

Create a new BeanWrapperImpl, wrapping a new instance of the specified class.

Usage

From source file:net.sourceforge.vulcan.spring.SpringBeanXmlEncoder.java

void encodeBean(Element root, String beanName, Object bean) {
    final BeanWrapper bw = new BeanWrapperImpl(bean);

    final PropertyDescriptor[] pds = bw.getPropertyDescriptors();
    final Element beanNode = new Element("bean");

    root.addContent(beanNode);/* ww  w .  j  a  v  a 2s .c om*/

    if (beanName != null) {
        beanNode.setAttribute("name", beanName);
    }

    if (factoryExpert.needsFactory(bean)) {
        encodeBeanByFactory(beanNode, bean);
    } else {
        beanNode.setAttribute("class", bean.getClass().getName());
    }

    for (PropertyDescriptor pd : pds) {
        if (pd.getWriteMethod() == null)
            continue;

        final Method readMethod = pd.getReadMethod();

        if (readMethod == null)
            continue;

        if (readMethod.getAnnotation(Transient.class) != null) {
            continue;
        }
        final String name = pd.getName();

        final Object value = bw.getPropertyValue(name);
        if (value == null)
            continue;

        final Element propertyNode = new Element("property");
        propertyNode.setAttribute("name", name);
        beanNode.addContent(propertyNode);

        encodeObject(propertyNode, value);
    }
}

From source file:com.smhdemo.common.datasource.generate.factory.custom.CustomDataSourceFactory.java

@SuppressWarnings("rawtypes")
@Override//  w  w  w  .j  a  v  a2 s .co  m
public DataSourceServiceable createService(Base alqcDataSource) {
    if (!(alqcDataSource instanceof Custom)) {
        logger.error("Custom??");
        throw new BaseRuntimeException("Custom??", new Object[] { alqcDataSource.getClass() });
    }
    Custom customDataSource = (Custom) alqcDataSource;

    // ???
    String serviceClassName = customDataSource.getServiceClass();
    DataSourceServiceable service;
    try {
        Class serviceClass = Class.forName(serviceClassName);
        service = (DataSourceServiceable) serviceClass.newInstance();
    } catch (Exception e) {
        logger.error("?Custom??", e);
        BaseRuntimeException ex = new BaseRuntimeException("?Custom??", e);
        ex.setArgs(new Object[] { serviceClassName });
        throw ex;
    }
    try {
        // Spring??
        BeanWrapperImpl bw = new BeanWrapperImpl(service);
        // CustomDataSourceDefinition
        CustomDataSourceDefinition def = getDefinitionByServiceClass(serviceClassName);
        // "propertyMap"?
        Map<String, Object> propMap = new HashMap<String, Object>();
        // ?
        Iterator pdi = def.getPropertyDefinitions().iterator();
        while (pdi.hasNext()) {
            Map pd = (Map) pdi.next();
            String name = (String) pd.get(CustomDataSourceDefinition.PARAM_NAME);
            Object deflt = pd.get(CustomDataSourceDefinition.PARAM_DEFAULT);
            Object value = customDataSource.getPropertyMap().get(name);
            if (value == null && deflt != null) {
                value = deflt;
            }
            // ?
            if (value != null) {
                if (bw.isWritableProperty(name)) {
                    bw.setPropertyValue(name, value);
                }
                propMap.put(name, value);
            }
        }
        if (bw.isWritableProperty(PROPERTY_MAP)) {
            bw.setPropertyValue(PROPERTY_MAP, propMap);
        }

    } catch (Exception e) {
        logger.error("Custom??", e);
        BaseRuntimeException ex = new BaseRuntimeException("Custom??", e);
        ex.setArgs(new Object[] { serviceClassName });
        throw ex;
    }

    return service;
}

From source file:com.sshdemo.common.extendds.generate.factory.custom.CustomDataSourceFactory.java

@SuppressWarnings("rawtypes")
@Override//from  w  w  w. jav  a2s .  c o  m
public EwcmsDataSourceServiceable createService(BaseDS alqcDataSource) {
    if (!(alqcDataSource instanceof CustomDS)) {
        logger.error("Custom??");
        throw new BaseRuntimeException("Custom??", new Object[] { alqcDataSource.getClass() });
    }
    CustomDS customDataSource = (CustomDS) alqcDataSource;

    // ???
    String serviceClassName = customDataSource.getServiceClass();
    EwcmsDataSourceServiceable service;
    try {
        Class serviceClass = Class.forName(serviceClassName);
        service = (EwcmsDataSourceServiceable) serviceClass.newInstance();
    } catch (Exception e) {
        logger.error("?Custom??", e);
        BaseRuntimeException ex = new BaseRuntimeException("?Custom??", e);
        ex.setArgs(new Object[] { serviceClassName });
        throw ex;
    }
    try {
        // Spring??
        BeanWrapperImpl bw = new BeanWrapperImpl(service);
        // CustomDataSourceDefinition
        CustomDataSourceDefinition def = getDefinitionByServiceClass(serviceClassName);
        // "propertyMap"?
        Map<String, Object> propMap = new HashMap<String, Object>();
        // ?
        Iterator pdi = def.getPropertyDefinitions().iterator();
        while (pdi.hasNext()) {
            Map pd = (Map) pdi.next();
            String name = (String) pd.get(CustomDataSourceDefinition.PARAM_NAME);
            Object deflt = pd.get(CustomDataSourceDefinition.PARAM_DEFAULT);
            Object value = customDataSource.getPropertyMap().get(name);
            if (value == null && deflt != null) {
                value = deflt;
            }
            // ?
            if (value != null) {
                if (bw.isWritableProperty(name)) {
                    bw.setPropertyValue(name, value);
                }
                propMap.put(name, value);
            }
        }
        if (bw.isWritableProperty(PROPERTY_MAP)) {
            bw.setPropertyValue(PROPERTY_MAP, propMap);
        }

    } catch (Exception e) {
        logger.error("Custom??", e);
        BaseRuntimeException ex = new BaseRuntimeException("Custom??", e);
        ex.setArgs(new Object[] { serviceClassName });
        throw ex;
    }

    return service;
}

From source file:com.aw.swing.mvp.grid.GridInfoProvider.java

/**
 * Get Object that will be used to get the data to be shown
 *
 * @return/* w  w  w . j  a  va2  s.  co m*/
 */
public ListProvider getListProvider(final Presenter presenter) {
    final GridInfoProvider gridInfoProvider = this;
    return new ListProvider() {
        public List getList(Object param) {
            List list = getValues((E) param);
            if (list == null) {
                list = new ArrayList();
            }
            if (!(presenter instanceof FindPresenter)) {
                Method method = null;
                try {
                    method = gridInfoProvider.getClass().getMethod("getValues", param.getClass());
                } catch (NoSuchMethodException e) {
                }
                if (method == null) {
                    method = ReflectionUtils.findMethodByName(gridInfoProvider.getClass(), "getValues");
                }
                DeleteableListField deleteableListField = AnnotationUtils.getAnnotation(method,
                        DeleteableListField.class);
                if (deleteableListField != null) {
                    if (!(list instanceof DeleteableList)) {
                        DeleteableList deleteableList = new DeleteableList();
                        deleteableList.init(list);
                        BeanWrapper bw = new BeanWrapperImpl(param);
                        bw.setPropertyValue(deleteableListField.value(), deleteableList);
                        return deleteableList;
                    }
                }
            }
            return list;
        }
    };
}

From source file:org.wallride.web.controller.admin.user.UserDescribeController.java

@RequestMapping
public String describe(@PathVariable String language, @RequestParam long id, String query, Model model) {
    User user = userService.getUserById(id);
    if (user == null) {
        throw new HttpNotFoundException();
    }/*from   www. ja v  a2  s  .c  o  m*/

    MutablePropertyValues mpvs = new MutablePropertyValues(
            UriComponentsBuilder.newInstance().query(query).build().getQueryParams());
    for (Iterator<PropertyValue> i = mpvs.getPropertyValueList().iterator(); i.hasNext();) {
        PropertyValue pv = i.next();
        boolean hasValue = false;
        for (String value : (List<String>) pv.getValue()) {
            if (StringUtils.hasText(value)) {
                hasValue = true;
                break;
            }
        }
        if (!hasValue) {
            i.remove();
        }
    }
    BeanWrapperImpl beanWrapper = new BeanWrapperImpl(new UserSearchForm());
    beanWrapper.setConversionService(conversionService);
    beanWrapper.setPropertyValues(mpvs, true, true);
    UserSearchForm form = (UserSearchForm) beanWrapper.getWrappedInstance();
    List<Long> ids = userService.getUserIds(form.toUserSearchRequest());
    if (!CollectionUtils.isEmpty(ids)) {
        int index = ids.indexOf(user.getId());
        if (index < ids.size() - 1) {
            Long next = ids.get(index + 1);
            model.addAttribute("next", next);
        }
        if (index > 0) {
            Long prev = ids.get(index - 1);
            model.addAttribute("prev", prev);
        }
    }

    model.addAttribute("user", user);
    model.addAttribute("query", query);
    return "user/describe";
}

From source file:com.senacor.core.manager.BaseManagerImpl.java

public List<T> find(final T t) {
    Session session = getSession();//from  ww w.  j a  v  a2  s.  com
    Criteria criteria = session.createCriteria(boClass);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(boClass);
    BeanWrapper beanWrapper = new BeanWrapperImpl(t);
    for (PropertyDescriptor descriptor : descriptors) {
        if (!descriptor.getName().equals("class") && !descriptor.getName().equals("id")) {
            Object value = beanWrapper.getPropertyValue(descriptor.getName());
            if (value != null) {
                criteria.add(Restrictions.eq(descriptor.getName(), value));
            }
        }
    }
    criteria.addOrder(Order.asc("id"));
    return criteria.list();
}

From source file:net.solarnetwork.node.util.ClassUtils.java

/**
 * Set bean property values on an object from a Map.
 * /*  w w  w  .ja  va  2 s .  c  o m*/
 * @param o
 *        the bean to set JavaBean properties on
 * @param values
 *        a Map of JavaBean property names and their corresponding values to
 *        set
 */
public static void setBeanProperties(Object o, Map<String, ?> values) {
    BeanWrapper bean = new BeanWrapperImpl(o);
    bean.setPropertyValues(values);
}

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

/**
 * Generates insert query for the specified object
 * @param object the object to generate insert query for
 * @return the insert SQL query//w w  w.ja  v a  2 s.  com
 */
public String generateInsertQuery(Object object) {
    BeanWrapper wr = new BeanWrapperImpl(object);
    String objectName = Convert.toSimpleName(object.getClass().getName());
    String tableName = Convert.toDB(objectName);
    PropertyDescriptor[] pdArr = wr.getPropertyDescriptors();

    //generate insert query
    StringBuffer query = new StringBuffer("insert into " + tableName + "(");
    StringBuffer values = new StringBuffer(" values(");
    for (int i = 0; i < pdArr.length; i++) {
        Class fieldClass = pdArr[i].getPropertyType(); //field class
        String columnName = Convert.toDB(pdArr[i].getName()); //column name
        String fieldName = pdArr[i].getName(); //field name
        //if (fieldName.equals("id")) continue; //remove if it does not break the sequence
        if (dbClasses.contains(fieldClass)) { //direct database field (Integer,String,Date, etc)
            query.append(columnName.equals("order") ? "\"order\"" : columnName);
            query.append(",");
            values.append(":").append(fieldName);
            values.append(",");
        }
        if (fieldClass.getPackage() != null && domainPackages.contains(fieldClass.getPackage().getName())) { //object
            query.append(Convert.toDB(fieldName)).append("_id");
            query.append(",");
            values.append(":").append(fieldName).append(".id");
            values.append(",");
        }
    }
    query.deleteCharAt(query.length() - 1);
    query.append(")");
    values.deleteCharAt(values.length() - 1);
    values.append(");");
    query.append(values);

    return query.toString();
}

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

/**
 * Get property value null if none/* w w  w. ja  v  a 2  s .c om*/
 * @param bean beam
 * @param name name
 * @return the property value
 */
public static Object getProperty(Object bean, String name) {
    try {
        BeanWrapper wrapper = new BeanWrapperImpl(bean);
        return wrapper.getPropertyValue(name);
    } catch (BeansException be) {
        log.error(be);
        return null;
    }
}

From source file:org.oncoblocks.centromere.core.test.TestRepository.java

@Override
public Iterable<Object> distinct(String field) {
    Set<Object> values = new HashSet<>();
    for (EntrezGene gene : geneMap.values()) {
        BeanWrapper wrapper = new BeanWrapperImpl(gene);
        if (wrapper.getPropertyValue(field) != null) {
            values.add(wrapper.getPropertyValue(field));
        }//ww w  . java  2  s  . co  m
    }
    return values;
}