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.kuali.ext.mm.TestDataPreparator.java

/**
 * Generates transaction data for a business object from properties
 * //ww  w .  ja  va 2s.  co m
 * @param businessObject the transction business object
 * @return the transction business object with data
 * @throws Exception thrown if an exception is encountered for any reason
 */
public static <T> T buildTestDataObject(Class<? extends T> clazz, Properties properties) {
    T testData = null;

    try {
        testData = clazz.newInstance();

        Iterator propsIter = properties.keySet().iterator();
        while (propsIter.hasNext()) {
            String propertyName = (String) propsIter.next();
            String propertyValue = (String) properties.get(propertyName);

            // if searchValue is empty and the key is not a valid property ignore
            if (StringUtils.isBlank(propertyValue) || !(PropertyUtils.isWriteable(testData, propertyName))) {
                continue;
            }

            String propertyType = PropertyUtils.getPropertyType(testData, propertyName).getSimpleName();
            Object finalPropertyValue = ObjectUtil.valueOf(propertyType, propertyValue);

            if (finalPropertyValue != null) {
                PropertyUtils.setProperty(testData, propertyName, finalPropertyValue);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Cannot build a test data object with the given data. " + e);
    }

    return testData;
}

From source file:org.kuali.kfs.gl.OJBUtility.java

/**
 * This method builds a map of business object with its property names and values
 * //  www  .  ja v  a2 s .  c o  m
 * @param businessObject the given business object
 * @return the map of business object with its property names and values
 */
public static LinkedHashMap buildPropertyMap(Object businessObject) {
    DynaClass dynaClass = WrapDynaClass.createDynaClass(businessObject.getClass());
    DynaProperty[] properties = dynaClass.getDynaProperties();
    LinkedHashMap propertyMap = new LinkedHashMap();

    try {
        for (int numOfProperty = 0; numOfProperty < properties.length; numOfProperty++) {
            String propertyName = properties[numOfProperty].getName();
            if (PropertyUtils.isWriteable(businessObject, propertyName)) {
                Object propertyValue = PropertyUtils.getProperty(businessObject, propertyName);
                propertyMap.put(propertyName, propertyValue);
            }
        }
    } catch (Exception e) {
        LOG.error("OJBUtility.buildPropertyMap()" + e);
    }
    return propertyMap;
}

From source file:org.kuali.kfs.gl.web.TestDataGenerator.java

/**
 * Generates transaction data for a business object from properties
 * /*w  ww  . j a v  a 2s  . co m*/
 * @param businessObject the transction business object
 * @return the transction business object with data
 * @throws Exception thrown if an exception is encountered for any reason
 */
public Transaction generateTransactionData(Transaction businessObject) throws Exception {
    Iterator propsIter = properties.keySet().iterator();
    while (propsIter.hasNext()) {
        String propertyName = (String) propsIter.next();
        String propertyValue = (String) properties.get(propertyName);

        // if searchValue is empty and the key is not a valid property ignore
        if (StringUtils.isBlank(propertyValue) || !(PropertyUtils.isWriteable(businessObject, propertyName))) {
            continue;
        }

        Object finalPropertyValue = getPropertyValue(businessObject, propertyName, propertyValue);
        if (finalPropertyValue != null) {
            PropertyUtils.setProperty(businessObject, propertyName, finalPropertyValue);
        }
    }
    setFiscalYear(businessObject);
    return businessObject;
}

From source file:org.kuali.kfs.sys.ObjectUtil.java

/**
 * Populate the property of the target object with the counterpart of the source object
 * //from  w  w  w  . jav  a  2 s.c  om
 * @param targetObject the target object
 * @param sourceObject the source object
 * @param property the specified propety of the target object
 * @param skipReferenceFields determine whether the referencing fields need to be populated
 */
public static void setProperty(Object targetObject, Object sourceObject, DynaProperty property,
        boolean skipReferenceFields) {
    String propertyName = property.getName();

    try {
        if (skipReferenceFields) {
            @SuppressWarnings("rawtypes")
            Class propertyType = property.getType();
            if (propertyType == null || PersistableBusinessObjectBase.class.isAssignableFrom(propertyType)
                    || List.class.isAssignableFrom(propertyType)) {
                return;
            }
        }

        if (PropertyUtils.isReadable(sourceObject, propertyName)
                && PropertyUtils.isWriteable(targetObject, propertyName)) {
            Object propertyValue = PropertyUtils.getProperty(sourceObject, propertyName);
            PropertyUtils.setProperty(targetObject, propertyName, propertyValue);
        }
    } catch (IllegalAccessException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(e.getMessage() + ":" + propertyName);
        }
    } catch (InvocationTargetException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(e.getMessage() + ":" + propertyName);
        }
    } catch (NoSuchMethodException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(e.getMessage() + ":" + propertyName);
        }
    } catch (IllegalArgumentException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(e.getMessage() + ":" + propertyName);
        }
    } catch (Exception e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(e.getMessage() + ":" + propertyName);
        }
    }
}

