Search in sources :

Example 46 with DBUtils

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

the class NoticeFactory method canDisplayEvent.

/**
 * This method determines the log status of an event associated with a
 * notification
 *
 * @param eventId
 *            the unique id of the event from the notice
 * @return true if the event is display, false if log only
 */
public static boolean canDisplayEvent(int eventId) {
    boolean display = false;
    final DBUtils d = new DBUtils(NoticeFactory.class);
    try {
        Connection connection = DataSourceFactory.getInstance().getConnection();
        d.watch(connection);
        PreparedStatement statement = connection.prepareStatement("SELECT eventDisplay FROM events WHERE eventid=?");
        d.watch(statement);
        statement.setInt(1, eventId);
        ResultSet results = statement.executeQuery();
        d.watch(results);
        results.next();
        String status = results.getString(1);
        if (status.equals("Y")) {
            display = true;
        }
    } catch (SQLException e) {
        LOG.error("Error getting event display status: {}", e.getMessage(), e);
    } finally {
        d.cleanUp();
    }
    return display;
}
Also used : SQLException(java.sql.SQLException) DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 47 with DBUtils

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

the class NoticeFactory method getNoticesForInterface.

/**
 * Return all notices (optionally only unacknowledged notices) sorted by id
 * for the given interface.
 *
 * @param nodeId a int.
 * @param ipAddress a {@link java.lang.String} object.
 * @param includeAcknowledged a boolean.
 * @return an array of {@link org.opennms.web.notification.Notification} objects.
 * @throws java.sql.SQLException if any.
 */
public static Notification[] getNoticesForInterface(int nodeId, String ipAddress, boolean includeAcknowledged, ServletContext servletContext) throws SQLException {
    if (ipAddress == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    Notification[] notices = null;
    final DBUtils d = new DBUtils(NoticeFactory.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        final StringBuilder select = new StringBuilder("SELECT * FROM NOTIFICATIONS WHERE NODEID=? AND INTERFACEID=?");
        if (!includeAcknowledged) {
            select.append(" AND RESPONDTIME IS NULL");
        }
        select.append(" ORDER BY NOTIFYID DESC");
        PreparedStatement stmt = conn.prepareStatement(select.toString());
        d.watch(stmt);
        stmt.setInt(1, nodeId);
        stmt.setString(2, ipAddress);
        ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        notices = rs2Notices(rs, servletContext);
    } finally {
        d.cleanUp();
    }
    return notices;
}
Also used : DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 48 with DBUtils

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

the class NoticeFactory method getNotice.

/**
 * Return a specific notice.
 *
 * @param noticeId a int.
 * @return a {@link org.opennms.web.notification.Notification} object.
 * @throws java.sql.SQLException if any.
 */
public static Notification getNotice(int noticeId, ServletContext servletContext) throws SQLException {
    Notification notice = null;
    DBUtils d = new DBUtils(NoticeFactory.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        PreparedStatement stmt = conn.prepareStatement("SELECT * FROM NOTIFICATION WHERE NOTIFYID=?");
        d.watch(stmt);
        stmt.setInt(1, noticeId);
        ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        Notification[] notices = rs2Notices(rs, servletContext);
        // what do I do if this actually returns more than one service?
        if (notices.length > 0) {
            notice = notices[0];
        }
    } finally {
        d.cleanUp();
    }
    return notice;
}
Also used : DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 49 with DBUtils

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

the class NoticeFactory method getNoticeCount.

/**
 * Count the number of notices for a given acknowledgement type.
 *
 * @param ackType a {@link org.opennms.web.notification.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 getNoticeCount(AcknowledgeType ackType, Filter[] filters) throws SQLException {
    if (ackType == null || filters == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    int noticeCount = 0;
    final DBUtils d = new DBUtils(NoticeFactory.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        final StringBuilder select = new StringBuilder("SELECT COUNT(NOTIFYID) AS NOTICECOUNT FROM NOTIFICATIONS WHERE");
        select.append(ackType.getAcknowledgeTypeClause());
        for (Filter filter : filters) {
            select.append(" AND");
            select.append(filter.getParamSql());
        }
        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()) {
            noticeCount = rs.getInt("NOTICECOUNT");
        }
    } finally {
        d.cleanUp();
    }
    return noticeCount;
}
Also used : Filter(org.opennms.web.filter.Filter) NodeFilter(org.opennms.web.notification.filter.NodeFilter) DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 50 with DBUtils

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

the class NoticeFactory method getNoticesForService.

/**
 * Return all notices (optionally only unacknowledged notices) sorted by id
 * for the given service type, regardless of what node or interface they
 * belong to.
 *
 * @param serviceId a int.
 * @param includeAcknowledged a boolean.
 * @return an array of {@link org.opennms.web.notification.Notification} objects.
 * @throws java.sql.SQLException if any.
 */
public static Notification[] getNoticesForService(int serviceId, boolean includeAcknowledged, ServletContext servletContext) throws SQLException {
    Notification[] notices = null;
    DBUtils d = new DBUtils(NoticeFactory.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        final StringBuilder select = new StringBuilder("SELECT * FROM NOTIFICATION WHERE SERVICEID=?");
        if (!includeAcknowledged) {
            select.append(" AND RESPONDTIME IS NULL");
        }
        select.append(" ORDER BY NOTIFIYID DESC");
        PreparedStatement stmt = conn.prepareStatement(select.toString());
        d.watch(stmt);
        stmt.setInt(1, serviceId);
        ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        notices = rs2Notices(rs, servletContext);
    } finally {
        d.cleanUp();
    }
    return notices;
}
Also used : DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

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