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

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

Introduction

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

Prototype

public static PropertyDescriptor[] getPropertyDescriptors(Object bean) 

Source Link

Document

Retrieve the property descriptors for the specified bean, introspecting and caching them the first time a particular bean class is encountered.

For more details see PropertyUtilsBean.

Usage

From source file:org.kuali.rice.krad.service.impl.DictionaryValidationServiceImpl.java

/**
 * @param businessObject - business object to validate
 *///from  ww w  .j  a  v  a2 s .c o  m
public void validateBusinessObjectsRecursively(Object businessObject, int depth) {
    if (KRADUtils.isNull(businessObject)) {
        return;
    }

    // validate primitives and any specific bo validation
    validateBusinessObject(businessObject);

    // call method to recursively find business objects and validate
    validateBusinessObjectsFromDescriptors(businessObject,
            PropertyUtils.getPropertyDescriptors(businessObject.getClass()), depth);
}

From source file:org.kuali.rice.krad.service.impl.KRADLegacyDataAdapterImpl.java

@Override
public void setObjectPropertyDeep(Object bo, String propertyName, Class type, Object propertyValue)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    DataObjectWrapper<Object> dataObjectWrapper = dataObjectService.wrap(bo);
    // Base return cases to avoid null pointers & infinite loops
    if (KRADUtils.isNull(bo) || !PropertyUtils.isReadable(bo, propertyName)
            || (propertyValue != null/*from  w w w . java2 s .  c  o  m*/
                    && propertyValue.equals(dataObjectWrapper.getPropertyValueNullSafe(propertyName)))
            || (type != null && !type.equals(KRADUtils.easyGetPropertyType(bo, propertyName)))) {
        return;
    }
    // Set the property in the BO
    KRADUtils.setObjectProperty(bo, propertyName, type, propertyValue);

    // Now drill down and check nested BOs and BO lists
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(bo.getClass());
    for (int i = 0; i < propertyDescriptors.length; i++) {

        PropertyDescriptor propertyDescriptor = propertyDescriptors[i];

        // Business Objects
        if (propertyDescriptor.getPropertyType() != null
                && (BusinessObject.class).isAssignableFrom(propertyDescriptor.getPropertyType())
                && PropertyUtils.isReadable(bo, propertyDescriptor.getName())) {
            Object nestedBo = dataObjectWrapper.getPropertyValueNullSafe(propertyDescriptor.getName());
            if (nestedBo instanceof BusinessObject) {
                setObjectPropertyDeep(nestedBo, propertyName, type, propertyValue);
            }
        }

        // Lists
        else if (propertyDescriptor.getPropertyType() != null
                && (List.class).isAssignableFrom(propertyDescriptor.getPropertyType())
                && dataObjectWrapper.getPropertyValueNullSafe(propertyDescriptor.getName()) != null) {

            List propertyList = (List) dataObjectWrapper.getPropertyValueNullSafe(propertyDescriptor.getName());
            for (Object listedBo : propertyList) {
                if (listedBo != null && listedBo instanceof BusinessObject) {
                    setObjectPropertyDeep(listedBo, propertyName, type, propertyValue);
                }
            } // end for
        }
    } // end for
}

From source file:org.kuali.rice.krad.util.DataTypeUtil.java

/**
 * Given a BusinessObject class and an attribute name, determines the class of that attribute on the BusinessObject class
 * @param boClass a class extending BusinessObject
 * @param attributeKey the name of a field on that class
 * @return the Class of the given attribute
 *///from w w  w . ja  v  a2s . c  o m
private static Class<?> thieveAttributeType(Class<?> boClass, String attributeKey) {
    for (PropertyDescriptor prop : PropertyUtils.getPropertyDescriptors(boClass)) {
        if (prop.getName().equals(attributeKey)) {
            return prop.getPropertyType();
        }
    }
    return null;
}

From source file:org.kuali.rice.krad.util.ObjectUtils.java

/**
 * Recursive; sets all occurences of the property in the object, its nested objects and its object lists with the
 * given value.//w  w w .  j av a 2 s. c  om
 *
 * @param bo
 * @param propertyName
 * @param type
 * @param propertyValue
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 */
public static void setObjectPropertyDeep(Object bo, String propertyName, Class type, Object propertyValue)
        throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    // Base return cases to avoid null pointers & infinite loops
    if (isNull(bo) || !PropertyUtils.isReadable(bo, propertyName)
            || (propertyValue != null && propertyValue.equals(getPropertyValue(bo, propertyName)))
            || (type != null && !type.equals(easyGetPropertyType(bo, propertyName)))) {
        return;
    }

    // need to materialize the updateable collections before resetting the property, because it may be used in the retrieval
    materializeUpdateableCollections(bo);

    // Set the property in the BO
    setObjectProperty(bo, propertyName, type, propertyValue);

    // Now drill down and check nested BOs and BO lists
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(bo.getClass());
    for (int i = 0; i < propertyDescriptors.length; i++) {

        PropertyDescriptor propertyDescriptor = propertyDescriptors[i];

        // Business Objects
        if (propertyDescriptor.getPropertyType() != null
                && (BusinessObject.class).isAssignableFrom(propertyDescriptor.getPropertyType())
                && PropertyUtils.isReadable(bo, propertyDescriptor.getName())) {
            Object nestedBo = getPropertyValue(bo, propertyDescriptor.getName());
            if (nestedBo instanceof BusinessObject) {
                setObjectPropertyDeep(nestedBo, propertyName, type, propertyValue);
            }
        }

        // Lists
        else if (propertyDescriptor.getPropertyType() != null
                && (List.class).isAssignableFrom(propertyDescriptor.getPropertyType())
                && getPropertyValue(bo, propertyDescriptor.getName()) != null) {

            List propertyList = (List) getPropertyValue(bo, propertyDescriptor.getName());
            for (Object listedBo : propertyList) {
                if (listedBo != null && listedBo instanceof BusinessObject) {
                    setObjectPropertyDeep(listedBo, propertyName, type, propertyValue);
                }
            } // end for
        }
    } // end for
}

