Search in sources :

Example 76 with DBUtils

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

the class NoticeFactory method acknowledge.

/**
     * Acknowledge a list of notices with the given username and the given time.
     *
     * @param noticeIds an array of int.
     * @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(int[] noticeIds, String user, Date time) throws SQLException {
    if (noticeIds == null || user == null || time == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    if (noticeIds.length > 0) {
        StringBuffer update = new StringBuffer("UPDATE NOTIFICATIONS SET RESPONDTIME=?, ANSWEREDBY=?");
        update.append(" WHERE NOTIFYID IN (");
        update.append(noticeIds[0]);
        for (int i = 1; i < noticeIds.length; i++) {
            update.append(",");
            update.append(noticeIds[i]);
        }
        update.append(")");
        update.append(" AND RESPONDTIME IS NULL");
        DBUtils d = new DBUtils(NoticeFactory.class);
        try {
            Connection conn = DataSourceFactory.getInstance().getConnection();
            d.watch(conn);
            PreparedStatement stmt = conn.prepareStatement(update.toString());
            d.watch(stmt);
            stmt.setTimestamp(1, new Timestamp(time.getTime()));
            stmt.setString(2, user);
            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 77 with DBUtils

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

the class EventFactory method getEventCount.

/**
     * Count the number of events for a given acknowledgement type.
     *
     * @param ackType a {@link org.opennms.web.event.AcknowledgeType} object.
     * @param filters an array of org$opennms$web$filter$Filter objects.
     * @return a int.
     * @throws java.sql.SQLException if any.
     */
public static int getEventCount(AcknowledgeType ackType, Filter[] filters) throws SQLException {
    if (ackType == null || filters == 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 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' ");
        PreparedStatement stmt = conn.prepareStatement(select.toString());
        d.watch(stmt);
        int parameterIndex = 1;
        for (Filter filter : filters) {
            parameterIndex += filter.bindParam(stmt, parameterIndex);
        }
        ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        if (rs.next()) {
            eventCount = rs.getInt("EVENTCOUNT");
        }
        stmt.close();
    } finally {
        d.cleanUp();
    }
    return eventCount;
}
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)

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