public void updateTasks() {
    int dayLength = 24 * 60 * 60 * 1000;
    Date d = new Date();
    d.setTime(d.getTime() + dayLength);
    String end =
        (1900 + d.getYear())
            + "-"
            + (d.getMonth() < 9 ? "0" : "")
            + (d.getMonth() + 1)
            + "-"
            + (d.getDate() < 9 ? "0" : "")
            + d.getDate();
    d = new Date();
    d.setTime(d.getTime() - dayLength * 3);
    String start =
        (1900 + d.getYear())
            + "-"
            + (d.getMonth() < 9 ? "0" : "")
            + (d.getMonth() + 1)
            + "-"
            + (d.getDate() < 9 ? "0" : "")
            + d.getDate();

    BpmServiceMain.sendGet(
        "/processInstances/tasks?assignee=" + BpmServiceMain.getUser(),
        new AsyncCallback<String>() {
          public void onFailure(Throwable caught) {}

          public void onSuccess(String result) {
            ArrayList<Task> tasks = Parse.parseProcessTasks(result);
            for (Task t : tasks) {
              if (t == null) break;
              if (contains(records1, t.get("id")) == -1)
                addRecordToArray(records1, createLGR(false, t));
            }
            for (ListGridRecord r : records1) {
              if (r == null) break;
              if (contains(tasks, r.getAttributeAsString("id")) == -1)
                removeRecordFromArray(records1, r);
            }
            ((ListGrid) sections[0].getItems()[0]).setData(records1);
          }
        });

    BpmServiceMain.sendGet(
        "/processInstances/tasks?candidate=" + BpmServiceMain.getUser(),
        new AsyncCallback<String>() {
          public void onFailure(Throwable caught) {}

          public void onSuccess(String result) {
            ArrayList<Task> tasks = Parse.parseProcessTasks(result);
            for (Task t : tasks) {
              if (t == null) break;
              if (contains(records2, t.get("id")) == -1)
                addRecordToArray(records2, createLGR(false, t));
            }
            for (ListGridRecord r : records2) {
              if (r == null) break;
              if (contains(tasks, r.getAttributeAsString("id")) == -1)
                removeRecordFromArray(records2, r);
            }
            ((ListGrid) sections[1].getItems()[0]).setData(records2);
          }
        });

    BpmServiceMain.sendGet(
        "/tasks/history?assignee=" + BpmServiceMain.getUser() + "&start=" + start + "&end=" + end,
        new AsyncCallback<String>() {
          public void onFailure(Throwable caught) {}

          public void onSuccess(String result) {
            ArrayList<Task> tasks = Parse.parseTasks(result);
            for (Task t : tasks) {
              if (t == null) break;
              if (contains(records3, t.get("id")) == -1)
                addRecordToArray(records3, createLGR(true, t));
            }
            for (ListGridRecord r : records3) {
              if (r == null) break;
              if (contains(tasks, r.getAttributeAsString("id")) == -1)
                removeRecordFromArray(records3, r);
            }
            ((ListGrid) sections[2].getItems()[0]).setData(records3);
          }
        });
  }
  public Tasks() {
    super();

    SectionStack sectionStack = new SectionStack();
    sectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
    sectionStack.setWidth100();
    sectionStack.setHeight100();

    sections = new SectionStackSection[3];
    for (int i = 0; i < 2; i++) {
      sections[i] = new SectionStackSection(i == 0 ? "My Tasks" : "Available Tasks");
      sections[i].setExpanded(true);
      sections[i].setResizeable(false);

      ListGridField complete = new ListGridField("complete", " ");
      complete.setType(ListGridFieldType.BOOLEAN);
      complete.setCanEdit(true);
      complete.setCanToggle(true);

      if (i == 0) {

        final MyTasksListGrid taskgrid = new MyTasksListGrid();
        taskgrid.setWidth100();
        taskgrid.setHeight100();
        taskgrid.setCanExpandRecords(true);
        taskgrid.setCanExpandMultipleRecords(false);
        taskgrid.setExpansionMode(ExpansionMode.DETAILS);
        taskgrid.setShowAllRecords(true);
        taskgrid.setExpansionCanEdit(true);
        taskgrid.addCellDoubleClickHandler(
            new CellDoubleClickHandler() {
              public void onCellDoubleClick(CellDoubleClickEvent event) {
                taskgrid.expandRecord(taskgrid.getRecord(event.getRowNum()));
              }
            });

        ListGridField processIntanceID = new ListGridField("$#processInstanceId", "Workflow");
        processIntanceID.setBaseStyle("linkLabel");
        processIntanceID.addRecordClickHandler(
            new RecordClickHandler() {
              public void onRecordClick(RecordClickEvent event) {
                Object[] args = {event.getValue(), true};
                PageManager.getInstance().setPageHistory(Pages.VIEWWORKFLOW, args);
              }
            });

        taskgrid.setFields(
            complete,
            new ListGridField("taskname", "Name"),
            new ListGridField("$#createTime", "Time"),
            new ListGridField("$#priority", "Priority"),
            new ListGridField("$#assignee", "Assignee"),
            new ListGridField("$#activityName", "ActivityName"),
            processIntanceID);
        sections[i].addItem(taskgrid);
      } else {
        CustomListGrid grid = new CustomListGrid();
        grid.setWidth100();
        grid.setHeight100();
        grid.setCanExpandRecords(true);
        grid.setCanExpandMultipleRecords(false);
        grid.setExpansionMode(ExpansionMode.DETAILS);
        grid.setShowAllRecords(true);

        complete.addRecordClickHandler(getRecordClickHandler(grid, i == 1));

        grid.setShowRecordComponents(true);
        grid.setShowRecordComponentsByCell(true);
        ListGridField assign = new ListGridField("Assign", "Assign");
        assign.setAlign(Alignment.CENTER);
        grid.setFields(
            complete,
            new ListGridField("taskname", "Name"),
            new ListGridField("$#createTime", "Time"),
            new ListGridField("$#priority", "Priority"),
            assign);
        sections[i].addItem(grid);
      }
    }

    sections[2] = new SectionStackSection("Event Log");
    sections[2].setExpanded(true);
    sections[2].setResizeable(false);
    ListGrid grid = new ListGrid();
    grid.setWidth100();
    grid.setHeight100();
    grid.setCanExpandRecords(false);
    grid.setShowAllRecords(true);
    grid.setCanEdit(false);
    grid.setFields(
        new ListGridField("$#endTime", "Time"),
        new ListGridField("$#id", "Task"),
        new ListGridField("$#assignee", "User"),
        new ListGridField("$#state", "Action"),
        new ListGridField("outcome", "Outcome"),
        new ListGridField("$#processInstanceId", "Workflow"));
    sections[2].addItem(grid);

    sectionStack.addSection(sections[0]);
    sectionStack.addSection(sections[1]);
    sectionStack.addSection(sections[2]);

    BpmServiceMain.sendGet(
        "/processInstances/tasks?assignee=" + BpmServiceMain.getUser(),
        new AsyncCallback<String>() {
          public void onFailure(Throwable arg0) {}

          public void onSuccess(String arg0) {
            ArrayList<Task> tasks = Parse.parseProcessTasks(arg0);

            records1 = new ListGridRecord[MAX_TASKS];
            int i = 0;
            for (Task t : tasks) {
              records1[i++] = createLGR(false, t);
            }
            ((MyTasksListGrid) sections[0].getItems()[0]).setData(records1);
          }
        });
    BpmServiceMain.sendGet(
        "/processInstances/tasks?candidate=" + BpmServiceMain.getUser(),
        new AsyncCallback<String>() {
          public void onFailure(Throwable arg0) {}

          public void onSuccess(String arg0) {
            ArrayList<Task> tasks = Parse.parseProcessTasks(arg0);
            records2 = new ListGridRecord[MAX_TASKS];
            int i = 0;
            for (Task t : tasks) {
              records2[i++] = createLGR(false, t);
            }
            ((CustomListGrid) sections[1].getItems()[0]).setData(records2);
          }
        });

    int dayLength = 24 * 60 * 60 * 1000;
    Date d = new Date();
    d.setTime(d.getTime() + dayLength);
    String end =
        (1900 + d.getYear())
            + "-"
            + (d.getMonth() < 9 ? "0" : "")
            + (d.getMonth() + 1)
            + "-"
            + (d.getDate() < 9 ? "0" : "")
            + d.getDate();
    d = new Date();
    d.setTime(d.getTime() - dayLength * 3);
    String start =
        (1900 + d.getYear())
            + "-"
            + (d.getMonth() < 9 ? "0" : "")
            + (d.getMonth() + 1)
            + "-"
            + (d.getDate() < 9 ? "0" : "")
            + d.getDate();
    BpmServiceMain.sendGet(
        "/tasks/history?assignee=" + BpmServiceMain.getUser() + "&start=" + start + "&end=" + end,
        new AsyncCallback<String>() {
          public void onFailure(Throwable arg0) {}

          public void onSuccess(String arg0) {
            ArrayList<Task> tasks = Parse.parseTasks(arg0);
            records3 = new ListGridRecord[MAX_TASKS];
            int i = 0;
            for (Task t : tasks) {
              records3[i++] = createLGR(true, t);
            }
            ((ListGrid) sections[2].getItems()[0]).setData(records3);
            ((ListGrid) sections[2].getItems()[0]).sort(0, SortDirection.DESCENDING);
          }
        });

    createPage(sectionStack, PageWidget.PAGE_TASKS);

    timer =
        new Timer() {
          public void run() {
            updateTasks();
          }
        };
    timer.scheduleRepeating(BpmServiceMain.getPollingRate());
  }
  // Submit button on Completion Variables pop-up
  protected void submit(Map<String, String> map, boolean assign) {
    String varString = "", outcomeString = "";
    if (map.get("Choose") != null) {
      outcomeString = "?outcome=" + map.get("Choose");
    } else if (map.get("mustChoose") != null) {
      SC.say("Please choose one of the available options before submitting.");
      return;
    }
    for (int i = 0; i < 5; i++) {
      String key = map.get("key" + i), value = map.get("value" + i);
      if (key != null && !key.equals("Note Name")) {
        // key = BpmServiceMain.xmlEncode(key);
        if (value == null || value == "Note or URL") {
          SC.say("The name for note #" + (i + 1) + " is filled in, but there is no value");
          return;
        }
        // value = BpmServiceMain.xmlEncode(value);
        varString += "&var=" + BpmServiceMain.urlEncode(key + ":" + value);
      } else if ((key == null || key.equals("Note Name"))
          && (value != null && !value.equals("Note or URL"))) {
        SC.say("The value for note #" + (i + 1) + " is filled in, but there is no name");
        return;
      }
    }
    if (outcomeString.equals("")) varString = varString.replaceFirst("&", "?");
    final String tempString = map.get("taskID") + outcomeString + varString;
    if (assign) {
      submitItem.disable();
      BpmServiceMain.sendPostNoLocation(
          "/tasks/active/" + map.get("taskID") + "?assignee=" + BpmServiceMain.getUser(),
          new AsyncCallback<Void>() {
            public void onFailure(Throwable arg0) {
              SC.say(
                  "Error. Please ensure that you are connected to the Internet, that the server is currently online, and that the task was not already assigned.");
              submitItem.enable();
            }

            public void onSuccess(Void arg0) {
              BpmServiceMain.sendDelete(
                  "/tasks/active/" + tempString,
                  true,
                  new AsyncCallback<Void>() {
                    public void onFailure(Throwable arg0) {
                      SC.say(
                          "Error. Please ensure that you are connected to the Internet, that the server is currently online, and that the task was not already completed.");
                      confirm.destroy();
                      updateTasks();
                    }

                    public void onSuccess(Void arg0) {
                      confirm.destroy();
                      updateTasks();
                    }
                  });
            }
          });
    } else {
      BpmServiceMain.sendDelete(
          "/tasks/active/" + tempString,
          true,
          new AsyncCallback<Void>() {
            public void onFailure(Throwable arg0) {
              SC.say(
                  "Error. Please ensure that you are connected to the Internet, that the server is currently online, and that the task was not already completed.");
            }

            public void onSuccess(Void arg0) {
              confirm.destroy();
              updateTasks();
            }
          });
    }
  }