Search in sources :

Example 36 with OnmsCriteria

use of org.opennms.netmgt.model.OnmsCriteria in project opennms by OpenNMS.

the class DaoWebNotificationRepository method getOnmsCriteria.

/**
 * TODO: Convert this function to use {@link Criteria} instead.
 */
private static final OnmsCriteria getOnmsCriteria(final NotificationCriteria notificationCriteria) {
    final OnmsCriteria criteria = new OnmsCriteria(OnmsNotification.class);
    criteria.createAlias("node", "node", OnmsCriteria.LEFT_JOIN);
    criteria.createAlias("serviceType", "serviceType", OnmsCriteria.LEFT_JOIN);
    criteria.createAlias("event", "event", OnmsCriteria.LEFT_JOIN);
    criteria.createAlias("event.distPoller", "distPoller", OnmsCriteria.LEFT_JOIN);
    notificationCriteria.visit(new NotificationCriteriaVisitor<RuntimeException>() {

        @Override
        public void visitAckType(AcknowledgeType ackType) throws RuntimeException {
            if (ackType == AcknowledgeType.ACKNOWLEDGED) {
                criteria.add(Restrictions.isNotNull("answeredBy"));
            } else if (ackType == AcknowledgeType.UNACKNOWLEDGED) {
                criteria.add(Restrictions.isNull("answeredBy"));
            }
        // AcknowledgeType.BOTH just adds no restriction
        }

        @Override
        public void visitFilter(Filter filter) throws RuntimeException {
            criteria.add(filter.getCriterion());
        }

        @Override
        public void visitLimit(int limit, int offset) throws RuntimeException {
            criteria.setMaxResults(limit);
            criteria.setFirstResult(offset);
        }

        @Override
        public void visitSortStyle(SortStyle sortStyle) throws RuntimeException {
            switch(sortStyle) {
                case LOCATION:
                    criteria.addOrder(Order.desc("event.distPoller.location"));
                    break;
                case RESPONDER:
                    criteria.addOrder(Order.desc("answeredBy"));
                    break;
                case PAGETIME:
                    criteria.addOrder(Order.desc("pageTime"));
                    break;
                case RESPONDTIME:
                    criteria.addOrder(Order.desc("respondTime"));
                    break;
                case NODE:
                    criteria.addOrder(Order.desc("node.label"));
                    break;
                case NODE_LOCATION:
                    criteria.addOrder(Order.desc("node.location.locationName"));
                    break;
                case INTERFACE:
                    criteria.addOrder(Order.desc("ipAddress"));
                    break;
                case SERVICE:
                    criteria.addOrder(Order.desc("serviceType.name"));
                    break;
                case ID:
                    criteria.addOrder(Order.desc("notifyId"));
                    break;
                case SEVERITY:
                    criteria.addOrder(Order.desc("event.eventSeverity"));
                    break;
                case REVERSE_LOCATION:
                    criteria.addOrder(Order.desc("event.distPoller.location"));
                    break;
                case REVERSE_RESPONDER:
                    criteria.addOrder(Order.asc("answeredBy"));
                    break;
                case REVERSE_PAGETIME:
                    criteria.addOrder(Order.asc("pageTime"));
                    break;
                case REVERSE_RESPONDTIME:
                    criteria.addOrder(Order.asc("respondTime"));
                    break;
                case REVERSE_NODE:
                    criteria.addOrder(Order.asc("node.label"));
                    break;
                case REVERSE_NODE_LOCATION:
                    criteria.addOrder(Order.asc("node.location.locationName"));
                    break;
                case REVERSE_INTERFACE:
                    criteria.addOrder(Order.asc("ipAddress"));
                    break;
                case REVERSE_SERVICE:
                    criteria.addOrder(Order.asc("serviceType.name"));
                    break;
                case REVERSE_ID:
                    criteria.addOrder(Order.asc("notifyId"));
                    break;
                case REVERSE_SEVERITY:
                    criteria.addOrder(Order.asc("event.eventSeverity"));
                    break;
            }
        }
    });
    return criteria;
}
Also used : Filter(org.opennms.web.filter.Filter) OnmsCriteria(org.opennms.netmgt.model.OnmsCriteria)

