diff --git a/services/src/main/java/org/jfrog/artifactory/client/aql/AqlQueryBuilder.java b/services/src/main/java/org/jfrog/artifactory/client/aql/AqlQueryBuilder.java index b07a2bfa..f1e57ff8 100644 --- a/services/src/main/java/org/jfrog/artifactory/client/aql/AqlQueryBuilder.java +++ b/services/src/main/java/org/jfrog/artifactory/client/aql/AqlQueryBuilder.java @@ -17,6 +17,7 @@ public class AqlQueryBuilder { private AqlRootElement root = new AqlRootElement(); private AqlItem sort; private AqlInclude include; + private int limit; public AqlQueryBuilder item(AqlItem item) { root.putAll(item.value()); @@ -83,15 +84,30 @@ public AqlQueryBuilder desc(String... by) { return this; } + public AqlQueryBuilder limit(int limit) { + if (limit > 0) { + this.limit = limit; + } + return this; + } + public String build() { try { ObjectMapper mapper = new ObjectMapper(); - return "items.find(" + getRootAsString(mapper) + ")" + getIncludeAsString() + getSortAsString(mapper); + return "items.find(" + getRootAsString(mapper) + ")" + getIncludeAsString() + getSortAsString(mapper) + getLimitAsString(); } catch (JsonProcessingException e) { throw new AqlBuilderException("Error serializing object to json: ", e); } } + private String getLimitAsString() { + return hasLimit() ? ".limit(" + limit + ")" : ""; + } + + private boolean hasLimit() { + return limit > 0; + } + private String getSortAsString(ObjectMapper mapper) throws JsonProcessingException { return hasSort() ? ".sort(" + mapper.writeValueAsString(sort) + ")" : ""; }