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