001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.util.Date;
006
007/** Represents a lock associated with a File on Box. */
008public class BoxLock extends BoxJSONObject {
009  private String type;
010  private Date expiresAt;
011  private Boolean isDownloadPrevented;
012  private BoxUser.Info createdBy;
013  private Date createdAt;
014  private String id;
015  private BoxAPIConnection api;
016
017  /**
018   * Constructs a base BoxLock object.
019   *
020   * @param type lock type, "lock" or "unlock".
021   * @param expiresAt lock expiration date.
022   */
023  public BoxLock(String type, Date expiresAt) {
024    super();
025    this.type = type;
026    this.expiresAt = expiresAt;
027    this.isDownloadPrevented = false;
028  }
029
030  /**
031   * Constructs a BoxLock object.
032   *
033   * @param type lock type, "lock" or "unlock".
034   * @param expiresAt lock expiration date.
035   * @param isDownloadPrevented if true, download is prevented while locked.
036   */
037  public BoxLock(String type, Date expiresAt, Boolean isDownloadPrevented) {
038    super();
039    this.type = type;
040    this.expiresAt = expiresAt;
041    this.isDownloadPrevented = isDownloadPrevented;
042  }
043
044  /**
045   * Constructs an BoxLock object using an already parsed JSON object.
046   *
047   * @param jsonObject the parsed JSON object.
048   * @param api API object.
049   */
050  BoxLock(JsonObject jsonObject, BoxAPIConnection api) {
051    super(jsonObject);
052    this.api = api;
053  }
054
055  /**
056   * Gets the lock type.
057   *
058   * @return the type of a lock.
059   */
060  public String getType() {
061    return this.type;
062  }
063
064  /**
065   * Gets a locks expiration date.
066   *
067   * @return the locks expiration date.
068   */
069  public Date getExpiresAt() {
070    return this.expiresAt;
071  }
072
073  /**
074   * Does the lock prevent downloads.
075   *
076   * @return true if lock prevents downloads.
077   */
078  public Boolean getIsDownloadPrevented() {
079    return this.isDownloadPrevented;
080  }
081
082  /**
083   * User who created the lock.
084   *
085   * @return Lock creator.
086   */
087  public BoxUser.Info getCreatedBy() {
088    return this.createdBy;
089  }
090
091  /** @return Lock's creation date. */
092  public Date getCreatedAt() {
093    return this.createdAt;
094  }
095
096  /** @return Lock's ID. */
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 (Exception e) {
130      throw new BoxDeserializationException(memberName, value.toString(), e);
131    }
132  }
133}