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