public static Result categories(String query, Integer limit) {
if (!request().accepts("application/json")) {
return status(Http.Status.NOT_ACCEPTABLE);
}
SqlQuery sqlQuery;
SqlQuery sqlCountQuery;
if (query != null && query.length() > 0) {
String sqlString =
"SELECT DISTINCT category FROM label WHERE lower(category) LIKE :category";
sqlQuery = Ebean
.createSqlQuery(sqlString)
.setParameter("category", "%" + query.toLowerCase() + "%");
sqlCountQuery = Ebean
.createSqlQuery("SELECT COUNT(*) AS cnt FROM (" + sqlString + ")")
.setParameter("category", "%" + query.toLowerCase() + "%");
} else {
String sqlString =
"SELECT DISTINCT category FROM label";
sqlQuery = Ebean
.createSqlQuery(sqlString);
sqlCountQuery = Ebean
.createSqlQuery("SELECT COUNT(*) AS cnt FROM (" + sqlString + ")");
}
int cnt = sqlCountQuery.findUnique().getInteger("cnt");
if (limit > MAX_FETCH_LABELS) {
limit = MAX_FETCH_LABELS;
}