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.lioland.harmony.services.dao.ODBClass.java

private ODocument createODocument(ODBClass odbc) {
    Class cls = odbc.getClass();/*from  w ww.  j  a  v a  2  s  .c  om*/
    ODocument doc;
    if (rid == null) {
        doc = new ODocument(cls.getSimpleName());
    } else {
        doc = getODocument();
    }
    for (Field field : cls.getDeclaredFields()) {
        try {
            String fieldName = field.getName();
            Object fieldValue = PropertyUtils.getProperty(odbc, fieldName);
            if (fieldValue instanceof ODBClass) {
                fieldValue = createODocument((ODBClass) fieldValue);
            }
            if (fieldValue != null) {
                doc.field(fieldName, fieldValue);
            }
        } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException
                | NoSuchMethodException ex) {
            Logger.getLogger(ODBClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return doc;
}

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

static ActivityStreamWithBLOBs constructActivity(Class<?> cls, Traceable traceableAnnotation, Object bean,
        String username, String action)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    ActivityStreamWithBLOBs activity = new ActivityStreamWithBLOBs();
    activity.setModule(ClassInfoMap.getModule(cls));
    activity.setType(ClassInfoMap.getType(cls));
    activity.setTypeid(String.valueOf(PropertyUtils.getProperty(bean, traceableAnnotation.idField())));
    activity.setCreatedtime(new GregorianCalendar().getTime());
    activity.setAction(action);// w  w w. jav  a  2  s . c  o m
    activity.setSaccountid((Integer) PropertyUtils.getProperty(bean, "saccountid"));
    activity.setCreateduser(username);

    Object nameObj = PropertyUtils.getProperty(bean, traceableAnnotation.nameField());
    String nameField;
    if (nameObj instanceof Date) {
        nameField = DateTimeUtils.formatDate((Date) nameObj, "MM/dd/yyyy");
    } else {
        nameField = nameObj.toString();
    }
    activity.setNamefield(nameField);

    if (!"".equals(traceableAnnotation.extraFieldName())) {
        Integer extraTypeId = (Integer) PropertyUtils.getProperty(bean, traceableAnnotation.extraFieldName());
        activity.setExtratypeid(extraTypeId);
    }
    return activity;
}

From source file:fr.mycellar.test.matchers.PropertiesMatcher.java

/**
 * {@inheritDoc}//from ww  w .jav a  2 s .  c o  m
 */
@Override
protected final boolean matchesSafely(T item, Description mismatchDescription) {
    boolean matches = true;
    mismatchDescription.appendText("had {");
    for (String property : matchers.keySet()) {
        Matcher<?> matcher = matchers.get(property);
        Object subItem;
        try {
            subItem = PropertyUtils.getProperty(item, property);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(
                    "Cannot get " + property + " from " + item.getClass() + " : " + e.getMessage(), e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(
                    "Cannot get " + property + " from " + item.getClass() + " : " + e.getMessage(), e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(
                    "Cannot get " + property + " from " + item.getClass() + " : " + e.getMessage(), e);
        }
        if (!matcher.matches(subItem)) {
            if (!matches) {
                mismatchDescription.appendText(", ");
            }
            mismatchDescription.appendText(property).appendText(" ");
            matcher.describeMismatch(subItem, mismatchDescription);
            matches = false;
        }
    }
    mismatchDescription.appendText("}");
    return matches;
}

From source file:com.redhat.rhn.common.validator.RequiredIfConstraint.java

/**
* Check the constraint against the value passed in as well as check the
* objectToCheck's referenced field that is 'requiredIf'.
* @param value the value we want to check to see if its valid or not only
*        if the objectToCheck's referenced requiredIf field is set to the
*        proper value./*from   w  w w.jav  a 2s. c  o m*/
* @param objectToCheck the Object we will query to find the associated field
* @return if the field is required or not.
*/
public boolean isRequired(Object value, Object objectToCheck) {

    String strValue = (String) value;
    Iterator i = fieldValueList.iterator();
    // Default to true, the
    boolean required = true;
    while (i.hasNext()) {
        // We have requiredIf fields defined, so now
        // we need to switch required to false unless
        // the below code changes that fact
        required = false;
        Map field = (Map) i.next();
        // The fieldValueList contains name/value pairs
        String fieldName = (String) field.keySet().toArray()[0];
        String fieldValue = (String) field.get(fieldName);
        // Get the field we want to check against
        String requiredIfValue = null;
        try {
            requiredIfValue = PropertyUtils.getProperty(objectToCheck, fieldName).toString();
            /*
             * Check for requiredIf tag without a value. This is the
             * equivalent of saying that this field is required if this
             * other field is not null.
             */
            // required tag doesn't contain a value
            if ((fieldValue == null || fieldValue.length() == 0) &&
            // but something is in the required field
                    (requiredIfValue != null && requiredIfValue.length() > 0)) {
                required = true; // set this required = true
            } else if (requiredIfValue.equals(fieldValue)) {
                log.debug("RequiredIf actual Value: " + requiredIfValue);
                log.debug("Requiredvalue: " + fieldValue);
                log.debug("Actual fieldvalue: " + value);
                required = true;
            }
            // If any of the fields match, we return true immediately
            if (required) {
                return required;
            }
        } catch (Exception e) {
            String errorMessage = "Exception trying to get bean property: " + e.toString();
            log.error(errorMessage, e);
            throw new ValidatorException(errorMessage, e);
        }
    }
    return required;
}

From source file:gov.nih.nci.calims2.ui.common.externalidentifier.ExternalIdentifierFormTest.java

/**
 * Test the controller getSubmittedEntity method.
 * @throws Exception /*from   w  w w  .j  a v  a  2  s  .  c  o m*/
 */
@SuppressWarnings("unchecked")
@Test
public void testGetSubmittedEntity() throws Exception {
    ExternalIdentifierForm form = new ExternalIdentifierForm();
    form.setStatus(ExternalIdentifierStatus.DEFAULT.getName());
    Stack<StackFrame> context = FlowContextHolder.newContext();
    StackFrame frame = new BaseStackFrame();
    context.push(frame);
    frame.addAttribute("persistentClass", persistentClass);
    frame.addAttribute("id", new Long(1));
    ExternalIdentifier entity = form.getSubmittedEntity();
    EntityWithId result = (EntityWithId) PropertyUtils.getProperty(entity, propertyName);
    CRUDFormAssert.assertEntity((Class<EntityWithId>) persistentClass, result, 1L);
    assertEquals("Wrong status in the entity", ExternalIdentifierStatus.DEFAULT, entity.getStatus());
}

From source file:com.esofthead.mycollab.module.project.view.settings.component.ProjectMemberMultiSelectComp.java

@Override
protected ItemSelectionComp<SimpleProjectMember> buildItem(final SimpleProjectMember item) {
    ItemSelectionComp<SimpleProjectMember> buildItem = super.buildItem(item);
    String userAvatarId = "";

    try {//from www.  j a v  a2s  .c o  m
        userAvatarId = (String) PropertyUtils.getProperty(item, "memberAvatarId");
    } catch (Exception e) {
        LOG.error("Error while getting project member avatar", e);
    }

    buildItem.setIcon(UserAvatarControlFactory.createAvatarResource(userAvatarId, 16));
    return buildItem;
}

From source file:javax.faces.component.AbstractUIComponentPropertyTest.java

@Test
public void testExplicitValue() throws Exception {
    for (T testValue : _testValues) {
        PropertyUtils.setProperty(_component, _property, testValue);
        Assert.assertEquals(testValue, PropertyUtils.getProperty(_component, _property));
    }//from w ww .  ja  va  2s.co m
}

From source file:br.com.renatoccosta.regexrenamer.api.element.Element.java

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();

    sb.append("<").append(getId());

    Field[] fields = this.getClass().getDeclaredFields();
    for (Field f : fields) {
        Parameter p = f.getAnnotation(Parameter.class);
        if (p != null) {
            String alias = p.alias().equals("") ? f.getName() : p.alias();

            try {
                sb.append(" ").append(alias).append("='").append(PropertyUtils.getProperty(this, f.getName()))
                        .append("'");
            } catch (IllegalAccessException ex) {
            } catch (InvocationTargetException ex) {
            } catch (NoSuchMethodException ex) {
            }/*from w w w.  j  av  a2  s.c  om*/
        }
    }

    return sb.toString();
}

From source file:br.com.renatoccosta.regexrenamer.view.ElementParametersTableModel.java

public Object getValueAt(int rowIndex, int columnIndex) {
    if (columnIndex == 0) {
        //column titles
        return params[rowIndex].getCaption();
    } else {//  w  w  w .j  a  v a  2 s  . c o  m
        try {
            //column values
            return PropertyUtils.getProperty(element, params[rowIndex].getField().getName());
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }
}

From source file:net.mlw.vlh.swing.support.DyanBeanTableModel.java

/**
 * @see javax.swing.table.TableModel#getValueAt(int, int)
 *//* ww w  . j a v a2  s.  c  o  m*/
public Object getValueAt(int row, int column) {
    try {
        Object bean = list.get(row);
        if (bean == null) {
            return null;
        }
        return PropertyUtils.getProperty(bean, properties[column]);
    } catch (Exception e) {
        return e;
    }
}