From source file:org.kuali.rice.krad.util.ObjectUtils.java

public static void setObjectPropertyDeep(Object bo, String propertyName, Class type, Object propertyValue,
        int depth)
        throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    // Base return cases to avoid null pointers & infinite loops
    if (depth == 0 || isNull(bo) || !PropertyUtils.isReadable(bo, propertyName)) {
        return;/*  w  ww  .j a v  a  2 s . c o  m*/
    }

    // need to materialize the updateable collections before resetting the property, because it may be used in the retrieval
    try {
        materializeUpdateableCollections(bo);
    } catch (ClassNotPersistableException ex) {
        //Not all classes will be persistable in a collection. For e.g. externalizable business objects.
        LOG.info("Not persistable dataObjectClass: " + bo.getClass().getName() + ", field: " + propertyName);
    }

    // Set the property in the BO
    setObjectProperty(bo, propertyName, type, propertyValue);

    // Now drill down and check nested BOs and BO lists
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(bo.getClass());
    for (int i = 0; i < propertyDescriptors.length; i++) {
        PropertyDescriptor propertyDescriptor = propertyDescriptors[i];

        // Business Objects
        if (propertyDescriptor.getPropertyType() != null
                && (BusinessObject.class).isAssignableFrom(propertyDescriptor.getPropertyType())
                && PropertyUtils.isReadable(bo, propertyDescriptor.getName())) {
            Object nestedBo = getPropertyValue(bo, propertyDescriptor.getName());
            if (nestedBo instanceof BusinessObject) {
                setObjectPropertyDeep(nestedBo, propertyName, type, propertyValue, depth - 1);
            }
        }

        // Lists
        else if (propertyDescriptor.getPropertyType() != null
                && (List.class).isAssignableFrom(propertyDescriptor.getPropertyType())
                && getPropertyValue(bo, propertyDescriptor.getName()) != null) {

            List propertyList = (List) getPropertyValue(bo, propertyDescriptor.getName());

            // Complete Hibernate Hack - fetches the proxied List into the PersistenceContext and sets it on the BO Copy.
            //                if (propertyList instanceof PersistentBag) {
            //                    try {
            //                        PersistentBag bag = (PersistentBag) propertyList;
            //                        PersistableBusinessObject pbo =
            //                                (PersistableBusinessObject) KRADServiceLocator.getEntityManagerFactory()
            //                                        .createEntityManager().find(bo.getClass(), bag.getKey());
            //                        Field field1 = pbo.getClass().getDeclaredField(propertyDescriptor.getName());
            //                        Field field2 = bo.getClass().getDeclaredField(propertyDescriptor.getName());
            //                        field1.setAccessible(true);
            //                        field2.setAccessible(true);
            //                        field2.set(bo, field1.get(pbo));
            //                        propertyList = (List) getPropertyValue(bo, propertyDescriptor.getName());
            //                        ;
            //                    } catch (Exception e) {
            //                        LOG.error(e.getMessage(), e);
            //                    }
            //                }
            // End Complete Hibernate Hack

            for (Object listedBo : propertyList) {
                if (listedBo != null && listedBo instanceof BusinessObject) {
                    setObjectPropertyDeep(listedBo, propertyName, type, propertyValue, depth - 1);
                }
            } // end for
        }
    } // end for
}

From source file:org.kuali.rice.krad.util.ObjectUtils.java

/**
 * This method checks for updateable collections on the business object provided and materializes the corresponding
 * collection proxies/* w ww  .j  a  v a  2  s  .  com*/
 *
 * @param bo The business object for which you want unpdateable, proxied collections materialized
 * @throws FormatException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 */
