@Test
  @Transactional
  @JUnitTemporaryDatabase
  public void testSort() {

    for (SortStyle style : SortStyle.values()) {
      AlarmCriteria sorted =
          new AlarmCriteria(new Filter[0], style, AcknowledgeType.UNACKNOWLEDGED, 100, 0);
      OnmsAlarm[] alarms = m_alarmRepo.getMatchingAlarms(AlarmUtil.getOnmsCriteria(sorted));
      assertTrue("Failed to sort with style " + style, alarms.length > 0);
    }
  }
Example #2
0
 /**
  * Convenience method for getting the SQL <em>ORDER BY</em> clause related to a given sort style.
  *
  * @param sortStyle a {@link org.opennms.web.event.SortStyle} object.
  * @return a {@link java.lang.String} object.
  */
 protected static String getOrderByClause(SortStyle sortStyle) {
   if (sortStyle == null) {
     throw new IllegalArgumentException("Cannot take null parameters.");
   }
   return sortStyle.getOrderByClause();
 }
Example #3
0
  /**
   * Return all notices (optionally only unacknowledged notices) sorted by the given sort style.
   *
   * <p><strong>Note: </strong> This limit/offset code is <em>Postgres specific!</em> Per <a
   * href="mailto:[email protected]">Shane </a>, this is okay for now until we can come up with an
   * Oracle alternative too.
   *
   * @param limit if -1 or zero, no limit or offset is used
   * @param offset if -1, no limit or offset if used
   * @param sortStyle a {@link org.opennms.web.outage.SortStyle} object.
   * @param outType a {@link org.opennms.web.outage.OutageType} object.
   * @param filters an array of org$opennms$web$filter$Filter objects.
   * @return an array of {@link org.opennms.web.outage.Outage} objects.
   * @throws java.sql.SQLException if any.
   */
  public static Outage[] getOutages(
      SortStyle sortStyle, OutageType outType, Filter[] filters, int limit, int offset)
      throws SQLException {
    if (sortStyle == null || outType == null || filters == null) {
      throw new IllegalArgumentException("Cannot take null parameters.");
    }

    boolean useLimits = false;
    if (limit > 0 && offset > -1) {
      useLimits = true;
    }

    Outage[] outages = null;
    final Connection conn = Vault.getDbConnection();
    final DBUtils d = new DBUtils(OutageFactory.class, conn);

    try {
      StringBuffer select =
          new StringBuffer(
              "SELECT OUTAGES.*, NODE.NODELABEL, IPINTERFACE.IPHOSTNAME, SERVICE.SERVICENAME, NOTIFICATIONS.NOTIFYID, NOTIFICATIONS.ANSWEREDBY FROM OUTAGES "
                  + "JOIN NODE USING(NODEID) "
                  + "JOIN IPINTERFACE ON OUTAGES.NODEID=IPINTERFACE.NODEID AND OUTAGES.IPADDR=IPINTERFACE.IPADDR "
                  + "JOIN IFSERVICES ON OUTAGES.NODEID=IFSERVICES.NODEID AND OUTAGES.IPADDR=IFSERVICES.IPADDR AND OUTAGES.SERVICEID=IFSERVICES.SERVICEID "
                  + "LEFT OUTER JOIN SERVICE ON OUTAGES.SERVICEID=SERVICE.SERVICEID "
                  + "LEFT OUTER JOIN NOTIFICATIONS ON SVCLOSTEVENTID=NOTIFICATIONS.EVENTID "
                  + "WHERE (NODE.NODETYPE != 'D' AND IPINTERFACE.ISMANAGED != 'D' AND IFSERVICES.STATUS != 'D') "
                  + "AND ");
      select.append(outType.getClause());

      for (Filter filter : filters) {
        select.append(" AND ");
        select.append(filter.getParamSql());
      }

      select.append(sortStyle.getOrderByClause());

      if (useLimits) {
        select.append(" LIMIT ");
        select.append(limit);
        select.append(" OFFSET ");
        select.append(offset);
      }

      log.debug(select.toString());

      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);

      outages = rs2Outages(rs);
    } finally {
      d.cleanUp();
    }

    return outages;
  }