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