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.module.crm.ui.components.RelatedReadItemField.java

@Override
protected Component initContent() {
    try {/*from   ww  w  . j  a v a  2 s.  c  om*/
        final Integer typeid = (Integer) PropertyUtils.getProperty(RelatedReadItemField.this.bean, "typeid");
        if (typeid == null) {
            return new Label("");
        }

        final String type = (String) PropertyUtils.getProperty(bean, "type");
        if (type == null || type.equals("")) {
            return new Label("");
        }

        FontAwesome relatedLink = null;
        String relateItemName = null;

        if ("Account".equals(type)) {
            AccountService accountService = ApplicationContextUtil.getSpringBean(AccountService.class);
            final SimpleAccount account = accountService.findById(typeid, AppContext.getAccountId());
            if (account != null) {
                relateItemName = account.getAccountname();
                relatedLink = CrmAssetsManager.getAsset(CrmTypeConstants.ACCOUNT);
            }
        } else if ("Campaign".equals(type)) {
            CampaignService campaignService = ApplicationContextUtil.getSpringBean(CampaignService.class);
            final SimpleCampaign campaign = campaignService.findById(typeid, AppContext.getAccountId());
            if (campaign != null) {
                relateItemName = campaign.getCampaignname();
                relatedLink = CrmAssetsManager.getAsset(CrmTypeConstants.CAMPAIGN);

            }
        } else if ("Contact".equals(type)) {
            ContactService contactService = ApplicationContextUtil.getSpringBean(ContactService.class);
            final SimpleContact contact = contactService.findById(typeid, AppContext.getAccountId());
            if (contact != null) {
                relateItemName = contact.getContactName();
                relatedLink = CrmAssetsManager.getAsset(CrmTypeConstants.CONTACT);

            }
        } else if ("Lead".equals(type)) {
            LeadService leadService = ApplicationContextUtil.getSpringBean(LeadService.class);
            final SimpleLead lead = leadService.findById(typeid, AppContext.getAccountId());
            if (lead != null) {
                relateItemName = lead.getLeadName();
                relatedLink = CrmAssetsManager.getAsset(CrmTypeConstants.LEAD);

            }
        } else if ("Opportunity".equals(type)) {
            OpportunityService opportunityService = ApplicationContextUtil
                    .getSpringBean(OpportunityService.class);
            final SimpleOpportunity opportunity = opportunityService.findById(typeid,
                    AppContext.getAccountId());
            if (opportunity != null) {
                relateItemName = opportunity.getOpportunityname();
                relatedLink = CrmAssetsManager.getAsset(CrmTypeConstants.OPPORTUNITY);

            }
        } else if ("Case".equals(type)) {
            CaseService caseService = ApplicationContextUtil.getSpringBean(CaseService.class);
            final SimpleCase cases = caseService.findById(typeid, AppContext.getAccountId());
            if (cases != null) {
                relateItemName = cases.getSubject();
                relatedLink = CrmAssetsManager.getAsset(CrmTypeConstants.CASE);
            }
        }

        LabelLink related = new LabelLink(relateItemName,
                CrmLinkBuilder.generateActivityPreviewLinkFull(type, typeid));
        if (relatedLink != null)
            related.setIconLink(relatedLink);

        if (relatedLink != null) {
            return related;
        } else {
            return new Label("");
        }

    } catch (Exception e) {
        return new Label("");
    }
}

From source file:gov.nih.nci.calims2.ui.generic.crud.SubFlowReturnTester.java

/**
 * Test the controller doReturnFromFlow method.
 * //www .  j  a v  a2 s.  c  o  m
 * @throws Exception if an exception occurs
 */
@SuppressWarnings("unchecked")
@Test
public void testDoReturnFromFlow() throws Exception {
    Class<T> controllerClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
            .getActualTypeArguments()[0];
    T controller = controllerClass.newInstance();
    ModelAndView model = new ModelAndView();
    model.addObject("form", controller.createForm());
    controller.doReturnFromFlow(model, subFlowId, 1L);
    Object property = PropertyUtils.getProperty(((CRUDForm) model.getModel().get("form")).getEntity(),
            propertyName);
    if (collection) {
        CRUDAssert.assertSubFlowEntities((Collection<EntityWithId>) property, 1L);
    } else {
        CRUDAssert.assertSubFlowEntity((EntityWithId) property, 1L);
    }
}

