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