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.esofthead.mycollab.common.interceptor.aspect.AuditLogAspect.java

@AfterReturning("(execution(public * com.esofthead.mycollab..service..*.updateWithSession(..)) || (execution(public * com.esofthead.mycollab..service..*.updateSelectiveWithSession(..))))  && args(bean, username)")
public void traceAfterUpdateActivity(JoinPoint joinPoint, Object bean, String username) {

    Advised advised = (Advised) joinPoint.getThis();
    Class<?> cls = advised.getTargetSource().getTargetClass();

    Traceable traceableAnnotation = cls.getAnnotation(Traceable.class);
    Integer activityStreamId = null;
    if (traceableAnnotation != null) {
        try {//from   ww  w.  jav a2 s  .  c o m
            ActivityStreamWithBLOBs activity = TraceableAspect.constructActivity(cls, traceableAnnotation, bean,
                    username, ActivityStreamConstants.ACTION_UPDATE);
            activityStreamId = activityStreamService.save(activity);
        } catch (Exception e) {
            LOG.error("Error when save activity for save action of service " + cls.getName(), e);
        }
    }

    try {
        Watchable watchableAnnotation = cls.getAnnotation(Watchable.class);
        if (watchableAnnotation != null) {
            String monitorType = ClassInfoMap.getType(cls);
            Integer 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(monitorType);
            monitorItem.setTypeid(typeId);
            monitorItem.setExtratypeid(extraTypeId);
            monitorItem.setUser(username);
            monitorItem.setSaccountid(sAccountId);
            monitorItemService.saveWithSession(monitorItem, username);

            // check whether the current user is in monitor list, if
            // not add him in
            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) {
            Integer sAccountId = (Integer) PropertyUtils.getProperty(bean, "saccountid");
            Integer auditLogId = saveAuditLog(cls, bean, username, sAccountId, activityStreamId);
            int typeId = (Integer) PropertyUtils.getProperty(bean, "id");
            // Save notification email
            RelayEmailNotificationWithBLOBs relayNotification = new RelayEmailNotificationWithBLOBs();
            relayNotification.setChangeby(username);
            relayNotification.setChangecomment("");
            relayNotification.setSaccountid(sAccountId);
            relayNotification.setType(ClassInfoMap.getType(cls));
            relayNotification.setTypeid("" + typeId);
            relayNotification.setEmailhandlerbean(notifyAgent.value().getName());
            if (auditLogId != null) {
                relayNotification.setExtratypeid(auditLogId);
            }

            relayNotification.setAction(MonitorTypeConstants.UPDATE_ACTION);

            relayEmailNotificationService.saveWithSession(relayNotification, username);
        }
    } catch (Exception e) {
        LOG.error("Error when save audit for save action of service " + cls.getName() + "and bean: "
                + BeanUtility.printBeanObj(bean), e);
    }
}

From source file:net.sf.excelutils.ExcelParser.java

/**
 * get the value from context by the expression
 * /*from   w ww .  j  av a  2 s  . c  o m*/
 * @param expr
 * @param context data object
 * @return Object the value of the expr
 */
public static Object getValue(Object context, String expr) {
    Object value = null;
    try {
        value = PropertyUtils.getProperty(context, expr);
        // ?
        int index = expr.indexOf(INDEXED_DELIM);
        int index2 = expr.indexOf(INDEXED_DELIM2, index);
        if (index >= 0 && index2 > 0 && index2 > index) {
            // ?,?1[]
            if (expr.indexOf(INDEXED_DELIM, index2) >= 0) {
                DynaBean bean = new LazyDynaBean();
                bean.set("list_value", value);
                value = getValue(bean, "list_value" + expr.substring(index2 + 1));
            }
        }
    } catch (Exception e) {
        return null;
    }
    return value;
}

From source file:com.yougou.merchant.api.common.MerchantLogTools.java

public static String buildMerchantAccountOperationNotes(MerchantUser source, MerchantUser target)
        throws Exception {
    if (target == null) {
        throw new NullPointerException("target");
    }//from w w  w. j  av  a 2  s. c om
    if (source == null) {
        return "??";
    }

    StringBuilder sb = new StringBuilder();
    for (Map.Entry<String, String> entry : ACCOUNT_TRANSLATABLE_FIELDS.entrySet()) {
        Object o1 = PropertyUtils.getProperty(source, entry.getKey());
        Object o2 = PropertyUtils.getProperty(target, entry.getKey());
        if (!ObjectUtils.equals(o1, o2)) {
            if (StringUtils.equals("status", entry.getKey())) {
                o1 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o1) ? "?" : "?";
                o2 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o2) ? "?" : "?";
            } else if (StringUtils.equals("isAdministrator", entry.getKey())
                    || StringUtils.equals("deleteFlag", entry.getKey())
                    || StringUtils.equals("isYougouAdmin", entry.getKey())) {
                o1 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o1) ? "" : "?";
                o2 = ObjectUtils.equals(NumberUtils.INTEGER_ONE, o2) ? "" : "?";
            }
            sb.append(MessageFormat.format("{0}??{1}?{2}{3}", entry.getValue(),
                    o1, o2, LINE_SEPARATOR));
        }
    }
    return sb.toString();
}

