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