Search in sources :

Example 41 with DBUtils

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

the class EventFactory method getEventCountForNode.

/**
     * Return the number of events for this node and the given acknowledgment
     * type.
     *
     * @param nodeId a int.
     * @param ackType a {@link org.opennms.web.event.AcknowledgeType} object.
     * @return a int.
     * @throws java.sql.SQLException if any.
     */
public static int getEventCountForNode(int nodeId, AcknowledgeType ackType) throws SQLException {
    if (ackType == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    int eventCount = 0;
    final Connection conn = DataSourceFactory.getInstance().getConnection();
    final DBUtils d = new DBUtils(EventFactory.class, conn);
    try {
        StringBuffer select = new StringBuffer("SELECT COUNT(EVENTID) AS EVENTCOUNT FROM EVENTS WHERE ");
        select.append(getAcknowledgeTypeClause(ackType));
        select.append(" AND NODEID=?");
        select.append(" AND EVENTDISPLAY='Y' ");
        final PreparedStatement stmt = conn.prepareStatement(select.toString());
        d.watch(stmt);
        stmt.setInt(1, nodeId);
        final ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        if (rs.next()) {
            eventCount = rs.getInt("EVENTCOUNT");
        }
    } finally {
        d.cleanUp();
    }
    return eventCount;
}
Also used : Connection(java.sql.Connection) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 42 with DBUtils

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

the class EventFactory method unacknowledgeAll.

/**
     * Unacknowledge all acknowledged events.
     *
     * @throws java.sql.SQLException if any.
     */
public static void unacknowledgeAll() throws SQLException {
    final DBUtils d = new DBUtils(EventFactory.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        PreparedStatement stmt = conn.prepareStatement("UPDATE EVENTS SET EVENTACKUSER=NULL, EVENTACKTIME=NULL WHERE EVENTACKUSER IS NOT NULL");
        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 43 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.");
    }
    StringBuffer update = new StringBuffer("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 44 with DBUtils

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

the class EventFactory method acknowledgeAll.

/**
     * Acknowledge all unacknowledged events with the given username and the
     * given time.
     *
     * @param user a {@link java.lang.String} object.
     * @param time a java$util$Date object.
     * @throws java.sql.SQLException if any.
     */
public static void acknowledgeAll(String user, Date time) throws SQLException {
    if (user == null || time == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    final DBUtils d = new DBUtils(EventFactory.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        PreparedStatement stmt = conn.prepareStatement("UPDATE EVENTS SET EVENTACKUSER=?, EVENTACKTIME=? WHERE EVENTACKUSER IS NULL");
        d.watch(stmt);
        stmt.setString(1, user);
        stmt.setTimestamp(2, new Timestamp(time.getTime()));
        stmt.executeUpdate();
    } finally {
        d.cleanUp();
    }
}
Also used : DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 45 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.");
    }
    StringBuffer update = new StringBuffer("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)77 Connection (java.sql.Connection)68 PreparedStatement (java.sql.PreparedStatement)64 ResultSet (java.sql.ResultSet)50 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 FilterParseException (org.opennms.netmgt.filter.api.FilterParseException)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 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 InetAddress (java.net.InetAddress)3 RequestDispatcher (javax.servlet.RequestDispatcher)3