001package com.box.sdk;
002
003import java.util.Date;
004
005import com.eclipsesource.json.JsonObject;
006import com.eclipsesource.json.JsonValue;
007
008/**
009 * Represents a link to a file or folder on Box.
010 */
011public class BoxSharedLink extends BoxJSONObject {
012    private String url;
013    private String downloadUrl;
014    private String vanityUrl;
015    private boolean isPasswordEnabled;
016    private String password;
017    private Date unsharedAt;
018    private long downloadCount;
019    private long previewCount;
020    private Access access;
021    private Access effectiveAccess;
022    private Permissions permissions;
023
024    /**
025     * Constructs a BoxSharedLink with default settings.
026     */
027    public BoxSharedLink() { }
028
029    /**
030     * Constructs a BoxSharedLink from a JSON string.
031     * @param  json the JSON encoded shared link.
032     */
033    public BoxSharedLink(String json) {
034        super(json);
035    }
036
037    BoxSharedLink(JsonObject jsonObject) {
038        super(jsonObject);
039    }
040
041    BoxSharedLink(BoxSharedLink.Access access, Date unshareDate, BoxSharedLink.Permissions permissions) {
042        this.setAccess(access);
043        this.setPermissions(permissions);
044
045        if (unshareDate != null) {
046            this.setUnsharedDate(unshareDate);
047        }
048    }
049    BoxSharedLink(BoxSharedLink.Access access, Date unshareDate, BoxSharedLink.Permissions permissions,
050                  String password) {
051        this.setAccess(access);
052        this.setPermissions(permissions);
053        this.setPassword(password);
054
055        if (unshareDate != null) {
056            this.setUnsharedDate(unshareDate);
057        }
058    }
059
060    /**
061     * Get the URL of this shared link.
062     * @return the URL of this shared link.
063     */
064    public String getURL() {
065        return this.url;
066    }
067
068    /**
069     * Gets the direct download URL of this shared link.
070     * @return the direct download URL of this shared link.
071     */
072    public String getDownloadURL() {
073        return this.downloadUrl;
074    }
075
076    /**
077     * Gets the vanity URL of this shared link.
078     * @return the vanity URL of this shared link.
079     */
080    public String getVanityURL() {
081        return this.vanityUrl;
082    }
083
084    /**
085     * Gets whether or not a password is enabled on this shared link.
086     * @return true if there's a password enabled on this shared link; otherwise false.
087     */
088    public boolean getIsPasswordEnabled() {
089        return this.isPasswordEnabled;
090    }
091
092    /**
093     * Gets the time that this shared link will be deactivated.
094     * @return the time that this shared link will be deactivated.
095     */
096    public Date getUnsharedDate() {
097        return this.unsharedAt;
098    }
099
100    /**
101     * Sets the time that this shared link will be deactivated.
102     * @param unsharedDate the time that this shared link will be deactivated.
103     */
104    public void setUnsharedDate(Date unsharedDate) {
105        this.unsharedAt = unsharedDate;
106        this.addPendingChange("unshared_at", unsharedDate.toString());
107    }
108
109    /**
110     * Gets the number of times that this shared link has been downloaded.
111     * @return the number of times that this link has been downloaded.
112     */
113    public long getDownloadCount() {
114        return this.downloadCount;
115    }
116
117    /**
118     * Gets the number of times that this shared link has been previewed.
119     * @return the number of times that this link has been previewed.
120     */
121    public long getPreviewCount() {
122        return this.previewCount;
123    }
124
125    /**
126     * Gets the access level of this shared link.
127     * @return the access level of this shared link.
128     */
129    public Access getAccess() {
130        return this.access;
131    }
132
133    /**
134     * Sets the access level of this shared link.
135     * @param access the new access level of this shared link.
136     */
137    public void setAccess(Access access) {
138        this.access = access;
139        this.addPendingChange("access", access.toJSONValue());
140    }
141
142    /**
143     * Sets the password of this shared link.
144     * @param password the password of this shared link.
145     */
146    public void setPassword(String password) {
147        this.password = password;
148        this.addPendingChange("password", password.toString());
149    }
150
151    /**
152     * Gets the effective access level of this shared link.
153     * @return the effective access level of this shared link.
154     *
155     * Note there is no setEffectiveAccess metho becaused this
156     * cannot be changed via the API
157     */
158    public Access getEffectiveAccess() {
159        return this.effectiveAccess;
160    }
161
162    /**
163     * Gets the permissions associated with this shared link.
164     * @return the permissions associated with this shared link.
165     */
166    public Permissions getPermissions() {
167        return this.permissions;
168    }
169
170    /**
171     * Sets the permissions associated with this shared link.
172     * @param permissions the new permissions for this shared link.
173     */
174    public void setPermissions(Permissions permissions) {
175        if (this.permissions == permissions) {
176            return;
177        }
178
179        this.removeChildObject("permissions");
180        this.permissions = permissions;
181        this.addChildObject("permissions", permissions);
182    }
183
184    private Access parseAccessValue(JsonValue value) {
185        String accessString = value.asString().toUpperCase();
186        return Access.valueOf(accessString);
187    }
188
189    @Override
190    void parseJSONMember(JsonObject.Member member) {
191        JsonValue value = member.getValue();
192        String memberName = member.getName();
193
194        try {
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 (Exception e) {
221            throw new BoxDeserializationException(memberName, value.toString(), e);
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}