From source file:net.sf.infrared.web.customtags.ColorThresholdTag.java

public int doStartTag() throws JspException {
    Object formBean = RequestUtils.lookup(pageContext, name, scope);

    String beanProperty = null;/*from   w w w  .  j a va2 s  . c  o m*/
    try {
        beanProperty = (String) PropertyUtils.getProperty(formBean, property);
    } catch (IllegalAccessException e) {
        throw new JspException("Unable to find the property " + property + " in the bean", e);
    } catch (InvocationTargetException e) {
        throw new JspException("Unable to find the property " + property + " in the bean", e);
    } catch (NoSuchMethodException e) {
        throw new JspException("Unable to find the property " + property + " in the bean", e);
    }
    double value = Double.parseDouble(beanProperty);

    if (value > COLOR_THRESHOLD)
        return (EVAL_BODY_INCLUDE);
    else
        return (SKIP_BODY);
}

From source file:com.genologics.ri.processexecution.ArtifactBase.java

protected void updateQCFlag(Linkable<Artifact> link) {
    qcFlag = null;/*from  ww  w  . ja  v  a  2s . c  o m*/
    try {
        String objectQCFlagString = PropertyUtils.getProperty(link, "qcFlag").toString();
        if (objectQCFlagString != null) {
            qcFlag = QCFlag.valueOf(objectQCFlagString);
        }
    } catch (Exception e) {
        // Ignore.
    }
}

From source file:com.gisgraphy.rest.BeanToRestParameter.java

public static Map<String, String> ToMap(Object object) {
    if (object == null) {
        throw new RestClientException("Can not get queryString for null object");
    }//from w  ww . j a v  a  2  s  .  c  om
    Map<String, String> result = new HashMap<String, String>();
    try {
        for (PropertyDescriptor thisPropertyDescriptor : Introspector
                .getBeanInfo(object.getClass(), Object.class).getPropertyDescriptors()) {
            Object property = PropertyUtils.getProperty(object, thisPropertyDescriptor.getName());
            if (property != null) {
                result.put(thisPropertyDescriptor.getName(), property.toString());
            }
        }
    } catch (Exception e) {
        throw new RestClientException("can not generate Map for bean : " + e.getMessage(), e);
    }
    return result;
}

From source file:io.milton.http.annotated.ContactDataAnnotationHandler.java

