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