001package com.box.sdk;
002
003import java.text.ParseException;
004import java.util.Date;
005
006import com.eclipsesource.json.JsonObject;
007import com.eclipsesource.json.JsonValue;
008
009/**
010 * Represents a link to a file or folder on Box.
011 */
012public class BoxSharedLink extends BoxJSONObject {
013    private String url;
014    private String downloadUrl;
015    private String vanityUrl;
016    private boolean isPasswordEnabled;
017    private String password;
018    private Date unsharedAt;
019    private long downloadCount;
020    private long previewCount;
021    private Access access;
022    private Access effectiveAccess;
023    private Permissions permissions;
024
025    /**
026     * Constructs a BoxSharedLink with default settings.
027     */
028    public BoxSharedLink() { }
029
030    /**
031     * Constructs a BoxSharedLink from a JSON string.
032     * @param  json the JSON encoded shared link.
033     */
034    public BoxSharedLink(String json) {
035        super(json);
036    }
037
038    BoxSharedLink(JsonObject jsonObject) {
039        super(jsonObject);
040    }
041
042    BoxSharedLink(BoxSharedLink.Access access, Date unshareDate, BoxSharedLink.Permissions permissions) {
043        this.setAccess(access);
044        this.setPermissions(permissions);
045
046        if (unshareDate != null) {
047            this.setUnsharedDate(unshareDate);
048        }
049    }
050    BoxSharedLink(BoxSharedLink.Access access, Date unshareDate, BoxSharedLink.Permissions permissions,
051                  String password) {
052        this.setAccess(access);
053        this.setPermissions(permissions);
054        this.setPassword(password);
055
056        if (unshareDate != null) {
057            this.setUnsharedDate(unshareDate);
058        }
059    }
060
061    /**
062     * Get the URL of this shared link.
063     * @return the URL of this shared link.
064     */
065    public String getURL() {
066        return this.url;
067    }
068
069    /**
070     * Gets the direct download URL of this shared link.
071     * @return the direct download URL of this shared link.
072     */
073    public String getDownloadURL() {
074        return this.downloadUrl;
075    }
076
077    /**
078     * Gets the vanity URL of this shared link.
079     * @return the vanity URL of this shared link.
080     */
081    public String getVanityURL() {
082        return this.vanityUrl;
083    }
084
085    /**
086     * Gets whether or not a password is enabled on this shared link.
087     * @return true if there's a password enabled on this shared link; otherwise false.
088     */
089    public boolean getIsPasswordEnabled() {
090        return this.isPasswordEnabled;
091    }
092
093    /**
094     * Gets the time that this shared link will be deactivated.
095     * @return the time that this shared link will be deactivated.
096     */
097    public Date getUnsharedDate() {
098        return this.unsharedAt;
099    }
100
101    /**
102     * Sets the time that this shared link will be deactivated.
103     * @param unsharedDate the time that this shared link will be deactivated.
104     */
105    public void setUnsharedDate(Date unsharedDate) {
106        this.unsharedAt = unsharedDate;
107        this.addPendingChange("unshared_at", unsharedDate.toString());
108    }
109
110    /**
111     * Gets the number of times that this shared link has been downloaded.
112     * @return the number of times that this link has been downloaded.
113     */
114    public long getDownloadCount() {
115        return this.downloadCount;
116    }
117
118    /**
119     * Gets the number of times that this shared link has been previewed.
120     * @return the number of times that this link has been previewed.
121     */
122    public long getPreviewCount() {
123        return this.previewCount;
124    }
125
126    /**
127     * Gets the access level of this shared link.
128     * @return the access level of this shared link.
129     */
130    public Access getAccess() {
131        return this.access;
132    }
133
134    /**
135     * Sets the access level of this shared link.
136     * @param access the new access level of this shared link.
137     */
138    public void setAccess(Access access) {
139        this.access = access;
140        this.addPendingChange("access", access.toJSONValue());
141    }
142
143    /**
144     * Sets the password of this shared link.
145     * @param password the password of this shared link.
146     */
147    public void setPassword(String password) {
148        this.password = password;
149        this.addPendingChange("password", password.toString());
150    }
151
152    /**
153     * Gets the effective access level of this shared link.
154     * @return the effective access level of this shared link.
155     *
156     * Note there is no setEffectiveAccess metho becaused this
157     * cannot be changed via the API
158     */
159    public Access getEffectiveAccess() {
160        return this.effectiveAccess;
161    }
162
163    /**
164     * Gets the permissions associated with this shared link.
165     * @return the permissions associated with this shared link.
166     */
167    public Permissions getPermissions() {
168        return this.permissions;
169    }
170
171    /**
172     * Sets the permissions associated with this shared link.
173     * @param permissions the new permissions for this shared link.
174     */
175    public void setPermissions(Permissions permissions) {
176        if (this.permissions == permissions) {
177            return;
178        }
179
180        this.removeChildObject("permissions");
181        this.permissions = permissions;
182        this.addChildObject("permissions", permissions);
183    }
184
185    private Access parseAccessValue(JsonValue value) {
186        String accessString = value.asString().toUpperCase();
187        return Access.valueOf(accessString);
188    }
189
190    @Override
191    void parseJSONMember(JsonObject.Member member) {
192        JsonValue value = member.getValue();
193        try {
194            String memberName = member.getName();
195            if (memberName.equals("url")) {
196                this.url = value.asString();
197            } else if (memberName.equals("download_url")) {
198                this.downloadUrl = value.asString();
199            } else if (memberName.equals("vanity_url")) {
200                this.vanityUrl = value.asString();
201            } else if (memberName.equals("is_password_enabled")) {
202                this.isPasswordEnabled = value.asBoolean();
203            } else if (memberName.equals("unshared_at")) {
204                this.unsharedAt = BoxDateFormat.parse(value.asString());
205            } else if (memberName.equals("download_count")) {
206                this.downloadCount = Double.valueOf(value.toString()).longValue();
207            } else if (memberName.equals("preview_count")) {
208                this.previewCount = Double.valueOf(value.toString()).longValue();
209            } else if (memberName.equals("access")) {
210                this.access = this.parseAccessValue(value);
211            } else if (memberName.equals("effective_access")) {
212                this.effectiveAccess = this.parseAccessValue(value);
213            } else if (memberName.equals("permissions")) {
214                if (this.permissions == null) {
215                    this.setPermissions(new Permissions(value.asObject()));
216                } else {
217                    this.permissions.update(value.asObject());
218                }
219            }
220        } catch (ParseException e) {
221            assert false : "A ParseException indicates a bug in the SDK.";
222        }
223    }
224
225    /**
226     * Contains permissions fields that can be set on a shared link.
227     */
228    public static class Permissions extends BoxJSONObject {
229        private boolean canDownload;
230        private boolean canPreview;
231
232        /**
233         * Constructs a Permissions object with all permissions disabled.
234         */
235        public Permissions() { }
236
237        /**
238         * Constructs a Permissions object from a JSON string.
239         * @param  json the JSON encoded shared link permissions.
240         */
241        public Permissions(String json) {
242            super(json);
243        }
244
245        Permissions(JsonObject jsonObject) {
246            super(jsonObject);
247        }
248
249        /**
250         * Gets whether or not the shared link can be downloaded.
251         * @return true if the shared link can be downloaded; otherwise false.
252         */
253        public boolean getCanDownload() {
254            return this.canDownload;
255        }
256
257        /**
258         * Sets whether or not the shared link can be downloaded.
259         * @param enabled true if the shared link can be downloaded; otherwise false.
260         */
261        public void setCanDownload(boolean enabled) {
262            this.canDownload = enabled;
263            this.addPendingChange("can_download", enabled);
264        }
265
266        /**
267         * Gets whether or not the shared link can be previewed.
268         * @return true if the shared link can be previewed; otherwise false.
269         */
270        public boolean getCanPreview() {
271            return this.canPreview;
272        }
273
274        /**
275         * Sets whether or not the shared link can be previewed.
276         * @param enabled true if the shared link can be previewed; otherwise false.
277         */
278        public void setCanPreview(boolean enabled) {
279            this.canPreview = enabled;
280            this.addPendingChange("can_preview", enabled);
281        }
282
283        @Override
284        void parseJSONMember(JsonObject.Member member) {
285            JsonValue value = member.getValue();
286            String memberName = member.getName();
287            if (memberName.equals("can_download")) {
288                this.canDownload = value.asBoolean();
289            } else if (memberName.equals("can_preview")) {
290                this.canPreview = value.asBoolean();
291            }
292        }
293    }
294
295    /**
296     * Enumerates the possible access levels that can be set on a shared link.
297     */
298    public enum Access {
299        /**
300         * The default access level for the user or enterprise.
301         */
302        DEFAULT (null),
303
304        /**
305         * The link can be accessed by anyone.
306         */
307        OPEN ("open"),
308
309        /**
310         * The link can be accessed by other users within the company.
311         */
312        COMPANY ("company"),
313
314        /**
315         * The link can be accessed by other collaborators.
316         */
317        COLLABORATORS ("collaborators");
318
319        private final String jsonValue;
320
321        private Access(String jsonValue) {
322            this.jsonValue = jsonValue;
323        }
324
325        String toJSONValue() {
326            return this.jsonValue;
327        }
328    }
329}