use of org.opennms.netmgt.model.OnmsAssetRecord in project opennms by OpenNMS.
the class AssetRecordDaoIT method testCreateAndGets.
@Test
@Transactional
public void testCreateAndGets() {
OnmsNode onmsNode = new OnmsNode(m_locationDao.getDefaultLocation(), "myNode");
m_nodeDao.save(onmsNode);
OnmsAssetRecord assetRecord = onmsNode.getAssetRecord();
assetRecord.setAssetNumber("imported-id: 7");
m_assetRecordDao.update(assetRecord);
m_assetRecordDao.flush();
//Test findAll method
Collection<OnmsAssetRecord> assetRecords = m_assetRecordDao.findAll();
assertEquals(7, assetRecords.size());
//Test countAll method
assertEquals(7, m_assetRecordDao.countAll());
}
use of org.opennms.netmgt.model.OnmsAssetRecord 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.opennms.netmgt.model.OnmsAssetRecord 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);
m_assetRecordDao.saveOrUpdate(assetRecord);
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();
}
Aggregations