List of usage examples for org.apache.commons.beanutils PropertyUtils getSimpleProperty
public static Object getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Return the value of the specified simple property of the specified bean, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:com.yukthi.validators.LessThanValidator.java
@Override public boolean isValid(Object bean, Object fieldValue) { Object otherValue = null;//from ww w. ja va 2 s . c om try { otherValue = PropertyUtils.getSimpleProperty(bean, lessThanField); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new IllegalStateException("Invalid/inaccessible property \"" + lessThanField + "\" specified with matchWith validator in bean: " + bean.getClass().getName()); } if (fieldValue == null || otherValue == null || !fieldValue.getClass().equals(otherValue.getClass())) { return true; } if (otherValue instanceof Number) { return (((Number) fieldValue).doubleValue() < ((Number) otherValue).doubleValue()); } if (otherValue instanceof Date) { Date dateValue = DateUtils.truncate((Date) fieldValue, Calendar.DATE); Date otherDateValue = DateUtils.truncate((Date) otherValue, Calendar.DATE); return (dateValue.compareTo(otherDateValue) < 0); } return true; }
From source file:com.yukthi.validators.LessThanEqualsValidator.java
@Override public boolean isValid(Object bean, Object fieldValue) { //fetch other field value Object otherValue = null;/*from www .j av a 2 s . c o m*/ try { otherValue = PropertyUtils.getSimpleProperty(bean, lessThanField); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new IllegalStateException("Invalid/inaccessible property \"" + lessThanField + "\" specified with matchWith validator in bean: " + bean.getClass().getName()); } //if value is null or of different data type if (fieldValue == null || otherValue == null || !fieldValue.getClass().equals(otherValue.getClass())) { return true; } //number comparison if (otherValue instanceof Number) { return (((Number) fieldValue).doubleValue() <= ((Number) otherValue).doubleValue()); } //date comparison if (otherValue instanceof Date) { Date dateValue = DateUtils.truncate((Date) fieldValue, Calendar.DATE); Date otherDateValue = DateUtils.truncate((Date) otherValue, Calendar.DATE); return (dateValue.compareTo(otherDateValue) <= 0); } return true; }
From source file:com.yukthi.validators.GreaterThanValidator.java
@Override public boolean isValid(Object bean, Object fieldValue) { //obtain field value to be compared Object otherValue = null;//www .ja va2 s .c om try { otherValue = PropertyUtils.getSimpleProperty(bean, greaterThanField); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new IllegalStateException("Invalid/inaccessible property \"" + greaterThanField + "\" specified with matchWith validator in bean: " + bean.getClass().getName()); } //if other value is null or of different type if (fieldValue == null || otherValue == null || !fieldValue.getClass().equals(otherValue.getClass())) { return true; } //number comparison if (otherValue instanceof Number) { return (((Number) fieldValue).doubleValue() > ((Number) otherValue).doubleValue()); } //date comparison if (otherValue instanceof Date) { Date dateValue = DateUtils.truncate((Date) fieldValue, Calendar.DATE); Date otherDateValue = DateUtils.truncate((Date) otherValue, Calendar.DATE); return (dateValue.compareTo(otherDateValue) > 0); } return true; }
From source file:com.sqewd.open.dal.api.persistence.AbstractEntity.java
/** * Get a string value representing the key columns. * /*w w w .j a va2 s . c om*/ * @return * @throws Exception */ @JsonIgnore public String getEntityKey() throws Exception { StringBuffer buff = new StringBuffer(); StructEntityReflect enref = ReflectionUtils.get().getEntityMetadata(getClass()); for (StructAttributeReflect attr : enref.Attributes) { if (attr.IsKeyColumn) { if (buff.length() > 0) buff.append(_KEY_SEPARATOR_); Object value = PropertyUtils.getSimpleProperty(this, attr.Field.getName()); if (value instanceof AbstractEntity) { buff.append(((AbstractEntity) value).getEntityKey()); } else { buff.append(value); } } } if (buff.length() == 0) return null; return buff.toString(); }
From source file:com.yukthi.validators.MandatoryOptionValidator.java
@Override public boolean isValid(Object bean, Object fieldValue) { //if value is provided for current field if (fieldValue != null) { return true; }//from w w w. ja va 2 s . com Object otherValue = null; String field = null; //check if value is provided for at least one field try { for (String otherField : fields) { field = otherField; otherValue = PropertyUtils.getSimpleProperty(bean, otherField); if (otherValue != null) { return true; } } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new IllegalStateException("Invalid/inaccessible property \"" + field + "\" specified with MandatoryOption validator on field: " + bean.getClass().getName() + "." + super.field.getName()); } return false; }
From source file:com.github.aynu.mosir.core.enterprise.persistence.StandardRepositoryImpl.java
/** * ???//from w ww. jav a 2 s . co m * @param object * @param fields ? * @return ? */ protected Map<String, Object> filter(final R object, final String... fields) { final Map<String, Object> filter = new HashMap<>(); for (final String field : fields) { try { filter.put(field, PropertyUtils.getSimpleProperty(object, field)); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new EnterpriseRuntimeException(e.getLocalizedMessage()); } } return filter; }
From source file:edu.duke.cabig.c3pr.utils.BeanUtils.java
/** * This methods performs deep comparison of two objects of the same class. * Comparison is performed only on properties exposed via the standard * JavaBean mechanism. Properties of primitive types, wrappers, * {@link String}, {@link CharSequence}, {@link Date}, {@link Enum} are * compared directly using {@link Object#equals(Object)}; other complex * properties are compared recursively. Elements of {@link Collection} * properties are iterated and compared. * /*from w w w. j av a 2 s . c om*/ * @param <T> * @param obj1 * @param obj2 * @return * @throws NullPointerException * if any of the parameters is null. */ public static <T> boolean deepCompare(T obj1, T obj2) { if (obj1 == obj2) { return true; } // if it's a "simple" object, do direct comparison. for (Class<?> cls : DIRECTLY_COMPARABLE_TYPES) { if (cls.isAssignableFrom(obj1.getClass())) { if (!obj1.equals(obj2)) { log.info("Values don't match: " + obj1 + " and " + obj2); System.out.println(); System.out.println("Values don't match: " + obj1 + " and " + obj2); return false; } else { return true; } } } try { PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(obj1.getClass()); for (PropertyDescriptor pd : descriptors) { // ignore properties which cannot be read. if (pd.getReadMethod() != null) { Class<?> type = pd.getPropertyType(); // this check will skip Object.getClass(). if (SKIP_TYPES.contains(type)) { continue; } String name = pd.getName(); Object v1 = PropertyUtils.getSimpleProperty(obj1, name); Object v2 = PropertyUtils.getSimpleProperty(obj2, name); if (v1 == v2 || (v1 == null && v2 == null)) { continue; } if ((v1 == null && v2 != null) || (v1 != null && v2 == null)) { log.info("Values don't match: " + v1 + " and " + v2); System.out.println(); System.out.println("Values don't match: " + v1 + " and " + v2); return false; } // Collections need special handling. if (Collection.class.isAssignableFrom(type)) { List l1 = new ArrayList((Collection) v1); List l2 = new ArrayList((Collection) v2); if (l1.size() != l2.size()) { log.info("Collection sizes don't match:" + l1 + l2); System.out.println(); System.out.println("Collection sizes don't match:" + l1 + ", " + l2); return false; } for (int i = 0; i < l1.size(); i++) { Object el1 = l1.get(i); Object el2 = l2.get(i); if (!deepCompare(el1, el2)) { return false; } } } else if (!deepCompare(v1, v2)) { return false; } } } } catch (Exception e) { throw new RuntimeException(ExceptionUtils.getFullStackTrace(e)); } return true; }
From source file:com.glweb.web.struts.actions.HelloAction.java
/** * @see org.apache.struts.action.Action#execute(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) *//*from www .j ava2s . c o m*/ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession _session = request.getSession(); String _action = (String) PropertyUtils.getSimpleProperty(form, "action"); if (null == _action) { return (mapping.getInputForward()); } // Was this transaction cancelled? if (isCancelled(request)) { if (getLogger().isInfoEnabled()) { getLogger().info(" " + mapping.getAttribute() + " - Hello transaction was cancelled"); } removeFormBean(mapping, request); _session.removeAttribute(Constants.USER_KEY); return (mapping.findForward("/view/hello/cancel")); } User _user = null; String _name = (String) PropertyUtils.getSimpleProperty(form, "name"); _user = new User(); _user.setName(_name); if (getLogger().isInfoEnabled()) { getLogger().info("user = " + _user); } _session.setAttribute(Constants.USER_KEY, _user); return (mapping.findForward("/view/hello/success")); }
From source file:com.mb.framework.util.property.PropertyUtilExt.java
/** * Copy property values from the origin bean to the destination bean for all * cases where the property names are the same. For each property, a * conversion is attempted as necessary. All combinations of standard * JavaBeans and DynaBeans as origin and destination are supported. * Properties that exist in the origin bean, but do not exist in the * destination bean (or are read-only in the destination bean) are silently * ignored.// w w w .ja v a2 s . co m * <p> * In addition to the method with the same name in the * <code>org.apache.commons.beanutils.PropertyUtils</code> class this method * can also copy properties of the following types: * <ul> * <li>java.lang.Integer</li> * <li>java.lang.Double</li> * <li>java.lang.Long</li> * <li>java.lang.Short</li> * <li>java.lang.Float</li> * <li>java.lang.String</li> * <li>java.lang.Boolean</li> * <li>java.sql.Date</li> * <li>java.sql.Time</li> * <li>java.sql.Timestamp</li> * <li>java.math.BigDecimal</li> * <li>a container-managed relations field.</li> * </ul> * * @param dest * Destination bean whose properties are modified * @param orig * Origin bean whose properties are retrieved * @throws IllegalAccessException * if the caller does not have access to the property accessor * method * @throws InvocationTargetException * if the property accessor method throws an exception * @throws NoSuchMethodException * if an accessor method for this propety cannot be found * @throws ClassNotFoundException * if an incorrect relations class mapping exists. * @throws InstantiationException * if an object of the mapped relations class can not be * constructed. */ public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, Exception { PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig); for (int i = 0; i < origDescriptors.length; i++) { String name = origDescriptors[i].getName(); if (PropertyUtils.getPropertyDescriptor(dest, name) != null) { Object origValue = PropertyUtils.getSimpleProperty(orig, name); String origParamType = origDescriptors[i].getPropertyType().getName(); try { // edited // if (origValue == null)throw new NullPointerException(); PropertyUtils.setSimpleProperty(dest, name, origValue); } catch (Exception e) { try { String destParamType = PropertyUtils.getPropertyType(dest, name).getName(); if (origValue instanceof String) { if (destParamType.equals("java.lang.Integer")) { Integer intValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { intValue = new Integer(sValue); } PropertyUtils.setSimpleProperty(dest, name, intValue); } else if (destParamType.equals("java.lang.Byte")) { Byte byteValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { byteValue = new Byte(sValue); } PropertyUtils.setSimpleProperty(dest, name, byteValue); } else if (destParamType.equals("java.lang.Double")) { Double doubleValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { doubleValue = new Double(sValue); } PropertyUtils.setSimpleProperty(dest, name, doubleValue); } else if (destParamType.equals("java.lang.Long")) { Long longValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { longValue = new Long(sValue); } PropertyUtils.setSimpleProperty(dest, name, longValue); } else if (destParamType.equals("java.lang.Short")) { Short shortValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { shortValue = new Short(sValue); } PropertyUtils.setSimpleProperty(dest, name, shortValue); } else if (destParamType.equals("java.lang.Float")) { Float floatValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { floatValue = new Float(sValue); } PropertyUtils.setSimpleProperty(dest, name, floatValue); } else if (destParamType.equals("java.sql.Date")) { java.sql.Date dateValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { dateValue = new java.sql.Date(DATE_FORMATTER.parse(sValue).getTime()); } PropertyUtils.setSimpleProperty(dest, name, dateValue); } else if (destParamType.equals("java.sql.Time")) { java.sql.Time dateValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { dateValue = new java.sql.Time(TIME_FORMATTER.parse(sValue).getTime()); } PropertyUtils.setSimpleProperty(dest, name, dateValue); } else if (destParamType.equals("java.sql.Timestamp")) { java.sql.Timestamp dateValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { dateValue = new java.sql.Timestamp(TIMESTAMP_FORMATTER.parse(sValue).getTime()); } PropertyUtils.setSimpleProperty(dest, name, dateValue); } else if (destParamType.equals("java.lang.Boolean")) { Boolean bValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { bValue = Boolean.valueOf(sValue); } PropertyUtils.setSimpleProperty(dest, name, bValue); } else if (destParamType.equals("java.math.BigDecimal")) { BigDecimal bdValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { bdValue = new BigDecimal(sValue); } PropertyUtils.setSimpleProperty(dest, name, bdValue); } } else if ((origValue != null) && (destParamType.equals("java.lang.String"))) { // we're transferring a business-layer value object // into a String-based Struts form bean.. if ("java.sql.Date".equals(origParamType)) { PropertyUtils.setSimpleProperty(dest, name, DATE_FORMATTER.format(origValue)); } else if ("java.sql.Timestamp".equals(origParamType)) { PropertyUtils.setSimpleProperty(dest, name, TIMESTAMP_FORMATTER.format(origValue)); } else if ("java.sql.Blob".equals(origParamType)) { // convert a Blob to a String.. Blob blob = (Blob) origValue; BufferedInputStream bin = null; try { int bytesRead; StringBuffer result = new StringBuffer(); byte[] buffer = new byte[READ_BUFFER_LENGTH]; bin = new BufferedInputStream(blob.getBinaryStream()); do { bytesRead = bin.read(buffer); if (bytesRead != -1) { result.append(new String(buffer, 0, bytesRead)); } } while (bytesRead == READ_BUFFER_LENGTH); PropertyUtils.setSimpleProperty(dest, name, result.toString()); } finally { if (bin != null) try { bin.close(); } catch (IOException ignored) { } } } else { PropertyUtils.setSimpleProperty(dest, name, origValue.toString()); } } } catch (Exception e2) { throw e2; } } } } }
From source file:com.roadmap.common.util.ObjectUtil.java
/** * Return the value of the specified simple property of the * specified bean, with no type conversions. * * @param Object object whose property is to be extracted * @param String name of the property to be extracted * //from w w w . ja va2 s.co m * @return Object The property value */ @SuppressWarnings("rawtypes") public static Object getPropertyValue(Object vo, String prop) { Object retVal = null; try { if (vo instanceof Map) { if (vo != null) { retVal = ((Map) vo).get(prop); } } else { retVal = PropertyUtils.getSimpleProperty(vo, prop); } } catch (Exception e) { //No need to handle } return retVal; }