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:com.mycollab.db.persistence.service.DefaultCrudService.java

@Override
public Integer saveWithSession(T record, String username) {
    if (!StringUtils.isBlank(username)) {
        try {/*from w w  w  . j  av a2s  .  com*/
            PropertyUtils.setProperty(record, "createduser", username);
        } catch (Exception e) {
        }
    }

    try {
        PropertyUtils.setProperty(record, "createdtime", new GregorianCalendar().getTime());
        PropertyUtils.setProperty(record, "lastupdatedtime", new GregorianCalendar().getTime());
    } catch (Exception e) {
    }

    getCrudMapper().insertAndReturnKey(record);
    try {
        return (Integer) PropertyUtils.getProperty(record, "id");
    } catch (Exception e) {
        return 0;
    }
}

From source file:com.ixcode.framework.javabean.format.JavaBeanFormatter.java

public String getPropertyValueAsString(Object javaBean, String propertyName, Locale locale)
        throws FormatterException {
    try {//from   w ww  .j a  v a2 s .c  om
        Object value = PropertyUtils.getProperty(javaBean, propertyName);

        String formatted;

        if (value instanceof String) {
            formatted = (String) value;
        } else if (value == null) {
            formatted = "";
        } else {
            Class propertyType = IntrospectionUtils.getPropertyType(javaBean, propertyName);
            IJavaBeanValueFormat format = getFormat(locale, propertyType);
            formatted = format.format(value);

        }

        return formatted;

    } catch (IllegalAccessException e) {
        throw new FormatterException(IntrospectionUtils.getJavaBeanName(javaBean), e);
    } catch (InvocationTargetException e) {
        throw new FormatterException(IntrospectionUtils.getJavaBeanName(javaBean), e.getCause());
    } catch (NoSuchMethodException e) {
        throw new FormatterException(IntrospectionUtils.getJavaBeanName(javaBean), e);
    } catch (JavaBeanException e) {
        throw new FormatterException(IntrospectionUtils.getJavaBeanName(javaBean), e);
    }
}

From source file:io.github.moosbusch.lumpi.gui.impl.SelectionAdapter.java

@Override
@SuppressWarnings(value = "unchecked")
public T evaluate() throws Exception {
    if (getExpression().isEmpty()) {
        return getValue();
    } else {//w  w  w.j av  a2  s .c om
        Object result = PropertyUtils.getProperty(getValue(), getExpression().toString());
        if (result != null) {
            return (T) result;
        }
    }
    return null;
}

From source file:com.esofthead.mycollab.vaadin.mvp.BeanItemCustomExt.java

public BeanItemCustomExt(T bean) {
    super(bean);/*from   www.ja va  2  s  . com*/

    try {
        for (String customFieldName : customFieldNames) {
            String propName = "customfield." + customFieldName;
            this.addItemProperty(propName,
                    new NestedMethodProperty(PropertyUtils.getProperty(bean, "customfield"), customFieldName));
        }

    } catch (Exception e) {
        throw new MyCollabException(e);
    }

}

From source file:com.ponysdk.core.export.util.PropertyUtil.java

public static Object getPropertyValue(final Object bean, final String propertyName)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Object propertyValue;// www. jav  a  2  s.co  m
    if (PropertyUtils.isReadable(bean, propertyName)) {
        propertyValue = PropertyUtils.getProperty(bean, propertyName);
        if (propertyValue == null) {
            propertyValue = NA;
        }
    } else {
        propertyValue = NA;
    }
    return propertyValue;
}

From source file:de.awtools.bean.DefaultPropertyMapper.java

/**
 * Eventuelle Exceptions, die bei der Extraktion auftreten, werden in eine
 * <code>RuntimeException</code> umgewandelt.
 *
 * @see Mapper#getProperty(java.lang.Object)
 *//* w ww .j  av a  2s  .  com*/
