• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

box / box-java-sdk / #5641

12 Nov 2025 04:30PM UTC coverage: 13.3% (+0.008%) from 13.292%
#5641

Pull #1562

github

web-flow
Merge 298170338 into d9fee1425
Pull Request #1562: chore(boxsdkgen): Update `.codegen.json` with commit hash of `codegen` and `openapi` spec [skip ci]

8374 of 62964 relevant lines covered (13.3%)

0.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.41
/src/main/java/com/box/sdk/MetadataQuery.java
1
package com.box.sdk;
2

3
import static java.util.stream.Collectors.toList;
4

5
import com.eclipsesource.json.Json;
6
import com.eclipsesource.json.JsonArray;
7
import com.eclipsesource.json.JsonObject;
8
import com.eclipsesource.json.JsonValue;
9
import java.util.ArrayList;
10
import java.util.Arrays;
11
import java.util.List;
12

13
/** Represents Metadata Query. */
14
public class MetadataQuery {
15
  static final String FROM = "from";
16
  static final String LIMIT = "limit";
17
  static final String QUERY = "query";
18
  static final String ANCESTOR_FOLDER_ID = "ancestor_folder_id";
19
  static final String MARKER = "marker";
20
  static final String ORDER_BY = "order_by";
21
  static final String FIELDS = "fields";
22
  static final String QUERY_PARAMS = "query_params";
23
  private final String from;
24
  private final int limit;
25
  private String query;
26
  private JsonObject queryParameters = new JsonObject();
1✔
27
  private String ancestorFolderId = "0";
1✔
28
  private List<OrderBy> orderBy = new ArrayList<>();
1✔
29
  private String marker;
30
  private List<String> fields = new ArrayList<>();
1✔
31

32
  /**
33
   * Creates Metadata Query
34
   *
35
   * @param from The template used in the query. Must be in the form scope_enterpriseID.templateKey
36
   * @param limit Max results to return for a single request (0-100 inclusive)
37
   */
38
  public MetadataQuery(String from, int limit) {
1✔
39
    this.from = from;
1✔
40
    this.limit = limit;
1✔
41
  }
1✔
42

43
  /**
44
   * Creates Metadata Query
45
   *
46
   * @param from The template used in the query. Must be in the form scope.templateKey
47
   */
48
  public MetadataQuery(String from) {
49
    this(from, 100);
×
50
  }
×
51

52
  /**
53
   * The logical expression of the query
54
   *
55
   * @param query Query string
56
   * @return Returns current MetadataQuery object
57
   */
58
  public MetadataQuery setQuery(String query) {
59
    this.query = query;
1✔
60
    return this;
1✔
61
  }
62

63
  /**
64
   * Sets the folder_id to which to restrain the query. If not set query starts at root level.
65
   *
66
   * @param ancestorFolderId The folder id
67
   * @return Returns current MetadataQuery object
68
   */
69
  public MetadataQuery setAncestorFolderId(String ancestorFolderId) {
70
    this.ancestorFolderId = ancestorFolderId;
1✔
71
    return this;
1✔
72
  }
73

74
  /**
75
   * The marker to use for requesting the next page
76
   *
77
   * @param marker Marker string.
78
   * @return Returns current MetadataQuery object
79
   */
80
  public MetadataQuery setMarker(String marker) {
81
    this.marker = marker;
1✔
82
    return this;
1✔
83
  }
84

85
  /**
86
   * The field_key(s) to order on and the corresponding direction(s)
87
   *
88
   * @param fields Fields with sort order
89
   * @return Returns current MetadataQuery object
90
   */
91
  public MetadataQuery setOrderBy(OrderBy... fields) {
92
    this.orderBy = new ArrayList<>();
1✔
93
    this.orderBy.addAll(Arrays.asList(fields));
1✔
94
    return this;
1✔
95
  }
96

97
  MetadataQuery setOrderBy(JsonArray orderBy) {
98
    if (orderBy != null) {
1✔
99
      this.orderBy = orderBy.values().stream().map(OrderBy::fromJson).collect(toList());
1✔
100
    }
101
    return this;
1✔
102
  }
103

104
  /**
105
   * The fields to retrieve.
106
   *
107
   * @param fields Field names
108
   * @return Returns current MetadataQuery object
109
   */
110
  public MetadataQuery setFields(String... fields) {
111
    this.fields = new ArrayList<>();
1✔
112
    this.fields.addAll(Arrays.asList(fields));
1✔
113
    return this;
1✔
114
  }
115

116
  /**
117
   * Adds parameter to query
118
   *
119
   * @param name Parameter name
120
   * @param value Parameter value
121
   * @return Returns current MetadataQuery object
122
   */
123
  public MetadataQuery addParameter(String name, String value) {
124
    this.queryParameters.add(name, value);
1✔
125
    return this;
1✔
126
  }
127

128
  /**
129
   * Adds parameter to query
130
   *
131
   * @param name Parameter name
132
   * @param value Parameter value
133
   * @return Returns current MetadataQuery object
134
   */
135
  public MetadataQuery addParameter(String name, int value) {
136
    this.queryParameters.add(name, value);
1✔
137
    return this;
1✔
138
  }
139

140
  /**
141
   * Adds parameter to query
142
   *
143
   * @param name Parameter name
144
   * @param value Parameter value
145
   * @return Returns current MetadataQuery object
146
   */
147
  public MetadataQuery addParameter(String name, boolean value) {
148
    this.queryParameters.add(name, value);
1✔
149
    return this;
1✔
150
  }
151

152
  /**
153
   * Adds parameter to query
154
   *
155
   * @param name Parameter name
156
   * @param value Parameter value
157
   * @return Returns current MetadataQuery object
158
   */
159
  public MetadataQuery addParameter(String name, float value) {
160
    this.queryParameters.add(name, value);
1✔
161
    return this;
1✔
162
  }
163

164
  /**
165
   * Adds parameter to query
166
   *
167
   * @param name Parameter name
168
   * @param value Parameter value
169
   * @return Returns current MetadataQuery object
170
   */
171
  public MetadataQuery addParameter(String name, long value) {
172
    this.queryParameters.add(name, value);
1✔
173
    return this;
1✔
174
  }
175

176
  /**
177
   * Adds parameter to query
178
   *
179
   * @param name Parameter name
180
   * @param value Parameter value
181
   * @return Returns current MetadataQuery object
182
   */
183
  public MetadataQuery addParameter(String name, double value) {
184
    this.queryParameters.add(name, value);
1✔
185
    return this;
1✔
186
  }
187

188
  /**
189
   * Adds parameter to query
190
   *
191
   * @param name Parameter name
192
   * @param value Parameter value
193
   * @return Returns current MetadataQuery object
194
   */
195
  public MetadataQuery addParameter(String name, JsonValue value) {
196
    this.queryParameters.add(name, Json.parse(value.toString()));
1✔
197
    return this;
1✔
198
  }
199

200
  MetadataQuery setQueryParams(JsonObject queryParameters) {
201
    this.queryParameters = new JsonObject(queryParameters);
1✔
202
    return this;
1✔
203
  }
204

205
  JsonObject toJsonObject() {
206
    JsonObject jsonObject = new JsonObject().add(FROM, from).add(LIMIT, limit);
1✔
207
    if (query != null) {
1✔
208
      jsonObject.add(QUERY, query);
1✔
209
    }
210
    if (ancestorFolderId != null) {
1✔
211
      jsonObject.add(ANCESTOR_FOLDER_ID, ancestorFolderId);
1✔
212
    }
213
    if (marker != null) {
1✔
214
      jsonObject.add(MARKER, marker);
1✔
215
    }
216
    if (!orderBy.isEmpty()) {
1✔
217
      JsonArray orderByJson = new JsonArray();
1✔
218
      orderBy.stream().map(OrderBy::toJsonObject).forEach(orderByJson::add);
1✔
219
      jsonObject.add(ORDER_BY, orderByJson);
1✔
220
    }
221
    if (!fields.isEmpty()) {
1✔
222
      JsonArray fieldsJson = new JsonArray();
1✔
223
      fields.forEach(fieldsJson::add);
1✔
224
      jsonObject.add(FIELDS, fieldsJson);
1✔
225
    }
226
    if (queryParameters.iterator().hasNext()) {
1✔
227
      jsonObject.add(QUERY_PARAMS, new JsonObject(queryParameters));
1✔
228
    }
229
    return jsonObject;
1✔
230
  }
231

232
  int getLimit() {
233
    return limit;
1✔
234
  }
235

236
  String getMarker() {
237
    return marker;
1✔
238
  }
239

240
  public static final class OrderBy {
241

242
    static final String FIELD_KEY = "field_key";
243
    static final String DIRECTION = "direction";
244
    static final String DIRECTION_ASCENDING = "asc";
245
    static final String DIRECTION_DESCENDING = "desc";
246
    private final String fieldName;
247
    private final String direction;
248

249
    private OrderBy(String fieldName, String direction) {
1✔
250
      this.fieldName = fieldName;
1✔
251
      this.direction = direction;
1✔
252
    }
1✔
253

254
    JsonObject toJsonObject() {
255
      return new JsonObject().add(FIELD_KEY, fieldName).add(DIRECTION, direction);
1✔
256
    }
257

258
    /**
259
     * Creates OrderBy for ascending sort with a specified field.
260
     *
261
     * @param fieldName Name of a field
262
     * @return OrderBy instance
263
     */
264
    public static OrderBy ascending(String fieldName) {
265
      return new OrderBy(fieldName, DIRECTION_ASCENDING);
1✔
266
    }
267

268
    /**
269
     * Creates OrderBy for descending sort with a specified field.
270
     *
271
     * @param fieldName Name of a field
272
     * @return OrderBy instance
273
     */
274
    public static OrderBy descending(String fieldName) {
275
      return new OrderBy(fieldName, DIRECTION_DESCENDING);
1✔
276
    }
277

278
    static OrderBy fromJson(JsonValue jsonValue) {
279
      if (jsonValue.isObject()) {
1✔
280
        JsonObject object = jsonValue.asObject();
1✔
281
        String fieldName = object.get(FIELD_KEY).asString();
1✔
282
        String direction = object.get(DIRECTION).asString().toLowerCase(java.util.Locale.ROOT);
1✔
283
        if (!DIRECTION_ASCENDING.equals(direction) && !DIRECTION_DESCENDING.equals(direction)) {
1✔
284
          throw new RuntimeException(
×
285
              String.format(
×
286
                  "Unsupported sort direction [%s] for field [%s]", direction, fieldName));
287
        }
288
        return object.getString(DIRECTION, "").equals(DIRECTION_ASCENDING)
1✔
289
            ? ascending(fieldName)
×
290
            : descending(fieldName);
1✔
291
      }
292
      throw new RuntimeException("Unsupported json " + jsonValue);
×
293
    }
294
  }
295
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc