Example usage for org.apache.commons.beanutils PropertyUtils isReadable

List of usage examples for org.apache.commons.beanutils PropertyUtils isReadable

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils isReadable.

Prototype

public static boolean isReadable(Object bean, String name) 

Source Link

Document

Return true if the specified property name identifies a readable property on the specified bean; otherwise, return false.

For more details see PropertyUtilsBean.

Usage

From source file:com.prashsoft.javakiva.LoanUtil.java

public List<Loan> getLoansDetails(String loanIds) {

    JSONObject jsonObject;/*  w w w .j  av  a 2 s  .com*/

    List loans = new ArrayList();

    String urlSuffix = "loans";
    String urlMethod = loanIds;

    Object bean = KivaUtil.getBeanResponse(urlSuffix, urlMethod, null);

    if ((bean == null) || (!(PropertyUtils.isReadable(bean, "loans"))))
        return null;

    List jsonLoans = (List) KivaUtil.getBeanProperty(bean, "loans");

    if (jsonLoans == null || jsonLoans.size() == 0)
        return null;

    Iterator loanIter = jsonLoans.iterator();

    while (loanIter.hasNext()) {

        jsonObject = JSONObject.fromObject(loanIter.next());
        bean = JSONObject.toBean(jsonObject);

        Loan loan = getLoan(bean);
        loans.add(loan);

    }
    return loans;
}

From source file:com.prashsoft.javakiva.LoanUtil.java

public List<Loan> getLoansFromBean(Object bean) {

    if (!(PropertyUtils.isReadable(bean, "loans")))
        return null;

    List jsonLoans = (List) KivaUtil.getBeanProperty(bean, "loans");

    if (jsonLoans == null || jsonLoans.size() == 0)
        return null;

    List loans = new ArrayList();
    JSONObject jsonObject;/*  ww  w. jav a 2 s  .c om*/
    Loan loan;
    Iterator loanIter = jsonLoans.iterator();

    while (loanIter.hasNext()) {

        jsonObject = JSONObject.fromObject(loanIter.next());
        bean = JSONObject.toBean(jsonObject);

        loan = getLoanDetails((Integer) KivaUtil.getBeanProperty(bean, "id"));
        loans.add(loan);

    }
    return loans;
}

From source file:com.prashsoft.javakiva.LoanUtil.java

public List<LoanUpdate> getLoanUpdates(Integer loanId) {

    if (loanId == null || loanId.intValue() <= 0)
        return null;

    String urlSuffix = "loans";
    String urlMethod = loanId.intValue() + "/updates";

    Object bean = KivaUtil.getBeanResponse(urlSuffix, urlMethod, null);

    if ((bean == null) || (!(PropertyUtils.isReadable(bean, "loan_updates"))))
        return null;

    List jsonLoanUpdates = (List) KivaUtil.getBeanProperty(bean, "loan_updates");

    if (jsonLoanUpdates == null || jsonLoanUpdates.size() == 0)
        return null;

    List<LoanUpdate> loanUpdates = new ArrayList();
    LoanUpdate loanUpdate;/*w ww. j a  v a2s . c o  m*/

    Iterator loanUpdateIter = jsonLoanUpdates.iterator();

    JSONObject loanUpdateJSONObject;
    Object loanUpdateBean;

    while (loanUpdateIter.hasNext()) {
        loanUpdate = new LoanUpdate();

        loanUpdateJSONObject = JSONObject.fromObject(loanUpdateIter.next());
        loanUpdateBean = JSONObject.toBean(loanUpdateJSONObject);

        if (KivaUtil.getBeanProperty(loanUpdateBean, "journal_entry") != null) {
            JSONObject journalEntryJSONObject = JSONObject
                    .fromObject(KivaUtil.getBeanProperty(loanUpdateBean, "journal_entry"));
            Object journalEntryBean = JSONObject.toBean(journalEntryJSONObject);
            loanUpdate.setJournalEntry(getJournalEntryFromBean(journalEntryBean));
        }

        if (KivaUtil.getBeanProperty(loanUpdateBean, "update_type") != null) {
            loanUpdate.setUpdateType(KivaUtil.getBeanProperty(loanUpdateBean, "update_type").toString());
        }

        if (KivaUtil.getBeanProperty(loanUpdateBean, "payment") != null) {
            JSONObject paymentJSONObject = JSONObject
                    .fromObject(KivaUtil.getBeanProperty(loanUpdateBean, "payment"));
            Object paymentBean = JSONObject.toBean(paymentJSONObject);
            loanUpdate.setPayment(getPaymentFromBean(paymentBean));
        }

        loanUpdates.add(loanUpdate);
    }
    return loanUpdates;

}

