001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.net.URL;
006
007/**
008 * Represents a BoxStoragePolicy.
009 */
010@BoxResourceType("storage_policy")
011public class BoxStoragePolicy extends BoxResource {
012
013    /**
014     * Storage Policies URL Template.
015     */
016    public static final URLTemplate STORAGE_POLICY_URL_TEMPLATE = new URLTemplate("storage_policies");
017
018    /**
019     * Storage Policies URL Template.
020     */
021    public static final URLTemplate STORAGE_POLICY_WITH_ID_URL_TEMPLATE = new URLTemplate("storage_policies/%s");
022
023    /**
024     * The default limit of entries per response.
025     */
026    private static final int DEFAULT_LIMIT = 100;
027
028    /**
029     * Constructs a BoxStoragePolicy with a given ID.
030     *
031     * @param api the API connection to be used by the BoxStoragePolicy.
032     * @param id  the ID of the BoxStoragePolicy.
033     */
034    public BoxStoragePolicy(BoxAPIConnection api, String id) {
035        super(api, id);
036    }
037
038    /**
039     * Returns all BoxStoragePolicy with specified fields.
040     *
041     * @param api    the API connection to be used by the resource.
042     * @param fields the fields to retrieve.
043     * @return an iterable with all the storage policies met search conditions.
044     */
045    public static Iterable<BoxStoragePolicy.Info> getAll(final BoxAPIConnection api, String... fields) {
046
047        return getAll(api, DEFAULT_LIMIT, fields);
048    }
049
050    /**
051     * Returns all BoxStoragePolicy with specified fields.
052     *
053     * @param api    the API connection to be used by the resource.
054     * @param limit  the limit of items per single response. The default is 100.
055     * @param fields the fields to retrieve.
056     * @return an iterable with all the storage policies met search conditions.
057     */
058    public static Iterable<BoxStoragePolicy.Info> getAll(final BoxAPIConnection api, int limit, String... fields) {
059
060        QueryStringBuilder builder = new QueryStringBuilder();
061        if (fields.length > 0) {
062            builder.appendParam("fields", fields);
063        }
064
065        URL url = STORAGE_POLICY_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString());
066        return new BoxResourceIterable<BoxStoragePolicy.Info>(api, url, limit) {
067
068            @Override
069            protected BoxStoragePolicy.Info factory(JsonObject jsonObject) {
070                BoxStoragePolicy storagePolicy = new BoxStoragePolicy(api, jsonObject.get("id").asString());
071
072                return storagePolicy.new Info(jsonObject);
073            }
074        };
075    }
076
077    /**
078     * Gets information for a Box Storage Policy with optional fields.
079     *
080     * @param fields the fields to retrieve.
081     * @return info about this item containing only the specified fields, including storage policy.
082     */
083    public BoxStoragePolicy.Info getInfo(String... fields) {
084        QueryStringBuilder builder = new QueryStringBuilder();
085        if (fields.length > 0) {
086            builder.appendParam("fields", fields);
087        }
088        URL url = STORAGE_POLICY_WITH_ID_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString(),
089            this.getID());
090
091        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
092        BoxJSONResponse response = (BoxJSONResponse) request.send();
093        return new Info(response.getJSON());
094    }
095
096    /**
097     * Checks if there is already a Storage Policy Assignment and creates one if one does not exist.
098     *
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}