Example 37 with OnmsCriteria

use of org.opennms.netmgt.model.OnmsCriteria in project opennms by OpenNMS.

the class NetworkElementFactory method getNodeIdsWithIpLike.

/* (non-Javadoc)
	 * @see org.opennms.web.element.NetworkElementFactoryInterface#getNodeIdsWithIpLike(java.lang.String)
	 */
@Override
public List<Integer> getNodeIdsWithIpLike(String iplike) {
    if (iplike == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    OnmsCriteria nodeCrit = new OnmsCriteria(OnmsNode.class);
    nodeCrit.createCriteria("ipInterfaces", "iface").add(OnmsRestrictions.ipLike(iplike));
    nodeCrit.add(Restrictions.ne("type", "D"));
    nodeCrit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Integer> nodeIds = new ArrayList<>();
    List<OnmsNode> nodes = m_nodeDao.findMatching(nodeCrit);
    for (OnmsNode node : nodes) {
        nodeIds.add(node.getId());
    }
    return nodeIds;
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) ArrayList(java.util.ArrayList) OnmsCriteria(org.opennms.netmgt.model.OnmsCriteria)

Example 38 with OnmsCriteria

use of org.opennms.netmgt.model.OnmsCriteria in project opennms by OpenNMS.

the class NetworkElementFactory method getInterface.

/* (non-Javadoc)
	 * @see org.opennms.web.element.NetworkElementFactoryInterface#getInterface(int, java.lang.String)
	 */
@Override
public Interface getInterface(int nodeId, String ipAddress) {
    OnmsCriteria criteria = new OnmsCriteria(OnmsIpInterface.class);
    criteria.createAlias("node", "node");
    criteria.add(Restrictions.eq("node.id", nodeId));
    criteria.add(Restrictions.eq("ipAddress", InetAddressUtils.addr(ipAddress)));
    criteria.setFetchMode("snmpInterface", FetchMode.JOIN);
    List<OnmsIpInterface> ifaces = m_ipInterfaceDao.findMatching(criteria);
    return ifaces.size() > 0 ? new Interface(ifaces.get(0)) : null;
}
Also used : OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) OnmsCriteria(org.opennms.netmgt.model.OnmsCriteria) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface)

Example 39 with OnmsCriteria

use of org.opennms.netmgt.model.OnmsCriteria in project opennms by OpenNMS.

the class DaoWebEventRepository method getOnmsCriteria.

