Search in sources :

Example 1 with InetAddressTypeEditor

use of org.opennms.netmgt.model.InetAddressTypeEditor in project opennms by OpenNMS.

the class RestUtils method setBeanProperties.

/**
 * <p>Use Spring's {@link PropertyAccessorFactory} to set values on the specified bean.
 * This call registers several {@link PropertyEditor} classes to properly convert
 * values.</p>
 *
 * <ul>
 * <li>{@link StringXmlCalendarPropertyEditor}</li>
 * <li>{@link ISO8601DateEditor}</li>
 * <li>{@link InetAddressTypeEditor}</li>
 * <li>{@link OnmsSeverityEditor}</li>
 * <li>{@link PrimaryTypeEditor}</li>
 * </ul>
 *
 * @param bean
 * @param properties
 */
public static void setBeanProperties(final Object bean, final MultivaluedMap<String, String> properties) {
    final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean);
    wrapper.registerCustomEditor(XMLGregorianCalendar.class, new StringXmlCalendarPropertyEditor());
    wrapper.registerCustomEditor(Date.class, new ISO8601DateEditor());
    wrapper.registerCustomEditor(InetAddress.class, new InetAddressTypeEditor());
    wrapper.registerCustomEditor(OnmsSeverity.class, new OnmsSeverityEditor());
    wrapper.registerCustomEditor(PrimaryType.class, new PrimaryTypeEditor());
    for (final String key : properties.keySet()) {
        final String propertyName = convertNameToPropertyName(key);
        if (wrapper.isWritableProperty(propertyName)) {
            final String stringValue = properties.getFirst(key);
            Object value = convertIfNecessary(wrapper, propertyName, stringValue);
            wrapper.setPropertyValue(propertyName, value);
        }
    }
}
Also used : InetAddressTypeEditor(org.opennms.netmgt.model.InetAddressTypeEditor) BeanWrapper(org.springframework.beans.BeanWrapper) PrimaryTypeEditor(org.opennms.netmgt.model.PrimaryTypeEditor) StringXmlCalendarPropertyEditor(org.opennms.netmgt.provision.persist.StringXmlCalendarPropertyEditor) OnmsSeverityEditor(org.opennms.netmgt.model.OnmsSeverityEditor)

Example 2 with InetAddressTypeEditor

use of org.opennms.netmgt.model.InetAddressTypeEditor in project opennms by OpenNMS.

the class OnmsRestService method getBeanWrapperForClass.

protected static BeanWrapper getBeanWrapperForClass(final Class<?> criteriaClass) {
    final BeanWrapper wrapper = new BeanWrapperImpl(criteriaClass);
    wrapper.registerCustomEditor(XMLGregorianCalendar.class, new StringXmlCalendarPropertyEditor());
    wrapper.registerCustomEditor(java.util.Date.class, new ISO8601DateEditor());
    wrapper.registerCustomEditor(java.net.InetAddress.class, new InetAddressTypeEditor());
    wrapper.registerCustomEditor(OnmsSeverity.class, new OnmsSeverityEditor());
    wrapper.registerCustomEditor(PrimaryType.class, new PrimaryTypeEditor());
    return wrapper;
}
Also used : InetAddressTypeEditor(org.opennms.netmgt.model.InetAddressTypeEditor) BeanWrapper(org.springframework.beans.BeanWrapper) ISO8601DateEditor(org.opennms.web.api.ISO8601DateEditor) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) PrimaryTypeEditor(org.opennms.netmgt.model.PrimaryTypeEditor) StringXmlCalendarPropertyEditor(org.opennms.netmgt.provision.persist.StringXmlCalendarPropertyEditor) OnmsSeverityEditor(org.opennms.netmgt.model.OnmsSeverityEditor)

Example 3 with InetAddressTypeEditor

use of org.opennms.netmgt.model.InetAddressTypeEditor in project opennms by OpenNMS.

the class PropertyUtils method getPathValue.

/**
 * <p>getPathValue</p>
 *
 * @param bean a {@link java.lang.Object} object.
 * @param path a {@link java.lang.String} object.
 * @param expectedClass a {@link java.lang.Class} object.
 * @param <T> a T object.
 * @return a T object.
 */
@SuppressWarnings("unchecked")
public static <T> T getPathValue(final Object bean, final String path, final Class<T> expectedClass) {
    final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean);
    wrapper.registerCustomEditor(java.net.InetAddress.class, new InetAddressTypeEditor());
    wrapper.registerCustomEditor(OnmsSeverity.class, new OnmsSeverityEditor());
    wrapper.registerCustomEditor(PrimaryType.class, new PrimaryTypeEditor());
    final Class<?> propType = wrapper.getPropertyType(path);
    if (propType == null) {
        // we were unable to find the property
        // for debug purposes
        Assert.notNull(propType, "propType in BeanUtils is null path: " + path);
        return null;
    }
    if (!expectedClass.isAssignableFrom(propType)) {
        throw new IllegalArgumentException("Could not retrieve property of type " + propType + " as type " + expectedClass);
    }
    return (T) wrapper.getPropertyValue(path);
}
Also used : InetAddressTypeEditor(org.opennms.netmgt.model.InetAddressTypeEditor) BeanWrapper(org.springframework.beans.BeanWrapper) PrimaryTypeEditor(org.opennms.netmgt.model.PrimaryTypeEditor) OnmsSeverityEditor(org.opennms.netmgt.model.OnmsSeverityEditor)

Aggregations

InetAddressTypeEditor (org.opennms.netmgt.model.InetAddressTypeEditor)3 OnmsSeverityEditor (org.opennms.netmgt.model.OnmsSeverityEditor)3 PrimaryTypeEditor (org.opennms.netmgt.model.PrimaryTypeEditor)3 BeanWrapper (org.springframework.beans.BeanWrapper)3 StringXmlCalendarPropertyEditor (org.opennms.netmgt.provision.persist.StringXmlCalendarPropertyEditor)2 ISO8601DateEditor (org.opennms.web.api.ISO8601DateEditor)1 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)1