001package com.box.sdk;
002
003import com.box.sdk.http.HttpMethod;
004import com.eclipsesource.json.JsonObject;
005import com.eclipsesource.json.JsonValue;
006import java.net.URL;
007
008/**
009 * Represents a BoxStoragePolicyAssignment.
010 */
011@BoxResourceType("storage_policy_assignment")
012public class BoxStoragePolicyAssignment extends BoxResource {
013
014    /**
015     * Storage Policies Assignment URL Template.
016     */
017    public static final URLTemplate STORAGE_POLICY_ASSIGNMENT_URL_TEMPLATE = new
018        URLTemplate("storage_policy_assignments");
019
020    /**
021     * Storage Policy Assignment URL Template.
022     */
023    public static final URLTemplate STORAGE_POLICY_ASSIGNMENT_WITH_ID_URL_TEMPLATE = new
024        URLTemplate("storage_policy_assignments/%s");
025
026    /**
027     * Constructs a BoxStoragePolicyAssignment for a BoxStoragePolicy with a givenID.
028     *
029     * @param api the API connection to be used by the file.
030     * @param id  the ID of the file.
031     */
032    public BoxStoragePolicyAssignment(BoxAPIConnection api, String id) {
033        super(api, id);
034    }
035
036    /**
037     * Create a BoxStoragePolicyAssignment for a BoxStoragePolicy.
038     *
039     * @param api      the API connection to be used by the resource.
040     * @param policyID the policy ID of the BoxStoragePolicy.
041     * @param userID   the user ID of the to assign the BoxStoragePolicy to.
042     * @return the information about the BoxStoragePolicyAssignment created.
043     */
044    public static BoxStoragePolicyAssignment.Info create(BoxAPIConnection api, String policyID, String userID) {
045        URL url = STORAGE_POLICY_ASSIGNMENT_URL_TEMPLATE.build(api.getBaseURL());
046        BoxJSONRequest request = new BoxJSONRequest(api, url, HttpMethod.POST);
047        JsonObject requestJSON = new JsonObject()
048            .add("storage_policy", new JsonObject()
049                .add("type", "storage_policy")
050                .add("id", policyID))
051            .add("assigned_to", new JsonObject()
052                .add("type", "user")
053                .add("id", userID));
054
055        request.setBody(requestJSON.toString());
056        BoxJSONResponse response = (BoxJSONResponse) request.send();
057        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
058
059        BoxStoragePolicyAssignment storagePolicyAssignment = new BoxStoragePolicyAssignment(api,
060            responseJSON.get("id").asString());
061
062        return storagePolicyAssignment.new Info(responseJSON);
063    }
064
065    /**
066     * Returns a BoxStoragePolicyAssignment information.
067     *
068     * @param api             the API connection to be used by the resource.
069     * @param resolvedForType the assigned entity type for the storage policy.
070     * @param resolvedForID   the assigned entity id for the storage policy.
071     * @return information about this {@link BoxStoragePolicyAssignment}.
072     */
073    public static BoxStoragePolicyAssignment.Info getAssignmentForTarget(final BoxAPIConnection api,
074                                                                         String resolvedForType, String resolvedForID) {
075        QueryStringBuilder builder = new QueryStringBuilder();
076        builder.appendParam("resolved_for_type", resolvedForType)
077            .appendParam("resolved_for_id", resolvedForID);
078        URL url = STORAGE_POLICY_ASSIGNMENT_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), builder.toString());
079        BoxAPIRequest request = new BoxAPIRequest(api, url, HttpMethod.GET);
080        BoxJSONResponse response = (BoxJSONResponse) request.send();
081
082        BoxStoragePolicyAssignment storagePolicyAssignment = new BoxStoragePolicyAssignment(api,
083            response.getJsonObject().get("entries").asArray().get(0).asObject().get("id").asString());
084        BoxStoragePolicyAssignment.Info info = storagePolicyAssignment.new
085            Info(response.getJsonObject().get("entries").asArray().get(0).asObject());
086
087        return info;
088    }
089
090    /**
091     * Checks if there is already a Storage Policy Assignment and creates one if one does not exist.
092     *
093     * @param api             the API connection to be used by the resource.
094     * @param storagePolicyID the ID of the Storage Policy you want to assign to user.
095     * @param userID          the ID of the user you want to assign the Storage Policy to.
096     * @return information about this {@link BoxStoragePolicyAssignment}.
097     */
098    public static BoxStoragePolicyAssignment.Info assign(BoxAPIConnection api, String storagePolicyID, String userID) {
099        BoxStoragePolicyAssignment.Info assignmentInfo = null;
100        assignmentInfo = getAssignmentForTarget(api, "user", userID);
101
102        if (assignmentInfo.getStoragePolicyID().equals(storagePolicyID)) {
103            return assignmentInfo;
104        }
105
106        if (assignmentInfo.getAssignedToType().equals("enterprise")) {
107            return create(api, storagePolicyID, userID);
108        }
109
110        assignmentInfo.setStoragePolicyID(storagePolicyID);
111        BoxStoragePolicyAssignment assignment = new BoxStoragePolicyAssignment(api, assignmentInfo.getID());
112        assignment.updateInfo(assignmentInfo);
113        return assignmentInfo;
114    }
115
116    /**
117     * Updates the information about the BoxStoragePolicyAssignment with any info fields that have been
118     * modified locally.
119     *
120     * @param info the updated info.
121     */
122    public void updateInfo(BoxStoragePolicyAssignment.Info info) {
123        URL url = STORAGE_POLICY_ASSIGNMENT_WITH_ID_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID());
124        BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
125        request.setBody(info.getPendingChanges());
126
127        BoxJSONResponse response = (BoxJSONResponse) request.send();
128        JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
129        info.update(responseJSON);
130    }
131
132    /**
133     * @return information about this {@link BoxStoragePolicyAssignment}.
134     */
135    public BoxStoragePolicyAssignment.Info getInfo() {
136        URL url = STORAGE_POLICY_ASSIGNMENT_WITH_ID_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID());
137        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, HttpMethod.GET);
138        BoxJSONResponse response = (BoxJSONResponse) request.send();
139
140        return new Info(JsonObject.readFrom(response.getJSON()));
141    }
142
143    /**
144     * Deletes this BoxStoragePolicyAssignment.
145     */
146    public void delete() {
147        URL url = STORAGE_POLICY_ASSIGNMENT_WITH_ID_URL_TEMPLATE.buildAlpha(this.getAPI().getBaseURL(), this.getID());
148        BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, HttpMethod.DELETE);
149
150        request.send();
151    }
152
153    /**
154     * Contains information about a BoxStoragePolicyAssignment.
155     */
156    public class Info extends BoxResource.Info {
157
158        /**
159         * @see #getStoragePolicyID()
160         */
161        private String storagePolicyID;
162
163        /**
164         * @see #getStoragePolicyType()
165         */
166        private String storagePolicyType;
167
168        /**
169         * @see #getAssignedToID()
170         */
171        private String assignedToID;
172
173        /**
174         * @see #getAssignedToType()
175         */
176        private String assignedToType;
177
178        /**
179         * Constructs an empty Info object.
180         */
181        public Info() {
182            super();
183        }
184
185        /**
186         * Constructs an Info object by parsing information from a JSON string.
187         *
188         * @param json the JSON string to parse.
189         */
190        public Info(String json) {
191            super(json);
192        }
193
194        /**
195         * Constructs an Info object using an already parsed JSON object.
196         *
197         * @param jsonObject the parsed JSON object.
198         */
199        Info(JsonObject jsonObject) {
200            super(jsonObject);
201        }
202
203        @Override
204        public BoxResource getResource() {
205            return BoxStoragePolicyAssignment.this;
206        }
207
208        /**
209         * @return the entity type that this is assigned to.
210         */
211        public String getAssignedToType() {
212            return this.assignedToType;
213        }
214
215        /**
216         * @return the entity id that this is assigned to.
217         */
218        public String getAssignedToID() {
219            return this.assignedToID;
220        }
221
222        /**
223         * @return storage policy id that is assigned to.
224         */
225        public String getStoragePolicyID() {
226            return this.storagePolicyID;
227        }
228
229        /**
230         * Sets the storage policy of the storage policy assignment.
231         *
232         * @param storagePolicyID the Id of the storage policy you wish to assign.
233         */
234        public void setStoragePolicyID(String storagePolicyID) {
235            this.storagePolicyID = storagePolicyID;
236            JsonObject storagePolicyObject = new JsonObject();
237            storagePolicyObject.add("type", "storage_policy");
238            storagePolicyObject.add("id", storagePolicyID);
239
240            this.addPendingChange("storage_policy", storagePolicyObject);
241        }
242
243        /**
244         * @return storage policy type that is assigned to.
245         */
246        public String getStoragePolicyType() {
247            return this.storagePolicyType;
248        }
249
250        /**
251         * {@inheritDoc}
252         */
253        @Override
254        void parseJSONMember(JsonObject.Member member) {
255            super.parseJSONMember(member);
256            String memberName = member.getName();
257            JsonValue value = member.getValue();
258            try {
259                if (memberName.equals("assigned_to")) {
260                    JsonObject assignmentJSON = value.asObject();
261                    this.assignedToType = assignmentJSON.get("type").asString();
262                    this.assignedToID = assignmentJSON.get("id").asString();
263                } else if (memberName.equals("storage_policy")) {
264                    JsonObject storagePolicyJSON = value.asObject();
265                    this.storagePolicyID = storagePolicyJSON.get("id").asString();
266                    this.storagePolicyType = storagePolicyJSON.get("type").asString();
267                }
268            } catch (Exception e) {
269                throw new BoxDeserializationException(memberName, value.toString(), e);
270            }
271        }
272    }
273}