public String execute(AnnoContactResource contactRes) {
    Object source = contactRes.getSource();
    try {/*from   w w w.  j av  a2  s . co  m*/
        Object value = null;
        ControllerMethod cm = getBestMethod(source.getClass());
        if (cm == null) {
            // look for an annotation on the source itself
            java.lang.reflect.Method m = annoResourceFactory.findMethodForAnno(source.getClass(), annoClass);
            if (m != null) {
                value = m.invoke(source, (Object) null);
            } else {
                for (String nameProp : PROP_NAMES) {
                    if (PropertyUtils.isReadable(source, nameProp)) {
                        Object oPropVal = PropertyUtils.getProperty(source, nameProp);
                        value = oPropVal;
                        break;
                    }
                }
            }
        } else {
            value = invoke(cm, contactRes);
        }
        if (value != null) {
            if (value instanceof String) {
                return (String) value;
            } else if (value instanceof byte[]) {
                byte[] bytes = (byte[]) value;
                return new String(bytes, "UTF-8");
            } else if (value instanceof InputStream) {
                InputStream in = (InputStream) value;
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                IOUtils.copy(in, bout);
                return bout.toString("UTF-8");
            } else {
                return value.toString();
            }
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new RuntimeException("Exception executing " + getClass() + " - " + source.getClass(), e);
    }
}

From source file:io.milton.http.annotated.ICalDataAnnotationHandler.java

public String execute(AnnoEventResource eventRes) {
    Object source = eventRes.getSource();
    try {//w  w  w  .  jav a 2  s . c o m
        Object value = null;
        ControllerMethod cm = getBestMethod(source.getClass());
        if (cm == null) {
            // look for an annotation on the source itself
            java.lang.reflect.Method m = annoResourceFactory.findMethodForAnno(source.getClass(), annoClass);
            if (m != null) {
                value = m.invoke(source, (Object) null);
            } else {
                for (String nameProp : CTAG_PROP_NAMES) {
                    if (PropertyUtils.isReadable(source, nameProp)) {
                        Object oPropVal = PropertyUtils.getProperty(source, nameProp);
                        value = oPropVal;
                        break;
                    }
                }
            }
        } else {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            value = invoke(cm, eventRes, bout);
            // These methods will often write to output stream, so must provide one as alternative to returning a value
            if (value == null) { // probably means void return type, so use outputstream
                byte[] arr = bout.toByteArray();
                if (arr.length > 0) {
                    value = arr;
                }
            }
        }
        if (value != null) {
            if (value instanceof String) {
                return (String) value;
            } else if (value instanceof byte[]) {
                byte[] bytes = (byte[]) value;
                return new String(bytes, "UTF-8");
            } else if (value instanceof InputStream) {
                InputStream in = (InputStream) value;
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                IOUtils.copy(in, bout);
                return bout.toString("UTF-8");
            } else {
                return value.toString();
            }
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new RuntimeException("Exception executing " + getClass() + " - " + source.getClass(), e);
    }
}

From source file:com.mooing.wss.common.util.BeanUtil.java

/**
 * ?JavaBean/*from  w  w  w . j  av  a  2s  .c  om*/
 *
 * @param bean JavaBean
 * @param property ??
 * @return value 
 * @throws PropertyAccessException 
 */
public static Object getBeanProperty(Object bean, String property) throws PropertyAccessException {

    Object value = null;
    try {
        value = PropertyUtils.getProperty(bean, property);
    } catch (IllegalArgumentException e) {
        throw new PropertyAccessException(e);
    } catch (IllegalAccessException e) {
        throw new PropertyAccessException(e);
    } catch (InvocationTargetException e) {
        throw new PropertyAccessException(e.getTargetException());
    } catch (NoSuchMethodException e) {
        throw new PropertyAccessException(e);
    }
    return value;
}

From source file:com.liferay.util.PropertyComparator.java

public int compare(Object obj1, Object obj2) {
    try {/* ww w  .  j a v a  2 s.com*/
        for (int i = 0; i < _propertyNames.length; i++) {
            String propertyName = _propertyNames[i];

            Object property1 = PropertyUtils.getProperty(obj1, propertyName);
            Object property2 = PropertyUtils.getProperty(obj2, propertyName);

            if (property1 instanceof String) {
                int result = property1.toString().compareToIgnoreCase(property2.toString());

                if (result != 0) {
                    return result;
                }
            }

            if (property1 instanceof Comparable) {
                int result = ((Comparable) property1).compareTo(property2);

                if (result != 0) {
                    return result;
                }
            }
        }
    } catch (NoSuchMethodException nsme) {
        Logger.error(this, nsme.getMessage(), nsme);
    } catch (InvocationTargetException ite) {
        Logger.error(this, ite.getMessage(), ite);
    } catch (IllegalAccessException iae) {
        Logger.error(this, iae.getMessage(), iae);
    }

    return -1;
}

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

public RelatedItemSelectionWindow(String title, RelatedListComp2 relatedList) {
    super(title);
    bodyContent = new MVerticalLayout();
    this.withContent(bodyContent).withCenter().withModal(true).withResizable(false);
    this.relatedListComp = relatedList;
    initUI();/*ww w. jav a 2s  . c om*/

    tableItem.addTableListener(event -> {
        try {
            Object rowItem = event.getData();
            Boolean selectedVal = (Boolean) PropertyUtils.getProperty(rowItem, selectedFieldName);
            if (selectedVal) {
                selectedItems.remove(rowItem);
                PropertyUtils.setProperty(rowItem, selectedFieldName, false);
            } else {
                selectedItems.add(rowItem);
                PropertyUtils.setProperty(rowItem, selectedFieldName, true);
            }
        } catch (Exception ex) {
            throw new MyCollabException(ex);
        }
    });
}