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

The following examples show how to use org.apache.commons.beanutils.BeanUtils#getProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnotherFieldEqualsSpecifiedValueValidator.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isValid(final Object value, final ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }

    try {
        String fieldValue       = BeanUtils.getProperty(value, fieldName);
        String dependFieldValue = BeanUtils.getProperty(value, dependFieldName);

        if (expectedFieldValue.equals(fieldValue) && dependFieldValue == null) {
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate())
                    .addNode(dependFieldName)
                    .addConstraintViolation();
            return false;
        }

    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
        throw new RuntimeException(ex);
    }

    return true;
}
 
Example 2
Source File: NotEmptyIfFieldSetValidator.java    From qonduit with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }

    try {
        final String fieldValue = BeanUtils.getProperty(value, fieldName);
        final String notNullFieldValue = BeanUtils.getProperty(value, notNullFieldName);
        if (StringUtils.equals(fieldValue, fieldSetValue) && StringUtils.isEmpty(notNullFieldValue)) {
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate())
                    .addPropertyNode(notNullFieldName).addConstraintViolation();
            return false;
        }
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new RuntimeException(e);
    }

    return true;
}
 
Example 3
Source File: ListPropertyConverter.java    From cia with Apache License 2.0 6 votes vote down vote up
/**
 * Append object presentation.
 *
 * @param stringBuilder
 *            the string builder
 * @param object
 *            the object
 */
private void appendObjectPresentation(final StringBuilder stringBuilder, final Object object) {
	try {
		final String beanProperty = BeanUtils.getProperty(object, property);

		if (beanProperty != null) {
			stringBuilder.append(beanProperty);
		} else {
			addFallbackValue(stringBuilder, object);
		}

	} catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
		LOGGER.warn("Problem getting property {}, object {} , exception {}", property, object, e);
	}
	stringBuilder.append(CONTENT_SEPARATOR);
}
 
Example 4
Source File: Table.java    From airtable.java with MIT License 6 votes vote down vote up
/**
 * Check properties of Attachement objects.
 * 
 * @param attachements
 * @throws AirtableException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException 
 */
private void checkPropertiesOfAttachement(List<Attachment> attachements) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (attachements != null) {
        for (int i = 0; i < attachements.size(); i++) {
            if (propertyExists(attachements.get(i), FIELD_ID) || propertyExists(attachements.get(i), "size") 
                    || propertyExists(attachements.get(i), "type") || propertyExists(attachements.get(i), "filename")) {
                
                final Field[] attributesPhotos = attachements.getClass().getDeclaredFields();
                for (Field attributePhoto : attributesPhotos) {
                    final String namePhotoAttribute = attributePhoto.getName();
                    if (FIELD_ID.equals(namePhotoAttribute) || "size".equals(namePhotoAttribute) 
                            || "type".equals(namePhotoAttribute) || "filename".equals(namePhotoAttribute)) {
                        if (BeanUtils.getProperty(attachements.get(i), namePhotoAttribute) != null) {
                            throw new AirtableException("Property " + namePhotoAttribute + " should be null!");
                        }
                    }
                }
            }
        }
    }
}
 
Example 5
Source File: Table.java    From airtable.java with MIT License 6 votes vote down vote up
/**
 * Checks if the Property Values of the item are valid for the Request.
 *
 * @param item
 * @throws AirtableException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 */
private void checkProperties(T item) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (propertyExists(item, FIELD_ID) || propertyExists(item, FIELD_CREATED_TIME)) {
        Field[] attributes = item.getClass().getDeclaredFields();
        for (Field attribute : attributes) {
            String attrName = attribute.getName();
            if (FIELD_ID.equals(attrName) || FIELD_CREATED_TIME.equals(attrName)) {
                if (BeanUtils.getProperty(item, attribute.getName()) != null) {
                    throw new AirtableException("Property " + attrName + " should be null!");
                }
            } else if ("photos".equals(attrName)) {
                List<Attachment> obj = (List<Attachment>) BeanUtilsBean.getInstance().getPropertyUtils().getProperty(item, "photos");
                checkPropertiesOfAttachement(obj);
            }
        }
    }

}
 
