Skip to content

Commit

Permalink
Update execute metadata query to match API response (#890)
Browse files Browse the repository at this point in the history
* Update execute metadata query to match API response

* Update CHANGELOG.md

* Fix linting error
  • Loading branch information
swfree committed May 13, 2021
1 parent e5535cd commit b68ebcc
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 386 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Next Release

__Breaking Changes:__
- Update execute metadata query to match API response ([#890](https://github.com/box/box-java-sdk/pull/890))

__New Features and Enhancements:__

__Bug Fixes:__
Expand Down
32 changes: 7 additions & 25 deletions doc/metadata_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,30 +152,6 @@ MetadataTemplate.deleteMetadataTemplate(api, "enterprise", "templateName");
Execute Metadata Query
--------------------------

There are two types of methods for executing a metadata query, methods without the fields parameter and with it. The methods without the fields parameters return data that is a `BoxMetadataQueryItem`.
The methods with the fields parameters return a `BoxItem.Info` object. Examples of these two types are shown below.

The [`executeMetadataQuery(BoxAPIConnection api, String from, String query, JsonObject queryParameters, String ancestorFolderId, String indexName, JsonArray orderBy)`][execute-metadata-query] method queries files and folders based on their metadata.

<!-- sample post_metadata_queries_execute_read -->
```java
String from = "enterprise_341532.test";
String query = "testfield = :arg";
String ancestorFolderId = "0";
JsonObject queryParameters = new JsonObject().add("arg", "test");
JsonArray orderBy = new JsonArray();
JsonObject primaryOrderBy = new JsonObject().add("field_key", "primarySortKey").add("direction", "asc");
JsonObject secondaryOrderBy = new JsonObject().add("field_key", "secondarySortKey").add("direction",
"asc");
orderBy.add(primaryOrderBy).add(secondaryOrderBy);

BoxResourceIterable<BoxMetadataQueryItem> results = MetadataTemplate.executeMetadataQuery(api, from, query, queryParameters, ancestorFolderId, null, orderBy);
for (BoxMetadataQueryItem r: results) {
String customFieldValue = r.getMetadata().get("enterprise_341532").get(0).get("/customField");
System.out.println(customFieldValue);
}
```

The [`executeMetadataQuery(BoxAPIConnection api, String from, String query, JsonObject queryParameters, String ancestorFolderId, String indexName, JsonArray orderBy, String ... fields)`][execute-metadata-query-with-fields] method queries files and folders based on their metadata and allows for fields to be passed in.

```java
Expand All @@ -189,11 +165,17 @@ JsonObject secondaryOrderBy = new JsonObject().add("field_key", "secondarySortKe
"asc");
orderBy.add(primaryOrderBy).add(secondaryOrderBy);

BoxResourceIterable<BoxMetadataQueryItem> results = MetadataTemplate.executeMetadataQuery(api, from, query, queryParameters, ancestorFolderId, null, orderBy, "id", "name", "metadata.enterprise_341532.test.photographer");
BoxResourceIterable<BoxItem.Info> results = MetadataTemplate.executeMetadataQuery(api, from, query, queryParameters, ancestorFolderId, null, orderBy, "id", "name", "metadata.enterprise_341532.test");
for (BoxItem.Info itemInfo : results) {
if (itemInfo instanceof BoxFile.Info) {
BoxFile.Info fileInfo = (BoxFile.Info) itemInfo;
// Do something with the file.

// Example with metadata
Metadata fileMetadata = fileInfo.getMetadata("test", "enterprise_341532");
String customFieldValue = fileMetadata.getString("/customField");
System.out.println(customFieldValue);

} else if (itemInfo instanceof BoxFolder.Info) {
BoxFolder.Info folderInfo = (BoxFolder.Info) itemInfo;
// Do something with the folder.
Expand Down
93 changes: 0 additions & 93 deletions src/main/java/com/box/sdk/BoxMetadataQueryItem.java

This file was deleted.

52 changes: 11 additions & 41 deletions src/main/java/com/box/sdk/MetadataTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,11 @@ public static void deleteMetadataTemplate(BoxAPIConnection api, String scope, St
* @param api The API connection to be used
* @param from The template used in the query. Must be in the form scope.templateKey
* @param ancestorFolderId The folder_id to which to restrain the query
* @return An iterable of BoxMetadataQueryItem search results
* @return An iterable of BoxItem.Info search results
*/
public static BoxResourceIterable<BoxMetadataQueryItem> executeMetadataQuery(final BoxAPIConnection api,
public static BoxResourceIterable<BoxItem.Info> executeMetadataQuery(final BoxAPIConnection api,
String from, String ancestorFolderId) {
return executeMetadataQuery(api, from, null, null, ancestorFolderId, null, null, 100, null);
return executeMetadataQuery(api, from, null, null, ancestorFolderId, null, null, 100, null, null);
}

/**
Expand All @@ -387,12 +387,12 @@ public static BoxResourceIterable<BoxItem.Info> executeMetadataQuery(final BoxAP
* @param query The logical expression of the query
* @param queryParameters Required if query present. The arguments for the query
* @param ancestorFolderId The folder_id to which to restrain the query
* @return An iterable of BoxMetadataQueryItem search results
* @return An iterable of BoxItem.Info search results
*/
public static BoxResourceIterable<BoxMetadataQueryItem> executeMetadataQuery(final BoxAPIConnection api,
public static BoxResourceIterable<BoxItem.Info> executeMetadataQuery(final BoxAPIConnection api,
String from, String query, JsonObject queryParameters,
String ancestorFolderId) {
return executeMetadataQuery(api, from, query, queryParameters, ancestorFolderId, null, null, 100, null);
return executeMetadataQuery(api, from, query, queryParameters, ancestorFolderId, null, null, 100, null, null);
}

/**
Expand Down Expand Up @@ -422,13 +422,14 @@ public static BoxResourceIterable<BoxItem.Info> executeMetadataQuery(final BoxAP
* @param ancestorFolderId The folder_id to which to restrain the query
* @param indexName The name of the Index to use
* @param orderBy The field_key(s) to order on and the corresponding direction(s)
* @return An iterable of BoxMetadataQueryItem search results
* @return An iterable of BoxItem.Info search results
*/
public static BoxResourceIterable<BoxMetadataQueryItem> executeMetadataQuery(final BoxAPIConnection api,
public static BoxResourceIterable<BoxItem.Info> executeMetadataQuery(final BoxAPIConnection api,
String from, String query, JsonObject queryParameters,
String ancestorFolderId, String indexName,
JsonArray orderBy) {
return executeMetadataQuery(api, from, query, queryParameters, ancestorFolderId, indexName, orderBy, 100, null);
return executeMetadataQuery(api, from, query, queryParameters, ancestorFolderId, indexName, orderBy, 100,
null, null);
}

/**
Expand All @@ -452,37 +453,6 @@ public static BoxResourceIterable<BoxItem.Info> executeMetadataQuery(final BoxAP
null, fields);
}

/**
* Executes a metadata query.
*
* @param api The API connection to be used
* @param from The template used in the query. Must be in the form scope.templateKey
* @param query The logical expression of the query
* @param queryParameters Required if query present. The arguments for the query
* @param ancestorFolderId The folder_id to which to restrain the query
* @param indexName The name of the Index to use
* @param orderBy The field_key(s) to order on and the corresponding direction(s)
* @param limit Max results to return for a single request (0-100 inclusive)
* @param marker The marker to use for requesting the next page
* @return An iterable of BoxMetadataQueryItem search results
*/
public static BoxResourceIterable<BoxMetadataQueryItem> executeMetadataQuery(final BoxAPIConnection api,
String from, String query, JsonObject queryParameters,
String ancestorFolderId, String indexName,
JsonArray orderBy, int limit, String marker) {
JsonObject jsonObject = createMetadataQueryBody(from, query, queryParameters, ancestorFolderId,
indexName, orderBy, limit, marker);

URL url = METADATA_QUERIES_URL_TEMPLATE.build(api.getBaseURL());
return new BoxResourceIterable<BoxMetadataQueryItem>(api, url, limit, jsonObject, marker) {

@Override
protected BoxMetadataQueryItem factory(JsonObject jsonObject) {
return new BoxMetadataQueryItem(jsonObject, api);
}
};
}

/**
* Executes a metadata query.
*
Expand Down Expand Up @@ -570,7 +540,7 @@ private static JsonObject createMetadataQueryBody(String from, String query, Jso
if (orderBy != null) {
jsonObject.add("order_by", orderBy);
}
if (fields.length > 0) {
if (fields != null && fields.length > 0) {
JsonArray fieldsBody = new JsonArray();
for (String field : fields) {
fieldsBody.add(field);
Expand Down
Loading

0 comments on commit b68ebcc

Please sign in to comment.