Time for action – working with tasks

We have used the REST API for fetching, updating, and adding actions to the business process and will also use it when we want to work with tasks. To access a task, we have to use the GET method with the URL /runtime/tasks and perform the following steps:

  1. First, we have to fetch processes by setting the Method field to GET and the URL field to /runtime/tasks, as shown in the following screenshot:
    Time for action – working with tasks
  2. By navigating to the preceding URL, we will get a list of the tasks available in the repository with all the details, as shown in the following screenshot:
    Time for action – working with tasks
  3. We can also use the REST API to access the tasks from the repository by adding the getTasks method in the Process_Deployment class, which will provide the list of tasks, as shown in the following code:
    public static JSONArray getTasks() throws JSONException, IOException {
        String uri = REST_URI + "/runtime/tasks";
        Representation response = getClientResource(uri).get(MediaType.APPLICATION_JSON);
        JSONObject object = new JSONObject(response.getText());
        if (object != null) {
          JSONArray taskArray = (JSONArray) object.get("data");
          return taskArray;
        }
        return null;
      }
  4. We can fetch a specific process based on its process ID; similarly, we can retrieve a specific task based on its task ID. For this, we have to specify this URL .. /runtime/tasks/{taskId} in combination with the GET method. To execute the task, we need it to be claimed by a specific person using the POST method. For that, we need to set values for the action and assignee parameters. We can create the claimTask method in the Process_Deployment class as follows:
    public static void claimTask(int id) throws JSONException, IOException {
        String uri = REST_URI + "/runtime/tasks/" + id;
          JSONObject my_data = new JSONObject();
          my_data.put("action", "claim");
          my_data.put("assignee", "kermit");
          Representation response = getClientResource(uri).post(my_data);
      }
  5. After claiming the task, we have to make sure that it is completed to make the process work smoothly. We need to set the action parameter to complete along with the POST method, as shown in the following code:
    public static void completeTask(int id) throws JSONException, IOException {
        String uri = REST_URI + "/runtime/tasks/" + id;
        JSONObject my_data = new JSONObject();
         my_data.put("action", "complete");
         Representation response = getClientResource(uri).post(my_data);
        }

What just happened?

We are done learning about the Task API: we got the list of tasks available within the repository, fetched a specific task based on the task ID, and then changed the action parameter for claiming the task. After claiming the task, the task was completed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset