use of org.opennms.core.criteria.Criteria in project opennms by OpenNMS.
the class BusinessServiceDaoIT method verifyFindMatching.
@Test
@Transactional
public void verifyFindMatching() {
/*
* Test that offset and limit work, when only ordered by id
*/
// create test data
Long bsId1 = m_businessServiceDao.save(new BusinessServiceEntityBuilder().name("BS 1.1").reduceFunction(new HighestSeverityEntity()).addReductionKey("bs1.key1", new IgnoreEntity(), 1).addReductionKey("bs1.key2", new IgnoreEntity(), 2).addReductionKey("bs1.key3", new IgnoreEntity(), 3).toEntity());
Long bsId2 = m_businessServiceDao.save(new BusinessServiceEntityBuilder().name("BS 2.0").reduceFunction(new HighestSeverityEntity()).addReductionKey("bs2.key1", new IgnoreEntity(), 1).addReductionKey("bs2.key2", new IgnoreEntity(), 2).addReductionKey("bs2.key3", new IgnoreEntity(), 3).toEntity());
m_businessServiceDao.flush();
// create criteria to limit result
org.opennms.core.criteria.Criteria criteria = new CriteriaBuilder(BusinessServiceEntity.class).offset(0).limit(2).orderBy("id").toCriteria();
// verify that entities are distinct
List<BusinessServiceEntity> filteredBusinessServices = m_businessServiceDao.findMatching(criteria);
Assert.assertEquals(2, filteredBusinessServices.size());
Assert.assertEquals(m_businessServiceDao.findAll().stream().sorted((bs1, bs2) -> bs1.getId().compareTo(bs2.getId())).collect(Collectors.toList()), filteredBusinessServices);
// create another bs for a more complex test
Long bsId3 = m_businessServiceDao.save(new BusinessServiceEntityBuilder().name("BS 3.1").reduceFunction(new HighestSeverityEntity()).addReductionKey("bs3.key3.1", new IgnoreEntity(), 4).addReductionKey("bs3.key3.2", new IgnoreEntity(), 5).addReductionKey("bs3.key3.3", new IgnoreEntity(), 6).toEntity());
m_businessServiceDao.flush();
// restrict to edge.weight > 3 and order by id descending
criteria.setAliases(Lists.newArrayList(new Alias("edges", "edge", Alias.JoinType.INNER_JOIN, Restrictions.ge("edge.weight", 3))));
criteria.setOrders(Lists.newArrayList(new Order("id", false)));
// Verify
filteredBusinessServices = m_businessServiceDao.findMatching(criteria);
Assert.assertEquals(2, filteredBusinessServices.size());
Assert.assertEquals(Lists.newArrayList(m_businessServiceDao.get(bsId3), m_businessServiceDao.get(bsId2)), filteredBusinessServices);
/*
* Verify that one can also order by name
*/
Criteria nameCriteria = new CriteriaBuilder(BusinessServiceEntity.class).ilike("name", "BS %.1").orderBy("name").limit(2).toCriteria();
filteredBusinessServices = m_businessServiceDao.findMatching(nameCriteria);
Assert.assertEquals(2, filteredBusinessServices.size());
Assert.assertEquals(Lists.newArrayList(m_businessServiceDao.get(bsId1), m_businessServiceDao.get(bsId3)), filteredBusinessServices);
}
use of org.opennms.core.criteria.Criteria in project opennms by OpenNMS.
the class BusinessServiceManagerImpl method transform.
/**
* The criteria is build on BusinessService classes.
* However we want to use the dao to filter. Therefore we have to perform a mapping from BusinessService to BusinessServiceEntity.
*
* @param input
* @return
*/
private Criteria transform(Criteria input) {
Criteria criteria = input.clone();
criteria.setClass(BusinessServiceEntity.class);
return criteria;
}
use of org.opennms.core.criteria.Criteria in project opennms by OpenNMS.
the class DefaultGeolocationResolver method resolve.
@Override
public Map<Integer, Coordinates> resolve(Collection<Integer> nodeIds) {
if (nodeIds == null || nodeIds.isEmpty()) {
// nothing to do
return new HashMap<>();
}
// Lookup all nodes and gather the address string
final Criteria criteria = new CriteriaBuilder(OnmsNode.class).in("id", nodeIds).toCriteria();
final Map<Integer, String> nodeIdAddressMap = nodeDao.findMatching(criteria).stream().filter(n -> getGeoLocation(n) != null).filter(n -> getGeoLocation(n).getLatitude() == null && getGeoLocation(n).getLongitude() == null).filter(n -> !Strings.isNullOrEmpty(getGeoLocation(n).asAddressString())).collect(Collectors.toMap(OnmsNode::getId, n -> n.getAssetRecord().getGeolocation().asAddressString()));
return resolve(nodeIdAddressMap);
}
use of org.opennms.core.criteria.Criteria in project opennms by OpenNMS.
the class DefaultSurveillanceViewService method getNotificationsWithCriterias.
/**
* Returns a list of notifications for a given list of nodes.
*
* @param rowCategories the row catgories
* @param colCategories the column categories
* @param customSeverity the custom severity mapping for notifications
* @param severity the severity for these nodes
* @param criterias the restrictions to use
* @return the list of notifications
*/
private List<OnmsNotification> getNotificationsWithCriterias(final Set<OnmsCategory> rowCategories, final Set<OnmsCategory> colCategories, final Map<OnmsNotification, String> customSeverity, final String severity, final Restriction... criterias) {
CriteriaBuilder criteriaBuilder = new CriteriaBuilder(OnmsNotification.class);
criteriaBuilder.alias("node", "node");
final List<String> parameters = new ArrayList<>(rowCategories.stream().map(OnmsCategory::getName).collect(Collectors.toList()));
parameters.addAll(colCategories.stream().map(OnmsCategory::getName).collect(Collectors.toList()));
final Type[] types = new Type[parameters.size()];
Arrays.fill(types, Type.STRING);
// Restrict on OnmsNotification.nodeId
criteriaBuilder.sql(createQuery("{alias}.nodeId", rowCategories, colCategories), parameters.toArray(new String[parameters.size()]), types);
criteriaBuilder.ne("node.type", "D");
criteriaBuilder.orderBy("pageTime", false);
Criteria myCriteria = criteriaBuilder.toCriteria();
for (Restriction criteria : criterias) {
myCriteria.addRestriction(criteria);
}
List<OnmsNotification> notifications = m_notificationDao.findMatching(myCriteria);
for (OnmsNotification onmsNotification : notifications) {
customSeverity.put(onmsNotification, severity);
}
return notifications;
}
use of org.opennms.core.criteria.Criteria in project opennms by OpenNMS.
the class HypericAckProcessor method fetchUnclearedHypericAlarms.
/**
* <p>fetchUnclearedHypericAlarms</p>
*
* @return a {@link java.util.List} object.
*/
public List<OnmsAlarm> fetchUnclearedHypericAlarms() {
// Query for existing, unacknowledged alarms in OpenNMS that were generated based on Hyperic alerts
Criteria criteria = new Criteria(OnmsAlarm.class);
// criteria.add(Restrictions.isNull("alarmAckUser"));
// Restrict to Hyperic alerts
criteria.addRestriction(new EqRestriction("uei", "uei.opennms.org/external/hyperic/alert"));
// Only consider alarms that are above severity NORMAL
// {@see org.opennms.netmgt.model.OnmsSeverity}
criteria.addRestriction(new GtRestriction("severity", OnmsSeverity.NORMAL));
// Query list of outstanding alerts with remote platform identifiers
return m_alarmDao.findMatching(criteria);
}
Aggregations