Skip to content

Commit c8413fe

Browse files
Add folder lock functionality (#856)
1 parent 15488a0 commit c8413fe

8 files changed

Lines changed: 397 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
__New Features and Enhancements:__
66

7+
- Add folder lock functionality ([#856](https://github.com/box/box-java-sdk/pull/856))
78
- Add support for search param to get shared link items ([#855](https://github.com/box/box-java-sdk/pull/855))
89

910
__Bug Fixes:__

doc/folders.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ group, and perform other common folder operations (move, copy, delete, etc.).
3939
- [Get All Cascade Policies on Folder](#get-all-cascade-policies-on-folder)
4040
- [Force Apply Cascade Policy on Folder](#force-apply-cascade-policy-on-folder)
4141
- [Delete Cascade Policy](#delete-cascade-policy)
42+
- [Lock a Folder](#lock-a-folder)
43+
- [Get All Locks on a Folder](#get-all-locks-on-a-folder)
44+
- [Delete A Lock on a Folder](#delete-a-lock-on-a-folder)
4245

4346
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
4447

@@ -687,3 +690,41 @@ policyToDelete.delete();
687690

688691
[delete-cascade-policy]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxMetadataCascadePolicy.html#delete--
689692

693+
Lock a Folder
694+
-----------------------------
695+
696+
To lock a folder and prevent it from being moved and/or deleted, call [`lock()`][lock] on a folder.
697+
698+
```java
699+
BoxFolder folder = new BoxFolder(api, "id");
700+
FolderLock.Info folderLock = folder.lock();
701+
```
702+
703+
[lock]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxFolder.html#lock--
704+
705+
Get All Locks on a Folder
706+
-----------------------------
707+
708+
To get all locks on a folder, call [`getlock()`][get-locks] on folder.
709+
710+
```java
711+
BoxFolder folder = new BoxFolder(this.api, "id");
712+
Iterable<BoxFolderLock.Info> locks = folder.getLocks();
713+
for (BoxFolderLock.Info lockInfo : locks) {
714+
// Do something with each lockInfo here
715+
}
716+
```
717+
718+
[get-locks]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxFolder.html#getLocks--
719+
720+
Delete a Lock on a Folder
721+
-----------------------------
722+
723+
To delete a lock on a folder, call [`delete()`][delete-lock] on a BoxFolderLock object. This cannot be called on a BoxFolder object.
724+
725+
```java
726+
BoxFolderLock folderLock = new BoxFolderLock(this.api, "folderLockID");
727+
folderLock.delete();
728+
```
729+
730+
[delete-lock]: http://opensource.box.com/box-java-sdk/javadoc/com/box/sdk/BoxFolderLock.html#delete--

src/main/java/com/box/sdk/BoxFolder.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ public enum SortDirection {
9999
* Upload Session URL Template.
100100
*/
101101
public static final URLTemplate UPLOAD_SESSION_URL_TEMPLATE = new URLTemplate("files/upload_sessions");
102+
/**
103+
* Folder Locks URL Template.
104+
*/
105+
public static final URLTemplate FOLDER_LOCK_URL_TEMPLATE = new URLTemplate("folder_locks");
102106

103107
/**
104108
* Constructs a BoxFolder for a folder with a given ID.
@@ -1200,6 +1204,55 @@ public Iterable<BoxMetadataCascadePolicy.Info> getMetadataCascadePolicies(String
12001204
return cascadePoliciesInfo;
12011205
}
12021206

1207+
/**
1208+
* Lock this folder.
1209+
*
1210+
* @return a created folder lock object.
1211+
*/
1212+
public BoxFolderLock.Info lock() {
1213+
JsonObject folderObject = new JsonObject();
1214+
folderObject.add("type", "folder");
1215+
folderObject.add("id", this.getID());
1216+
1217+
JsonObject lockedOperations = new JsonObject();
1218+
lockedOperations.add("move", true);
1219+
lockedOperations.add("delete", true);
1220+
1221+
1222+
JsonObject body = new JsonObject();
1223+
body.add("folder", folderObject);
1224+
body.add("locked_operations", lockedOperations);
1225+
1226+
BoxJSONRequest request =
1227+
new BoxJSONRequest(this.getAPI(), FOLDER_LOCK_URL_TEMPLATE.build(this.getAPI().getBaseURL()),
1228+
"POST");
1229+
request.setBody(body.toString());
1230+
BoxJSONResponse response = (BoxJSONResponse) request.send();
1231+
JsonObject responseJSON = JsonObject.readFrom(response.getJSON());
1232+
1233+
BoxFolderLock createdFolderLock = new BoxFolderLock(this.getAPI(), responseJSON.get("id").asString());
1234+
return createdFolderLock.new Info(responseJSON);
1235+
}
1236+
1237+
/**
1238+
* Get the lock on this folder.
1239+
*
1240+
* @return a folder lock object.
1241+
*/
1242+
public Iterable<BoxFolderLock.Info> getLocks() {
1243+
String queryString = new QueryStringBuilder().appendParam("folder_id", this.getID()).toString();
1244+
final BoxAPIConnection api = this.getAPI();
1245+
return new BoxResourceIterable<BoxFolderLock.Info>(api,
1246+
FOLDER_LOCK_URL_TEMPLATE.buildWithQuery(api.getBaseURL(), queryString), 100) {
1247+
@Override
1248+
protected BoxFolderLock.Info factory(JsonObject jsonObject) {
1249+
BoxFolderLock folderLock =
1250+
new BoxFolderLock(api, jsonObject.get("id").asString());
1251+
return folderLock.new Info(jsonObject);
1252+
}
1253+
};
1254+
}
1255+
12031256
/**
12041257
* Contains information about a BoxFolder.
12051258
*/
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
}

src/main/java/com/box/sdk/BoxResource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private static Map<String, Class<? extends BoxResource>> initResourceClassByType
6464
result.put(getResourceType(BoxWebLink.class), BoxWebLink.class);
6565
result.put(getResourceType(BoxStoragePolicy.class), BoxStoragePolicy.class);
6666
result.put(getResourceType(BoxStoragePolicyAssignment.class), BoxStoragePolicyAssignment.class);
67+
result.put(getResourceType(BoxFolderLock.class), BoxFolderLock.class);
6768

6869
return Collections.unmodifiableMap(result);
6970
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"id": "12345678",
3+
"type": "folder_lock",
4+
"created_at": "2020-09-14T23:12:53Z",
5+
"created_by": {
6+
"id": "11446498",
7+
"type": "user"
8+
},
9+
"folder": {
10+
"id": "12345",
11+
"type": "folder",
12+
"etag": "1",
13+
"name": "Contracts",
14+
"sequence_id": "3"
15+
},
16+
"lock_type": "freeze",
17+
"locked_operations": {
18+
"delete": true,
19+
"move": true
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"entries": [
3+
{
4+
"folder": {
5+
"id": "12345",
6+
"etag": "1",
7+
"type": "folder",
8+
"sequence_id": "3",
9+
"name": "Contracts"
10+
},
11+
"id": "12345678",
12+
"type": "folder_lock",
13+
"created_by": {
14+
"id": "11446498",
15+
"type": "user"
16+
},
17+
"created_at": "2020-09-14T23:12:53Z",
18+
"locked_operations": {
19+
"move": true,
20+
"delete": true
21+
},
22+
"lock_type": "freeze"
23+
}
24+
],
25+
"limit": 1000,
26+
"next_marker": null
27+
}

0 commit comments

Comments
 (0)