001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.net.URL;
006
007/** Represents a BoxStoragePolicy. */
008@BoxResourceType("storage_policy")
009public class BoxStoragePolicy extends BoxResource {
010
011  /** Storage Policies URL Template. */
012  public static final URLTemplate STORAGE_POLICY_URL_TEMPLATE = new URLTemplate("storage_policies");
013
014  /** Storage Policies URL Template. */
015  public static final URLTemplate STORAGE_POLICY_WITH_ID_URL_TEMPLATE =
016      new URLTemplate("storage_policies/%s");
017
018  /** The default limit of entries per response. */
019  private static final int DEFAULT_LIMIT = 100;
020
021  /**
022   * Constructs a BoxStoragePolicy with a given ID.
023   *
024   * @param api the API connection to be used by the BoxStoragePolicy.
025   * @param id the ID of the BoxStoragePolicy.
026   */
027  public BoxStoragePolicy(BoxAPIConnection api, String id) {
028    super(api, id);
029  }
030
031  /**
032   * Returns all BoxStoragePolicy with specified fields.
033   *
034   * @param api the API connection to be used by the resource.
035   * @param fields the fields to retrieve.
036   * @return an iterable with all the storage policies met search conditions.
037   */
038  public static Iterable<BoxStoragePolicy.Info> getAll(
039      final BoxAPIConnection api, String... fields) {
040
041    return getAll(api, DEFAULT_LIMIT, fields);
042  }
043
044  /**
045   * Returns all BoxStoragePolicy with specified fields.
046   *
047   * @param api the API connection to be used by the resource.
048   * @param limit the limit of items per single response. The default is 100.
049   * @param fields the fields to retrieve.
050   * @return an iterable with all the storage policies met search conditions.
051   */
052  public static Iterable<BoxStoragePolicy.Info> getAll(
053      final BoxAPIConnection api, int limit, String... fields) {
054
055    QueryStringBuilder builder = new QueryStringBuilder();
056    if (fields.length > 0) {
057      builder.appendParam("fields", fields);
058    }
059
060    URL url = STORAGE_POLICY_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString());
061    return new BoxResourceIterable<BoxStoragePolicy.Info>(api, url, limit) {
062
063      @Override
064      protected BoxStoragePolicy.Info factory(JsonObject jsonObject) {
065        BoxStoragePolicy storagePolicy = new BoxStoragePolicy(api, jsonObject.get("id").asString());
066
067        return storagePolicy.new Info(jsonObject);
068      }
069    };
070  }
071
072  /**
073   * Gets information for a Box Storage Policy with optional fields.
074   *
075   * @param fields the fields to retrieve.
076   * @return info about this item containing only the specified fields, including storage policy.
077   */
078  public BoxStoragePolicy.Info getInfo(String... fields) {
079    QueryStringBuilder builder = new QueryStringBuilder();
080    if (fields.length > 0) {
081      builder.appendParam("fields", fields);
082    }
083    URL url =
084        STORAGE_POLICY_WITH_ID_URL_TEMPLATE.buildWithQuery(
085            this.getAPI().getBaseURL(), builder.toString(), this.getID());
086
087    BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "GET");
088    try (BoxJSONResponse response = request.send()) {
089      return new Info(response.getJSON());
090    }
091  }
092
093  /**
094   * Checks if there is already a Storage Policy Assignment and creates one if one does not exist.
095   *
096   * @param userID the ID of the user you want to assign the Storage Policy to.
097   * @return information about this {@link BoxStoragePolicyAssignment}.
098   */
099  public BoxStoragePolicyAssignment.Info assign(String userID) {
100    return BoxStoragePolicyAssignment.assign(this.getAPI(), this.getID(), userID);
101  }
102
103  /** Contains information about the BoxStoragePolicy. */
104  public class Info extends BoxResource.Info {
105
106    /** @see #getStoragePolicyName() */
107    private String storagePolicyName;
108
109    /** Constructs an empty Info object. */
110    public Info() {
111      super();
112    }
113
114    /**
115     * Constructs an Info object by parsing information from a JSON string.
116     *
117     * @param json the JSON string to parse.
118     */
119    public Info(String json) {
120      super(json);
121    }
122
123    /**
124     * Constructs an Info object using an already parsed JSON object.
125     *
126     * @param jsonObject the parsed JSON object.
127     */
128    Info(JsonObject jsonObject) {
129      super(jsonObject);
130    }
131
132    /** {@inheritDoc} */
133    @Override
134    public BoxStoragePolicy getResource() {
135      return BoxStoragePolicy.this;
136    }
137
138    /** @return the name of the storage policy. */
139    public String getStoragePolicyName() {
140      return this.storagePolicyName;
141    }
142
143    @Override
144    void parseJSONMember(JsonObject.Member member) {
145      super.parseJSONMember(member);
146      String memberName = member.getName();
147      JsonValue value = member.getValue();
148      try {
149        if (memberName.equals("name")) {
150          this.storagePolicyName = value.asString();
151        }
152      } catch (Exception e) {
153        throw new BoxDeserializationException(memberName, value.toString(), e);
154      }
155    }
156  }
157}