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        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "GET");
092        try (BoxJSONResponse response = request.send()) {
093            return new Info(response.getJSON());
094        }
095    }
096
097    /**
098     * Checks if there is already a Storage Policy Assignment and creates one if one does not exist.
099     *
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 (Exception e) {
167                throw new BoxDeserializationException(memberName, value.toString(), e);
168            }
169        }
170    }
171}