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

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

Introduction

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

Prototype

public static void setProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Set 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:org.andromda.timetracker.web.timecarddetails.TimecardController.java

/**
 * @param event//from   w  w  w .  ja v a  2 s.  c om
 * @return timecardDetails
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public String timecardDetails(final FacesEvent event) {
    String forward = null;
    final Object currentForm = this.resolveVariable("form");
    try {
        final TimecardDetailsFormImpl form = this.getTimecardDetailsForm();
        form.setEvent(event);
        this.setForm("form", form, true);
        // - pass any properties from the previous form along
        FormPopulator.populateForm(currentForm, form, false);
        // - populate the form with any event attributes that may match
        // IMPORTANT: it isn't possible to automatically populate any property named "id" since that
        // is a reserved name in JSF (the id of a component), therefore we have to unfortunately ignore any available "id" attribute
        FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                (Map) this.getRequestAttribute(ACTION_EVENT_ATTRIBUTES), new String[] { "id" });
        if (event != null) {
            final Map eventProperties = new HashMap();
            eventProperties.put(event.getComponent().getId(), event.getSource());
            FormPopulator.populateFormFromPropertyMapAssignableTypesOnly(form, null, eventProperties);
            FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                    event.getComponent().getAttributes(), new String[] { "id" });
        }
        // - populate the form with any request attributes that may match
        FormPopulator.populateFormFromRequestAttributes(form, form.getDateTimeFormatters(), false, true);
        // - populate the form with any request parameters that may match
        FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                this.getContext().getExternalContext().getRequestParameterMap());
        //trigger method execution
        _timecardDetails_started(form);

        forward = _populateScreen(form);
        final FacesMessage.Severity messageSeverity = this.getMaximumMessageSeverity();
        if (messageSeverity != null
                && FacesMessage.SEVERITY_ERROR.getOrdinal() <= messageSeverity.getOrdinal()) {
            // - copy any messages to the 'currentForm'
            PropertyUtils.setProperty(currentForm, "jsfMessages",
                    PropertyUtils.getProperty(form, "jsfMessages"));
            this.setForm("form", currentForm, false);
        }
        if (event != null) {
            TimecardDetailsViewPopulator.populateFormAndViewVariables(this.getContext(), form);
        }
    } catch (final Throwable throwable) {
        this.setForm("form", currentForm, false);
        // - set the forward to null so that we stay on the current view
        forward = null;
        try {
            // - the exception is re-thrown by the exception handler and handled by the catch below if it can't get a messageKey
            //   (no reason to check for presence of messageKey)
            this.addErrorMessage(
                    Messages.get(PatternMatchingExceptionHandler.instance().handleException(throwable),
                            PatternMatchingExceptionHandler.instance().getMessageArguments(throwable)));
        } catch (Throwable exception) {
            logger.error(exception.getMessage(), exception);
            this.addExceptionMessage(exception);
        }
    }
    return forward;
}

From source file:org.andromda.timetracker.web.timecardsearch.SearchController.java

/**
 * @param event/*  www.  java 2 s . c  o m*/
 * @return searchTimecardsSearch
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public String searchTimecardsSearch(final FacesEvent event) {
    String forward = null;
    final Object currentForm = this.resolveVariable("form");
    try {
        final SearchTimecardsSearchFormImpl form = this.getSearchTimecardsSearchForm();
        form.setEvent(event);
        this.setForm("form", form, true);
        // - pass any properties from the previous form along
        FormPopulator.populateForm(currentForm, form, false);
        // - populate the form with any event attributes that may match
        // IMPORTANT: it isn't possible to automatically populate any property named "id" since that
        // is a reserved name in JSF (the id of a component), therefore we have to unfortunately ignore any available "id" attribute
        FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                (Map) this.getRequestAttribute(ACTION_EVENT_ATTRIBUTES), new String[] { "id" });
        if (event != null) {
            final Map eventProperties = new HashMap();
            eventProperties.put(event.getComponent().getId(), event.getSource());
            FormPopulator.populateFormFromPropertyMapAssignableTypesOnly(form, null, eventProperties);
            FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                    event.getComponent().getAttributes(), new String[] { "id" });
        }
        // - populate the form with any request attributes that may match
        FormPopulator.populateFormFromRequestAttributes(form, form.getDateTimeFormatters(), false, true);
        // - populate the form with any request parameters that may match
        FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                this.getContext().getExternalContext().getRequestParameterMap());
        //trigger method execution
        _searchTimecards_search(form);

        forward = _populateSearchScreen(form);
        if ("search-timecards-search-timecards".equals(forward)) {
            forward = null; //the destination form is the same, stay on the current view
        }
        final FacesMessage.Severity messageSeverity = this.getMaximumMessageSeverity();
        if (messageSeverity != null
                && FacesMessage.SEVERITY_ERROR.getOrdinal() <= messageSeverity.getOrdinal()) {
            // - copy any messages to the 'currentForm'
            PropertyUtils.setProperty(currentForm, "jsfMessages",
                    PropertyUtils.getProperty(form, "jsfMessages"));
            this.setForm("form", currentForm, false);
        }
        if (event != null) {
            SearchTimecardsViewPopulator.populateFormAndViewVariables(this.getContext(), form);
        }
    } catch (final Throwable throwable) {
        this.setForm("form", currentForm, false);
        // - set the forward to null so that we stay on the current view
        forward = null;
        try {
            // - the exception is re-thrown by the exception handler and handled by the catch below if it can't get a messageKey
            //   (no reason to check for presence of messageKey)
            this.addErrorMessage(
                    Messages.get(PatternMatchingExceptionHandler.instance().handleException(throwable),
                            PatternMatchingExceptionHandler.instance().getMessageArguments(throwable)));
        } catch (Throwable exception) {
            logger.error(exception.getMessage(), exception);
            this.addExceptionMessage(exception);
        }
    }
    return forward;
}

