Skip to content

Commit

Permalink
feat(aql): Adding Pagination to AQL Query Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
sailingcat committed Mar 29, 2023
1 parent 5cb0b0e commit 876f002
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,109 +14,138 @@
import static org.jfrog.artifactory.client.aql.AqlInclude.buildWithElements;

public class AqlQueryBuilder {
private AqlRootElement root = new AqlRootElement();
private AqlItem sort;
private AqlInclude include;
private AqlRootElement root = new AqlRootElement();
private AqlItem sort;
private AqlInclude include;
private Integer limit;
private Integer offset;

public AqlQueryBuilder item(AqlItem item) {
root.putAll(item.value());
return this;
}

public AqlQueryBuilder item(AqlItem item) {
root.putAll(item.value());
return this;
}

public AqlQueryBuilder elements(AqlItem... items) {
if (isNotEmpty(items)) {
root.putAll(Arrays.stream(items)
.map(item -> item.value().entrySet())
.flatMap(Collection::stream)
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
}
return this;
}
public AqlQueryBuilder elements(AqlItem... items) {
if (isNotEmpty(items)) {
root.putAll(Arrays.stream(items)
.map(item -> item.value().entrySet())
.flatMap(Collection::stream)
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
}
return this;
}

public AqlQueryBuilder array(String key, AqlItem... items) {
if (isNotEmpty(items)) {
root.put(key, items);
}
return this;
}

public AqlQueryBuilder and(AqlItem... items) {
if (isNotEmpty(items)) {
root.putAll(AqlItem.and((Object[])items).value());
}
return this;
}

public AqlQueryBuilder and(Collection<AqlItem> items) {
return and(setToArray(items));
}

public AqlQueryBuilder or(AqlItem... items) {
if (isNotEmpty(items)) {
root.putAll(AqlItem.or((Object[])items).value());
}
return this;
}

public AqlQueryBuilder or(Collection<AqlItem> items) {
return or(setToArray(items));
}

public AqlQueryBuilder include(String... elements) {
if (isNotEmpty(elements)) {
include = buildWithElements(elements);
}
return this;
}

public AqlQueryBuilder asc(String... by) {
if (isNotEmpty(by)) {
this.sort = AqlItem.asc(by);
}
return this;
}

public AqlQueryBuilder desc(String... by) {
if (isNotEmpty(by)) {
this.sort = AqlItem.desc(by);
}
return this;
}

public AqlQueryBuilder limit(int limit) {
this.limit = limit;
return this;
}

public AqlQueryBuilder offset(int offset) {
this.offset = offset;
return this;
}

public String build() {
try {
ObjectMapper mapper = new ObjectMapper();
return "items.find(" + getRootAsString(mapper) + ")" + getIncludeAsString() + getSortAsString(
mapper) + getOffsetAsString(mapper) + getLimitAsString(mapper);
} catch (JsonProcessingException e) {
throw new AqlBuilderException("Error serializing object to json: ", e);
}
}

private String getSortAsString(ObjectMapper mapper) throws JsonProcessingException {
return hasSort() ? ".sort(" + mapper.writeValueAsString(sort) + ")" : "";
}

private String getIncludeAsString() {
return hasInclude() ? include.toString() : "";
}

private String getOffsetAsString(ObjectMapper mapper) throws JsonProcessingException {
return hasOffset() ? ".offset(" + mapper.writeValueAsString(offset) + ")" : "";
}

private String getLimitAsString(ObjectMapper mapper) throws JsonProcessingException {
return hasLimit() ? ".limit(" + mapper.writeValueAsString(limit) + ")" : "";
}

private String getRootAsString(ObjectMapper mapper) throws JsonProcessingException {
return hasRoot() ? mapper.writeValueAsString(root) : "";
}

public AqlQueryBuilder array(String key, AqlItem... items) {
if (isNotEmpty(items)) {
root.put(key, items);
}
return this;
}
private boolean hasInclude() {
return include != null && include.isNotEmpty();
}

public AqlQueryBuilder and(AqlItem... items) {
if (isNotEmpty(items)) {
root.putAll(AqlItem.and((Object[]) items).value());
}
return this;
}
private boolean hasSort() {
return sort != null && sort.isNotEmpty();
}

public AqlQueryBuilder and(Collection<AqlItem> items) {
return and(setToArray(items));
}
private boolean hasLimit() {
return limit != null;
}

public AqlQueryBuilder or(AqlItem... items) {
if (isNotEmpty(items)) {
root.putAll(AqlItem.or((Object[]) items).value());
}
return this;
}
private boolean hasOffset() {
return offset != null;
}

public AqlQueryBuilder or(Collection<AqlItem> items) {
return or(setToArray(items));
}
private boolean hasRoot() {
return root != null && root.isNotEmpty();
}

public AqlQueryBuilder include(String... elements) {
if (isNotEmpty(elements)) {
include = buildWithElements(elements);
}
return this;
}

public AqlQueryBuilder asc(String... by) {
if (isNotEmpty(by)) {
this.sort = AqlItem.asc(by);
}
return this;
}

public AqlQueryBuilder desc(String... by) {
if (isNotEmpty(by)) {
this.sort = AqlItem.desc(by);
}
return this;
}

public String build() {
try {
ObjectMapper mapper = new ObjectMapper();
return "items.find(" + getRootAsString(mapper) + ")" + getIncludeAsString() + getSortAsString(mapper);
} catch (JsonProcessingException e) {
throw new AqlBuilderException("Error serializing object to json: ", e);
}
}

private String getSortAsString(ObjectMapper mapper) throws JsonProcessingException {
return hasSort() ? ".sort(" + mapper.writeValueAsString(sort) + ")" : "";
}

private String getIncludeAsString() {
return hasInclude() ? include.toString() : "";
}

private String getRootAsString(ObjectMapper mapper) throws JsonProcessingException {
return hasRoot() ? mapper.writeValueAsString(root) : "";
}

private boolean hasSort() {
return sort != null && sort.isNotEmpty();
}

private boolean hasInclude() {
return include != null && include.isNotEmpty();
}

private boolean hasRoot() {
return root != null && root.isNotEmpty();
}

private AqlItem[] setToArray(Collection<AqlItem> items) {
return items.toArray(new AqlItem[0]);
}
private AqlItem[] setToArray(Collection<AqlItem> items) {
return items.toArray(new AqlItem[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ public void orTest() {
+ "]})"));
}


@Test
public void limitTest() {
String result = new AqlQueryBuilder()
.limit(123)
.build();

assertThat(result, notNullValue());
assertThat(result, is("items.find().limit(123)"));
}

@Test
public void offsetTest() {
String result = new AqlQueryBuilder()
.offset(123)
.build();

assertThat(result, notNullValue());
assertThat(result, is("items.find().offset(123)"));
}

@Test
public void addNestedFilters() {
final String result = new AqlQueryBuilder()
Expand Down Expand Up @@ -190,6 +211,8 @@ public void variousElements() {
.item(aqlItem("property", "value"))
.include("name", "repo")
.asc("name", "repo")
.offset(1)
.limit(2)
.build();

assertThat(result, notNullValue());
Expand All @@ -206,7 +229,9 @@ public void variousElements() {
+ "\"property\":\"value\""
+ "})"
+ ".include(\"name\",\"repo\")"
+ ".sort({\"$asc\":[\"name\",\"repo\"]})"));
+ ".sort({\"$asc\":[\"name\",\"repo\"]})"
+ ".offset(1)"
+ ".limit(2)"));
}

@Test
Expand Down

0 comments on commit 876f002

Please sign in to comment.