use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class ServiceDetectorRegistryImpl method createDetector.
private static ServiceDetector createDetector(ServiceDetectorFactory<? extends ServiceDetector> factory, Map<String, String> properties) {
if (factory == null) {
return null;
}
final ServiceDetector detector = factory.createDetector();
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(detector);
wrapper.setPropertyValues(properties);
return detector;
}
use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class MonitoringLocationsRestService method updateMonitoringLocation.
@PUT
@Path("{monitoringLocation}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Transactional
public Response updateMonitoringLocation(@PathParam("monitoringLocation") String monitoringLocation, MultivaluedMapImpl params) {
writeLock();
try {
OnmsMonitoringLocation def = m_monitoringLocationDao.get(monitoringLocation);
LOG.debug("updateMonitoringLocation: updating monitoring location {}", monitoringLocation);
if (params.isEmpty())
return Response.notModified().build();
boolean modified = false;
final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(def);
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("updateMonitoringLocation: monitoring location {} updated", monitoringLocation);
m_monitoringLocationDao.save(def);
return Response.noContent().build();
}
return Response.notModified().build();
} finally {
writeUnlock();
}
}
use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class NodeRestService method updateCategoryForNode.
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/{nodeCriteria}/categories/{categoryName}")
public Response updateCategoryForNode(@PathParam("nodeCriteria") final String nodeCriteria, @PathParam("categoryName") final String categoryName, MultivaluedMapImpl params) {
writeLock();
try {
OnmsNode node = m_nodeDao.get(nodeCriteria);
if (node == null) {
throw getException(Status.BAD_REQUEST, "Node {} was not found.", nodeCriteria);
}
OnmsCategory category = getCategory(node, categoryName);
if (category == null) {
throw getException(Status.BAD_REQUEST, "Category {} not found on node {}", categoryName, nodeCriteria);
}
LOG.debug("updateCategory: updating category {}", category);
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(category);
boolean updated = false;
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);
updated = true;
}
}
if (updated) {
LOG.debug("updateCategory: category {} updated", category);
m_categoryDao.saveOrUpdate(category);
} else {
LOG.debug("updateCategory: no fields updated in category {}", category);
}
return Response.noContent().build();
} finally {
writeUnlock();
}
}
use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class UserRestService method updateUser.
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("{userCriteria}")
public Response updateUser(@Context final SecurityContext securityContext, @PathParam("userCriteria") final String userCriteria, final MultivaluedMapImpl params) {
writeLock();
try {
if (!hasEditRights(securityContext)) {
throw getException(Status.BAD_REQUEST, "User {} does not have write access to users!", securityContext.getUserPrincipal().getName());
}
final OnmsUser user = getOnmsUser(userCriteria);
LOG.debug("updateUser: updating user {}", user);
boolean modified = false;
final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(user);
for (final String key : params.keySet()) {
if (wrapper.isWritableProperty(key)) {
final String stringValue = params.getFirst(key);
final Object value = wrapper.convertIfNecessary(stringValue, wrapper.getPropertyType(key));
wrapper.setPropertyValue(key, value);
modified = true;
}
}
if (modified) {
LOG.debug("updateUser: user {} updated", user);
try {
m_userManager.save(user);
} catch (final Throwable t) {
throw getException(Status.INTERNAL_SERVER_ERROR, t);
}
return Response.noContent().build();
}
return Response.notModified().build();
} finally {
writeUnlock();
}
}
use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class CategoryRestService method updateCategory.
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/{categoryName}")
public Response updateCategory(@PathParam("categoryName") final String categoryName, final MultivaluedMapImpl params) {
writeLock();
try {
OnmsCategory category = m_categoryDao.findByName(categoryName);
if (category == null) {
throw getException(Status.BAD_REQUEST, "Category with name '{}' was not found.", categoryName);
}
LOG.debug("updateCategory: updating category {}", category);
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(category);
boolean modified = false;
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;
}
}
LOG.debug("updateCategory: category {} updated", category);
if (modified) {
m_categoryDao.saveOrUpdate(category);
return Response.noContent().build();
}
return Response.notModified().build();
} finally {
writeUnlock();
}
}
Aggregations