From source file:org.andromda.timetracker.web.timecardsearch.SearchController.java

/**
 * @param event//from  www  .j  a  v  a  2s  .  c om
 * @return searchTimecardsDetails
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public String searchTimecardsDetails(final FacesEvent event) {
    String forward = null;
    final Object currentForm = this.resolveVariable("form");
    try {
        final SearchTimecardsDetailsFormImpl form = this.getSearchTimecardsDetailsForm();
        form.setEvent(event);
        this.setForm("form", form, true);
        // - pass any properties from the previous form along
        FormPopulator.populateForm(currentForm, form, false);
        // - populate the form with any event attributes that may match
        // IMPORTANT: it isn't possible to automatically populate any property named "id" since that
        // is a reserved name in JSF (the id of a component), therefore we have to unfortunately ignore any available "id" attribute
        FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                (Map) this.getRequestAttribute(ACTION_EVENT_ATTRIBUTES), new String[] { "id" });
        if (event != null) {
            final Map eventProperties = new HashMap();
            eventProperties.put(event.getComponent().getId(), event.getSource());
            FormPopulator.populateFormFromPropertyMapAssignableTypesOnly(form, null, eventProperties);
            FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                    event.getComponent().getAttributes(), new String[] { "id" });
        }
        // - populate the form with any request attributes that may match
        FormPopulator.populateFormFromRequestAttributes(form, form.getDateTimeFormatters(), false, true);
        // - populate the form with any request parameters that may match
        FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                this.getContext().getExternalContext().getRequestParameterMap());
        //trigger method execution
        _searchTimecards_details(form);

        forward = _initializeTimecardId(form);
        if ("search-timecards-search-timecards".equals(forward)) {
            forward = null; //the destination form is the same, stay on the current view
        }
        final FacesMessage.Severity messageSeverity = this.getMaximumMessageSeverity();
        if (messageSeverity != null
                && FacesMessage.SEVERITY_ERROR.getOrdinal() <= messageSeverity.getOrdinal()) {
            // - copy any messages to the 'currentForm'
            PropertyUtils.setProperty(currentForm, "jsfMessages",
                    PropertyUtils.getProperty(form, "jsfMessages"));
            this.setForm("form", currentForm, false);
        }
        if (event != null) {
            SearchTimecardsViewPopulator.populateFormAndViewVariables(this.getContext(), form);
        }
    } catch (final Throwable throwable) {
        this.setForm("form", currentForm, false);
        // - set the forward to null so that we stay on the current view
        forward = null;
        try {
            // - the exception is re-thrown by the exception handler and handled by the catch below if it can't get a messageKey
            //   (no reason to check for presence of messageKey)
            this.addErrorMessage(
                    Messages.get(PatternMatchingExceptionHandler.instance().handleException(throwable),
                            PatternMatchingExceptionHandler.instance().getMessageArguments(throwable)));
        } catch (Throwable exception) {
            logger.error(exception.getMessage(), exception);
            this.addExceptionMessage(exception);
        }
    }
    return forward;
}

From source file:org.andromda.timetracker.web.timecardsearch.SearchController.java

/**
 * @param event// w w w.  j av a 2  s. c o  m
 * @return searchTimecards
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public String searchTimecards(final FacesEvent event) {
    String forward = null;
    final Object currentForm = this.resolveVariable("form");
    try {
        final SearchTimecardsFormImpl form = this.getSearchTimecardsForm();
        form.setEvent(event);
        this.setForm("form", form, true);
        // - pass any properties from the previous form along
        FormPopulator.populateForm(currentForm, form, false);
        // - populate the form with any event attributes that may match
        // IMPORTANT: it isn't possible to automatically populate any property named "id" since that
        // is a reserved name in JSF (the id of a component), therefore we have to unfortunately ignore any available "id" attribute
        FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                (Map) this.getRequestAttribute(ACTION_EVENT_ATTRIBUTES), new String[] { "id" });
        if (event != null) {
            final Map eventProperties = new HashMap();
            eventProperties.put(event.getComponent().getId(), event.getSource());
            FormPopulator.populateFormFromPropertyMapAssignableTypesOnly(form, null, eventProperties);
            FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                    event.getComponent().getAttributes(), new String[] { "id" });
        }
        // - populate the form with any request attributes that may match
        FormPopulator.populateFormFromRequestAttributes(form, form.getDateTimeFormatters(), false, true);
        // - populate the form with any request parameters that may match
        FormPopulator.populateFormFromPropertyMap(form, form.getDateTimeFormatters(),
                this.getContext().getExternalContext().getRequestParameterMap());
        //trigger method execution
        _searchTimecards_started(form);

        forward = _populateSearchScreen(form);
        final FacesMessage.Severity messageSeverity = this.getMaximumMessageSeverity();
        if (messageSeverity != null
                && FacesMessage.SEVERITY_ERROR.getOrdinal() <= messageSeverity.getOrdinal()) {
            // - copy any messages to the 'currentForm'
            PropertyUtils.setProperty(currentForm, "jsfMessages",
                    PropertyUtils.getProperty(form, "jsfMessages"));
            this.setForm("form", currentForm, false);
        }
        if (event != null) {
            SearchTimecardsViewPopulator.populateFormAndViewVariables(this.getContext(), form);
        }
    } catch (final Throwable throwable) {
        this.setForm("form", currentForm, false);
        // - set the forward to null so that we stay on the current view
        forward = null;
        try {
            // - the exception is re-thrown by the exception handler and handled by the catch below if it can't get a messageKey
            //   (no reason to check for presence of messageKey)
            this.addErrorMessage(
                    Messages.get(PatternMatchingExceptionHandler.instance().handleException(throwable),
                            PatternMatchingExceptionHandler.instance().getMessageArguments(throwable)));
        } catch (Throwable exception) {
            logger.error(exception.getMessage(), exception);
            this.addExceptionMessage(exception);
        }
    }
    return forward;
}

From source file:org.apache.ambari.view.hive.resources.jobs.StoredOperationHandle.java

public StoredOperationHandle(Map<String, Object> stringObjectMap)
        throws InvocationTargetException, IllegalAccessException {
    for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {
        try {//from  w w  w . j a  v  a 2s .  co  m
            PropertyUtils.setProperty(this, entry.getKey(), entry.getValue());
        } catch (NoSuchMethodException e) {
            //do nothing, skip
        }
    }
}

From source file:org.apache.ambari.view.hive.resources.jobs.viewJobs.JobImpl.java

public JobImpl(Map<String, Object> stringObjectMap) throws InvocationTargetException, IllegalAccessException {
    for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {
        try {//ww w .  j a v  a 2 s.  c  om
            PropertyUtils.setProperty(this, entry.getKey(), entry.getValue());
        } catch (NoSuchMethodException e) {
            //do nothing, skip
        }
    }
}

From source file:org.apache.ddlutils.io.DataReader.java

/**
 * Converts the column value read from the XML stream to an object and sets it at the given bean.
 * //from   w ww.ja  v  a 2s . c  o  m
 * @param bean   The bean
 * @param table  The table definition
 * @param column The column definition
 * @param value  The value as a string
 */
