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

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

Introduction

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

Prototype

public static Object getProperty(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:jp.co.ctc_g.jse.core.validation.constraints.feature.afterequalsto.AfterEqualsToValidator.java

/**
 * {@inheritDoc}//from   w  w w.  j  a v a 2s.  c  o  m
 */
@Override
public boolean isValid(Object suspect, ConstraintValidatorContext context) {
    Object f = null;
    Object t = null;
    try {
        f = PropertyUtils.getProperty(suspect, afterEqualsTo.from());
        t = PropertyUtils.getProperty(suspect, afterEqualsTo.to());
    } catch (IllegalAccessException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-AFTER-EQUALS-TO#0001"),
                    Maps.hash("from", afterEqualsTo.from()).map("to", afterEqualsTo.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-AFTER-EQUALS-TO#0001", e);
    } catch (InvocationTargetException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-AFTER-EQUALS-TO#0002"),
                    Maps.hash("from", afterEqualsTo.from()).map("to", afterEqualsTo.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-AFTER-EQUALS-TO#0002", e);
    } catch (NoSuchMethodException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-AFTER-EQUALS-TO#0003"),
                    Maps.hash("from", afterEqualsTo.from()).map("to", afterEqualsTo.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-AFTER-EQUALS-TO#0003", e);
    }
    if (f == null || t == null)
        return true;
    return Validators.afterEqualsTo(Validators.toDate(f, afterEqualsTo.pattern()),
            Validators.toDate(t, afterEqualsTo.pattern())) ? true : addErrors(context);
}

From source file:jp.co.ctc_g.jse.core.validation.constraints.feature.lessthan.LessThanValidator.java

/**
 * {@inheritDoc}/*w  w w . j  ava  2 s .c  o  m*/
 */
@Override
public boolean isValid(Object suspect, ConstraintValidatorContext context) {
    Object f = null;
    Object t = null;
    try {
        f = PropertyUtils.getProperty(suspect, lessThan.from());
        t = PropertyUtils.getProperty(suspect, lessThan.to());
    } catch (IllegalAccessException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-LESS-THAN#0001"),
                    Maps.hash("from", lessThan.from()).map("to", lessThan.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-LESS-THAN#0001", e);
    } catch (InvocationTargetException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-LESS-THAN#0002"),
                    Maps.hash("from", lessThan.from()).map("to", lessThan.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-LESS-THAN#0002", e);
    } catch (NoSuchMethodException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-LESS-THAN#0003"),
                    Maps.hash("from", lessThan.from()).map("to", lessThan.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-LESS-THAN#0003", e);
    }
    if (f == null || t == null)
        return true;
    return Validators.lessThan(Validators.toBigDecimal(f), Validators.toBigDecimal(t)) ? true
            : addErrors(context);
}

From source file:ch.algotrader.esper.io.CollectionInputAdapter.java

@Override
public SendableEvent read() throws EPException {
    if (this.stateManager.getState() == AdapterState.DESTROYED) {
        return null;
    }//  ww w . ja  v  a2s  .co  m

    if (this.eventsToSend.isEmpty()) {

        if (this.iterator.hasNext()) {

            try {
                Object baseObject = this.iterator.next();
                Date date = (Date) PropertyUtils.getProperty(baseObject, this.timeStampColumn);

                if (date == null) {
                    throw new IllegalStateException("missing time stamp");
                }

                return new SendableBaseObjectEvent(baseObject, date.getTime(), this.scheduleSlot);
            } catch (Exception e) {
                throw new EPException("problem getting timestamp column", e);
            }

        } else {
            if (this.stateManager.getState() == AdapterState.STARTED) {
                stop();
            } else {
                destroy();
            }
            return null;
        }
    } else {
        SendableEvent event = this.eventsToSend.first();
        this.eventsToSend.remove(event);
        return event;
    }
}

From source file:edu.northwestern.bioinformatics.studycalendar.domain.tools.hibernate.ScheduledActivityStateType.java

public Object getPropertyValue(Object component, int property) throws HibernateException {
    String name = getPropertyNames()[property];
    if (PropertyUtils.isReadable(component, name)) {
        try {//from  www.ja  v  a  2 s.  co  m
            return PropertyUtils.getProperty(component, name);
        } catch (IllegalAccessException e) {
            throw new HibernateException("Failed to read property " + name + " from " + component, e);
        } catch (InvocationTargetException e) {
            throw new HibernateException("Failed to read property " + name + " from " + component, e);
        } catch (NoSuchMethodException e) {
            throw new HibernateException("Failed to read property " + name + " from " + component, e);
        }
    } else {
        return null;
    }
}

From source file:jp.co.ctc_g.jse.core.validation.constraints.feature.beforeequalsto.BeforeEqualsToValidator.java

/**
 * {@inheritDoc}//  www.  j ava  2 s .co  m
 */
@Override
public boolean isValid(Object suspect, ConstraintValidatorContext context) {
    Object f = null;
    Object t = null;
    try {
        f = PropertyUtils.getProperty(suspect, beforeEqualsTo.from());
        t = PropertyUtils.getProperty(suspect, beforeEqualsTo.to());
    } catch (IllegalAccessException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-BEFORE-EQUALS-TO#0001"),
                    Maps.hash("from", beforeEqualsTo.from()).map("to", beforeEqualsTo.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-BEFORE-EQUALS-TO#0001", e);
    } catch (InvocationTargetException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-BEFORE-EQUALS-TO#0002"),
                    Maps.hash("from", beforeEqualsTo.from()).map("to", beforeEqualsTo.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-BEFORE-EQUALS-TO#0002", e);
    } catch (NoSuchMethodException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-BEFORE-EQUALS-TO#0003"),
                    Maps.hash("from", beforeEqualsTo.from()).map("to", beforeEqualsTo.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-BEFORE-EQUALS-TO#0003", e);
    }
    if (f == null || t == null)
        return true;
    return Validators.beforeEqualsTo(Validators.toDate(f, beforeEqualsTo.pattern()),
            Validators.toDate(t, beforeEqualsTo.pattern())) ? true : addErrors(context);
}

From source file:com.esofthead.mycollab.common.interceptor.aspect.MonitorItemAspect.java

@AfterReturning("execution(public * com.esofthead.mycollab..service..*.saveWithSession(..)) && args(bean, username)")
public void traceSaveActivity(JoinPoint joinPoint, Object bean, String username) {
    Advised advised = (Advised) joinPoint.getThis();
    Class<?> cls = advised.getTargetSource().getTargetClass();
    try {/*from  w  w  w .jav  a 2  s . c  om*/
        Watchable watchableAnnotation = cls.getAnnotation(Watchable.class);
        if (watchableAnnotation != null) {
            int sAccountId = (Integer) PropertyUtils.getProperty(bean, "saccountid");
            int typeId = (Integer) PropertyUtils.getProperty(bean, "id");
            Integer extraTypeId = null;
            if (!"".equals(watchableAnnotation.extraTypeId())) {
                extraTypeId = (Integer) PropertyUtils.getProperty(bean, watchableAnnotation.extraTypeId());
            }

            MonitorItem monitorItem = new MonitorItem();
            monitorItem.setMonitorDate(new GregorianCalendar().getTime());
            monitorItem.setType(ClassInfoMap.getType(cls));
            monitorItem.setTypeid(typeId);
            monitorItem.setExtratypeid(extraTypeId);
            monitorItem.setUser(username);
            monitorItem.setSaccountid(sAccountId);

            monitorItemService.saveWithSession(monitorItem, username);
            LOG.debug("Save monitor item: " + BeanUtility.printBeanObj(monitorItem));

            if (!watchableAnnotation.userFieldName().equals("")) {
                String moreUser = (String) PropertyUtils.getProperty(bean, watchableAnnotation.userFieldName());
                if (moreUser != null && !moreUser.equals(username)) {
                    monitorItem.setId(null);
                    monitorItem.setUser(moreUser);
                    monitorItemService.saveWithSession(monitorItem, moreUser);
                }
            }
        }

        NotifyAgent notifyAgent = cls.getAnnotation(NotifyAgent.class);
        if (notifyAgent != null) {
            int sAccountId = (Integer) PropertyUtils.getProperty(bean, "saccountid");
            int typeId = (Integer) PropertyUtils.getProperty(bean, "id");
            RelayEmailNotificationWithBLOBs relayNotification = new RelayEmailNotificationWithBLOBs();
            relayNotification.setChangeby(username);
            relayNotification.setChangecomment("");
            relayNotification.setSaccountid(sAccountId);
            relayNotification.setType(ClassInfoMap.getType(cls));
            relayNotification.setAction(MonitorTypeConstants.CREATE_ACTION);
            relayNotification.setTypeid("" + typeId);
            relayNotification.setEmailhandlerbean(notifyAgent.value().getName());
            relayEmailNotificationService.saveWithSession(relayNotification, username);
            // Save notification item
        }
    } catch (Exception e) {
        LOG.error("Error when save relay email notification for save action of service " + cls.getName(), e);
    }
}

From source file:jp.co.ctc_g.jse.core.validation.constraints.feature.greaterthan.GreaterThanValidator.java

/**
 * {@inheritDoc}/* w  ww .j  av a 2s.  c  o m*/
 */
@Override
public boolean isValid(Object suspect, ConstraintValidatorContext context) {
    Object f = null;
    Object t = null;
    try {
        f = PropertyUtils.getProperty(suspect, greaterThan.from());
        t = PropertyUtils.getProperty(suspect, greaterThan.to());
    } catch (IllegalAccessException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-GREATER-THAN#0001"),
                    Maps.hash("from", greaterThan.from()).map("to", greaterThan.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-GREATER-THAN#0001", e);
    } catch (InvocationTargetException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-GREATER-THAN#0002"),
                    Maps.hash("from", greaterThan.from()).map("to", greaterThan.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-GREATER-THAN#0002", e);
    } catch (NoSuchMethodException e) {
        if (L.isDebugEnabled()) {
            L.debug(Strings.substitute(R.getString("D-VALIDATOR-GREATER-THAN#0003"),
                    Maps.hash("from", greaterThan.from()).map("to", greaterThan.to()).map("target",
                            suspect.getClass().getSimpleName())));
        }
        throw new InternalException(AfterValidator.class, "E-VALIDATOR-GREATER-THAN#0003", e);
    }
    if (f == null || t == null)
        return true;
    return Validators.greaterThan(Validators.toBigDecimal(f), Validators.toBigDecimal(t)) ? true
            : addErrors(context);
}

From source file:com.redhat.rhn.common.util.DynamicComparator.java

/**
 * {@inheritDoc}//from  ww  w . java 2s .c o m
 */
public int compare(Object o1, Object o2) {
    Comparable val1 = null;
    Comparable val2 = null;
    try {
        val1 = (Comparable) PropertyUtils.getProperty(o1, fieldName);
        val2 = (Comparable) PropertyUtils.getProperty(o2, fieldName);
        if (val1 instanceof String && val2 instanceof String) {
            return order * getCollator().compare(val1, val2);
        }
        // a < b = -1, a > b = 1 , a== b =0

        if (val1 == null && val2 != null) {
            return order * -1;
        } else if (val1 != null && val2 == null) {
            return order * 1;
        } else if (val1 == val2) {
            return 0;
        }
        return order * val1.compareTo(val2);
    } catch (Exception e) {
        throw new IllegalArgumentException("Exception trying to compare " + "two objects: o1: " + o1 + " o2: "
                + o2 + " with field: " + this.fieldName + " generated this exception: " + e);
    }
}

From source file:de.topicmapslab.kuria.runtime.TextBinding.java

/**
 *  {@inheritDoc}/*from  www .  j a  va  2  s.  c  om*/
 */
public String getText(Object instance)
        throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    if (fieldName == null)
        return instance.toString();
    Object object = PropertyUtils.getProperty(instance, fieldName);
    if (object == null)
        return "<null>";

    if (object instanceof String)
        return (String) object;

    return object.toString();
}

From source file:ca.sqlpower.testutil.TestUtils.java

/**
 * Sets all the settable properties on the given target object which are not
 * in the given ignore set.//from  w  ww.j  av a  2 s  .c om
 * <p>
 * TODO merge this with what is in Architect's TestUtils class. This was
 * originally refactored out of there.
 * 
 * @param target
 *            The object to change the properties of
 * @param propertiesToIgnore
 *            The properties of target not to modify or read
 * @return A Map describing the new values of all the non-ignored, readable
 *         properties in target.
 */
public static Map<String, Object> setAllInterestingProperties(Object target, Set<String> propertiesToIgnore,
        NewValueMaker valueMaker) throws Exception {

    PropertyDescriptor props[] = PropertyUtils.getPropertyDescriptors(target);
    for (int i = 0; i < props.length; i++) {
        Object oldVal = null;
        if (PropertyUtils.isReadable(target, props[i].getName()) && props[i].getReadMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {
            oldVal = PropertyUtils.getProperty(target, props[i].getName());
        }
        if (PropertyUtils.isWriteable(target, props[i].getName()) && props[i].getWriteMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {

            Object newVal = valueMaker.makeNewValue(props[i].getPropertyType(), oldVal, props[i].getName());

            System.out.println("Changing property \"" + props[i].getName() + "\" to \"" + newVal + "\"");
            PropertyUtils.setProperty(target, props[i].getName(), newVal);

        }
    }

    // read them all back at the end in case there were dependencies between properties
    return TestUtils.getAllInterestingProperties(target, propertiesToIgnore);
}