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 lock associated with a File on Box.
011 */
012public class BoxLock extends BoxJSONObject {
013    private String type;
014    private Date expiresAt;
015    private Boolean isDownloadPrevented;
016    private BoxUser.Info createdBy;
017    private Date createdAt;
018    private String id;
019    private BoxAPIConnection api;
020
021    /**
022     * Constructs a base BoxLock object.
023     * @param type lock type, "lock" or "unlock".
024     * @param expiresAt lock expiration date.
025     */
026    public BoxLock(String type, Date expiresAt) {
027        super();
028        this.type = type;
029        this.expiresAt = expiresAt;
030        this.isDownloadPrevented = false;
031    }
032
033    /**
034     * Constructs a BoxLock object.
035     * @param type lock type, "lock" or "unlock".
036     * @param expiresAt lock expiration date.
037     * @param isDownloadPrevented if true, download is prevented while locked.
038     */
039    public BoxLock(String type, Date expiresAt, Boolean isDownloadPrevented) {
040        super();
041        this.type = type;
042        this.expiresAt = expiresAt;
043        this.isDownloadPrevented = isDownloadPrevented;
044    }
045
046    /**
047     * Constructs an BoxLock object using an already parsed JSON object.
048     * @param  jsonObject the parsed JSON object.
049     */
050    BoxLock(JsonObject jsonObject, BoxAPIConnection api) {
051        super(jsonObject);
052        this.api = api;
053    }
054
055    /**
056     * Gets the lock type.
057     * @return the type of a lock.
058     */
059    public String getType() {
060        return this.type;
061    }
062
063    /**
064     * Gets a locks expiration date.
065     * @return the locks expiration date.
066     */
067    public Date getExpiresAt() {
068        return this.expiresAt;
069    }
070
071    /**
072     * Does the lock prevent downloads.
073     * @return true if lock prevents downloads.
074     */
075    public Boolean getIsDownloadPrevented() {
076        return this.isDownloadPrevented;
077    }
078
079    /**
080     * User who created the lock.
081     * @return Lock creator.
082     */
083    public BoxUser.Info getCreatedBy() {
084        return this.createdBy;
085    }
086
087    /**
088     * @return Lock's creation date.
089     */
090    public Date getCreatedAt() {
091        return this.createdAt;
092    }
093
094    /**
095     * @return Lock's ID.
096     */
097    public String getId() {
098        return this.id;
099    }
100
101    @Override
102    protected void parseJSONMember(JsonObject.Member member) {
103        super.parseJSONMember(member);
104
105        String memberName = member.getName();
106        JsonValue value = member.getValue();
107
108        try {
109            if (memberName.equals("type")) {
110                this.type = value.asString();
111            } else if (memberName.equals("expires_at")) {
112                this.expiresAt = BoxDateFormat.parse(value.asString());
113            } else if (memberName.equals("is_download_prevented")) {
114                this.isDownloadPrevented = value.asBoolean();
115            } else if (memberName.equals("created_by")) {
116                JsonObject userJSON = value.asObject();
117                if (this.createdBy == null) {
118                    String userID = userJSON.get("id").asString();
119                    BoxUser user = new BoxUser(this.api, userID);
120                    this.createdBy = user.new Info(userJSON);
121                } else {
122                    this.createdBy.update(userJSON);
123                }
124            } else if (memberName.equals("created_at")) {
125                this.createdAt = BoxDateFormat.parse(value.asString());
126            } else if (memberName.equals("id")) {
127                this.id = value.toString();
128            }
129        } catch (ParseException e) {
130            assert false : "A ParseException indicates a bug in the SDK.";
131        }
132    }
133}