private void setColumnValue(DynaBean bean, Table table, Column column, String value)
        throws DdlUtilsXMLException {
    SqlTypeConverter converter = _converterConf.getRegisteredConverter(table, column);
    Object propValue = (converter != null ? converter.convertFromString(value, column.getTypeCode()) : value);

    try {
        PropertyUtils.setProperty(bean, column.getName(), propValue);
    } catch (NoSuchMethodException ex) {
        throw new DdlUtilsXMLException("Undefined column " + column.getName());
    } catch (IllegalAccessException ex) {
        throw new DdlUtilsXMLException("Could not set bean property for column " + column.getName(), ex);
    } catch (InvocationTargetException ex) {
        throw new DdlUtilsXMLException("Could not set bean property for column " + column.getName(), ex);
    }
}

From source file:org.apache.ddlutils.io.DataReaderKeys.java

private void setColumnValue(DynaBean bean, Table table, Column column, String value)
        throws DdlUtilsXMLException {
    SqlTypeConverter converter = _converterConf.getRegisteredConverter(table, column);
    Object propValue = (converter != null ? converter.convertFromString(value, column.getTypeCode()) : value);

    try {/* w ww.  j  a  va2s. co m*/
        PropertyUtils.setProperty(bean, column.getName(), propValue);
    } catch (NoSuchMethodException ex) {
        throw new DdlUtilsXMLException("Undefined column " + column.getName());
    } catch (IllegalAccessException ex) {
        throw new DdlUtilsXMLException("Could not set bean property for column " + column.getName(), ex);
    } catch (InvocationTargetException ex) {
        throw new DdlUtilsXMLException("Could not set bean property for column " + column.getName(), ex);
    }
}