public Object getProperty(final Object domainObject) {
    try {
        return PropertyUtils.getProperty(domainObject, property);
    } catch (IllegalAccessException ex) {
        throw new RuntimeException(ex);
    } catch (InvocationTargetException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchMethodException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.yukthi.validators.GreaterThanEqualsValidator.java

@Override
public boolean isValid(Object bean, Object fieldValue) {
    if (bean == null) {
        return true;
    }//from w ww  . ja  v  a2 s .  c om

    //fetch other field value
    Object otherValue = null;

    try {
        otherValue = PropertyUtils.getProperty(bean, greaterThanField);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
        throw new IllegalStateException("Invalid/inaccessible property \"" + greaterThanField
                + "\" specified with matchWith validator in bean: " + bean.getClass().getName());
    }

    //ensure other field value is present and is of same 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:jp.co.ctc_g.jse.core.validation.constraints.feature.after.AfterValidator.java

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

From source file:com.jeesoft.common.validator.FieldMatchValidator.java

public boolean isValid(final Object value, final ConstraintValidatorContext context) {
    boolean isFirstEqualSecond = false;

    try {/*  w  w  w  . ja v a 2s  . c  o  m*/
        final Object firstObj = PropertyUtils.getProperty(value, firstFieldName);
        final Object secondObj = PropertyUtils.getProperty(value, secondFieldName);

        isFirstEqualSecond = firstObj == null && secondObj == null
                || firstObj != null && firstObj.equals(secondObj);
    } catch (final Exception exception) {
        // TODO set the logger to log this error.
        System.out.println("Error occoured in com.jeesoft.common.validator.FieldMatchValidator :" + exception);
    }

    if (!isFirstEqualSecond) {
        context.buildConstraintViolationWithTemplate(errorMessage).addNode(secondFieldName)
                .addConstraintViolation();
    }

    return isFirstEqualSecond;
}

From source file:com.mycollab.mobile.module.crm.view.activity.RelatedItemSelectionField.java

@Override
public void setValue(Integer typeid) {
    try {/*from   w  w  w  . ja  va 2 s.co m*/
        String type = (String) PropertyUtils.getProperty(bean, "type");
        if (type != null && typeid != null) {
            if ("Account".equals(type)) {
                AccountService accountService = AppContextUtil.getSpringBean(AccountService.class);
                SimpleAccount account = accountService.findById(typeid, MyCollabUI.getAccountId());
                if (account != null) {
                    navButton.setCaption(account.getAccountname());
                }
            } else if ("Campaign".equals(type)) {
                CampaignService campaignService = AppContextUtil.getSpringBean(CampaignService.class);
                SimpleCampaign campaign = campaignService.findById(typeid, MyCollabUI.getAccountId());
                if (campaign != null) {
                    navButton.setCaption(campaign.getCampaignname());
                }
            } else if ("Contact".equals(type)) {
                ContactService contactService = AppContextUtil.getSpringBean(ContactService.class);
                SimpleContact contact = contactService.findById(typeid, MyCollabUI.getAccountId());
                if (contact != null) {
                    navButton.setCaption(contact.getContactName());
                }
            } else if ("Lead".equals(type)) {
                LeadService leadService = AppContextUtil.getSpringBean(LeadService.class);
                SimpleLead lead = leadService.findById(typeid, MyCollabUI.getAccountId());
                if (lead != null) {
                    navButton.setCaption(lead.getLeadName());
                }
            } else if ("Opportunity".equals(type)) {
                OpportunityService opportunityService = AppContextUtil.getSpringBean(OpportunityService.class);
                SimpleOpportunity opportunity = opportunityService.findById(typeid, MyCollabUI.getAccountId());
                if (opportunity != null) {
                    navButton.setCaption(opportunity.getOpportunityname());
                }
            } else if ("Case".equals(type)) {
                CaseService caseService = AppContextUtil.getSpringBean(CaseService.class);
                SimpleCase cases = caseService.findById(typeid, MyCollabUI.getAccountId());
                if (cases != null) {
                    navButton.setCaption(cases.getSubject());
                }
            }
        }
    } catch (Exception e) {
        LOG.error("Error when set type", e);
    }
}