Example usage for org.apache.commons.beanutils PropertyUtils isWriteable

List of usage examples for org.apache.commons.beanutils PropertyUtils isWriteable

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils isWriteable.

Prototype

public static boolean isWriteable(Object bean, String name) 

Source Link

Document

Return true if the specified property name identifies a writeable property on the specified bean; otherwise, return false.

For more details see PropertyUtilsBean.

Usage

From source file:org.apache.velocity.tools.ToolInfo.java

protected void setProperty(Object tool, String name, Object value) throws Exception {
    if (PropertyUtils.isWriteable(tool, name)) {
        //TODO? support property conversion here?
        //      heavy-handed way is BeanUtils.copyProperty(...)
        PropertyUtils.setProperty(tool, name, value);
    }/*from w  w  w.  j  a v  a2  s  . c o m*/
}

From source file:org.beanfuse.model.EntityUtils.java

/**
 * <pre>/*from   w w w  .  j a va 2s  .co m*/
 *    merge???(orig)????.?
 *    ?????.?component ?entity?
 *    ?id?0,???null.
 *                                        
 *    ?????.???(Set)?
 *    ?????(hibernate)?
 *    orig??dest added at
 *    ?
 * </pre>
 * 
 * @deprecated
 * @see EntityUtil#evictEmptyProperty
 * @param dest
 * @param orig
 *            ?dest
 */
public static void merge(Object dest, Object orig) {
    String attr = "";
    try {
        Set attrs = PropertyUtils.describe(orig).keySet();
        attrs.remove("class");
        for (Iterator it = attrs.iterator(); it.hasNext();) {
            attr = (String) it.next();
            if (!PropertyUtils.isWriteable(orig, attr)) {
                continue;
            }
            Object value = PropertyUtils.getProperty(orig, attr);
            if (null != value) {
                if (value instanceof Component) {
                    Object savedValue = PropertyUtils.getProperty(dest, attr);
                    if (null == savedValue) {
                        PropertyUtils.setProperty(dest, attr, value);
                    } else {
                        merge(savedValue, value);
                    }
                } else if (value instanceof Collection) {
                    continue;
                } else if (value instanceof Entity) {
                    Serializable key = (Serializable) PropertyUtils.getProperty(value, ((Entity) value).key());
                    if (null == key) {
                        continue;
                    } else if (new EmptyKeyPredicate().evaluate(key)) {
                        PropertyUtils.setProperty(dest, attr, null);
                    } else {
                        PropertyUtils.setProperty(dest, attr, value);
                    }
                } else {
                    PropertyUtils.setProperty(dest, attr, value);
                }
            }
        }
    } catch (Exception e) {
        logger.error("meger error", e);
        if (logger.isDebugEnabled()) {
            logger.debug(
                    "error occur in reflection of attr:" + attr + " of entity " + dest.getClass().getName());
        }
        return;
    }
}

From source file:org.beanfuse.model.EntityUtils.java

/**
 * ().<br>/* w w w  . j  a  v  a 2s.c o m*/
 * 
 * <pre>
 *  hibernate????.
 *  (Entity)???id??.
 *  ?
 * &lt;code&gt;
 * Component
 * &lt;/code&gt;
 *  ?
 *  ?????????.
 *  // evict collection
 *  if (value instanceof Collection) {
 *    if (((Collection) value).isEmpty())
 *    map.put(attr, null);
 *  }
 * </pre>
 * 
 * @see ValidEntityPredicate
 * @param entity
 */
public static void evictEmptyProperty(Object entity) {
    if (null == entity) {
        return;
    }
    boolean isEntity = false;
    if (entity instanceof Entity) {
        isEntity = true;
    }
    BeanMap map = new BeanMap(entity);
    List attList = new ArrayList();
    attList.addAll(map.keySet());
    attList.remove("class");
    for (Iterator iter = attList.iterator(); iter.hasNext();) {
        String attr = (String) iter.next();
        if (!PropertyUtils.isWriteable(entity, attr)) {
            continue;
        }
        Object value = map.get(attr);
        if (null == value) {
            continue;
        } else {
            // evict invalid entity key
            if (isEntity && attr.equals(((Entity) entity).key())) {
                if (!ValidEntityKeyPredicate.getInstance().evaluate(value)) {
                    map.put(attr, null);
                }
            }
            // evict invalid entity
            if (value instanceof Entity && !ValidEntityPredicate.getInstance().evaluate(value)) {
                map.put(attr, null);
            } else if (value instanceof Component) {
                // evict component recursively
                evictEmptyProperty(value);
            }
        }
    }
}