From source file:org.apache.ddlutils.io.SetColumnPropertyFromSubElementRule.java

/**
 * {@inheritDoc}/*from  w  ww  .jav a2s .co m*/
 */
public void body(String text) throws Exception {
    String attrValue = text.trim();

    if (_usesBase64 && (attrValue != null)) {
        attrValue = new String(Base64.decodeBase64(attrValue.getBytes()));
    }

    Object propValue = (_converter != null ? _converter.convertFromString(attrValue, _column.getTypeCode())
            : attrValue);

    if (digester.getLogger().isDebugEnabled()) {
        digester.getLogger().debug("[SetColumnPropertyFromSubElementRule]{" + digester.getMatch()
                + "} Setting property '" + _column.getName() + "' to '" + propValue + "'");
    }

    PropertyUtils.setProperty(digester.peek(), _column.getName(), propValue);
}

From source file:org.apache.ddlutils.io.SetColumnPropertyRule.java

/**
 * {@inheritDoc}//ww w  .jav  a2  s.  co  m
 */
public void begin(Attributes attributes) throws Exception {
    Object bean = digester.peek();

    for (int idx = 0; idx < attributes.getLength(); idx++) {
        String attrName = attributes.getLocalName(idx);

        if ("".equals(attrName)) {
            attrName = attributes.getQName(idx);
        }
        if ((_caseSensitive && attrName.equals(_column.getName()))
                || (!_caseSensitive && attrName.equalsIgnoreCase(_column.getName()))) {
            String attrValue = attributes.getValue(idx);
            Object propValue = (_converter != null
                    ? _converter.convertFromString(attrValue, _column.getTypeCode())
                    : attrValue);

            if (digester.getLogger().isDebugEnabled()) {
                digester.getLogger().debug("[SetColumnPropertyRule]{" + digester.getMatch()
                        + "} Setting property '" + _column.getName() + "' to '" + propValue + "'");
            }

            PropertyUtils.setProperty(bean, _column.getName(), propValue);
        }
    }
}