001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004
005/**
006 * <p>BoxMetadataFilter is used to help organize the request for when making metadata filter request
007 * in conjuction with search. The translation will look something like this:
008 * [{"templateKey":"marketingCollateral", "scope":"enterprise", "filters":{"documentType": "datasheet"}}]</p>
009 */
010public class BoxMetadataFilter {
011    private String templateKey;
012    private String scope = "enterprise";
013    private JsonObject filtersList;
014
015    /**
016     * Constructor for BoxMetadataFilter that initizlizes the JSON Object.
017     */
018    public BoxMetadataFilter() {
019        this.filtersList = new JsonObject();
020    }
021
022    /**
023     * Returns the template key that currently set.
024     *
025     * @return this.String template key.
026     */
027    public String getTemplateKey() {
028        return this.templateKey;
029    }
030
031    /**
032     * Set the current template key for the search filter.
033     *
034     * @param templateKey must be a metadata template key.
035     */
036    public void setTemplateKey(String templateKey) {
037        this.templateKey = templateKey;
038    }
039
040    /**
041     * return this.a list of the current filters that are being set.
042     *
043     * @return this.JsonObject filterList.
044     */
045    public JsonObject getFiltersList() {
046        return this.filtersList;
047    }
048
049    /**
050     * Set a filter to the filterList, example: key=documentType, value=special.
051     *
052     * @param key   the key that the filter should be looking for.
053     * @param value the specific value that corresponds to the key.
054     */
055    public void addFilter(String key, String value) {
056        this.filtersList.add(key, value);
057    }
058
059    /**
060     * Set a NumberRanger filter to the filter numbers, example: key=documentNumber, lt : 20, gt : 5.
061     *
062     * @param key       the key that the filter should be looking for.
063     * @param sizeRange the specific value that corresponds to the key.
064     */
065    public void addNumberRangeFilter(String key, SizeRange sizeRange) {
066        JsonObject opObj = new JsonObject();
067
068        if (sizeRange.getLowerBoundBytes() != 0) {
069            opObj.add("gt", sizeRange.getLowerBoundBytes());
070        }
071        if (sizeRange.getUpperBoundBytes() != 0) {
072            opObj.add("lt", sizeRange.getUpperBoundBytes());
073        }
074
075        this.filtersList.add(key, opObj);
076    }
077
078    /**
079     * Set a filter to the filterList, example: key=documentNumber, gt : "", lt : "".
080     *
081     * @param key       the key that the filter should be looking for.
082     * @param dateRange the date range that is start and end dates
083     */
084    public void addDateRangeFilter(String key, DateRange dateRange) {
085
086        JsonObject opObj = new JsonObject();
087
088        if (dateRange.getFromDate() != null) {
089            String dateGtString = BoxDateFormat.format(dateRange.getFromDate());
090            //workaround replacing + and - 000 at the end with 'Z'
091            dateGtString = dateGtString.replaceAll("(\\+|-)(?!-\\|?!\\+)\\d+$", "Z");
092            opObj.add("gt", dateGtString);
093        }
094        if (dateRange.getToDate() != null) {
095            String dateLtString = BoxDateFormat.format(dateRange.getToDate());
096            //workaround replacing + and - 000 at the end with 'Z'
097            dateLtString = dateLtString.replaceAll("(\\+|-)(?!-\\|?!\\+)\\d+$", "Z");
098            opObj.add("lt", dateLtString);
099        }
100
101        this.filtersList.add(key, opObj);
102    }
103
104    /**
105     * return this.the current scope being used.
106     *
107     * @return this.String scope.
108     */
109    public String getScope() {
110        return this.scope;
111    }
112
113    /**
114     * Set the scope for the key, currently only "enterprise" and "global" are allowed.
115     *
116     * @param scope the scope on which to find the template.
117     */
118    public void setScope(String scope) {
119        this.scope = scope;
120    }
121}