Java Code Examples for org.apache.commons.beanutils.BeanUtils.copyProperty()

The following are Jave code examples for showing how to use copyProperty() of the org.apache.commons.beanutils.BeanUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: audible   File: TypeSafeCopy.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
public void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {
    PropertyDescriptor[] origDescriptors =
            PropertyUtils.getPropertyDescriptors(orig);
    for (PropertyDescriptor origDescriptor : origDescriptors) {
        String name = origDescriptor.getName();
        if ("class".equals(name)) {
            continue; // No point in trying to set an object's class
        }
        if (PropertyUtils.isReadable(orig, name) &&
                PropertyUtils.isWriteable(dest, name)) {


            try {
                Class origPropClass = PropertyUtils.getPropertyType(orig, name);
                Class destPropClass = PropertyUtils.getPropertyType(dest, name);

                if (destPropClass.isAssignableFrom(origPropClass)) {
                    Object value =
                            PropertyUtils.getSimpleProperty(orig, name);
                    BeanUtils.copyProperty(dest, name, value);
                }
            } catch (NoSuchMethodException e) {
                // Should not happen
            }
        }
    }
}
 
Example 2
Project: pho   File: PhoenixProjectedResultMapper.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
public <R> Iterable<R> mapResults(ResultSet resultSet, final Class<R> clz) throws SQLException,
        InstantiationException, IllegalAccessException, InvocationTargetException {
    Set<String> metadataColumns = extractColumnNames(resultSet);
    List<R> resultsList = new ArrayList<R>();
    boolean resultIsNumber = Number.class.isAssignableFrom(clz);
    while (resultSet.next()) {
        R instance = null;
        if (!resultIsNumber) {
            instance = clz.newInstance();
        }
        for (String columnName : metadataColumns) {
            Object value = resultSet.getObject(columnName);
            if (value != null) {
                log.debug(value.toString());
                if (resultIsNumber) {
                    instance = (R) value;
                    break;
                }
                
                EntityPropertyBinding entityProperty = entityPropertiesResolver.resolveEntityPropertyBindingByStoreMappingName(columnName, clz);
                if (entityProperty != null) {
                    BeanUtils.copyProperty(instance, entityProperty.getNameFullPath(), value);
                }

            }
        }
        resultsList.add(instance);
    }

    return resultsList;
}
 
Example 3
Project: coordinated-entry   File: QueryExecutorImpl.java   View Source Code Vote up 5 votes
@Transactional(propagation = Propagation.REQUIRED)
public Object insert(Object entity) {
	try{
		BeanUtils.setProperty(entity,"projectGroupCode",SecurityContextUtil.getUserAccount().getProjectGroup().getProjectGroupCode());
		BeanUtils.setProperty(entity, "updatedAt", LocalDateTime.now());
		BeanUtils.setProperty(entity, "createdAt", LocalDateTime.now());
		BeanUtils.copyProperty(entity, "user", SecurityContextUtil.getUserAccount().getUsername());
	return getCurrentSession().save(entity);
	}catch(Exception e){
		e.printStackTrace();
		return null;
	}
}
 
Example 4
Project: coordinated-entry   File: QueryExecutorImpl.java   View Source Code Vote up 5 votes
public Object update(Object entity) {
	try {
		BeanUtils.setProperty(entity, "updatedAt", LocalDateTime.now());
		BeanUtils.copyProperty(entity, "user", SecurityContextUtil.getUserAccount().getUsername());
		getCurrentSession().update(entity);
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
	return null;
}
 
Example 5
Project: coordinated-entry   File: QueryExecutorImpl.java   View Source Code Vote up 5 votes
public void delete(Object entity) {		
		try {
			BeanUtils.copyProperty(entity, "user", SecurityContextUtil.getUserAccount().getUsername());
			BeanUtils.copyProperty(entity, "updatedAt", LocalDateTime.now());
			BeanUtils.copyProperty(entity, "deleted",true);
			getCurrentSession().update(entity);
		} catch (Exception e) {
			getCurrentSession().delete(entity);
		}
}