Search in sources :

Example 96 with BeanWrapper

use of org.springframework.beans.BeanWrapper 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)

Example 97 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class PropertyUtils method getProperties.

/**
 * <p>getProperties</p>
 *
 * @param bean a {@link java.lang.Object} object.
 * @return a {@link java.util.Collection} object.
 */
public static Collection<String> getProperties(Object bean) {
    Collection<String> props = new LinkedList<>();
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean);
    for (PropertyDescriptor pd : wrapper.getPropertyDescriptors()) {
        props.add(pd.getName());
    }
    return props;
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) PropertyDescriptor(java.beans.PropertyDescriptor) LinkedList(java.util.LinkedList)

Example 98 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class AssetRecordResource method updateAssetRecord.

/**
 * <p>updateAssetRecord</p>
 *
 * @param nodeCriteria a {@link java.lang.String} object.
 * @param params a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
 * @return a {@link javax.ws.rs.core.Response} object.
 */
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response updateAssetRecord(@PathParam("nodeCriteria") final String nodeCriteria, final MultivaluedMapImpl params) {
    OnmsNode node = m_nodeDao.get(nodeCriteria);
    if (node == null) {
        throw getException(Status.BAD_REQUEST, "updateAssetRecord: Can't find node " + nodeCriteria);
    }
    OnmsAssetRecord assetRecord = getAssetRecord(node);
    if (assetRecord == null) {
        throw getException(Status.BAD_REQUEST, "updateAssetRecord: Node " + node + " could not update ");
    }
    if (assetRecord.getGeolocation() == null) {
        assetRecord.setGeolocation(new OnmsGeolocation());
    }
    LOG.debug("updateAssetRecord: updating asset {}", assetRecord);
    boolean modified = false;
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(assetRecord);
    wrapper.registerCustomEditor(Date.class, new ISO8601DateEditor());
    for (String key : params.keySet()) {
        if (wrapper.isWritableProperty(key)) {
            String stringValue = params.getFirst(key);
            Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
            wrapper.setPropertyValue(key, value);
            modified = true;
        }
    }
    if (modified) {
        LOG.debug("updateAssetRecord: assetRecord {} updated", assetRecord);
        node.setAssetRecord(assetRecord);
        m_nodeDao.saveOrUpdate(node);
        try {
            sendEvent(EventConstants.ASSET_INFO_CHANGED_EVENT_UEI, node.getId());
        } catch (EventProxyException e) {
            throw getException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
        }
        return Response.noContent().build();
    }
    return Response.notModified().build();
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) ISO8601DateEditor(org.opennms.web.api.ISO8601DateEditor) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 99 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class AssetSuggestionsRestService method getAssetSuggestions.

/**
 * Gets the asset suggestions.
 *
 * @return the asset suggestions
 */
@GET
@Path("suggestions")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
public Suggestions getAssetSuggestions() {
    final Suggestions suggestions = new Suggestions();
    final List<OnmsAssetRecord> distinctAssetProperties = m_assetDao.getDistinctProperties();
    final List<String> attributes = PropertyUtils.getProperties(new OnmsAssetRecord()).stream().filter(a -> !BLACK_LIST.contains(a)).collect(Collectors.toList());
    distinctAssetProperties.forEach(record -> {
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(record);
        attributes.forEach(attribute -> {
            if (!suggestions.containsKey(attribute)) {
                suggestions.put(attribute, new SuggestionList());
            }
            final Object value = wrapper.getPropertyValue(attribute);
            if (value != null) {
                final SuggestionList list = suggestions.get(attribute);
                if (!list.contains(value.toString())) {
                    list.add(value.toString());
                }
            }
        });
    });
    return suggestions;
}
Also used : Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper) Path(javax.ws.rs.Path) PropertyAccessorFactory(org.springframework.beans.PropertyAccessorFactory) BeanWrapper(org.springframework.beans.BeanWrapper) Autowired(org.springframework.beans.factory.annotation.Autowired) AssetRecordDao(org.opennms.netmgt.dao.api.AssetRecordDao) JaxbListWrapper(org.opennms.core.config.api.JaxbListWrapper) InitializingBean(org.springframework.beans.factory.InitializingBean) HashSet(java.util.HashSet) MediaType(javax.ws.rs.core.MediaType) Map(java.util.Map) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) PropertyUtils(org.opennms.web.svclayer.support.PropertyUtils) Collection(java.util.Collection) Set(java.util.Set) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) TreeMap(java.util.TreeMap) Collections(java.util.Collections) XmlElement(javax.xml.bind.annotation.XmlElement) Assert(org.springframework.util.Assert) BeanWrapper(org.springframework.beans.BeanWrapper) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 100 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class ForeignSourceRestService method updateForeignSource.

/**
 * <p>updateForeignSource</p>
 *
 * @param foreignSource a {@link java.lang.String} object.
 * @param params a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
 * @return a {@link javax.ws.rs.core.Response} object.
 */
@PUT
@Path("{foreignSource}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Transactional
public Response updateForeignSource(@Context final UriInfo uriInfo, @PathParam("foreignSource") String foreignSource, MultivaluedMapImpl params) {
    writeLock();
    try {
        ForeignSource fs = getActiveForeignSource(foreignSource);
        LOG.debug("updateForeignSource: updating foreign source {}", foreignSource);
        if (params.isEmpty())
            return Response.notModified().build();
        boolean modified = false;
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(fs);
        wrapper.registerCustomEditor(Duration.class, new StringIntervalPropertyEditor());
        for (final String key : params.keySet()) {
            if (wrapper.isWritableProperty(key)) {
                Object value = null;
                String stringValue = params.getFirst(key);
                value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
                wrapper.setPropertyValue(key, value);
                modified = true;
            }
        }
        if (modified) {
            LOG.debug("updateForeignSource: foreign source {} updated", foreignSource);
            fs.updateDateStamp();
            m_pendingForeignSourceRepository.save(fs);
            return Response.accepted().header("Location", getRedirectUri(uriInfo)).build();
        } else {
            return Response.notModified().build();
        }
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) ForeignSource(org.opennms.netmgt.provision.persist.foreignsource.ForeignSource) StringIntervalPropertyEditor(org.opennms.netmgt.provision.persist.StringIntervalPropertyEditor) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

BeanWrapper (org.springframework.beans.BeanWrapper)144 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)79 Test (org.junit.jupiter.api.Test)31 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)18 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)18 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 Consumes (javax.ws.rs.Consumes)16 PUT (javax.ws.rs.PUT)16 Path (javax.ws.rs.Path)16 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 HashSet (java.util.HashSet)12 BeansException (org.springframework.beans.BeansException)12 PropertyDescriptor (java.beans.PropertyDescriptor)11 OnmsNode (org.opennms.netmgt.model.OnmsNode)9 Test (org.junit.Test)6 BigDecimal (java.math.BigDecimal)5