001package com.box.sdk; 002 003import com.eclipsesource.json.Json; 004import com.eclipsesource.json.JsonArray; 005import com.eclipsesource.json.JsonObject; 006import com.eclipsesource.json.JsonValue; 007import java.net.URL; 008import java.util.ArrayList; 009import java.util.Date; 010import java.util.List; 011 012/** 013 * Represents a task on Box. Tasks can have a due date and can be assigned to a specific user. 014 */ 015@BoxResourceType("task") 016public class BoxTask extends BoxResource { 017 018 /** 019 * Task URL Template. 020 */ 021 public static final URLTemplate TASK_URL_TEMPLATE = new URLTemplate("tasks/%s"); 022 /** 023 * Get Assignments URL Template. 024 */ 025 public static final URLTemplate GET_ASSIGNMENTS_URL_TEMPLATE = new URLTemplate("tasks/%s/assignments"); 026 /** 027 * Add Task Assignment URL Template. 028 */ 029 public static final URLTemplate ADD_TASK_ASSIGNMENT_URL_TEMPLATE = new URLTemplate("task_assignments"); 030 031 /** 032 * Constructs a BoxTask for a task with a given ID. 033 * 034 * @param api the API connection to be used by the task. 035 * @param id the ID of the task. 036 */ 037 public BoxTask(BoxAPIConnection api, String id) { 038 super(api, id); 039 } 040 041 /** 042 * Deletes this task. 043 */ 044 public void delete() { 045 URL url = TASK_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 046 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "DELETE"); 047 BoxAPIResponse response = request.send(); 048 response.disconnect(); 049 } 050 051 /** 052 * Adds a new assignment to this task. 053 * 054 * @param assignTo the user to assign the assignment to. 055 * @return information about the newly added task assignment. 056 */ 057 public BoxTaskAssignment.Info addAssignment(BoxUser assignTo) { 058 JsonObject taskJSON = new JsonObject(); 059 taskJSON.add("type", "task"); 060 taskJSON.add("id", this.getID()); 061 062 JsonObject assignToJSON = new JsonObject(); 063 assignToJSON.add("id", assignTo.getID()); 064 065 JsonObject requestJSON = new JsonObject(); 066 requestJSON.add("task", taskJSON); 067 requestJSON.add("assign_to", assignToJSON); 068 069 URL url = ADD_TASK_ASSIGNMENT_URL_TEMPLATE.build(this.getAPI().getBaseURL()); 070 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "POST"); 071 request.setBody(requestJSON.toString()); 072 BoxJSONResponse response = (BoxJSONResponse) request.send(); 073 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 074 075 BoxTaskAssignment addedAssignment = new BoxTaskAssignment(this.getAPI(), responseJSON.get("id").asString()); 076 return addedAssignment.new Info(responseJSON); 077 } 078 079 /** 080 * Adds a new assignment to this task using user's login as identifier. 081 * 082 * @param assignToLogin the login of user to assign the task to. 083 * @return information about the newly added task assignment. 084 */ 085 public BoxTaskAssignment.Info addAssignmentByLogin(String assignToLogin) { 086 JsonObject taskJSON = new JsonObject(); 087 taskJSON.add("type", "task"); 088 taskJSON.add("id", this.getID()); 089 090 JsonObject assignToJSON = new JsonObject(); 091 assignToJSON.add("login", assignToLogin); 092 093 JsonObject requestJSON = new JsonObject(); 094 requestJSON.add("task", taskJSON); 095 requestJSON.add("assign_to", assignToJSON); 096 097 URL url = ADD_TASK_ASSIGNMENT_URL_TEMPLATE.build(this.getAPI().getBaseURL()); 098 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "POST"); 099 request.setBody(requestJSON.toString()); 100 BoxJSONResponse response = (BoxJSONResponse) request.send(); 101 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 102 103 BoxTaskAssignment addedAssignment = new BoxTaskAssignment(this.getAPI(), responseJSON.get("id").asString()); 104 return addedAssignment.new Info(responseJSON); 105 } 106 107 /** 108 * Gets any assignments for this task. 109 * 110 * @return a list of assignments for this task. 111 */ 112 public List<BoxTaskAssignment.Info> getAssignments() { 113 URL url = GET_ASSIGNMENTS_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 114 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 115 BoxJSONResponse response = (BoxJSONResponse) request.send(); 116 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 117 118 int totalCount = responseJSON.get("total_count").asInt(); 119 List<BoxTaskAssignment.Info> assignments = new ArrayList<>(totalCount); 120 JsonArray entries = responseJSON.get("entries").asArray(); 121 for (JsonValue value : entries) { 122 JsonObject assignmentJSON = value.asObject(); 123 BoxTaskAssignment assignment = new BoxTaskAssignment(this.getAPI(), assignmentJSON.get("id").asString()); 124 BoxTaskAssignment.Info info = assignment.new Info(assignmentJSON); 125 assignments.add(info); 126 } 127 128 return assignments; 129 } 130 131 /** 132 * Gets an iterable of all the assignments of this task. 133 * 134 * @param fields the fields to retrieve. 135 * @return an iterable containing info about all the assignments. 136 */ 137 public Iterable<BoxTaskAssignment.Info> getAllAssignments(String... fields) { 138 final QueryStringBuilder builder = new QueryStringBuilder(); 139 if (fields.length > 0) { 140 builder.appendParam("fields", fields); 141 } 142 return () -> { 143 URL url = GET_ASSIGNMENTS_URL_TEMPLATE.buildWithQuery( 144 BoxTask.this.getAPI().getBaseURL(), builder.toString(), BoxTask.this.getID()); 145 return new BoxTaskAssignmentIterator(BoxTask.this.getAPI(), url); 146 }; 147 } 148 149 /** 150 * Gets information about this task. 151 * 152 * @return info about this task. 153 */ 154 public Info getInfo() { 155 URL url = TASK_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 156 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 157 BoxJSONResponse response = (BoxJSONResponse) request.send(); 158 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 159 return new Info(responseJSON); 160 } 161 162 /** 163 * Gets information about this task. 164 * 165 * @param fields the fields to retrieve. 166 * @return info about this task. 167 */ 168 public Info getInfo(String... fields) { 169 QueryStringBuilder builder = new QueryStringBuilder(); 170 if (fields.length > 0) { 171 builder.appendParam("fields", fields); 172 } 173 URL url = TASK_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), builder.toString(), this.getID()); 174 BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET"); 175 BoxJSONResponse response = (BoxJSONResponse) request.send(); 176 JsonObject responseJSON = Json.parse(response.getJSON()).asObject(); 177 return new Info(responseJSON); 178 } 179 180 /** 181 * Updates the information about this task with any info fields that have been modified locally. 182 * 183 * <p>The only fields that will be updated are the ones that have been modified locally. For example, the following 184 * code won't update any information (or even send a network request) since none of the info's fields were 185 * changed:</p> 186 * 187 * <pre>BoxTask task = new BoxTask(api, id); 188 * BoxTask.Info info = task.getInfo(); 189 * task.updateInfo(info);</pre> 190 * 191 * @param info the updated info. 192 */ 193 public void updateInfo(BoxTask.Info info) { 194 URL url = TASK_URL_TEMPLATE.build(this.getAPI().getBaseURL(), this.getID()); 195 BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT"); 196 request.setBody(info.getPendingChanges()); 197 BoxJSONResponse response = (BoxJSONResponse) request.send(); 198 JsonObject jsonObject = Json.parse(response.getJSON()).asObject(); 199 info.update(jsonObject); 200 } 201 202 /** 203 * Enumerates the possible actions that a task can have. 204 */ 205 public enum Action { 206 /** 207 * The task must be reviewed. 208 */ 209 REVIEW("review"), 210 211 /** 212 * The task must be completed. 213 */ 214 COMPLETE("complete"); 215 216 private final String jsonValue; 217 218 Action(String jsonValue) { 219 this.jsonValue = jsonValue; 220 } 221 222 static Action fromJSONString(String jsonValue) { 223 if (jsonValue.equals("review")) { 224 return REVIEW; 225 } else if (jsonValue.equals("complete")) { 226 return COMPLETE; 227 } else { 228 throw new IllegalArgumentException("The provided JSON value isn't a valid Action."); 229 } 230 } 231 232 String toJSONString() { 233 return this.jsonValue; 234 } 235 } 236 237 /** 238 * Enumerates the possible completion rules for a task. 239 */ 240 public enum CompletionRule { 241 242 /** 243 * The task must be completed by all assignees. 244 */ 245 ALL_ASSIGNEES("all_assignees"), 246 247 /** 248 * The task must be completed by at least one assignee. 249 */ 250 ANY_ASSIGNEE("any_assignee"); 251 252 private final String jsonValue; 253 254 CompletionRule(String jsonValue) { 255 this.jsonValue = jsonValue; 256 } 257 258 String toJSONString() { 259 return this.jsonValue; 260 } 261 } 262 263 /** 264 * Contains information about a BoxTask. 265 */ 266 public class Info extends BoxResource.Info { 267 private BoxFile.Info item; 268 private Date dueAt; 269 private String action; 270 private String completionRule; 271 private String message; 272 private List<BoxTaskAssignment.Info> taskAssignments; 273 private boolean completed; 274 private BoxUser.Info createdBy; 275 private Date createdAt; 276 277 /** 278 * Constructs an empty Info object. 279 */ 280 public Info() { 281 super(); 282 } 283 284 /** 285 * Constructs an Info object by parsing information from a JSON string. 286 * 287 * @param json the JSON string to parse. 288 */ 289 public Info(String json) { 290 super(json); 291 } 292 293 /** 294 * Constructs an Info object using an already parsed JSON object. 295 * 296 * @param jsonObject the parsed JSON object. 297 */ 298 Info(JsonObject jsonObject) { 299 super(jsonObject); 300 } 301 302 @Override 303 public BoxTask getResource() { 304 return BoxTask.this; 305 } 306 307 /** 308 * Gets the file associated with this task. 309 * 310 * @return the file associated with this task. 311 */ 312 public BoxFile.Info getItem() { 313 return this.item; 314 } 315 316 /** 317 * Gets the date at which this task is due. 318 * 319 * @return the date at which this task is due. 320 */ 321 public Date getDueAt() { 322 return this.dueAt; 323 } 324 325 /** 326 * Sets the task's due date. 327 * 328 * @param dueAt the task's due date. 329 */ 330 public void setDueAt(Date dueAt) { 331 this.dueAt = dueAt; 332 this.addPendingChange("due_at", BoxDateFormat.format(dueAt)); 333 } 334 335 /** 336 * @return the action the task assignee will be prompted to do. 337 * @deprecated Please use getTaskType() 338 * <p> 339 * Gets the action the task assignee will be prompted to do. 340 */ 341 @Deprecated 342 public Action getAction() { 343 return Action.REVIEW; 344 } 345 346 /** 347 * Gets the action the task assignee will be prompted to do. 348 * 349 * @return the action the task assignee will be prompted to do. 350 */ 351 public String getTaskType() { 352 return this.action; 353 } 354 355 /** 356 * Returns the completion rule for the task. 357 * 358 * @return the task completion rule. 359 */ 360 public String getCompletionRule() { 361 return this.completionRule; 362 } 363 364 /** 365 * Sets the task's completion rule. 366 * 367 * @param completionRule the new completion rule. 368 */ 369 public void setCompletionRule(CompletionRule completionRule) { 370 this.completionRule = completionRule.toJSONString(); 371 this.addPendingChange("completion_rule", completionRule.toJSONString()); 372 } 373 374 /** 375 * Gets the message that will be included with this task. 376 * 377 * @return the message that will be included with this task. 378 */ 379 public String getMessage() { 380 return this.message; 381 } 382 383 /** 384 * Sets the task's message. 385 * 386 * @param message the task's new message. 387 */ 388 public void setMessage(String message) { 389 this.message = message; 390 this.addPendingChange("message", message); 391 } 392 393 /** 394 * Gets the collection of task assignments associated with this task. 395 * 396 * @return the collection of task assignments associated with this task. 397 */ 398 public List<BoxTaskAssignment.Info> getTaskAssignments() { 399 return this.taskAssignments; 400 } 401 402 /** 403 * Gets whether or not this task has been completed. 404 * 405 * @return whether or not this task has been completed. 406 */ 407 public boolean isCompleted() { 408 return this.completed; 409 } 410 411 /** 412 * Gets the user who created this task. 413 * 414 * @return the user who created this task. 415 */ 416 public BoxUser.Info getCreatedBy() { 417 return this.createdBy; 418 } 419 420 /** 421 * Gets when this task was created. 422 * 423 * @return when this task was created. 424 */ 425 public Date getCreatedAt() { 426 return this.createdAt; 427 } 428 429 @Override 430 void parseJSONMember(JsonObject.Member member) { 431 super.parseJSONMember(member); 432 433 String memberName = member.getName(); 434 JsonValue value = member.getValue(); 435 try { 436 if (memberName.equals("item")) { 437 JsonObject itemJSON = value.asObject(); 438 String itemID = itemJSON.get("id").asString(); 439 BoxFile file = new BoxFile(getAPI(), itemID); 440 this.item = file.new Info(itemJSON); 441 } else if (memberName.equals("due_at")) { 442 this.dueAt = BoxDateFormat.parse(value.asString()); 443 } else if (memberName.equals("action")) { 444 this.action = value.asString(); 445 } else if (memberName.equals("completion_rule")) { 446 this.completionRule = value.asString(); 447 } else if (memberName.equals("message")) { 448 this.message = value.asString(); 449 } else if (memberName.equals("task_assignment_collection")) { 450 this.taskAssignments = this.parseTaskAssignmentCollection(value.asObject()); 451 } else if (memberName.equals("is_completed")) { 452 this.completed = value.asBoolean(); 453 } else if (memberName.equals("created_by")) { 454 JsonObject userJSON = value.asObject(); 455 String userID = userJSON.get("id").asString(); 456 BoxUser user = new BoxUser(getAPI(), userID); 457 this.createdBy = user.new Info(userJSON); 458 } else if (memberName.equals("created_at")) { 459 this.createdAt = BoxDateFormat.parse(value.asString()); 460 } 461 462 } catch (Exception e) { 463 throw new BoxDeserializationException(memberName, value.toString(), e); 464 } 465 } 466 467 private List<BoxTaskAssignment.Info> parseTaskAssignmentCollection(JsonObject jsonObject) { 468 int count = jsonObject.get("total_count").asInt(); 469 List<BoxTaskAssignment.Info> taskAssignmentCollection = new ArrayList<>(count); 470 JsonArray entries = jsonObject.get("entries").asArray(); 471 for (JsonValue value : entries) { 472 JsonObject entry = value.asObject(); 473 String id = entry.get("id").asString(); 474 BoxTaskAssignment assignment = new BoxTaskAssignment(getAPI(), id); 475 taskAssignmentCollection.add(assignment.new Info(entry)); 476 } 477 478 return taskAssignmentCollection; 479 } 480 } 481}