Example usage for org.springframework.beans BeanWrapperImpl getWrappedInstance

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

Introduction

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

Prototype

public final Object getWrappedInstance() 

Source Link

Usage

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  w  w w  .  j  ava  2 s .  com

    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:org.wallride.web.controller.admin.page.PageDescribeController.java

@RequestMapping
public String describe(@PathVariable String language, @RequestParam long id, String query, Model model,
        RedirectAttributes redirectAttributes) {
    Page page = pageService.getPageById(id);
    if (page == null) {
        throw new HttpNotFoundException();
    }//w ww  .j  a  va  2 s.  c o  m

    if (!page.getLanguage().equals(language)) {
        Page target = pageService.getPageByCode(page.getCode(), language);
        if (target != null) {
            redirectAttributes.addAttribute("id", target.getId());
            return "redirect:/_admin/{language}/pages/describe?id={id}";
        } else {
            redirectAttributes.addFlashAttribute("original", page);
            redirectAttributes.addAttribute("code", page.getCode());
            return "redirect:/_admin/{language}/pages/create?code={code}";
        }
    }

    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 PageSearchForm());
    beanWrapper.setConversionService(conversionService);
    beanWrapper.setPropertyValues(mpvs, true, true);
    PageSearchForm form = (PageSearchForm) beanWrapper.getWrappedInstance();
    List<Long> ids = pageService.getPageIds(form.toPageSearchRequest());
    if (!CollectionUtils.isEmpty(ids)) {
        int index = ids.indexOf(page.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("page", page);
    model.addAttribute("query", query);
    return "page/describe";
}

From source file:org.wallride.web.controller.admin.article.ArticleDescribeController.java

@RequestMapping
public String describe(@PathVariable String language, @RequestParam long id, String query, Model model,
        RedirectAttributes redirectAttributes) {
    Article article = articleService.getArticleById(id);
    if (article == null) {
        throw new HttpNotFoundException();
    }//from   w w  w .ja  v  a 2s  .c  o  m

    if (!article.getLanguage().equals(language)) {
        Article target = articleService.getArticleByCode(article.getCode(), language);
        if (target != null) {
            redirectAttributes.addAttribute("id", target.getId());
            return "redirect:/_admin/{language}/articles/describe?id={id}";
        } else {
            redirectAttributes.addFlashAttribute("original", article);
            redirectAttributes.addAttribute("code", article.getCode());
            return "redirect:/_admin/{language}/articles/create?code={code}";
        }
    }

    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 ArticleSearchForm());
    beanWrapper.setConversionService(conversionService);
    beanWrapper.setPropertyValues(mpvs, true, true);
    ArticleSearchForm form = (ArticleSearchForm) beanWrapper.getWrappedInstance();
    List<Long> ids = articleService.getArticleIds(form.toArticleSearchRequest());
    if (!CollectionUtils.isEmpty(ids)) {
        int index = ids.indexOf(article.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("article", article);
    model.addAttribute("query", query);
    return "article/describe";
}

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

protected void mapRecursively(ResultSet rs, Set columns, String tableName, Class objectClass, List parentList) {
    try {// www .  j  a  v  a  2 s .  c  o  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();
        }
    }
}

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

/**
 * Get and modify the PropertyDescriptors associated with the BeanWrapper.
 * @param bwrap/*  w  w  w . jav  a  2 s  .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:org.springframework.beans.BeanWrapperImpl.java

/**
 * Create new BeanWrapperImpl for the given object,
 * registering a nested path that the object is in.
 * @param object object wrapped by this BeanWrapper
 * @param nestedPath the nested path of the object
 * @param superBw the containing BeanWrapper (must not be {@code null})
 *//*www.jav  a  2 s . c  o  m*/
private BeanWrapperImpl(Object object, String nestedPath, BeanWrapperImpl superBw) {
    setWrappedInstance(object, nestedPath, superBw.getWrappedInstance());
    setExtractOldValueForEditor(superBw.isExtractOldValueForEditor());
    setAutoGrowNestedPaths(superBw.isAutoGrowNestedPaths());
    setAutoGrowCollectionLimit(superBw.getAutoGrowCollectionLimit());
    setConversionService(superBw.getConversionService());
    setSecurityContext(superBw.acc);
}

From source file:org.springframework.beans.BeanWrapperImpl.java

/**
 * Retrieve a BeanWrapper for the given nested property.
 * Create a new one if not found in the cache.
 * <p>Note: Caching nested BeanWrappers is necessary now,
 * to keep registered custom editors for nested properties.
 * @param nestedProperty property to create the BeanWrapper for
 * @return the BeanWrapper instance, either cached or newly created
 *//*from ww w.  jav a  2 s  .  c om*/
private BeanWrapperImpl getNestedBeanWrapper(String nestedProperty) {
    if (this.nestedBeanWrappers == null) {
        this.nestedBeanWrappers = new HashMap<String, BeanWrapperImpl>();
    }
    // Get value of bean property.
    PropertyTokenHolder tokens = getPropertyNameTokens(nestedProperty);
    String canonicalName = tokens.canonicalName;
    Object value = getPropertyValue(tokens);
    if (value == null || (value.getClass().equals(javaUtilOptionalClass) && OptionalUnwrapper.isEmpty(value))) {
        if (isAutoGrowNestedPaths()) {
            value = setDefaultValue(tokens);
        } else {
            throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + canonicalName);
        }
    }

    // Lookup cached sub-BeanWrapper, create new one if not found.
    BeanWrapperImpl nestedBw = this.nestedBeanWrappers.get(canonicalName);
    if (nestedBw == null || nestedBw.getWrappedInstance() != (value.getClass().equals(javaUtilOptionalClass)
            ? OptionalUnwrapper.unwrap(value)
            : value)) {
        if (logger.isTraceEnabled()) {
            logger.trace("Creating new nested BeanWrapper for property '" + canonicalName + "'");
        }
        nestedBw = newNestedBeanWrapper(value, this.nestedPath + canonicalName + NESTED_PROPERTY_SEPARATOR);
        // Inherit all type-specific PropertyEditors.
        copyDefaultEditorsTo(nestedBw);
        copyCustomEditorsTo(nestedBw, canonicalName);
        this.nestedBeanWrappers.put(canonicalName, nestedBw);
    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("Using cached nested BeanWrapper for property '" + canonicalName + "'");
        }
    }
    return nestedBw;
}