001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.util.Date;
006
007/**
008 * Represents a lock associated with a File on Box.
009 */
010public class BoxLock extends BoxJSONObject {
011    private String type;
012    private Date expiresAt;
013    private Boolean isDownloadPrevented;
014    private BoxUser.Info createdBy;
015    private Date createdAt;
016    private String id;
017    private BoxAPIConnection api;
018
019    /**
020     * Constructs a base BoxLock object.
021     *
022     * @param type      lock type, "lock" or "unlock".
023     * @param expiresAt lock expiration date.
024     */
025    public BoxLock(String type, Date expiresAt) {
026        super();
027        this.type = type;
028        this.expiresAt = expiresAt;
029        this.isDownloadPrevented = false;
030    }
031
032    /**
033     * Constructs a BoxLock object.
034     *
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     *
049     * @param jsonObject the parsed JSON object.
050     * @param api API object.
051     */
052    BoxLock(JsonObject jsonObject, BoxAPIConnection api) {
053        super(jsonObject);
054        this.api = api;
055    }
056
057    /**
058     * Gets the lock type.
059     *
060     * @return the type of a lock.
061     */
062    public String getType() {
063        return this.type;
064    }
065
066    /**
067     * Gets a locks expiration date.
068     *
069     * @return the locks expiration date.
070     */
071    public Date getExpiresAt() {
072        return this.expiresAt;
073    }
074
075    /**
076     * Does the lock prevent downloads.
077     *
078     * @return true if lock prevents downloads.
079     */
080    public Boolean getIsDownloadPrevented() {
081        return this.isDownloadPrevented;
082    }
083
084    /**
085     * User who created the lock.
086     *
087     * @return Lock creator.
088     */
089    public BoxUser.Info getCreatedBy() {
090        return this.createdBy;
091    }
092
093    /**
094     * @return Lock's creation date.
095     */
096    public Date getCreatedAt() {
097        return this.createdAt;
098    }
099
100    /**
101     * @return Lock's ID.
102     */
103    public String getId() {
104        return this.id;
105    }
106
107    @Override
108    protected void parseJSONMember(JsonObject.Member member) {
109        super.parseJSONMember(member);
110
111        String memberName = member.getName();
112        JsonValue value = member.getValue();
113
114        try {
115            if (memberName.equals("type")) {
116                this.type = value.asString();
117            } else if (memberName.equals("expires_at")) {
118                this.expiresAt = BoxDateFormat.parse(value.asString());
119            } else if (memberName.equals("is_download_prevented")) {
120                this.isDownloadPrevented = value.asBoolean();
121            } else if (memberName.equals("created_by")) {
122                JsonObject userJSON = value.asObject();
123                if (this.createdBy == null) {
124                    String userID = userJSON.get("id").asString();
125                    BoxUser user = new BoxUser(this.api, userID);
126                    this.createdBy = user.new Info(userJSON);
127                } else {
128                    this.createdBy.update(userJSON);
129                }
130            } else if (memberName.equals("created_at")) {
131                this.createdAt = BoxDateFormat.parse(value.asString());
132            } else if (memberName.equals("id")) {
133                this.id = value.toString();
134            }
135        } catch (Exception e) {
136            throw new BoxDeserializationException(memberName, value.toString(), e);
137        }
138    }
139}