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            String memberName = member.getName();
158            if (memberName.equals("url")) {
159                this.url = value.asString();
160            } else if (memberName.equals("download_url")) {
161                this.downloadUrl = value.asString();
162            } else if (memberName.equals("vanity_url")) {
163                this.vanityUrl = value.asString();
164            } else if (memberName.equals("is_password_enabled")) {
165                this.isPasswordEnabled = value.asBoolean();
166            } else if (memberName.equals("unshared_at")) {
167                this.unsharedAt = BoxDateFormat.parse(value.asString());
168            } else if (memberName.equals("download_count")) {
169                this.downloadCount = Double.valueOf(value.toString()).longValue();
170            } else if (memberName.equals("preview_count")) {
171                this.previewCount = Double.valueOf(value.toString()).longValue();
172            } else if (memberName.equals("access")) {
173                String accessString = value.asString().toUpperCase();
174                this.access = Access.valueOf(accessString);
175            } else if (memberName.equals("permissions")) {
176                if (this.permissions == null) {
177                    this.setPermissions(new Permissions(value.asObject()));
178                } else {
179                    this.permissions.update(value.asObject());
180                }
181            }
182        } catch (ParseException e) {
183            assert false : "A ParseException indicates a bug in the SDK.";
184        }
185    }
186
187    /**
188     * Contains permissions fields that can be set on a shared link.
189     */
190    public static class Permissions extends BoxJSONObject {
191        private boolean canDownload;
192        private boolean canPreview;
193
194        /**
195         * Constructs a Permissions object with all permissions disabled.
196         */
197        public Permissions() { }
198
199        /**
200         * Constructs a Permissions object from a JSON string.
201         * @param  json the JSON encoded shared link permissions.
202         */
203        public Permissions(String json) {
204            super(json);
205        }
206
207        Permissions(JsonObject jsonObject) {
208            super(jsonObject);
209        }
210
211        /**
212         * Gets whether or not the shared link can be downloaded.
213         * @return true if the shared link can be downloaded; otherwise false.
214         */
215        public boolean getCanDownload() {
216            return this.canDownload;
217        }
218
219        /**
220         * Sets whether or not the shared link can be downloaded.
221         * @param enabled true if the shared link can be downloaded; otherwise false.
222         */
223        public void setCanDownload(boolean enabled) {
224            this.canDownload = enabled;
225            this.addPendingChange("can_download", enabled);
226        }
227
228        /**
229         * Gets whether or not the shared link can be previewed.
230         * @return true if the shared link can be previewed; otherwise false.
231         */
232        public boolean getCanPreview() {
233            return this.canPreview;
234        }
235
236        /**
237         * Sets whether or not the shared link can be previewed.
238         * @param enabled true if the shared link can be previewed; otherwise false.
239         */
240        public void setCanPreview(boolean enabled) {
241            this.canPreview = enabled;
242            this.addPendingChange("can_preview", enabled);
243        }
244
245        @Override
246        void parseJSONMember(JsonObject.Member member) {
247            JsonValue value = member.getValue();
248            String memberName = member.getName();
249            if (memberName.equals("can_download")) {
250                this.canDownload = value.asBoolean();
251            } else if (memberName.equals("can_preview")) {
252                this.canPreview = value.asBoolean();
253            }
254        }
255    }
256
257    /**
258     * Enumerates the possible access levels that can be set on a shared link.
259     */
260    public enum Access {
261        /**
262         * The default access level for the user or enterprise.
263         */
264        DEFAULT (null),
265
266        /**
267         * The link can be accessed by anyone.
268         */
269        OPEN ("open"),
270
271        /**
272         * The link can be accessed by other users within the company.
273         */
274        COMPANY ("company"),
275
276        /**
277         * The link can be accessed by other collaborators.
278         */
279        COLLABORATORS ("collaborators");
280
281        private final String jsonValue;
282
283        private Access(String jsonValue) {
284            this.jsonValue = jsonValue;
285        }
286
287        String toJSONValue() {
288            return this.jsonValue;
289        }
290    }
291}