Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/java/com/box/sdk/BoxFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,34 @@ public Iterator<BoxItem.Info> iterator() {
};
}

/**
* Returns an iterable containing the items in this folder sorted by name and direction.
* @param sort the field to sort by, can be set as `name`, `id`, and `date`.
* @param direction the direction to display the item results.
* @param offset the index of the first child item to retrieve.
* @param limit the maximum number of children to retrieve after the offset.
* @param fields the fields to retrieve.
* @return an iterable containing the items in this folder.
*/
public Iterable<BoxItem.Info> getChildren(String sort, SortDirection direction, final long offset, final long limit,
final String... fields) {
QueryStringBuilder builder = new QueryStringBuilder()
.appendParam("sort", sort)
.appendParam("direction", direction.toString());

if (fields.length > 0) {
builder.appendParam("fields", fields).toString();
}
final String query = builder.toString();
return new Iterable<BoxItem.Info>() {
@Override
public Iterator<BoxItem.Info> iterator() {
URL url = GET_ITEMS_URL.buildWithQuery(getAPI().getBaseURL(), query, getID());
return new BoxItemIterator(getAPI(), url, limit, offset);
}
};
}

/**
* Retrieves a specific range of child items in this folder.
*
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/box/sdk/BoxItemIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ public boolean shouldInclude(JsonObject jsonObject) {
});
}

BoxItemIterator(BoxAPIConnection api, URL url, long limit, long offset) {
this.api = api;

this.jsonIterator = new JSONIterator(api, url, limit, offset);
this.jsonIterator.setFilter(new Filter<JsonObject>() {
@Override
public boolean shouldInclude(JsonObject jsonObject) {
String type = jsonObject.get("type").asString();
return (type.equals("file") || type.equals("folder") || type.equals("web_link"));
}
});
}

public boolean hasNext() {
return this.jsonIterator.hasNext();
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/box/sdk/JSONIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public JSONIterator(BoxAPIConnection api, URL url, long limit) {
this.limit = limit;
}

public JSONIterator(BoxAPIConnection api, URL url, long limit, long offset) {
this.api = api;
this.url = url;
this.limit = limit;
this.offset = offset;
}

public boolean hasNext() {
if (this.nextJsonObject == null) {
this.nextJsonObject = this.loadNextJsonObject();
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/box/sdk/BoxFolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,36 @@ public void testGetFolderItemsWithSort() throws IOException {
Assert.assertEquals("Test 2", boxItem2.getName());
}

@Test
@Category(UnitTest.class)
public void testGetFolderItemsWithOffsetAndLimit() throws IOException {
String result = "";
final String folderID = "12345";
final String sortField = "name";
final String folderItemsURL = "/folders/" + folderID + "/items/";

result = TestConfig.getFixture("BoxFolder/GetFolderItemsWithSort200");

WIRE_MOCK_CLASS_RULE.stubFor(WireMock.get(WireMock.urlPathEqualTo(folderItemsURL))
.withQueryParam("sort", WireMock.equalTo("name"))
.withQueryParam("direction", WireMock.equalTo("ASC"))
.withQueryParam("fields", WireMock.equalTo("name"))
.withQueryParam("limit", WireMock.equalTo("500"))
.withQueryParam("offset", WireMock.equalTo("10"))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(result)
.withStatus(200)));

BoxFolder folder = new BoxFolder(this.api, "12345");
Iterator<BoxItem.Info> itemIterator = folder.getChildren("name",
BoxFolder.SortDirection.ASC, 10, 500, "name").iterator();
BoxItem.Info boxItem1 = itemIterator.next();
Assert.assertEquals("Test", boxItem1.getName());
BoxItem.Info boxItem2 = itemIterator.next();
Assert.assertEquals("Test 2", boxItem2.getName());
}

@Test
@Category(UnitTest.class)
public void testSetMetadataReturnsCorrectly() throws IOException {
Expand Down