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:cn.fql.utility.ClassUtility.java

/**
 * Export property value of specified object
 *
 * @param obj          object instance// www.  j a  v a  2 s.c  om
 * @param propertyName property name
 * @return property value of object
 */
public static Object getPropertyValue(Object obj, String propertyName) {
    Object result = null;
    if (propertyName != null) {
        try {
            result = PropertyUtils.getProperty(obj, propertyName);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    return result;
}

From source file:com.mycollab.module.crm.ui.components.RelatedEditItemField.java

public void setType(String type) {
    relatedItemComboBox.select(type);//from   w  ww . j  ava 2  s  .  co  m
    try {
        Integer typeid = (Integer) PropertyUtils.getProperty(bean, "typeid");
        if (typeid != null) {
            if (CrmTypeConstants.ACCOUNT.equals(type)) {
                AccountService accountService = AppContextUtil.getSpringBean(AccountService.class);
                SimpleAccount account = accountService.findById(typeid, MyCollabUI.getAccountId());
                if (account != null) {
                    itemField.setValue(account.getAccountname());
                }
            } else if (CrmTypeConstants.CAMPAIGN.equals(type)) {
                CampaignService campaignService = AppContextUtil.getSpringBean(CampaignService.class);
                SimpleCampaign campaign = campaignService.findById(typeid, MyCollabUI.getAccountId());
                if (campaign != null) {
                    itemField.setValue(campaign.getCampaignname());
                }
            } else if (CrmTypeConstants.CONTACT.equals(type)) {
                ContactService contactService = AppContextUtil.getSpringBean(ContactService.class);
                SimpleContact contact = contactService.findById(typeid, MyCollabUI.getAccountId());
                if (contact != null) {
                    itemField.setValue(contact.getContactName());
                }
            } else if (CrmTypeConstants.LEAD.equals(type)) {
                LeadService leadService = AppContextUtil.getSpringBean(LeadService.class);
                SimpleLead lead = leadService.findById(typeid, MyCollabUI.getAccountId());
                if (lead != null) {
                    itemField.setValue(lead.getLeadName());
                }
            } else if (CrmTypeConstants.OPPORTUNITY.equals(type)) {
                OpportunityService opportunityService = AppContextUtil.getSpringBean(OpportunityService.class);
                SimpleOpportunity opportunity = opportunityService.findById(typeid, MyCollabUI.getAccountId());
                if (opportunity != null) {
                    itemField.setValue(opportunity.getOpportunityname());
                }
            } else if (CrmTypeConstants.CASE.equals(type)) {
                CaseService caseService = AppContextUtil.getSpringBean(CaseService.class);
                SimpleCase cases = caseService.findById(typeid, MyCollabUI.getAccountId());
                if (cases != null) {
                    itemField.setValue(cases.getSubject());
                }
            }
        }

    } catch (Exception e) {
        LOG.error("Error when set type", e);
    }
}

From source file:ReflectionUtils.java

/**
 * (getter),List.//from  w  ww  . j  a va2  s.  co m
 * 
 * @param collection
 *            .
 * @param propertyName
 *            .
 */
@SuppressWarnings("unchecked")
public static List fetchElementPropertyToList(final Collection collection, final String propertyName) {
    List list = new ArrayList();

    try {
        for (Object obj : collection) {
            list.add(PropertyUtils.getProperty(obj, propertyName));
        }
    } catch (Exception e) {
        convertToUncheckedException(e);
    }

    return list;
}

From source file:com.projity.exchange.ResourceMappingForm.java

public void setMergeField(MergeField mergeField) {
    this.mergeField = mergeField;
    Map mergeFieldMap = new HashMap();
    HashSet notMergedValues = new HashSet();
    Object resource;/*  w w w  .  ja  v  a 2  s.  com*/
    if (mergeField != NO_MERGE)
        for (Iterator i = resources.iterator(); i.hasNext();) {
            resource = i.next();
            try {
                Object value = PropertyUtils.getProperty(resource, mergeField.getProjityName());
                if (notMergedValues.contains(value))
                    continue;
                if (mergeFieldMap.containsKey(value)) { //not duplicates
                    mergeFieldMap.remove(value);
                    notMergedValues.add(value);
                } else
                    mergeFieldMap.put(value, resource);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    selectedResources.clear();
    Object value;
    for (Iterator i = importedResources.iterator(); i.hasNext();) {
        resource = i.next();
        if (mergeField == NO_MERGE)
            selectedResources.add(unassignedResource);
        else {
            try {
                value = PropertyUtils.getProperty(resource, mergeField.getImportName());
                if (value == null || !mergeFieldMap.containsKey(value))
                    selectedResources.add(unassignedResource);
                else
                    selectedResources.add(mergeFieldMap.get(value));
            } catch (Exception e) {
                selectedResources.add(unassignedResource);
            }
        }

    }
}

From source file:com.yukthitech.persistence.NativeQueryFactory.java

/**
 * Fetches the query template with specified "name" and builds the query using specified "context".
 * If the query uses "param" directive, corresponding values will be collected into "paramValues".
 * //from ww w  .  j a v a  2s. c  o m
 * @param name Name of the query to be fetched
 * @param outputParamValues Collected param values that needs to be passed to query as prepared statement params
 * @param context Context to be used to parse template into query
 * @return Built query
 */
public String buildQuery(String name, List<Object> outputParamValues, Object context) {
    String rawQuery = queryMap.get(name);

    //if no query found with specified name
    if (rawQuery == null) {
        throw new InvalidArgumentException("No query found with specified name - {}", name);
    }

    try {
        //build the final query (replacing the param expressions)
        String query = freeMarkerEngine.processTemplate(name, rawQuery, context);
        StringBuffer finalQuery = new StringBuffer();
        Matcher matcher = QUERY_PARAM_PATTERN.matcher(query);
        String property = null;

        while (matcher.find()) {
            property = matcher.group(1);
            matcher.appendReplacement(finalQuery, "?");

            outputParamValues.add(PropertyUtils.getProperty(context, property));
        }

        matcher.appendTail(finalQuery);

        return finalQuery.toString();
    } catch (Exception ex) {
        throw new InvalidStateException(ex, "An exception occurred while building query: " + name);
    }
}

From source file:net.jazdw.rql.parser.listfilter.ListFilter.java

private Object getProperty(Object item, String propName) {
    Object property;/*  w  ww .  j  av  a2 s  .  co m*/
    try {
        property = PropertyUtils.getProperty(item, propName);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new UnsupportedOperationException(
                String.format("Could not retrieve property '%s' from list object", propName));
    }
    return property;
}

From source file:com.krawler.br.exp.Variable.java

/**
 * find the property within the given object
 *
 * @param container the container object of property
 * @param props array of property names in sequence
 * @param level level of property from its top container
 * @return object containing the value of property
 * @throws java.lang.IllegalAccessException if property is no accessible
 * @throws java.lang.IllegalArgumentException if arguments are incorrect
 * @throws java.lang.reflect.InvocationTargetException if the target method
 * can not be invoked/*  w w  w .j a va 2s  .  co m*/
 * @throws com.krawler.utils.json.base.JSONException if exception occured
 * while accessing the property value from json
 * @throws java.lang.NoSuchMethodException if the container is a POJO and the
 * getter does not exists for the given property.
 */
private Object getProperty(Object container, String prop) throws IllegalAccessException,
        IllegalArgumentException, InvocationTargetException, NoSuchMethodException, ProcessException {
    if (container == null)
        return container;
    if (container instanceof java.util.Map) {
        return ((java.util.Map) container).get(prop);
    } else if (container instanceof JSONObject) {
        return ((JSONObject) container).opt(prop);
    } else {
        return PropertyUtils.getProperty(container, prop);
    }
}

From source file:com.mycollab.module.project.ui.components.AbstractPreviewItemComp.java

protected void toggleFavorite() {
    try {//from   ww w. ja  v a  2 s  . c om
        if (isFavorite()) {
            favoriteBtn.removeStyleName("favorite-btn-selected");
            favoriteBtn.addStyleName("favorite-btn");
        } else {
            favoriteBtn.addStyleName("favorite-btn-selected");
            favoriteBtn.removeStyleName("favorite-btn");
        }
        FavoriteItem favoriteItem = new FavoriteItem();
        favoriteItem.setExtratypeid(CurrentProjectVariables.getProjectId());
        favoriteItem.setType(getType());
        favoriteItem.setTypeid(PropertyUtils.getProperty(beanItem, "id").toString());
        favoriteItem.setSaccountid(MyCollabUI.getAccountId());
        favoriteItem.setCreateduser(UserUIContext.getUsername());
        FavoriteItemService favoriteItemService = AppContextUtil.getSpringBean(FavoriteItemService.class);
        favoriteItemService.saveOrDelete(favoriteItem);
    } catch (Exception e) {
        LOG.error("Error while set favorite flag to bean", e);
    }
}

From source file:com.esofthead.mycollab.module.project.ui.components.AbstractPreviewItemComp.java

protected void toggleFavorite() {
    try {/*from w  w w  . j  av a 2 s .  c  om*/
        if (isFavorite()) {
            favoriteBtn.removeStyleName("favorite-btn-selected");
            favoriteBtn.addStyleName("favorite-btn");
        } else {
            favoriteBtn.addStyleName("favorite-btn-selected");
            favoriteBtn.removeStyleName("favorite-btn");
        }
        FavoriteItem favoriteItem = new FavoriteItem();
        favoriteItem.setExtratypeid(CurrentProjectVariables.getProjectId());
        favoriteItem.setType(getType());
        favoriteItem.setTypeid(PropertyUtils.getProperty(beanItem, "id").toString());
        favoriteItem.setSaccountid(AppContext.getAccountId());
        favoriteItem.setCreateduser(AppContext.getUsername());
        FavoriteItemService favoriteItemService = ApplicationContextUtil
                .getSpringBean(FavoriteItemService.class);
        favoriteItemService.saveOrDelete(favoriteItem);
    } catch (Exception e) {
        LOG.error("Error while set favorite flag to bean", e);
    }
}

From source file:com.mycollab.mobile.ui.AbstractPreviewItemComp.java

private boolean isFavorite() {
    try {// www  .  ja v  a 2  s. c  om
        FavoriteItemService favoriteItemService = AppContextUtil.getSpringBean(FavoriteItemService.class);
        return favoriteItemService.isUserFavorite(UserUIContext.getUsername(), getType(),
                PropertyUtils.getProperty(beanItem, "id").toString());
    } catch (Exception e) {
        LOG.error("Error while check favorite", e);
        return false;
    }
}