Search in sources :

Example 36 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class EventFactory method getParmsForEventId.

public static Map<String, String> getParmsForEventId(int eventId) throws SQLException {
    final Connection conn = DataSourceFactory.getInstance().getConnection();
    final DBUtils d = new DBUtils(EventFactory.class, conn);
    final Map<String, String> result = Maps.newHashMap();
    try {
        PreparedStatement stmt = conn.prepareStatement("SELECT name, value FROM event_parameters WHERE eventid = ?");
        d.watch(stmt);
        stmt.setInt(1, eventId);
        ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        while (rs.next()) {
            result.put(rs.getString("name"), rs.getString("value"));
        }
    } finally {
        d.cleanUp();
    }
    return result;
}
Also used : Connection(java.sql.Connection) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 37 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class EventFactory method unacknowledge.

/**
 * Unacknowledge events that match the given filter criteria.
 *
 * @param filters an array of org$opennms$web$filter$Filter objects.
 * @throws java.sql.SQLException if any.
 */
public static void unacknowledge(Filter[] filters) throws SQLException {
    if (filters == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    final StringBuilder update = new StringBuilder("UPDATE EVENTS SET EVENTACKUSER=NULL, EVENTACKTIME=NULL WHERE");
    update.append(getAcknowledgeTypeClause(AcknowledgeType.ACKNOWLEDGED));
    for (Filter filter : filters) {
        update.append(" AND");
        update.append(filter.getParamSql());
    }
    final DBUtils d = new DBUtils(EventFactory.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        PreparedStatement stmt = conn.prepareStatement(update.toString());
        d.watch(stmt);
        int parameterIndex = 1;
        for (Filter filter : filters) {
            parameterIndex += filter.bindParam(stmt, parameterIndex);
        }
        stmt.executeUpdate();
    } finally {
        d.cleanUp();
    }
}
Also used : IfIndexFilter(org.opennms.web.event.filter.IfIndexFilter) SeverityFilter(org.opennms.web.event.filter.SeverityFilter) Filter(org.opennms.web.filter.Filter) InterfaceFilter(org.opennms.web.event.filter.InterfaceFilter) NodeFilter(org.opennms.web.event.filter.NodeFilter) ServiceFilter(org.opennms.web.event.filter.ServiceFilter) DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 38 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class EventFactory method unacknowledge.

/**
 * Unacknowledge a list of events.
 *
 * @param eventIds an array of int.
 * @throws java.sql.SQLException if any.
 */
public static void unacknowledge(int[] eventIds) throws SQLException {
    if (eventIds == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    if (eventIds.length > 0) {
        final StringBuilder update = new StringBuilder("UPDATE EVENTS SET EVENTACKUSER=NULL, EVENTACKTIME=NULL");
        update.append(" WHERE EVENTID IN (");
        update.append(eventIds[0]);
        for (int i = 1; i < eventIds.length; i++) {
            update.append(",");
            update.append(eventIds[i]);
        }
        update.append(")");
        DBUtils d = new DBUtils(EventFactory.class);
        try {
            Connection conn = DataSourceFactory.getInstance().getConnection();
            d.watch(conn);
            PreparedStatement stmt = conn.prepareStatement(update.toString());
            d.watch(stmt);
            stmt.executeUpdate();
        } finally {
            d.cleanUp();
        }
    }
}
Also used : DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 39 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class EventFactory method getEventCountBySeverity.

/**
 * Count the number of events for a given acknowledgement type.
 *
 * @return An array of event counts. Each index of the array corresponds to
 *         the event severity for the counts (indeterminate is 1, critical
 *         is 7, etc).
 * @param ackType a {@link org.opennms.web.event.AcknowledgeType} object.
 * @param filters an array of org$opennms$web$filter$Filter objects.
 * @throws java.sql.SQLException if any.
 */
public static int[] getEventCountBySeverity(AcknowledgeType ackType, Filter[] filters) throws SQLException {
    if (ackType == null || filters == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    int[] eventCounts = new int[8];
    final Connection conn = DataSourceFactory.getInstance().getConnection();
    final DBUtils d = new DBUtils(EventFactory.class, conn);
    try {
        final StringBuilder select = new StringBuilder("SELECT EVENTSEVERITY, COUNT(*) AS EVENTCOUNT FROM EVENTS LEFT OUTER JOIN NODE USING (NODEID) LEFT OUTER JOIN SERVICE USING (SERVICEID) WHERE ");
        select.append(getAcknowledgeTypeClause(ackType));
        for (Filter filter : filters) {
            select.append(" AND");
            select.append(filter.getParamSql());
        }
        select.append(" AND EVENTDISPLAY='Y'");
        select.append(" GROUP BY EVENTSEVERITY");
        final PreparedStatement stmt = conn.prepareStatement(select.toString());
        d.watch(stmt);
        int parameterIndex = 1;
        for (Filter filter : filters) {
            parameterIndex += filter.bindParam(stmt, parameterIndex);
        }
        final ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        while (rs.next()) {
            int severity = rs.getInt("EVENTSEVERITY");
            int eventCount = rs.getInt("EVENTCOUNT");
            eventCounts[severity] = eventCount;
        }
    } finally {
        d.cleanUp();
    }
    return eventCounts;
}
Also used : IfIndexFilter(org.opennms.web.event.filter.IfIndexFilter) SeverityFilter(org.opennms.web.event.filter.SeverityFilter) Filter(org.opennms.web.filter.Filter) InterfaceFilter(org.opennms.web.event.filter.InterfaceFilter) NodeFilter(org.opennms.web.event.filter.NodeFilter) ServiceFilter(org.opennms.web.event.filter.ServiceFilter) Connection(java.sql.Connection) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 40 with DBUtils

use of org.opennms.core.utils.DBUtils in project opennms by OpenNMS.

the class EventFactory method acknowledge.

/**
 * Acknowledge with the given username and the given time all events that
 * match the given filter criteria.
 *
 * @param filters an array of org$opennms$web$filter$Filter objects.
 * @param user a {@link java.lang.String} object.
 * @param time a java$util$Date object.
 * @throws java.sql.SQLException if any.
 */
public static void acknowledge(Filter[] filters, String user, Date time) throws SQLException {
    if (filters == null || user == null || time == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    final StringBuilder update = new StringBuilder("UPDATE EVENTS SET EVENTACKUSER=?, EVENTACKTIME=? WHERE");
    update.append(getAcknowledgeTypeClause(AcknowledgeType.UNACKNOWLEDGED));
    for (Filter filter : filters) {
        update.append(" AND");
        update.append(filter.getParamSql());
    }
    final DBUtils d = new DBUtils(EventFactory.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        PreparedStatement stmt = conn.prepareStatement(update.toString());
        d.watch(stmt);
        stmt.setString(1, user);
        stmt.setTimestamp(2, new Timestamp(time.getTime()));
        int parameterIndex = 3;
        for (Filter filter : filters) {
            parameterIndex += filter.bindParam(stmt, parameterIndex);
        }
        stmt.executeUpdate();
    } finally {
        d.cleanUp();
    }
}
Also used : IfIndexFilter(org.opennms.web.event.filter.IfIndexFilter) SeverityFilter(org.opennms.web.event.filter.SeverityFilter) Filter(org.opennms.web.filter.Filter) InterfaceFilter(org.opennms.web.event.filter.InterfaceFilter) NodeFilter(org.opennms.web.event.filter.NodeFilter) ServiceFilter(org.opennms.web.event.filter.ServiceFilter) DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Aggregations

DBUtils (org.opennms.core.utils.DBUtils)79 Connection (java.sql.Connection)70 PreparedStatement (java.sql.PreparedStatement)70 ResultSet (java.sql.ResultSet)51 SQLException (java.sql.SQLException)17 ArrayList (java.util.ArrayList)14 Timestamp (java.sql.Timestamp)12 Statement (java.sql.Statement)10 Filter (org.opennms.web.filter.Filter)9 ServletException (javax.servlet.ServletException)5 IfIndexFilter (org.opennms.web.event.filter.IfIndexFilter)5 InterfaceFilter (org.opennms.web.event.filter.InterfaceFilter)5 NodeFilter (org.opennms.web.event.filter.NodeFilter)5 ServiceFilter (org.opennms.web.event.filter.ServiceFilter)5 SeverityFilter (org.opennms.web.event.filter.SeverityFilter)5 Date (java.util.Date)4 FilterParseException (org.opennms.netmgt.filter.api.FilterParseException)4 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 InetAddress (java.net.InetAddress)3 HashMap (java.util.HashMap)3