public static void materializeUpdateableCollections(Object bo)
        throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (isNotNull(bo)) {
        PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(bo.getClass());
        for (int i = 0; i < propertyDescriptors.length; i++) {
            if (KNSServiceLocator.getPersistenceStructureService().hasCollection(bo.getClass(),
                    propertyDescriptors[i].getName())
                    && KNSServiceLocator.getPersistenceStructureService().isCollectionUpdatable(bo.getClass(),
                            propertyDescriptors[i].getName())) {
                Collection updateableCollection = (Collection) getPropertyValue(bo,
                        propertyDescriptors[i].getName());
                if ((updateableCollection != null) && ProxyHelper.isCollectionProxy(updateableCollection)) {
                    materializeObjects(updateableCollection);
                }
            }
        }
    }
}

From source file:org.lightadmin.core.web.ApplicationController.java

private Object cloneEntityOfDomain(String entityId, String domainTypeName) {
    DomainTypeAdministrationConfiguration domainTypeConfiguration = configuration.forEntityName(domainTypeName);
    DynamicJpaRepository repository = domainTypeConfiguration.getRepository();

    PersistentEntity persistentEntity = domainTypeConfiguration.getPersistentEntity();
    Serializable id = (Serializable) conversionService.convert(entityId,
            persistentEntity.getIdProperty().getActualType());

    Object found = repository.findOne(id);
    if (found != null) {
        try {/*from   w w  w  .  j  av  a 2 s . c om*/
            Object newInstance = null;
            if (found instanceof CloneableEntity) {
                newInstance = CloneableEntity.class.cast(found).clone();
            } else {
                newInstance = domainTypeConfiguration.getDomainType().newInstance();

                BeanUtils.copyProperties(found, newInstance, persistentEntity.getIdProperty().getName());

                PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(newInstance);

                for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {

                    if (propertyDescriptor.getReadMethod() != null
                            && propertyDescriptor.getWriteMethod() != null) {
                        Object value = propertyDescriptor.getReadMethod().invoke(newInstance);

                        Object newValue = null;
                        try {
                            if (value instanceof SortedSet) {
                                newValue = new TreeSet(SortedSet.class.cast(value));
                            } else if (value instanceof Set) {
                                newValue = new HashSet(Set.class.cast(value));
                            } else if (value instanceof SortedMap) {
                                newValue = new TreeMap(SortedMap.class.cast(value));
                            } else if (value instanceof Collection) {
                                newValue = new ArrayList(Collection.class.cast(value));
                            } else if (value instanceof Map) {
                                newValue = new HashMap(Map.class.cast(value));
                            }
                        } catch (Throwable t) {
                            if (logger.isWarnEnabled()) {
                                logger.warn("Can't clone " + propertyDescriptor.getName(), t);
                            }
                        }

                        if (newValue != null) {
                            propertyDescriptor.getWriteMethod().invoke(newInstance, newValue);
                        }
                    }

                }
            }

            Object saved = repository.saveAndFlush(newInstance);

            PersistentProperty idProperty = persistentEntity.getIdProperty();
            Field idField = idProperty.getField();
            idField.setAccessible(true);
            return idProperty.usePropertyAccess() ? idProperty.getGetter().invoke(saved) : idField.get(saved);
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    } else {
        return null;
    }
}

From source file:org.lunifera.runtime.web.vaadin.databinding.component.internal.SimpleAccessorProperty.java

/**
 * Returns the property descriptor for the given class and the property.
 * /*from w ww. java  2s.  c  o m*/
 * @param componentClass
 * @param property
 * @return
 */
protected PropertyDescriptor getPropertyDescriptor(Class<?> componentClass, String property) {
    PropertyDescriptor result = null;
    for (PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(componentClass)) {
        if (descriptor.getName().equals(property)) {
            result = descriptor;
            break;
        }
    }
    return result;
}

From source file:org.malaguna.cmdit.service.reflection.HibernateProxyUtils.java

private void deepLoadDomainObject(AbstractObject<?> object, Object guideObj) {
    PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(unproxy(object));

    for (PropertyDescriptor property : properties) {
        String pName = property.getName();

        if (PropertyUtils.isWriteable(object, pName) && PropertyUtils.isReadable(object, pName)) {

            try {
                Object propGuideObject = property.getReadMethod().invoke(guideObj);

                if (null != propGuideObject) {
                    Object unproxied = deepLoad(property.getReadMethod().invoke(object), propGuideObject);
                    property.getWriteMethod().invoke(object, unproxied);
                }//from   w w  w.  j  av  a 2s  .c o  m
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.nekorp.workflow.desktop.view.model.validacion.ValidacionParticular.java

public void evaluaTodo(ViewValidIndicator evaluacionGeneral) {
    try {// ww  w  . ja v  a  2s.c  om
        for (PropertyDescriptor x : PropertyUtils.getPropertyDescriptors(this)) {
            if (EstatusValidacion.class.isAssignableFrom(x.getPropertyType())) {
                EstatusValidacion arg = (EstatusValidacion) PropertyUtils.getProperty(this, x.getName());
                if (!arg.isValido()) {
                    evaluacionGeneral.setValido(false);
                    return;
                }
            }
        }
        evaluacionGeneral.setValido(true);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
        throw new IllegalArgumentException("No se logro evaluar", ex);
    }
}