From source file:org.kuali.rice.kns.maintenance.KualiMaintainableImpl.java

protected void performFieldForceUpperCase(String fieldNamePrefix, BusinessObject bo,
        MaintainableFieldDefinition fieldDefinition, Map fieldValues) {
    MessageMap errorMap = GlobalVariables.getMessageMap();
    String fieldName = fieldDefinition.getName();
    String mapKey = fieldNamePrefix + fieldName;
    if (fieldValues != null && fieldValues.get(mapKey) != null) {
        if (PropertyUtils.isWriteable(bo, fieldName) && ObjectUtils.getNestedValue(bo, fieldName) != null) {

            try {
                Class type = ObjectUtils.easyGetPropertyType(bo, fieldName);
                // convert to upperCase based on data dictionary
                Class businessObjectClass = bo.getClass();
                boolean upperCase = false;
                try {
                    upperCase = getDataDictionaryService().getAttributeForceUppercase(businessObjectClass,
                            fieldName);/*from w w  w.  j  a  va 2 s .c o  m*/
                } catch (UnknownBusinessClassAttributeException t) {
                    boolean catchme = true;
                    // throw t;
                }

                Object fieldValue = ObjectUtils.getNestedValue(bo, fieldName);

                if (upperCase && fieldValue instanceof String) {
                    fieldValue = ((String) fieldValue).toUpperCase();
                }
                ObjectUtils.setObjectProperty(bo, fieldName, type, fieldValue);
            } catch (FormatException e) {
                errorMap.putError(fieldName, e.getErrorKey(), e.getErrorArgs());
            } catch (IllegalAccessException e) {
                LOG.error("unable to populate business object" + e.getMessage());
                throw new RuntimeException(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                LOG.error("unable to populate business object" + e.getMessage());
                throw new RuntimeException(e.getMessage(), e);
            } catch (NoSuchMethodException e) {
                LOG.error("unable to populate business object" + e.getMessage());
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    }
}

From source file:org.kuali.rice.kns.maintenance.KualiMaintainableImpl.java

protected void performFieldForceUpperCase(BusinessObject bo, Map fieldValues) {
    MessageMap errorMap = GlobalVariables.getMessageMap();

    try {//w  w  w .  ja  v  a 2  s .co m
        for (Iterator iter = fieldValues.keySet().iterator(); iter.hasNext();) {
            String propertyName = (String) iter.next();

            if (PropertyUtils.isWriteable(bo, propertyName) && fieldValues.get(propertyName) != null) {
                // if the field propertyName is a valid property on the bo
                // class
                Class type = ObjectUtils.easyGetPropertyType(bo, propertyName);
                try {
                    // Keep the convert to upperCase logic here. It will be
                    // used in populateNewCollectionLines,
                    // populateNewSubCollectionLines
                    // convert to upperCase based on data dictionary
                    Class businessObjectClass = bo.getClass();
                    boolean upperCase = false;
                    try {
                        upperCase = getDataDictionaryService().getAttributeForceUppercase(businessObjectClass,
                                propertyName);
                    } catch (UnknownBusinessClassAttributeException t) {
                        boolean catchme = true;
                        // throw t;
                    }

                    Object fieldValue = fieldValues.get(propertyName);

                    if (upperCase && fieldValue instanceof String) {
                        fieldValue = ((String) fieldValue).toUpperCase();
                    }
                    ObjectUtils.setObjectProperty(bo, propertyName, type, fieldValue);
                } catch (FormatException e) {
                    errorMap.putError(propertyName, e.getErrorKey(), e.getErrorArgs());
                }
            }
        }
    } catch (IllegalAccessException e) {
        LOG.error("unable to populate business object" + e.getMessage());
        throw new RuntimeException(e.getMessage(), e);
    } catch (InvocationTargetException e) {
        LOG.error("unable to populate business object" + e.getMessage());
        throw new RuntimeException(e.getMessage(), e);
    } catch (NoSuchMethodException e) {
        LOG.error("unable to populate business object" + e.getMessage());
        throw new RuntimeException(e.getMessage(), e);
    }

}

From source file:org.kuali.rice.kns.util.FieldUtils.java

private static boolean isPropertyWritable(Object bean, String name) {
    try {/*from  w w  w  .  j ava2 s  .c om*/
        return PropertyUtils.isWriteable(bean, name);
    } catch (NestedNullException e) {
        return false;
    }
}

From source file:org.kuali.rice.kns.web.struts.action.KualiMaintenanceDocumentAction.java

protected void copyParametersToBO(Map<String, String> parameters, PersistableBusinessObject newBO)
        throws Exception {
    for (String parmName : parameters.keySet()) {
        String propertyValue = parameters.get(parmName);

        if (StringUtils.isNotBlank(propertyValue)) {
            String propertyName = parmName;
            // set value of property in bo
            if (PropertyUtils.isWriteable(newBO, propertyName)) {
                Class type = ObjectUtils.easyGetPropertyType(newBO, propertyName);
                if (type != null && Formatter.getFormatter(type) != null) {
                    Formatter formatter = Formatter.getFormatter(type);
                    Object obj = formatter.convertFromPresentationFormat(propertyValue);
                    ObjectUtils.setObjectProperty(newBO, propertyName, obj.getClass(), obj);
                } else {
                    ObjectUtils.setObjectProperty(newBO, propertyName, String.class, propertyValue);
                }/* w  w w . j  a v  a2 s.  c  om*/
            }
        }
    }
}

From source file:org.kuali.rice.kns.web.struts.form.pojo.PojoPluginTest.java

/**
 * <p>Testing scenario that isWriteable blows up with NestedNullException when property value is null
 * KULRICE-6877: KualiMaintainbleImpl#performCollectionForceUpperCase blowing up</p>
 *
 * @throws Exception/*from   w  ww .  java2 s.com*/
 */
@Test
public void testNestedNullIsWriteable() throws Exception {

    // We need to initialize PropertyUtils to use our plugins
    new PojoPlugin().init(null, new ModuleConfigImpl());

    TestCollectionHolderHolder tchh = new TestCollectionHolderHolder();
    assertTrue(PropertyUtils.isWriteable(tchh, "tch2.collection"));

}

From source file:org.kuali.rice.kns.web.struts.form.pojo.PojoPropertyUtilsBeanTest.java

/**
 * This test was added for KULRICE-12283 and is correct, but the fix IU contributed caused an IT to fail
 * so the change was reverted and this test is being ignored until that contribution is finished.
 *///from  ww w  .  ja v a  2s  .  c  om
@Test
/**
 * This test checks to ensure that the PropertyUtils method which delegates
 * to the PojoPropertyUtilsBean in the KNS properly handles checking if a
 * nested property is writeable.
 */
public void testNestedPropertyIsWriteable() {
    ReadonlyBean testBean = new ReadonlyBean();
    ReadonlyWrappingBean readonlyBean = new ReadonlyWrappingBean(testBean);
    WriteableWrappingBean writeableBean = new WriteableWrappingBean(testBean);

    assertFalse(PropertyUtils.isWriteable(readonlyBean, "bean.value"));
    assertFalse(PropertyUtils.isWriteable(writeableBean, "bean.value"));
}