From source file:org.beanfuse.model.EntityUtils.java

/**
 * ?//from w  w w . j a v  a 2  s.com
 * 
 * @param entity
 * @param ignoreDefault
 *            
 * @return
 */
public static boolean isEmpty(Entity entity, boolean ignoreDefault) {
    BeanMap map = new BeanMap(entity);
    List attList = new ArrayList();
    attList.addAll(map.keySet());
    attList.remove("class");
    try {
        for (Iterator iter = attList.iterator(); iter.hasNext();) {
            String attr = (String) iter.next();
            if (!PropertyUtils.isWriteable(entity, attr)) {
                continue;
            }
            Object value = map.get(attr);
            if (null == value) {
                continue;
            }
            if (ignoreDefault) {
                if (value instanceof Number) {
                    if (((Number) value).intValue() != 0) {
                        return false;
                    }
                } else if (value instanceof String) {
                    String str = (String) value;
                    if (StringUtils.isNotEmpty(str)) {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    } catch (Exception e) {
        logger.error("isEmpty error", e);
    }
    return true;
}

From source file:org.beanfuse.query.ConditionUtils.java

/**
 * ????<br>// w w w. j  a  v  a  2 s .  co  m
 * ????"?"(?component)<br>
 * :null,Collection
 * 
 * @param alias
 * @param entity
 * @param mode
 * @return
 */
public static List extractConditions(String alias, final Entity entity) {
    if (null == entity) {
        return Collections.EMPTY_LIST;
    }
    final List conditions = new ArrayList();

    StringBuilder aliasBuilder = new StringBuilder(alias == null ? "" : alias);
    if (aliasBuilder.length() > 0 && !alias.endsWith(".")) {
        aliasBuilder.append(".");
    }
    String attr = "";
    try {
        final Map beanMap = PropertyUtils.describe(entity);
        for (final Iterator iter = beanMap.keySet().iterator(); iter.hasNext();) {
            attr = (String) iter.next();
            // ???
            if (!PropertyUtils.isWriteable(entity, attr)) {
                continue;
            }
            final Object value = PropertyUtils.getProperty(entity, attr);
            if (null == value) {
                continue;
            }
            if (!(value instanceof Collection)) {
                addAttrCondition(conditions, alias + attr, value);
            }
        }
    } catch (Exception e) {
        logger.debug("error occur in extractConditions for  bean {} with attr named {}", entity, attr);
    }
    return conditions;
}

From source file:org.beanfuse.query.ConditionUtils.java

private static List extractComponent(final String prefix, final Component component) {
    if (null == component) {
        return Collections.EMPTY_LIST;
    }/*from  w  w  w .ja  v  a  2 s .  co m*/
    final List conditions = new ArrayList();
    String attr = "";
    try {
        final Map beanMap = PropertyUtils.describe(component);
        for (final Iterator iter = beanMap.keySet().iterator(); iter.hasNext();) {
            attr = (String) iter.next();
            if ("class".equals(attr)) {
                continue;
            }
            if (!PropertyUtils.isWriteable(component, attr)) {
                continue;
            }
            final Object value = PropertyUtils.getProperty(component, attr);
            if (value == null) {
                continue;
            } else if (value instanceof Collection) {
                if (((Collection) value).isEmpty()) {
                    continue;
                }
            } else {
                addAttrCondition(conditions, prefix + "." + attr, value);
            }
        }

    } catch (Exception e) {
        logger.warn("error occur in extractComponent of component:" + component + "with attr named :" + attr);
    }
    return conditions;
}

From source file:org.beangle.model.persist.hibernate.CriterionUtils.java

/**
 * ?. ??./*from  w  w w.j  av  a  2  s .co m*/
 * 
 * @param entity
 * @param excludePropertes
 * @param mode
 * @return
 */
@SuppressWarnings("rawtypes")
public static List<Criterion> getEntityCriterions(String nestedName, Object entity, String[] excludePropertes,
        MatchMode mode, boolean ignoreZero) {
    if (null == entity) {
        return Collections.emptyList();
    }
    List<Criterion> criterions = CollectUtils.newArrayList();
    BeanMap map = new BeanMap(entity);
    Set keySet = map.keySet();
    Collection properties = null;
    if (null == excludePropertes) {
        List proList = CollectUtils.newArrayList();
        proList.addAll(keySet);
        properties = proList;
    } else {
        properties = CollectionUtils.subtract(keySet, Arrays.asList(excludePropertes));
    }
    properties.remove("class");

    for (Iterator iter = properties.iterator(); iter.hasNext();) {
        String propertyName = (String) iter.next();
        if (!PropertyUtils.isWriteable(entity, propertyName)) {
            continue;
        }
        Object value = map.get(propertyName);
        addCriterion(nestedName, entity, excludePropertes, propertyName, value, criterions, mode, ignoreZero);
    }
    return criterions;
}

From source file:org.beangle.model.persist.hibernate.CriterionUtils.java

/**
 * ?//ww  w.  j a v  a 2s .c  o m
 * 
 * @param entity
 * @param property
 *            ????outcomponent.innercomponent
 * @param excludePropertes
 *            ??entityProperty.componentProperty
 * @param enableLike
 * @return
 */
private static List<Criterion> getComponentCriterions(String nestedName, Object entity, String property,
        String[] excludePropertes, MatchMode mode, boolean ignoreZero) {
    List<Criterion> criterions = CollectUtils.newArrayList();
    Component component = null;
    try {
        component = (Component) PropertyUtils.getProperty(entity, property);
    } catch (Exception e) {
        return Collections.emptyList();
    }
    if (null == component) {
        return Collections.emptyList();
    }
    BeanMap map = new BeanMap(component);
    Set<String> properties = map.keySet();
    Set<String> excludeSet = null;
    if (null == excludePropertes) {
        excludeSet = Collections.emptySet();
    } else {
        excludeSet = CollectUtils.newHashSet();
        excludeSet.addAll(Arrays.asList(excludePropertes));
    }
    for (Iterator<String> iter = properties.iterator(); iter.hasNext();) {
        String propertyName = iter.next();
        String cascadeName = property + "." + propertyName;
        if (excludeSet.contains(cascadeName) || "class".equals(propertyName)) {
            continue;
        }
        if (!PropertyUtils.isWriteable(component, propertyName)) {
            continue;
        }
        Object value = map.get(propertyName);

        addCriterion(nestedName, entity, excludePropertes, cascadeName, value, criterions, mode, ignoreZero);
    }
    return criterions;
}

From source file:org.beangle.model.query.builder.ConditionUtils.java

/**
 * ????<br>//from ww w  .j  a  v  a2  s.co  m
 * ????"?"(?component)<br>
 * :null,Collection
 * 
 * @param alias
 * @param entity
 * @param mode
 * @return
 */
public static List<Condition> extractConditions(final String alias, final Entity<?> entity) {
    if (null == entity) {
        return Collections.emptyList();
    }
    final List<Condition> conditions = new ArrayList<Condition>();

    StringBuilder aliasBuilder = new StringBuilder(alias == null ? "" : alias);
    if (aliasBuilder.length() > 0 && !alias.endsWith(".")) {
        aliasBuilder.append(".");
    }
    String attr = "";
    try {
        @SuppressWarnings("unchecked")
        final Set<String> props = PropertyUtils.describe(entity).keySet();
        for (final Iterator<String> iter = props.iterator(); iter.hasNext();) {
            attr = iter.next();
            // ???
            if (!PropertyUtils.isWriteable(entity, attr)) {
                continue;
            }
            final Object value = PropertyUtils.getProperty(entity, attr);
            if (null == value) {
                continue;
            }
            if (!(value instanceof Collection<?>)) {
                addAttrCondition(conditions, alias + attr, value);
            }
        }
    } catch (Exception e) {
        logger.debug("error occur in extractConditions for  bean {} with attr named {}", entity, attr);
    }
    return conditions;
}

From source file:org.beangle.model.query.builder.ConditionUtils.java

private static List<Condition> extractComponent(final String prefix, final Component component) {
    if (null == component) {
        return Collections.emptyList();
    }/*from  w w w. j a v a2s . c  om*/
    final List<Condition> conditions = CollectUtils.newArrayList();
    String attr = "";
    try {
        @SuppressWarnings("unchecked")
        final Set<String> props = PropertyUtils.describe(component).keySet();
        for (final Iterator<String> iter = props.iterator(); iter.hasNext();) {
            attr = iter.next();
            if ("class".equals(attr)) {
                continue;
            }
            if (!PropertyUtils.isWriteable(component, attr)) {
                continue;
            }
            final Object value = PropertyUtils.getProperty(component, attr);
            if (value == null) {
                continue;
            } else if (value instanceof Collection<?>) {
                if (((Collection<?>) value).isEmpty()) {
                    continue;
                }
            } else {
                addAttrCondition(conditions, prefix + "." + attr, value);
            }
        }

    } catch (Exception e) {
        logger.warn("error occur in extractComponent of component:" + component + "with attr named :" + attr);
    }
    return conditions;
}