From 56fc89e9df638be392a025a2c7f13c52fb99ce49 Mon Sep 17 00:00:00 2001 From: Asher Breton Date: Thu, 24 Sep 2020 14:22:53 +0300 Subject: [PATCH] Added limit to AqlQueryBuilder --- .../client/aql/AqlQueryBuilder.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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) + ")" : ""; }