001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005import java.net.URL;
006import java.util.Date;
007import java.util.HashMap;
008import java.util.Map;
009
010/**
011 * Represents a lock on a folder.
012 *
013 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link
014 * BoxAPIException} (unchecked meaning that the compiler won't force you to handle it) if an error
015 * occurs. If you wish to implement custom error handling for errors related to the Box REST API,
016 * you should capture this exception explicitly.
017 */
018@BoxResourceType("folder_lock")
019public class BoxFolderLock extends BoxResource {
020  /** Delete Folder Locks URL Template. */
021  public static final URLTemplate DELETE_FOLDER_LOCK_URL_TEMPLATE =
022      new URLTemplate("folder_locks/%s");
023
024  /**
025   * Constructs a BoxFolderLock with a given ID.
026   *
027   * @param api the API connection to be used by the folder lock.
028   * @param id the ID of the folder lock.
029   */
030  public BoxFolderLock(BoxAPIConnection api, String id) {
031    super(api, id);
032  }
033
034  /** Delete the lock on this folder. */
035  public void delete() {
036    URL url = DELETE_FOLDER_LOCK_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID());
037    BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE");
038    request.send().close();
039  }
040
041  /** Contains information about a BoxFolderLock. */
042  public class Info extends BoxResource.Info {
043    private BoxFolder.Info folder;
044    private BoxUser.Info createdBy;
045    private Date createdAt;
046    private String lockType;
047    private Map<String, Boolean> lockedOperations;
048
049    /** Constructs an empty Info object. */
050    public Info() {
051      super();
052    }
053
054    /**
055     * Constructs an Info object by parsing information from a JSON string.
056     *
057     * @param json the JSON string to parse.
058     */
059    public Info(String json) {
060      super(json);
061    }
062
063    /**
064     * Constructs an Info object using an already parsed JSON object.
065     *
066     * @param jsonObject the parsed JSON object.
067     */
068    Info(JsonObject jsonObject) {
069      super(jsonObject);
070    }
071
072    @Override
073    public BoxResource getResource() {
074      return BoxFolderLock.this;
075    }
076
077    /**
078     * Gets the folder that the lock applies to.
079     *
080     * @return The folder that the lock applies to.
081     */
082    public BoxFolder.Info getFolder() {
083      return this.folder;
084    }
085
086    /**
087     * Gets the user or group that created the lock.
088     *
089     * @return the user or group that created the lock.
090     */
091    public BoxUser.Info getCreatedBy() {
092      return this.createdBy;
093    }
094
095    /**
096     * Gets the date the folder lock object was created.
097     *
098     * @return the date the folder lock object was created.
099     */
100    public Date getCreatedAt() {
101      return this.createdAt;
102    }
103
104    /**
105     * Gets the lock type, always freeze.
106     *
107     * @return the lock type, always freeze.
108     */
109    public String getLockType() {
110      return this.lockType;
111    }
112
113    /**
114     * Gets the operations that have been locked.
115     *
116     * @return the operations that have been locked.
117     */
118    public Map<String, Boolean> getLockedOperations() {
119      return this.lockedOperations;
120    }
121
122    /** {@inheritDoc} */
123    @Override
124    protected void parseJSONMember(JsonObject.Member member) {
125      super.parseJSONMember(member);
126
127      String memberName = member.getName();
128      JsonValue value = member.getValue();
129
130      try {
131        if (memberName.equals("folder")) {
132          JsonObject folderJSON = value.asObject();
133          String folderID = folderJSON.get("id").asString();
134          BoxFolder folder = new BoxFolder(getAPI(), folderID);
135          this.folder = folder.new Info(folderJSON);
136        } else if (memberName.equals("created_by")) {
137          JsonObject userJSON = value.asObject();
138          if (this.createdBy == null) {
139            String userID = userJSON.get("id").asString();
140            BoxUser user = new BoxUser(getAPI(), userID);
141            this.createdBy = user.new Info(userJSON);
142          } else {
143            this.createdBy.update(userJSON);
144          }
145        } else if (memberName.equals("created_at")) {
146          this.createdAt = BoxDateFormat.parse(value.asString());
147
148        } else if (memberName.equals("lock_type")) {
149          this.lockType = value.asString();
150
151        } else if (memberName.equals("locked_operations")) {
152          JsonObject lockedOperationsJSON = value.asObject();
153          Map<String, Boolean> operationsMap = new HashMap<>();
154          for (JsonObject.Member operationMember : lockedOperationsJSON) {
155            String operation = operationMember.getName();
156            Boolean operationBoolean = operationMember.getValue().asBoolean();
157            operationsMap.put(operation, operationBoolean);
158          }
159          this.lockedOperations = operationsMap;
160        }
161      } catch (Exception e) {
162        throw new BoxDeserializationException(memberName, value.toString(), e);
163      }
164    }
165  }
166}