Example 6
Source File: ActionLoggerInterceptor.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void includeActionProperties(ActionLogRecord actionRecord, Object action) {
    if (StringUtils.isEmpty(this.getIncludeActionProperties())) {
        return;
    }
    String[] propertyToInclude = this.getIncludeActionProperties().split(",");
    StringBuilder params = new StringBuilder(actionRecord.getParameters());
    for (String property : propertyToInclude) {
        try {
            Object value = BeanUtils.getProperty(action, property);
            if (null != value) {
                params.append(property).append("=").append(value.toString());
                params.append("\n");
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            _logger.debug("Error extracting property " + property + " from action", ex);
        }
    }
    actionRecord.setParameters(params.toString());
}
 
Example 7
Source File: DumpData.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
private <T> void dump(Class<T> cls, EntityManager em) throws Exception {
	/** 创建最终存储文件的目录 */
	File directory = new File(dir, cls.getName());
	FileUtils.forceMkdir(directory);
	FileUtils.cleanDirectory(directory);
	int count = 0;
	int size = Config.dumpRestoreData().getBatchSize();
	String id = "";
	List<T> list = null;
	do {
		list = this.list(em, cls, id, size);
		if (ListTools.isNotEmpty(list)) {
			count = count + list.size();
			id = BeanUtils.getProperty(list.get(list.size() - 1), JpaObject.id_FIELDNAME);
			File file = new File(directory, count + ".json");
			FileUtils.write(file, pureGsonDateFormated.toJson(list), DefaultCharset.charset);
		}
		em.clear();
		Runtime.getRuntime().gc();
	} while (ListTools.isNotEmpty(list));
	this.catalog.put(cls.getName(), count);
}
 
Example 8
Source File: OneNotBlankValidator.java    From data-prep with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isValid(final Object value, final ConstraintValidatorContext context) {
    try {
        for (final String name : fieldsName) {
            final String prop = BeanUtils.getProperty(value, name);
            if (isNotBlank(prop)) {
                return true;
            }
        }
    } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
    }

    context.disableDefaultConstraintViolation();
    context
            .buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate())
            .addPropertyNode(fieldsName[0])
            .addConstraintViolation();
    return false;
}
 
Example 9
Source File: XmlUrlRewriteRulesExporter.java    From knox with Apache License 2.0 6 votes vote down vote up
private Element createElement( Document document, String name, Object bean )
    throws IntrospectionException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
  Element element = document.createElement( name );
  BeanInfo beanInfo = Introspector.getBeanInfo( bean.getClass(), Object.class );
  for( PropertyDescriptor propInfo: beanInfo.getPropertyDescriptors() ) {
    String propName = propInfo.getName();
    if( propInfo.getReadMethod() != null && String.class.isAssignableFrom( propInfo.getPropertyType() ) ) {
      String propValue = BeanUtils.getProperty( bean, propName );
      if( propValue != null && !propValue.isEmpty() ) {
        // Doing it the hard way to avoid having the &'s in the query string escaped at &amp;
        Attr attr = document.createAttribute( propName );
        attr.setValue( propValue );
        element.setAttributeNode( attr );
        //element.setAttribute( propName, propValue );
      }
    }
  }
  return element;
}
 
Example 10
Source File: FieldMatchValidator.java    From viritin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isValid(final Object value,
        final ConstraintValidatorContext context) {
    try {
        final Object firstObj = BeanUtils.getProperty(value,
                firstFieldName);
        final Object secondObj = BeanUtils.getProperty(value,
                secondFieldName);

        return firstObj == null && secondObj == null || firstObj != null && firstObj.
                equals(secondObj);
    } catch (final IllegalAccessException | NoSuchMethodException | InvocationTargetException ignore) {
        // ignore
    }
    return true;
}
 
Example 11
Source File: Table.java    From airtable.java with MIT License 5 votes vote down vote up
/**
 * Get the String Id from the item.
 *
 * @param item
 * @return
 * @throws AirtableException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 */
private String getIdOfItem(T item) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (propertyExists(item, FIELD_ID)) {
        final String id = BeanUtils.getProperty(item, FIELD_ID);
        if (id != null) {
            return id;
        }
    }
    throw new AirtableException("Id of " + item + " not Found!");
}
 
Example 12
Source File: JTextComponentBinding.java    From PyramidShader with GNU General Public License v3.0 5 votes vote down vote up
public void put(IValidatable bean) {
	try {
		String s = BeanUtils.getProperty(bean, _property);
		// XXX displays zeros as blank
		_textComponent.setText(s != null && !s.equals("0") ? s : "");
	} catch (Exception e) {
		throw new BindingException(e);
	}
}
 
Example 13
Source File: BaseReteNode.java    From urule with Apache License 2.0 5 votes vote down vote up
protected Object fetchData(Object object,String property){
	try {
		return BeanUtils.getProperty(object, property);
	} catch (Exception e) {
		throw new RuleException(e);
	}
}
 
