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);
}
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;
}
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();
}
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;
}
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();
}
}
Aggregations