From source file:org.ajax4jsf.application.DebugOutputMaker.java

/**
 * Out component with properties and it's facets and childrens
 * @param context TODO/*from   www .j a  v a  2s  . c  o m*/
 * @param out
 * @param viewRoot
 */
private void writeComponent(FacesContext context, PrintWriter out, UIComponent component, String facetName) {
    String clientId = "_tree:" + component.getClientId(context);
    out.println("<dt onclick=\"toggle('" + clientId + "')\" class='tree'>");
    writeToggleMark(out, clientId);
    // out component name
    if (null != facetName) {
        out.print("Facet:'" + facetName + "' ");
    }
    out.println("<code>" + component.getClass().getName() + "</code> Id:[" + component.getId() + "]");
    out.println("</dt>");
    out.println("<dd id='" + clientId + "' style='display:none;'   class='tree' ><ul   class='tree'>");
    // out bean properties
    try {
        PropertyDescriptor propertyDescriptors[] = PropertyUtils.getPropertyDescriptors(component);
        for (int i = 0; i < propertyDescriptors.length; i++) {
            if (PropertyUtils.isReadable(component, propertyDescriptors[i].getName())) {
                String name = propertyDescriptors[i].getName();
                ValueBinding vb = component.getValueBinding(name);
                if (vb != null) {
                    writeAttribute(out, name, vb.getExpressionString());
                } else {
                    if (!IGNORE_ATTRIBUTES.contains(name)) {
                        try {
                            String value = BeanUtils.getProperty(component, name);
                            writeAttribute(out, name, value);
                        } catch (Exception e) {
                            writeAttribute(out, name, null);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        // Do nothing - we in error page.
    }

    // out bindings
    // out attributes map
    for (Iterator it = component.getAttributes().entrySet().iterator(); it.hasNext();) {
        Map.Entry entry = (Map.Entry) it.next();
        writeAttribute(out, (String) entry.getKey(), entry.getValue());
    }
    // out listeners
    out.println("</ul></dd>");
    if (component.getFacetsAndChildren().hasNext()) {
        out.println("<dd class='tree_childs'><dl class='tree_childs'>");
        // out childs of this component
        // facets
        for (Iterator facetEntry = component.getFacets().entrySet().iterator(); facetEntry.hasNext();) {
            Map.Entry entry = (Map.Entry) facetEntry.next();
            writeComponent(context, out, (UIComponent) entry.getValue(), (String) entry.getKey());
        }
        // childs components
        for (Iterator childIter = component.getChildren().iterator(); childIter.hasNext();) {
            UIComponent child = (UIComponent) childIter.next();
            writeComponent(context, out, child, null);
        }
        out.println("</dl></dd>");
    }

}

From source file:org.ajax4jsf.application.DebugOutputMaker.java

private void writeVariables(PrintWriter out, Map vars, String caption) {
    out.print("<table><caption>");
    out.print(caption);//w w  w  .j a  v a  2  s. com
    out.println(
            "</caption><thead><tr><th style=\"width: 10%; \">Name</th><th style=\"width: 90%; \">Value</th></tr></thead><tbody>");
    boolean written = false;
    if (!vars.isEmpty()) {
        SortedMap map = new TreeMap(vars);
        Map.Entry entry = null;
        String key = null;
        for (Iterator itr = map.entrySet().iterator(); itr.hasNext();) {
            entry = (Map.Entry) itr.next();
            key = entry.getKey().toString();
            if (key.indexOf('.') == -1) {
                out.println("<tr><td>");
                out.println(key.replaceAll("<", LT).replaceAll(">", GT));
                out.println("</td><td><span class='value'>");
                Object value = entry.getValue();
                out.println(value.toString().replaceAll("<", LT).replaceAll(">", GT));
                out.println("</span>");
                try {
                    PropertyDescriptor propertyDescriptors[] = PropertyUtils.getPropertyDescriptors(value);
                    if (propertyDescriptors.length > 0) {
                        out.print("<div class='properties'><ul class=\'properties\'>");
                        for (int i = 0; i < propertyDescriptors.length; i++) {
                            String beanPropertyName = propertyDescriptors[i].getName();
                            if (PropertyUtils.isReadable(value, beanPropertyName)) {
                                out.print("<li class=\'properties\'>");
                                out.print(beanPropertyName + " = "
                                        + BeanUtils.getProperty(value, beanPropertyName));
                                out.print("</li>");

                            }
                        }
                        out.print("</ul></div>");
                    }
                } catch (Exception e) {
                    // TODO: log exception
                }

                out.println("</td></tr>");
                written = true;
            }
        }
    }
    if (!written) {
        out.println("<tr><td colspan=\"2\"><em>None</em></td></tr>");
    }
    out.println("</tbody></table>");
}

From source file:org.andromda.presentation.gui.FormPopulator.java

/**
 * Copies the properties from the <code>fromForm</code> to the <code>toForm</code>.  Only passes not-null values to the toForm.
 *
 * @param fromForm the form from which we're populating
 * @param toForm the form to which we're populating
 * @param override whether or not properties that have already been copied, should be overridden.
 *///from  w w w. ja  v a2 s  .  c om
@SuppressWarnings("unchecked") // apache commons-beanutils PropertyUtils has no generics
public static final void populateForm(final Object fromForm, final Object toForm, boolean override) {
    if (fromForm != null && toForm != null) {
        try {
            final Map<String, Object> parameters = PropertyUtils.describe(fromForm);
            for (final Iterator<String> iterator = parameters.keySet().iterator(); iterator.hasNext();) {
                final String name = iterator.next();
                if (PropertyUtils.isWriteable(toForm, name)) {
                    // - the property name used for checking whether or not the property value has been set
                    final String isSetPropertyName = name + "Set";
                    Boolean isToFormPropertySet = null;
                    Boolean isFromFormPropertySet = null;
                    // - only if override isn't true do we care whether or not the to property has been populated
                    if (!override) {
                        if (PropertyUtils.isReadable(toForm, isSetPropertyName)) {
                            isToFormPropertySet = (Boolean) PropertyUtils.getProperty(toForm,
                                    isSetPropertyName);
                        }
                    }
                    // - only if override is set to true, we check to see if the from form property has been set
                    if (override) {
                        if (PropertyUtils.isReadable(fromForm, isSetPropertyName)) {
                            isFromFormPropertySet = (Boolean) PropertyUtils.getProperty(fromForm,
                                    isSetPropertyName);
                        }
                    }
                    if (!override || (override && isFromFormPropertySet != null
                            && isFromFormPropertySet.booleanValue())) {
                        if (override || (isToFormPropertySet == null || !isToFormPropertySet.booleanValue())) {
                            final PropertyDescriptor toDescriptor = PropertyUtils.getPropertyDescriptor(toForm,
                                    name);
                            if (toDescriptor != null) {
                                Object fromValue = parameters.get(name);
                                // - only populate if not null
                                if (fromValue != null) {
                                    final PropertyDescriptor fromDescriptor = PropertyUtils
                                            .getPropertyDescriptor(fromForm, name);
                                    // - only attempt to set if the types match
                                    if (fromDescriptor.getPropertyType() == toDescriptor.getPropertyType()) {
                                        PropertyUtils.setProperty(toForm, name, fromValue);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (final Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }
}

From source file:org.andromda.presentation.gui.FormPopulator.java

/**
 * Populates the form from the given map of properties. If a matching property is null or an empty
 * string, then null is placed on the form.
 *
 * @param form the form to populate.//from  ww  w .ja v  a 2  s. c o  m
 * @param formatters any date or time formatters.
 * @param properties the properties to populate from.
 * @param ignoreProperties names of any properties to ignore when it comes to populating on the form.
 * @param override whether or not to override properties already set on the given form.
 * @param assignableTypesOnly whether or not copying should be attempted only if the property types are assignable.
 */
@SuppressWarnings("unchecked") // apache commons-beanutils PropertyUtils has no generics
public static final void populateFormFromPropertyMap(final Object form,
        final Map<String, DateFormat> formatters, final Map<String, ?> properties,
        final String[] ignoreProperties, boolean override, boolean assignableTypesOnly) {
    if (properties != null) {
        try {
            final Collection<String> ignoredProperties = ignoreProperties != null
                    ? Arrays.asList(ignoreProperties)
                    : new ArrayList<String>();
            final Map<String, Object> formProperties = PropertyUtils.describe(form);
            for (final Iterator<String> iterator = formProperties.keySet().iterator(); iterator.hasNext();) {
                final String name = iterator.next();
                if (PropertyUtils.isWriteable(form, name) && !ignoredProperties.contains(name)) {
                    final PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(form, name);
                    if (descriptor != null) {
                        boolean populateProperty = true;
                        if (!override) {
                            final String isSetPropertyName = name + "Set";
                            if (PropertyUtils.isReadable(form, isSetPropertyName)) {
                                final Boolean isPropertySet = (Boolean) PropertyUtils.getProperty(form,
                                        isSetPropertyName);
                                if (isPropertySet.booleanValue()) {
                                    populateProperty = false;
                                }
                            }
                        }
                        if (populateProperty) {
                            final Object property = properties.get(name);

                            // - only convert if the string is not empty
                            if (property != null) {
                                Object value = null;
                                if (property instanceof String) {
                                    final String propertyAsString = (String) property;
                                    if (propertyAsString.trim().length() > 0) {
                                        DateFormat formatter = formatters != null ? formatters.get(name) : null;
                                        // - if the formatter is available we use that, otherwise we attempt to convert
                                        if (formatter != null) {
                                            try {
                                                value = formatter.parse(propertyAsString);
                                            } catch (ParseException parseException) {
                                                // - try the default formatter (handles the default java.util.Date.toString() format)
                                                formatter = formatters != null ? formatters.get(null) : null;
                                                value = formatter.parse(propertyAsString);
                                            }
                                        } else {
                                            value = ConvertUtils.convert(propertyAsString,
                                                    descriptor.getPropertyType());
                                        }
                                    }
                                    // - don't attempt to set null on primitive fields
                                    if (value != null || !descriptor.getPropertyType().isPrimitive()) {
                                        PropertyUtils.setProperty(form, name, value);
                                    }
                                } else {
                                    value = property;
                                    try {
                                        if (!assignableTypesOnly || descriptor.getPropertyType()
                                                .isAssignableFrom(value.getClass())) {
                                            PropertyUtils.setProperty(form, name, value);
                                        }
                                    } catch (Exception exception) {
                                        final String valueTypeName = value.getClass().getName();
                                        final String propertyTypeName = descriptor.getPropertyType().getName();
                                        final StringBuilder message = new StringBuilder(
                                                "Can not set form property '" + name + "' of type: "
                                                        + propertyTypeName + " with value: " + value);
                                        if (!descriptor.getPropertyType().isAssignableFrom(value.getClass())) {
                                            message.append("; " + valueTypeName + " is not assignable to "
                                                    + propertyTypeName);
                                        }
                                        throw new IllegalArgumentException(
                                                message + ": " + exception.toString());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (final Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }
}

From source file:org.andromda.presentation.gui.PatternMatchingExceptionHandler.java

/**
 * Attempts to retrieve any arguments from the exception from a property named "messageArguments"
 * on the exception./*from  w  w w  . ja  va 2s  . c o m*/
 *
 * @param throwable the Exception containing the message to retrieve
 * @return the retrieved message arguments (if any) from the exception.
 * @throws Throwable
 */
public Object[] getMessageArguments(final Throwable throwable) throws Throwable {
    Object[] arguments = null;
    final Throwable cause = this.findRootCause(throwable);
    if (cause != null && cause.getMessage() != null) {
        if (PropertyUtils.isReadable(cause, MESSAGE_ARGUMENTS)) {
            arguments = (Object[]) PropertyUtils.getProperty(cause, MESSAGE_ARGUMENTS);
        }
    }
    return arguments;
}

From source file:org.andromda.timetracker.service.UserDoesNotExistException.java

/**
 * Finds the root cause of the parent exception
 * by traveling up the exception tree. Performs printStackTrace if
 * an exception is thrown./*from   w  ww  .j  ava  2  s  . co  m*/
 * @param th Throwable to find the cause from
 * @return targetException Throwable cause
 */
private static Throwable findRootCause(Throwable th) {
    if (th != null) {
        // Reflectively get any JMX or EJB exception causes.
        try {
            Throwable targetException = null;
            // java.lang.reflect.InvocationTargetException
            // or javax.management.ReflectionException
            String exceptionProperty = "targetException";
            if (PropertyUtils.isReadable(th, exceptionProperty)) {
                targetException = (Throwable) PropertyUtils.getProperty(th, exceptionProperty);
            } else {
                exceptionProperty = "causedByException";
                //javax.ejb.EJBException
                if (PropertyUtils.isReadable(th, exceptionProperty)) {
                    targetException = (Throwable) PropertyUtils.getProperty(th, exceptionProperty);
                }
            }
            if (targetException != null) {
                th = targetException;
            }
        } catch (Exception ex) {
            // just print the exception and continue
            ex.printStackTrace();
        }

        if (th.getCause() != null) {
            th = th.getCause();
            th = findRootCause(th);
        }
    }
    return th;
}

From source file:org.andromda.timetracker.web.timecarddetails.TimecardDetailsFormImpl.java

/**
 * @param items//from www  .  j  a  v a  2 s.  c  o  m
 * @param valueProperty
 * @param labelProperty
 */
public void setTimecardIdBackingList(Collection<? extends Object> items, String valueProperty,
        String labelProperty) {
    this.timecardIdValueList = null;
    this.timecardIdLabelList = null;
    if (items != null) {
        this.timecardIdValueList = new Object[items.size()];
        this.timecardIdLabelList = new Object[items.size()];

        try {
            final List<String> labelProperties = labelProperty == null ? null
                    : new ArrayList<String>(Arrays.asList(labelProperty.split("[\\W&&[^\\.]]+")));
            final List<String> labelDelimiters = labelProperty == null ? null
                    : new ArrayList<String>(Arrays.asList(labelProperty.split("[\\w\\.]+")));
            int ctr = 0;
            for (final Iterator<? extends Object> iterator = items.iterator(); iterator.hasNext(); ctr++) {
                final Object item = iterator.next();
                if (item != null) {
                    this.timecardIdValueList[ctr] = valueProperty == null ? item
                            : PropertyUtils.getProperty(item, valueProperty.trim());
                    if (labelProperties == null) {
                        this.timecardIdLabelList[ctr] = item;
                    } else {
                        final StringBuilder labelText = new StringBuilder();
                        int ctr2 = 0;
                        do {
                            if (!labelDelimiters.isEmpty()) {
                                labelText.append(labelDelimiters.get(ctr2));
                            }
                            String property = null;
                            if (ctr2 < labelProperties.size()) {
                                property = labelProperties.get(ctr2);
                            }
                            if (property != null && property.length() > 0) {
                                if (PropertyUtils.isReadable(item, property)) {
                                    Object value = PropertyUtils.getProperty(item, property);
                                    if (value != null) {
                                        if (value instanceof String) {
                                            if (((String) value).trim().length() == 0) {
                                                value = null;
                                            }
                                        }
                                        if (value != null) {
                                            labelText.append(value);
                                        }
                                    }
                                } else {
                                    labelText.append(property);
                                }
                            }
                            ctr2++;
                        } while (ctr2 < labelDelimiters.size());
                        this.timecardIdLabelList[ctr] = labelText.toString().replaceAll("\\s+", " ").trim();
                    }
                }
            }
        } catch (final Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }
}