001package com.box.sdk;
002
003import com.eclipsesource.json.JsonArray;
004import com.eclipsesource.json.JsonObject;
005import java.util.List;
006
007/**
008 * Used to Setup Box Search Parameters
009 *
010 * <p>Advanced Search support here allows a number of parameters be specified to take full advantage of
011 * box search capabilities. Query parameter is required in all cases except when Metadata templates
012 * searching is being used.</p>
013 */
014public class BoxSearchParameters {
015    private String query;
016    private List<String> fields;
017    private String scope;
018    private List<String> fileExtensions;
019    private DateRange createdRange;
020    private DateRange updatedRange;
021    private SizeRange sizeRange;
022    private List<String> ownerUserIds;
023    private List<String> ancestorFolderIds;
024    private List<String> contentTypes;
025    private String type;
026    private String trashContent;
027    private BoxMetadataFilter metadataFilter;
028    private String sort;
029    private String direction;
030    private List<String> deleterUserIds;
031    private DateRange deletedRange;
032
033    /**
034     * Creates a Box Search Parameters Objects without query set, specific for Metadata Only Searches.
035     */
036    public BoxSearchParameters() {
037    }
038
039    /**
040     * Creates a Box Search Parameters Objects with a query initiated.
041     *
042     * @param query parameter.
043     */
044    public BoxSearchParameters(String query) {
045        this.query = query;
046    }
047
048    /**
049     * Clears the Parameters before performing a new search.
050     *
051     * @return this.true;
052     */
053    public boolean clearParameters() {
054        this.query = null;
055        this.fields = null;
056        this.scope = null;
057        this.fileExtensions = null;
058        this.createdRange = null;
059        this.updatedRange = null;
060        this.sizeRange = null;
061        this.ownerUserIds = null;
062        this.ancestorFolderIds = null;
063        this.contentTypes = null;
064        this.type = null;
065        this.trashContent = null;
066        this.metadataFilter = null;
067        this.sort = null;
068        this.direction = null;
069        this.deleterUserIds = null;
070        this.deletedRange = null;
071        return true;
072    }
073
074    /**
075     * Get existing query String that is being used.
076     *
077     * @return this.query string.
078     */
079    public String getQuery() {
080        return this.query;
081    }
082
083    /**
084     * Set query string for that will be used to search.
085     *
086     * @param query is a String value.
087     */
088    public void setQuery(String query) {
089        this.query = query;
090    }
091
092    /**
093     * Get the existing fields that are used for the search criteria.
094     *
095     * @return this.List of fields.
096     */
097    public List<String> getFields() {
098        return this.fields;
099    }
100
101    /**
102     * Set the existing fields that are used for the search criteria.
103     *
104     * @param fields specify what fields to be returned.
105     */
106    public void setFields(List<String> fields) {
107        this.fields = fields;
108    }
109
110    /**
111     * Get the scope on which you want to search, ["enterprise","scope"].
112     *
113     * @return this.current scope that is set.
114     */
115    public String getScope() {
116        return this.scope;
117    }
118
119    /**
120     * Set the scope for how you want to search, ["enterprise","scope"].
121     *
122     * @param scope set scope you want to search.
123     */
124    public void setScope(String scope) {
125        this.scope = scope;
126    }
127
128    /**
129     * Get file extension filter (jpg,docx).
130     *
131     * @return this.list of extensions.
132     */
133    public List<String> getFileExtensions() {
134        return this.fileExtensions;
135    }
136
137    /**
138     * Set file extension by providing a list of strings [jpg,docx].
139     *
140     * @param fileExtensions applied as a filter for extensions.
141     */
142    public void setFileExtensions(List<String> fileExtensions) {
143        this.fileExtensions = fileExtensions;
144    }
145
146    /**
147     * Get the DateRange filter to specify the when a file was created.
148     *
149     * @return this.createdRange DateRange.
150     */
151    public DateRange getCreatedRange() {
152        return this.createdRange;
153    }
154
155    /**
156     * Set the from DateRange filter to specify when a file was created.
157     *
158     * @param createdRange a start and end date on which a file was created.
159     */
160    public void setCreatedRange(DateRange createdRange) {
161        this.createdRange = createdRange;
162    }
163
164    /**
165     * Get the DateRange filter to specify the when a file was updated.
166     *
167     * @return this.updatedRange DateRange.
168     */
169    public DateRange getUpdatedRange() {
170        return this.updatedRange;
171    }
172
173    /**
174     * Set the from DateRange filter to specify when a file was updated.
175     *
176     * @param updatedRange a start and end date on which a file was updated.
177     */
178    public void setUpdatedRange(DateRange updatedRange) {
179        this.updatedRange = updatedRange;
180    }
181
182    /**
183     * Return the size range that is being used as a filter.
184     *
185     * @return this.sizeRange.
186     */
187    public SizeRange getSizeRange() {
188        return this.sizeRange;
189    }
190
191    /**
192     * Set the file size range for lower and upper bounds Bytes.
193     *
194     * @param sizeRange a size filter.
195     */
196    public void setSizeRange(SizeRange sizeRange) {
197        this.sizeRange = sizeRange;
198    }
199
200    /**
201     * Return the list of owner id's that are being applied as a search filter on the results.
202     *
203     * @return ownerUserIds.
204     */
205    public List<String> getOwnerUserIds() {
206        return this.ownerUserIds;
207    }
208
209    /**
210     * Set the owner id's to be applied as a filter to restrict the results on specific file owners.
211     *
212     * @param ownerUserIds applied ownerId's.
213     */
214    public void setOwnerUserIds(List<String> ownerUserIds) {
215        this.ownerUserIds = ownerUserIds;
216    }
217
218    /**
219     * Return the list of folder ids that is currently being used as applied filter.
220     *
221     * @return ancestorFolderIds.
222     */
223    public List<String> getAncestorFolderIds() {
224        return this.ancestorFolderIds;
225    }
226
227    /**
228     * Set the list of folder id's to be applied as a filter on the search filters.
229     *
230     * @param ancestorFolderIds a list of folder ids that will contain results to specific folders.
231     */
232    public void setAncestorFolderIds(List<String> ancestorFolderIds) {
233        this.ancestorFolderIds = ancestorFolderIds;
234    }
235
236    /**
237     * Return content types that or being applied as a filter (name,description,tags,file_content).
238     *
239     * @return contentTypes.
240     */
241    public List<String> getContentTypes() {
242        return this.contentTypes;
243    }
244
245    /**
246     * Set list of content types that will be used as a filters.
247     *
248     * @param contentTypes a list of content types such as (name,description,tags,file_content).
249     */
250    public void setContentTypes(List<String> contentTypes) {
251        this.contentTypes = contentTypes;
252    }
253
254    /**
255     * Return the type you want to return in your search.
256     *
257     * @return String type.
258     */
259    public String getType() {
260        return this.type;
261    }
262
263    /**
264     * Set the type you want to search for can be file, folder, or web_link.
265     *
266     * @param type can be file, folder, or web_link.
267     */
268    public void setType(String type) {
269        this.type = type;
270    }
271
272    /**
273     * Return the specified trash search preference.
274     *
275     * @return String trashContent.
276     */
277    public String getTrashContent() {
278        return this.trashContent;
279    }
280
281    /**
282     * Set trash filter. Can be trashed_only or non_trashed_only, without this parameter default to non_trashed_only.
283     *
284     * @param trashContent Can be trashed_only or non_trashed_only.
285     */
286    public void setTrashContent(String trashContent) {
287        this.trashContent = trashContent;
288    }
289
290    /**
291     * Retrieve the existing BoxMetaDataFilter.
292     *
293     * @return this.BoxMetaDataFilter
294     */
295    public BoxMetadataFilter getMetadataFilter() {
296        return this.metadataFilter;
297    }
298
299    /**
300     * Set the current list of Metadata Filters.
301     *
302     * @param metadataFilter a list of the current metadata filters.
303     */
304    public void setMetadataFilter(BoxMetadataFilter metadataFilter) {
305        this.metadataFilter = metadataFilter;
306    }
307
308    /**
309     * Retrieve the sort field for Box Search.
310     *
311     * @return String identifier for Sort.
312     */
313    public String getSort() {
314        return this.sort;
315    }
316
317    /**
318     * Set the sort field for Box Search.
319     *
320     * @param sortBy the field to sort the Box Search results by.
321     */
322    public void setSort(String sortBy) {
323        this.sort = sortBy;
324    }
325
326    /**
327     * Retrieves the sort direction for Box Search results.
328     *
329     * @return The direction of the Box Search sort.
330     */
331    public String getDirection() {
332        return this.direction;
333    }
334
335    /**
336     * Set the direction of the sort for the Box Search results.
337     *
338     * @param direction can be DESC or ASC.
339     */
340    public void setDirection(String direction) {
341        this.direction = direction;
342    }
343
344    /**
345     * Limits the search results to items that were deleted by the given list of users, defined as a list of comma separated user IDs.
346     * The trash_content parameter needs to be set to trashed_only.
347     *
348     * @return deleterUserIds.
349     */
350    public List<String> getDeleterUserIds() {
351        return this.deleterUserIds;
352    }
353
354    /**
355     * Limits the search results to items that were deleted by the given list of users, defined as a list of comma separated user IDs.
356     * The trash_content parameter needs to be set to trashed_only.
357     *
358     * @param deleterUserIds a list of user ids.
359     */
360    public void setDeleterUserIds(List<String> deleterUserIds) {
361        this.deleterUserIds = deleterUserIds;
362    }
363
364    /**
365     * Limits the search results to items that were deleted within the given date range.
366     * The trash_content parameter needs to be set to trashed_only.
367     *
368     * @return deletedRange.
369     */
370    public DateRange getDeletedRange() {
371        return this.deletedRange;
372    }
373
374    /**
375     * Limits the search results to items that were deleted within the given date range.
376     * The trash_content parameter needs to be set to trashed_only.
377     *
378     * @param deletedRange a date range.
379     */
380    public void setDeletedRange(DateRange deletedRange) {
381        this.deletedRange = deletedRange;
382    }
383
384    /**
385     * Checks String to see if the parameter is null.
386     *
387     * @param paramValue Object that will be checked if null.
388     * @return this.true if the parameter that is being checked is not null
389     */
390    private boolean isNullOrEmpty(Object paramValue) {
391        boolean isNullOrEmpty = false;
392        if (paramValue == null) {
393            isNullOrEmpty = true;
394        }
395        if (paramValue instanceof String) {
396            if (((String) paramValue).trim().equalsIgnoreCase("")) {
397                isNullOrEmpty = true;
398            }
399        } else if (paramValue instanceof List) {
400            return ((List) paramValue).isEmpty();
401        }
402        return isNullOrEmpty;
403    }
404
405    /**
406     * Add BoxMetaDataFilter to the JsonArray boxMetadataFilterRequestArray.
407     *
408     * @return JsonArray that is formated Json request
409     */
410    private JsonArray formatBoxMetadataFilterRequest() {
411        JsonArray boxMetadataFilterRequestArray = new JsonArray();
412
413        JsonObject boxMetadataFilter = new JsonObject()
414            .add("templateKey", this.metadataFilter.getTemplateKey())
415            .add("scope", this.metadataFilter.getScope())
416            .add("filters", this.metadataFilter.getFiltersList());
417        boxMetadataFilterRequestArray.add(boxMetadataFilter);
418
419        return boxMetadataFilterRequestArray;
420    }
421
422    /**
423     * Concat a List into a CSV String.
424     *
425     * @param list list to concat
426     * @return csv string
427     */
428    private String listToCSV(List<String> list) {
429        String csvStr = "";
430        for (String item : list) {
431            csvStr += "," + item;
432        }
433
434        return csvStr.length() > 1 ? csvStr.substring(1) : csvStr;
435    }
436
437    /**
438     * Get the Query Paramaters to be used for search request.
439     *
440     * @return this.QueryStringBuilder.
441     */
442    public QueryStringBuilder getQueryParameters() {
443        QueryStringBuilder builder = new QueryStringBuilder();
444
445        if (this.isNullOrEmpty(this.query) && this.metadataFilter == null) {
446            throw new BoxAPIException(
447                "BoxSearchParameters requires either a search query or Metadata filter to be set."
448            );
449        }
450
451        //Set the query of the search
452        if (!this.isNullOrEmpty(this.query)) {
453            builder.appendParam("query", this.query);
454        }
455        //Set the scope of the search
456        if (!this.isNullOrEmpty(this.scope)) {
457            builder.appendParam("scope", this.scope);
458        }
459        //Acceptable Value: "jpg,png"
460        if (!this.isNullOrEmpty(this.fileExtensions)) {
461            builder.appendParam("file_extensions", this.listToCSV(this.fileExtensions));
462        }
463        //Created Date Range: From Date - To Date
464        if ((this.createdRange != null)) {
465            builder.appendParam("created_at_range", this.createdRange.buildRangeString());
466        }
467        //Updated Date Range: From Date - To Date
468        if ((this.updatedRange != null)) {
469            builder.appendParam("updated_at_range", this.updatedRange.buildRangeString());
470        }
471        //Filesize Range
472        if ((this.sizeRange != null)) {
473            builder.appendParam("size_range", this.sizeRange.buildRangeString());
474        }
475        //Owner Id's
476        if (!this.isNullOrEmpty(this.ownerUserIds)) {
477            builder.appendParam("owner_user_ids", this.listToCSV(this.ownerUserIds));
478        }
479        //Ancestor ID's
480        if (!this.isNullOrEmpty(this.ancestorFolderIds)) {
481            builder.appendParam("ancestor_folder_ids", this.listToCSV(this.ancestorFolderIds));
482        }
483        //Content Types: "name, description"
484        if (!this.isNullOrEmpty(this.contentTypes)) {
485            builder.appendParam("content_types", this.listToCSV(this.contentTypes));
486        }
487        //Type of File: "file,folder,web_link"
488        if (this.type != null) {
489            builder.appendParam("type", this.type);
490        }
491        //Trash Content
492        if (!this.isNullOrEmpty(this.trashContent)) {
493            builder.appendParam("trash_content", this.trashContent);
494        }
495        //Metadata filters
496        if (this.metadataFilter != null) {
497            builder.appendParam("mdfilters", this.formatBoxMetadataFilterRequest().toString());
498        }
499        //Fields
500        if (!this.isNullOrEmpty(this.fields)) {
501            builder.appendParam("fields", this.listToCSV(this.fields));
502        }
503        //Sort
504        if (!this.isNullOrEmpty(this.sort)) {
505            builder.appendParam("sort", this.sort);
506        }
507        //Direction
508        if (!this.isNullOrEmpty(this.direction)) {
509            builder.appendParam("direction", this.direction);
510        }
511        //Deleter User Ids
512        if (!this.isNullOrEmpty(this.deleterUserIds)) {
513            builder.appendParam("deleter_user_ids", this.listToCSV(this.deleterUserIds));
514        }
515        //Deleted Range
516        if ((this.deletedRange != null)) {
517            builder.appendParam("deleted_range", this.deletedRange.buildRangeString());
518        }
519
520        return builder;
521    }
522}