From source file:com.zb.app.common.core.lang.BeanUtils.java

/**
 * ?Beanmap,map/*from   ww w .  j a  v  a 2  s  .co  m*/
 * 
 * @param bean
 * @return
 */
public static Map<String, String> getFieldValueMap(Object bean) {
    Class<?> cls = bean.getClass();
    Map<String, String> valueMap = new LinkedHashMap<String, String>();
    Field[] fields = getAllFields(new ArrayList<Field>(), cls);

    for (Field field : fields) {
        try {
            if (field == null || field.getName() == null) {
                continue;
            }
            if (StringUtils.equals("serialVersionUID", field.getName())) {
                continue;
            }

            Object fieldVal = PropertyUtils.getProperty(bean, field.getName());

            String result = null;
            if (null != fieldVal) {
                if (StringUtils.equals("Date", field.getType().getSimpleName())) {
                    result = DateViewTools.formatFullDate((Date) fieldVal);
                } else {
                    result = String.valueOf(fieldVal);
                }
            }
            valueMap.put(field.getName(), result == null ? StringUtils.EMPTY : result);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            continue;
        }
    }
    return valueMap;
}

From source file:net.sourceforge.fenixedu.presentationTier.TagLib.OptionsTag.java

/**
 * Process the end of this tag./* w  w w.j a  va  2 s. co  m*/
 * 
 * @exception JspException
 *                if a JSP exception has occurred
 */
@Override
public int doEndTag() throws JspException {

    // Acquire the select tag we are associated with
    SelectTag selectTag = (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);
    if (selectTag == null) {
        throw new JspException(messages.getMessage("optionsTag.select"));
    }
    StringBuilder sb = new StringBuilder();

    // If a collection was specified, use that mode to render options
    if (collection != null) {
        Iterator collIterator = getIterator(collection, null);
        while (collIterator.hasNext()) {

            Object bean = collIterator.next();
            Object value = null;
            Object label = null;

            try {
                value = PropertyUtils.getProperty(bean, property);
                if (value == null) {
                    value = "";
                }
            } catch (IllegalAccessException e) {
                throw new JspException(messages.getMessage("getter.access", property, collection));
            } catch (InvocationTargetException e) {
                Throwable t = e.getTargetException();
                throw new JspException(messages.getMessage("getter.result", property, t.toString()));
            } catch (NoSuchMethodException e) {
                throw new JspException(messages.getMessage("getter.method", property, collection));
            }

            try {
                if (labelProperty != null) {
                    label = PropertyUtils.getProperty(bean, labelProperty);
                } else {
                    label = value;
                }
                if (label == null) {
                    label = "";
                }
            } catch (IllegalAccessException e) {
                throw new JspException(messages.getMessage("getter.access", labelProperty, collection));
            } catch (InvocationTargetException e) {
                Throwable t = e.getTargetException();
                throw new JspException(messages.getMessage("getter.result", labelProperty, t.toString()));
            } catch (NoSuchMethodException e) {
                throw new JspException(messages.getMessage("getter.method", labelProperty, collection));
            }

            String stringValue = value.toString();
            addOption(sb, stringValue, label.toString(), selectTag.isMatched(stringValue));

        }

    }

    // Otherwise, use the separate iterators mode to render options
    else {

        // Construct iterators for the values and labels collections
        Iterator valuesIterator = getIterator(name, property);
        Iterator labelsIterator = null;
        if ((labelName == null) && (labelProperty == null)) {
            labelsIterator = getIterator(name, property); // Same coll.
        } else {
            labelsIterator = getIterator(labelName, labelProperty);
        }

        // Render the options tags for each element of the values coll.
        while (valuesIterator.hasNext()) {
            String value = valuesIterator.next().toString();
            String label = value;
            if (labelsIterator.hasNext()) {
                label = labelsIterator.next().toString();
            }
            addOption(sb, value, label, selectTag.isMatched(value));
        }
    }

    // Render this element to our writer
    ResponseUtils.write(pageContext, sb.toString());

    // Evaluate the remainder of this page
    return EVAL_PAGE;

}

From source file:com.esofthead.mycollab.vaadin.ui.MultiSelectComp.java

private void displaySelectedItems() {
    componentsDisplay.setReadOnly(false);
    componentsDisplay.setValue(getDisplaySelectedItemsString());
    componentsDisplay.setReadOnly(true);
    Ul ul = new Ul();
    try {/*from w  w  w .  ja  v  a 2  s. c  o m*/
        for (T item : selectedItems) {
            String objDisplayName = (String) PropertyUtils.getProperty(item, propertyDisplayField);
            ul.appendChild(new Li().appendText(objDisplayName));
        }
    } catch (Exception e) {
        log.error("Error when build tooltip", e);
    }
    componentsDisplay.setDescription(ul.write());
}

From source file:com.fengduo.bee.commons.core.lang.BeanUtils.java

/**
 * ?Beanmap,map/* w w  w .  j a v a2 s .c o m*/
 * 
 * @param bean
 * @return
 */
public static Map<String, String> getFieldValueMap(Object bean) {
    Class<?> cls = bean.getClass();
    Map<String, String> valueMap = new LinkedHashMap<String, String>();
    LinkedList<Field> fields = _getAllFields(new LinkedList<Field>(), cls);

    for (Field field : fields) {
        try {
            if (field == null || field.getName() == null) {
                continue;
            }
            if (StringUtils.equals("serialVersionUID", field.getName())) {
                continue;
            }

            Object fieldVal = PropertyUtils.getProperty(bean, field.getName());

            String result = null;
            if (null != fieldVal) {
                if (StringUtils.equals("Date", field.getType().getSimpleName())) {
                    result = DateViewTools.formatFullDate((Date) fieldVal);
                } else {
                    result = String.valueOf(fieldVal);
                }
            }
            valueMap.put(field.getName(), result == null ? StringUtils.EMPTY : result);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            continue;
        }
    }
    return valueMap;
}

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

@Override
public void fireValueChange(Object data) {
    try {// w ww. j av  a  2  s  .c om
        Object dataId = PropertyUtils.getProperty(data, "id");
        if (dataId == null) {
            PropertyUtils.setProperty(bean, "type", null);
            return;
        }

        setInternalValue((Integer) dataId);

        if (data instanceof SimpleAccount) {
            PropertyUtils.setProperty(bean, "type", CrmTypeConstants.ACCOUNT);
            navButton.setCaption(((SimpleAccount) data).getAccountname());
        } else if (data instanceof SimpleCampaign) {
            PropertyUtils.setProperty(bean, "type", CrmTypeConstants.CAMPAIGN);
            navButton.setCaption(((SimpleCampaign) data).getCampaignname());
        } else if (data instanceof SimpleContact) {
            PropertyUtils.setProperty(bean, "type", CrmTypeConstants.CONTACT);
            navButton.setCaption(((SimpleContact) data).getContactName());
        } else if (data instanceof SimpleLead) {
            PropertyUtils.setProperty(bean, "type", CrmTypeConstants.LEAD);
            navButton.setCaption(((SimpleLead) data).getLeadName());
        } else if (data instanceof SimpleOpportunity) {
            PropertyUtils.setProperty(bean, "type", CrmTypeConstants.OPPORTUNITY);
            navButton.setCaption(((SimpleOpportunity) data).getOpportunityname());
        } else if (data instanceof SimpleCase) {
            PropertyUtils.setProperty(bean, "type", CrmTypeConstants.CASE);
            navButton.setCaption(((SimpleCase) data).getSubject());
        }
    } catch (Exception e) {
        LOG.error("Error when fire value", e);
    }
}

From source file:com.esofthead.mycollab.vaadin.web.ui.MultiSelectComp.java

private void displaySelectedItems() {
    componentsText.setReadOnly(false);/*w  ww .  ja  v a 2 s .co m*/
    componentsText.setValue(selectedItems.size() + " selected");
    componentsText.setReadOnly(true);
    Ul ul = new Ul();
    try {
        for (T item : selectedItems) {
            String objDisplayName = (String) PropertyUtils.getProperty(item, propertyDisplayField);
            ul.appendChild(new Li().appendText(objDisplayName));
        }
    } catch (Exception e) {
        LOG.error("Error when build tooltip", e);
    }
    componentsText.setDescription(ul.write());
}

From source file:com.hgcode.shiro.ShiroJetTags.java

/**
 * Displays the user's principal or a property of the user's principal.
 *///from w ww.ja  v  a  2s.  com
public static void principal(JetTagContext ctx, String property)
        throws IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    final Subject subject = getSubject();

    String val = null;
    Object principal = subject.getPrincipal();
    if (principal != null) {
        if (property == null) {
            val = principal.toString();
        } else {
            Object propertyValue = PropertyUtils.getProperty(principal, property);
            val = propertyValue != null ? propertyValue.toString() : "null";
        }
    }

    if (val != null) {
        ctx.getWriter().print(val);
    }
}