public List<Activity> listActivitiesChronologically(String contextId, String tenantDomain) { List<Activity> activities = listActivities(contextId, tenantDomain); Collections.sort( activities, new Comparator<Activity>() { @Override public int compare(Activity a1, Activity a2) { return a1.getTimestamp() - a2.getTimestamp(); } }); return activities; }
public List<Activity> listActivities(String contextId, String tenantDomain) { List<Activity> activities = null; Connection connection = getConnection(); if (connection != null) { PreparedStatement statement = null; ResultSet resultSet = null; try { statement = connection.prepareStatement(SELECT_CQL); statement.setString(1, contextId); resultSet = statement.executeQuery(); activities = new ArrayList<Activity>(); while (resultSet.next()) { JsonObject body = (JsonObject) parser.parse(resultSet.getString(BODY_COLUMN)); String tenant = getTenant(body); if (tenantDomain.equals(tenant)) { Activity activity = new Activity(body.getAsJsonObject(), resultSet.getInt(TIMESTAMP_COLUMN)); activities.add(activity); } } } catch (SQLException e) { String message = e.getMessage(); // we'll ignore the "Keyspace EVENT_KS does not exist" error, // this happens when there are 0 activities in Cassandra. if (!(message.startsWith("Keyspace ") && message.endsWith(" does not exist"))) { LOG.error("Can't retrieve activities form cassandra.", e); } } finally { try { if (statement != null) { statement.close(); } if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // ignore } closeConnection(connection); } } if (activities != null) { return activities; } else { return Collections.emptyList(); } }