use of org.zalando.nakadi.exceptions.ServiceUnavailableException in project nakadi by zalando.
the class SubscriptionDbRepository method listSubscriptions.
public List<Subscription> listSubscriptions(final Set<String> eventTypes, final Optional<String> owningApplication, final int offset, final int limit) throws ServiceUnavailableException {
final StringBuilder queryBuilder = new StringBuilder("SELECT s_subscription_object FROM zn_data.subscription ");
final List<String> clauses = Lists.newArrayList();
final List<Object> params = Lists.newArrayList();
owningApplication.ifPresent(owningApp -> {
clauses.add(" s_subscription_object->>'owning_application' = ? ");
params.add(owningApp);
});
if (!eventTypes.isEmpty()) {
final String clause = eventTypes.stream().map(et -> " s_subscription_object->'event_types' @> ?::jsonb").collect(Collectors.joining(" AND "));
clauses.add(clause);
eventTypes.stream().map(et -> format("\"{0}\"", et)).forEach(params::add);
}
if (!clauses.isEmpty()) {
queryBuilder.append(" WHERE ");
queryBuilder.append(StringUtils.join(clauses, " AND "));
}
queryBuilder.append(" ORDER BY s_subscription_object->>'created_at' DESC LIMIT ? OFFSET ? ");
params.add(limit);
params.add(offset);
try {
return jdbcTemplate.query(queryBuilder.toString(), params.toArray(), rowMapper);
} catch (final DataAccessException e) {
LOG.error("Database error when listing subscriptions", e);
throw new ServiceUnavailableException("Error occurred when running database request");
}
}
Aggregations