private OnmsCriteria getOnmsCriteria(final EventCriteria eventCriteria) {
    final OnmsCriteria criteria = new OnmsCriteria(OnmsEvent.class);
    criteria.createAlias("alarm", "alarm", OnmsCriteria.LEFT_JOIN);
    criteria.createAlias("node", "node", OnmsCriteria.LEFT_JOIN);
    criteria.createAlias("serviceType", "serviceType", OnmsCriteria.LEFT_JOIN);
    criteria.createAlias("distPoller", "distPoller", OnmsCriteria.LEFT_JOIN);
    criteria.add(new EventDisplayFilter("Y").getCriterion());
    eventCriteria.visit(new EventCriteriaVisitor<RuntimeException>() {

        @Override
        public void visitAckType(AcknowledgeType ackType) throws RuntimeException {
            if (ackType == AcknowledgeType.ACKNOWLEDGED) {
                criteria.add(Restrictions.isNotNull("eventAckUser"));
            } else if (ackType == AcknowledgeType.UNACKNOWLEDGED) {
                criteria.add(Restrictions.isNull("eventAckUser"));
            }
        }

        @Override
        public void visitFilter(Filter filter) throws RuntimeException {
            criteria.add(filter.getCriterion());
        }

        @Override
        public void visitLimit(int limit, int offset) throws RuntimeException {
            criteria.setMaxResults(limit);
            criteria.setFirstResult(offset);
        }

        @Override
        public void visitSortStyle(SortStyle sortStyle) throws RuntimeException {
            switch(sortStyle) {
                case ID:
                    criteria.addOrder(Order.desc("id"));
                    break;
                case INTERFACE:
                    criteria.addOrder(Order.desc("ipAddr"));
                    break;
                case NODE:
                    criteria.addOrder(Order.desc("node.label"));
                    break;
                case POLLER:
                    criteria.addOrder(Order.desc("distPoller"));
                    break;
                case SERVICE:
                    criteria.addOrder(Order.desc("serviceType.name"));
                    break;
                case SEVERITY:
                    criteria.addOrder(Order.desc("eventSeverity"));
                    break;
                case TIME:
                    criteria.addOrder(Order.desc("eventTime"));
                    break;
                case LOCATION:
                    criteria.addOrder(Order.desc("distPoller.location"));
                    break;
                case SYSTEMID:
                    criteria.addOrder(Order.desc("distPoller.id"));
                    break;
                case REVERSE_ID:
                    criteria.addOrder(Order.asc("id"));
                    break;
                case REVERSE_INTERFACE:
                    criteria.addOrder(Order.asc("ipAddr"));
                    break;
                case REVERSE_NODE:
                    criteria.addOrder(Order.asc("node.label"));
                    break;
                case REVERSE_POLLER:
                    criteria.addOrder(Order.asc("distPoller"));
                    break;
                case REVERSE_SERVICE:
                    criteria.addOrder(Order.desc("serviceType.name"));
                    break;
                case REVERSE_SEVERITY:
                    criteria.addOrder(Order.asc("eventSeverity"));
                    break;
                case REVERSE_TIME:
                    criteria.addOrder(Order.asc("eventTime"));
                    break;
                case REVERSE_LOCATION:
                    criteria.addOrder(Order.asc("distPoller.location"));
                    break;
                case REVERSE_SYSTEMID:
                    criteria.addOrder(Order.asc("distPoller.id"));
                    break;
            }
        }
    });
    return criteria;
}
Also used : EventDisplayFilter(org.opennms.web.event.filter.EventDisplayFilter) Filter(org.opennms.web.filter.Filter) OnmsCriteria(org.opennms.netmgt.model.OnmsCriteria) EventDisplayFilter(org.opennms.web.event.filter.EventDisplayFilter)

Example 40 with OnmsCriteria

use of org.opennms.netmgt.model.OnmsCriteria in project opennms by OpenNMS.

the class HibernateCriteriaConverterIT method testNodeQuery.

@Test
@JUnitTemporaryDatabase
public void testNodeQuery() throws Exception {
    List<OnmsNode> nodes;
    // first, try with OnmsCriteria
    final OnmsCriteria crit = new OnmsCriteria(OnmsNode.class);
    crit.add(org.hibernate.criterion.Restrictions.isNotNull("id"));
    nodes = m_nodeDao.findMatching(crit);
    assertEquals(6, nodes.size());
    // then the same with the builder
    final CriteriaBuilder cb = new CriteriaBuilder(OnmsNode.class);
    cb.isNotNull("id");
    nodes = m_nodeDao.findMatching(cb.toCriteria());
    assertEquals(6, nodes.size());
    cb.eq("label", "node1").join("ipInterfaces", "ipInterface").eq("ipInterface.ipAddress", "192.168.1.1");
    nodes = m_nodeDao.findMatching(cb.toCriteria());
    assertEquals(1, nodes.size());
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsCriteria(org.opennms.netmgt.model.OnmsCriteria) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Aggregations

OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)51 Transactional (org.springframework.transaction.annotation.Transactional)8 Filter (org.opennms.web.filter.Filter)7 Test (org.junit.Test)6 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)6 OnmsNode (org.opennms.netmgt.model.OnmsNode)5 ArrayList (java.util.ArrayList)4 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)4 OnmsSnmpInterface (org.opennms.netmgt.model.OnmsSnmpInterface)3 AlarmCriteria (org.opennms.web.alarm.filter.AlarmCriteria)3 Date (java.util.Date)2 List (java.util.List)2 Criteria (org.hibernate.Criteria)2 Session (org.hibernate.Session)2 JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)2 OnmsAcknowledgment (org.opennms.netmgt.model.OnmsAcknowledgment)2 AcknowledgedByFilter (org.opennms.web.alarm.filter.AcknowledgedByFilter)2 SeverityFilter (org.opennms.web.alarm.filter.SeverityFilter)2 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)2 SyndContent (com.rometools.rome.feed.synd.SyndContent)1