|
| 1 | +package com.box.sdk; |
| 2 | + |
| 3 | +import java.net.URL; |
| 4 | +import java.util.Date; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import com.eclipsesource.json.JsonObject; |
| 9 | +import com.eclipsesource.json.JsonValue; |
| 10 | + |
| 11 | +/** |
| 12 | + * Represents a lock on a folder. |
| 13 | + * |
| 14 | + * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked |
| 15 | + * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error |
| 16 | + * handling for errors related to the Box REST API, you should capture this exception explicitly.</p> |
| 17 | + */ |
| 18 | +@BoxResourceType("folder_lock") |
| 19 | +public class BoxFolderLock extends BoxResource { |
| 20 | + /** |
| 21 | + * Delete Folder Locks URL Template. |
| 22 | + */ |
| 23 | + public static final URLTemplate DELETE_FOLDER_LOCK_URL_TEMPLATE = new URLTemplate("folder_locks/%s"); |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructs a BoxFolderLock with a given ID. |
| 27 | + * |
| 28 | + * @param api the API connection to be used by the folder lock. |
| 29 | + * @param id the ID of the folder lock. |
| 30 | + */ |
| 31 | + public BoxFolderLock(BoxAPIConnection api, String id) { |
| 32 | + super(api, id); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Delete the lock on this folder. |
| 37 | + */ |
| 38 | + public void delete() { |
| 39 | + URL url = DELETE_FOLDER_LOCK_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); |
| 40 | + BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE"); |
| 41 | + BoxAPIResponse response = request.send(); |
| 42 | + response.disconnect(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Contains information about a BoxFolderLock. |
| 47 | + */ |
| 48 | + public class Info extends BoxResource.Info { |
| 49 | + private BoxFolder.Info folder; |
| 50 | + private BoxUser.Info createdBy; |
| 51 | + private Date createdAt; |
| 52 | + private String lockType; |
| 53 | + private Map<String, Boolean> lockedOperations; |
| 54 | + |
| 55 | + /** |
| 56 | + * Constructs an empty Info object. |
| 57 | + */ |
| 58 | + public Info() { |
| 59 | + super(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Constructs an Info object by parsing information from a JSON string. |
| 64 | + * |
| 65 | + * @param json the JSON string to parse. |
| 66 | + */ |
| 67 | + public Info(String json) { |
| 68 | + super(json); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Constructs an Info object using an already parsed JSON object. |
| 73 | + * |
| 74 | + * @param jsonObject the parsed JSON object. |
| 75 | + */ |
| 76 | + Info(JsonObject jsonObject) { |
| 77 | + super(jsonObject); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public BoxResource getResource() { |
| 82 | + return BoxFolderLock.this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Gets the folder that the lock applies to. |
| 87 | + * |
| 88 | + * @return The folder that the lock applies to. |
| 89 | + */ |
| 90 | + public BoxFolder.Info getFolder() { |
| 91 | + return this.folder; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Gets the user or group that created the lock. |
| 96 | + * |
| 97 | + * @return the user or group that created the lock. |
| 98 | + */ |
| 99 | + public BoxUser.Info getCreatedBy() { |
| 100 | + return this.createdBy; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets the date the folder lock object was created. |
| 105 | + * |
| 106 | + * @return the date the folder lock object was created. |
| 107 | + */ |
| 108 | + public Date getCreatedAt() { |
| 109 | + return this.createdAt; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Gets the lock type, always freeze. |
| 114 | + * |
| 115 | + * @return the lock type, always freeze. |
| 116 | + */ |
| 117 | + public String getLockType() { |
| 118 | + return this.lockType; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Gets the operations that have been locked. |
| 123 | + * |
| 124 | + * @return the operations that have been locked. |
| 125 | + */ |
| 126 | + public Map<String, Boolean> getLockedOperations() { |
| 127 | + return this.lockedOperations; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * {@inheritDoc} |
| 132 | + */ |
| 133 | + @Override |
| 134 | + protected void parseJSONMember(JsonObject.Member member) { |
| 135 | + super.parseJSONMember(member); |
| 136 | + |
| 137 | + String memberName = member.getName(); |
| 138 | + JsonValue value = member.getValue(); |
| 139 | + |
| 140 | + try { |
| 141 | + if (memberName.equals("folder")) { |
| 142 | + JsonObject folderJSON = value.asObject(); |
| 143 | + String folderID = folderJSON.get("id").asString(); |
| 144 | + BoxFolder folder = new BoxFolder(getAPI(), folderID); |
| 145 | + this.folder = folder.new Info(folderJSON); |
| 146 | + } else if (memberName.equals("created_by")) { |
| 147 | + JsonObject userJSON = value.asObject(); |
| 148 | + if (this.createdBy == null) { |
| 149 | + String userID = userJSON.get("id").asString(); |
| 150 | + BoxUser user = new BoxUser(getAPI(), userID); |
| 151 | + this.createdBy = user.new Info(userJSON); |
| 152 | + } else { |
| 153 | + this.createdBy.update(userJSON); |
| 154 | + } |
| 155 | + } else if (memberName.equals("created_at")) { |
| 156 | + this.createdAt = BoxDateFormat.parse(value.asString()); |
| 157 | + |
| 158 | + } else if (memberName.equals("lock_type")) { |
| 159 | + this.lockType = value.asString(); |
| 160 | + |
| 161 | + } else if (memberName.equals("locked_operations")) { |
| 162 | + JsonObject lockedOperationsJSON = value.asObject(); |
| 163 | + Map<String, Boolean> operationsMap = new HashMap<String, Boolean>(); |
| 164 | + for (JsonObject.Member operationMember : lockedOperationsJSON) { |
| 165 | + String operation = operationMember.getName(); |
| 166 | + Boolean operationBoolean = operationMember.getValue().asBoolean(); |
| 167 | + operationsMap.put(operation, operationBoolean); |
| 168 | + } |
| 169 | + this.lockedOperations = operationsMap; |
| 170 | + } |
| 171 | + } catch (Exception e) { |
| 172 | + throw new BoxDeserializationException(memberName, value.toString(), e); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments