use of org.opennms.core.criteria.Criteria in project opennms by OpenNMS.
the class EventUtilDaoImpl method getHardwareFieldValue.
@Override
public String getHardwareFieldValue(String parm, long nodeId) {
final Matcher matcher = HW_PARM_PATTERN.matcher(parm);
if (!matcher.matches()) {
LOG.warn("Unsupported hardware field parameter '{}'.", parm);
return null;
}
final String hwFieldSelector = matcher.group(1);
final String hwField = matcher.group(2).toLowerCase();
// Retrieve the entity with a like query
if (hwFieldSelector.startsWith("~")) {
final String likeQuery = hwFieldSelector.substring(1);
LOG.debug("Retrieving hardware field value {} on {} with like query {}", parm, nodeId, likeQuery);
Criteria criteria = new Criteria(OnmsHwEntity.class).setAliases(Arrays.asList(new Alias[] { new Alias("node", "node", JoinType.LEFT_JOIN) })).addRestriction(new EqRestriction("node.id", (int) nodeId)).addRestriction(new LikeRestriction("entPhysicalName", likeQuery)).setOrders(Arrays.asList(new Order[] { Order.desc("id") }));
List<OnmsHwEntity> hwEntities = hwEntityDao.findMatching(criteria);
System.err.println(hwEntities);
if (hwEntities.size() < 1) {
return null;
}
return getStringPropertyByName(hwField, hwEntities.get(0), hwEntityDescriptorsByName);
}
// Retrieve the entity by index if the select is an integer
try {
int index = Integer.parseInt(hwFieldSelector);
OnmsHwEntity hwEntity = hwEntityDao.findEntityByIndex((int) nodeId, index);
if (hwEntity == null) {
// No entry with this index
return null;
}
return getStringPropertyByName(hwField, hwEntity, hwEntityDescriptorsByName);
} catch (NumberFormatException e) {
// pass
}
// Retrieve the entity by name
OnmsHwEntity hwEntity = hwEntityDao.findEntityByName((int) nodeId, hwFieldSelector);
if (hwEntity == null) {
// No entry with this name
return null;
}
return getStringPropertyByName(hwField, hwEntity, hwEntityDescriptorsByName);
}
use of org.opennms.core.criteria.Criteria in project opennms by OpenNMS.
the class IpLikeSearchProvider method query.
/**
* This method processes the <SearchQuery> that the user has typed and returns a <SearchResult> list
* of matching IP addresses as well as the query string itself, which is collapsible, to act
* as a subnet container.
*/
@Override
public List<SearchResult> query(SearchQuery searchQuery, GraphContainer graphContainer) {
LOG.info("SearchProvider->query: called with search query: '{}'", searchQuery);
List<SearchResult> results = new ArrayList<>();
String queryString = searchQuery.getQueryString();
if (!isIpLikeQuery(queryString)) {
LOG.debug("SearchProvider->query: query not IPLIKE compatible.");
return results;
}
CriteriaBuilder bldr = new CriteriaBuilder(OnmsIpInterface.class);
bldr.iplike("ipAddr", queryString).orderBy("ipAddress", true);
Criteria dbQueryCriteria = bldr.toCriteria();
List<OnmsIpInterface> ips;
// since it seems to do something very similar in its matches method.
try {
ips = ipInterfaceProvider.findMatching(dbQueryCriteria);
if (ips.size() == 0) {
return results;
} else {
if (isIpLikeQuery(queryString)) {
LOG.debug("SearchProvider->query: adding IPLIKE search spec '{}' to the search results.", queryString);
SearchResult searchResult = new SearchResult(getSearchProviderNamespace(), queryString, queryString, queryString, SearchResult.COLLAPSIBLE, !SearchResult.COLLAPSED);
if (!results.contains(searchResult)) {
results.add(searchResult);
}
}
}
Set<String> ipAddrs = new HashSet<>();
LOG.info("SearchProvider->query: creating IP address set.");
for (OnmsIpInterface ip : ips) {
String hostAddress = ip.getIpAddress().getHostAddress();
LOG.debug("SearchProvider->query: adding '{}' to set of IPs.", hostAddress);
ipAddrs.add(hostAddress);
}
LOG.info("SearchProvider->query: building search result from set of IPs.");
IPLOOP: for (String ip : ipAddrs) {
if (findCriterion(ip, graphContainer) != null) {
continue IPLOOP;
} else {
SearchResult searchResult = createSearchResult(ip, queryString);
if (!results.contains(searchResult)) {
results.add(searchResult);
}
}
}
LOG.info("SearchProvider->query: found: '{}' IP interfaces.", ips.size());
} catch (Exception e) {
LOG.error("SearchProvider-query: caught exception during iplike query: {}", e);
}
LOG.info("SearchProvider->query: built search result with {} results.", results.size());
return results;
}
use of org.opennms.core.criteria.Criteria in project opennms by OpenNMS.
the class NodeDaoIT method testCriteriaBuilderOrderBy.
@Test
@Transactional
public void testCriteriaBuilderOrderBy() {
CriteriaBuilder cb = new CriteriaBuilder(OnmsNode.class);
cb.alias("ipInterfaces", "ipInterface").distinct();
// TODO: Make this work but we need to put the fields into
// an aggregator function since node->ipInterfaces is a 1->M
// relationship.
//
// cb.orderBy("ipInterfaces.ipAddress").distinct();
Criteria criteria = cb.toCriteria();
System.out.println("Criteria: " + criteria.toString());
List<OnmsNode> nodes = m_nodeDao.findMatching(criteria);
nodes.stream().forEach(System.out::println);
assertEquals(6, nodes.size());
}
use of org.opennms.core.criteria.Criteria in project opennms by OpenNMS.
the class NotificationDaoIT method testCriteria.
/**
* Test to make sure that orderBy across an alias works.
*/
@Test
@Transactional
public void testCriteria() {
Criteria criteria = new Criteria(OnmsNotification.class);
criteria.setAliases(Arrays.asList(new Alias[] { new Alias("node", "node", JoinType.LEFT_JOIN), new Alias("node.snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN), new Alias("node.ipInterfaces", "ipInterface", JoinType.LEFT_JOIN), new Alias("event", "event", JoinType.LEFT_JOIN), new Alias("usersNotified", "usersNotified", JoinType.LEFT_JOIN), new Alias("serviceType", "serviceType", JoinType.LEFT_JOIN) }));
criteria.setOrders(Arrays.asList(new Order[] { new Order("event.id", false), new Order("event.eventSeverity", false), new Order("node.label", false), new Order("serviceType.name", false) }));
m_notificationDao.findMatching(criteria);
CriteriaBuilder builder = new CriteriaBuilder(OnmsNotification.class);
builder.alias("node", "node", JoinType.LEFT_JOIN);
builder.alias("node.snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN);
builder.alias("node.ipInterfaces", "ipInterface", JoinType.LEFT_JOIN);
builder.alias("event", "event", JoinType.LEFT_JOIN);
builder.alias("usersNotified", "usersNotified", JoinType.LEFT_JOIN);
builder.alias("serviceType", "serviceType", JoinType.LEFT_JOIN);
builder.orderBy("event.id", false);
builder.orderBy("event.eventSeverity", false);
builder.orderBy("node.label", false);
builder.orderBy("serviceType.name", false);
m_notificationDao.findMatching(builder.toCriteria());
}
use of org.opennms.core.criteria.Criteria in project opennms by OpenNMS.
the class DefaultReportStoreService method getAll.
/**
* <p>getAll</p>
*
* @return a {@link java.util.List} object.
*/
@Override
public List<ReportCatalogEntry> getAll() {
final Criteria onmsCrit = new Criteria(ReportCatalogEntry.class);
onmsCrit.setOrders(Arrays.asList(new Order[] { Order.desc("date") }));
return m_reportCatalogDao.findMatching(onmsCrit);
}
Aggregations