Example 14
Source File: FieldMatchValidator.java    From jakduk-api with MIT License 5 votes vote down vote up
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {

    try {
        final Object firstObj = BeanUtils.getProperty(value, firstFieldName);
        final Object secondObj = BeanUtils.getProperty(value, secondFieldName);

        return firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
    } catch (final Exception ignore) {
        // ignore
    }

    return true;
}
 
Example 15
Source File: DatabaseMetadataUtils.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get virtual column string value
 */
public static String getString(DatabaseMetadataValue value, String column) throws DatabaseException, IllegalAccessException,
		InvocationTargetException, NoSuchMethodException {
	List<DatabaseMetadataType> types = DatabaseMetadataDAO.findAllTypes(value.getTable());

	for (DatabaseMetadataType emt : types) {
		if (emt.getVirtualColumn().equals(column)) {
			return BeanUtils.getProperty(value, emt.getRealColumn());
		}
	}

	return null;
}
 
Example 16
Source File: BeanUtil.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
public static String getValue(Object bean,String  field ) {
	if(bean == null) return null;
	String retVal = "";
	try {
		retVal = BeanUtils.getProperty(bean, field);
	} catch(Exception e) {
		e.printStackTrace();
	}
	return retVal;
}
 
Example 17
Source File: JTextComponentBinding.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void put(IValidatable bean) {
	try {
		String s = BeanUtils.getProperty(bean, _property);
		// XXX displays zeros as blank
		_textComponent.setText(s != null && !s.equals("0") ? s : "");
	} catch (Exception e) {
		throw new BindingException(e);
	}
}
 
Example 18
Source File: JsonConfigurationAction.java    From oxTrust with MIT License 4 votes vote down vote up
private void processPasswordProperty(Object current, String property)
		throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, EncryptionException {
	String currentValue = BeanUtils.getProperty(current, property);
	BeanUtils.setProperty(current, property, encryptionService.encrypt(currentValue));
}
 
Example 19
Source File: BeanUtil.java    From feilong-core with Apache License 2.0 3 votes vote down vote up
/**
 * 使用 {@link BeanUtils#getProperty(Object, String)} 类从对象中取得属性值,不care值原本是什么类型,统统转成 {@link String}返回.
 * 
 * <p>
 * 值转成字符串的规则,参见 {@link ConvertUtil#toString(Object)},比如如果发现值是数组,只会取第一个元素重新构造数组转到<code>toObj</code>中
 * </p>
 *
 * @param bean
 *            bean
 * @param propertyName
 *            属性名称 (can be nested/indexed/mapped/combo),参见 <a href="#propertyName">propertyName</a>
 * @return 使用{@link BeanUtils#getProperty(Object, String)} 从对象中取得属性值
 * @throws NullPointerException
 *             如果 <code>bean</code> 是null,或者如果 <code>propertyName</code> 是null
 * @throws IllegalArgumentException
 *             如果 <code>propertyName</code> 是blank
 * @throws BeanOperationException
 *             在调用 {@link BeanUtils#getProperty(Object, String)}过程中有任何异常,转成{@link BeanOperationException}返回
 * @see org.apache.commons.beanutils.BeanUtils#getProperty(Object, String)
 * @see org.apache.commons.beanutils.PropertyUtils#getProperty(Object, String)
 * @see com.feilong.core.bean.PropertyUtil#getProperty(Object, String)
 * @since 1.9.0 change access to private
 */
private static String getProperty(Object bean,String propertyName){
    Validate.notNull(bean, "bean can't be null!");
    Validate.notBlank(propertyName, "propertyName can't be blank!");

    //---------------------------------------------------------------
    try{
        return BeanUtils.getProperty(bean, propertyName);
    }catch (Exception e){
        String pattern = "getProperty exception,bean:[{}],propertyName:[{}]";
        throw new BeanOperationException(Slf4jUtil.format(pattern, bean, propertyName), e);
    }
}
 
Example 20
Source File: PropertyService.java    From oxTrust with MIT License 2 votes vote down vote up
/**
 * Returns object property value
 * 
 * @param bean Bean
 * @param name Property value
 * @return Value of property
 * @throws NoSuchMethodException 
 * @throws InvocationTargetException 
 * @throws IllegalAccessException 
 */
public Object getPropertyValue(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
	return BeanUtils.getProperty(bean, name);
}