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