/** Removes a task. */
  @Override
  public void unAssignTask(Task task) throws WorkflowException {
    String componentId = task.getProcessInstance().getModelId();
    ComponentInst compoInst = null;

    try {
      compoInst = AdminReference.getAdminService().getComponentInst(componentId);
    } catch (AdminException e) {
      throw new WorkflowException(
          "TaskManagerImpl.unassignTask", "workflowEngine.EX_GET_COMPONENT_INST", e);
    }

    TodoBackboneAccess todoBBA = new TodoBackboneAccess();

    if (task.getUser() != null) {
      todoBBA.removeEntriesFromExternal(
          compoInst.getDomainFatherId(), componentId, getExternalId(task));
    } else {
      String role = task.getUserRoleName();
      List<User> usersInRole = task.getProcessInstance().getUsersInRole(role);
      for (User userInRole : usersInRole) {
        TaskImpl taskImpl =
            new TaskImpl(userInRole, role, task.getProcessInstance(), task.getState());
        todoBBA.removeEntriesFromExternal(
            compoInst.getDomainFatherId(),
            componentId,
            getExternalId(taskImpl, userInRole.getUserId()));
      }
    }
  }
  /**
   * Adds a new task in the user's todos. Returns the external id given by the external todo system.
   */
  @Override
  public void assignTask(Task task, User delegator) throws WorkflowException {
    String componentId = task.getProcessInstance().getModelId();
    ComponentInst compoInst = null;

    try {
      compoInst = AdminReference.getAdminService().getComponentInst(componentId);
    } catch (AdminException e) {
      throw new WorkflowException(
          "TaskManagerImpl.assignTask", "workflowEngine.EX_GET_COMPONENT_INST", e);
    }

    TodoDetail todo = new TodoDetail();
    todo.setId(task.getProcessInstance().getInstanceId());
    todo.setSpaceId(compoInst.getDomainFatherId());
    todo.setComponentId(componentId);
    todo.setName("activite : " + task.getState().getLabel(task.getUserRoleName(), "fr"));
    if (delegator != null) {
      todo.setDelegatorId(delegator.getUserId());
    } else {
      SilverTrace.error(
          "workflowEngine",
          "TaskManagerImpl.assignTask",
          "root.MSG_GEN_PARAM_VALUE",
          "Undefined delegator for new task : " + todo.getName());
    }

    TodoBackboneAccess todoBBA = new TodoBackboneAccess();
    Vector<Attendee> attendees = new Vector<Attendee>();
    if (task.getUser() != null) {
      // add todo to specified user
      attendees.add(new Attendee(task.getUser().getUserId()));
      todo.setAttendees(attendees);
      todo.setExternalId(getExternalId(task));
      todoBBA.addEntry(todo);
    } else {
      List<User> users = null;
      if (StringUtil.isDefined(task.getGroupId())) {
        // get users according to group
        users = task.getProcessInstance().getUsersInGroup(task.getGroupId());
      } else {
        // get users according to role
        users = task.getProcessInstance().getUsersInRole(task.getUserRoleName());
      }
      for (User user : users) {
        attendees.clear();
        attendees.add(new Attendee(user.getUserId()));
        todo.setAttendees(attendees);
        todo.setExternalId(getExternalId(task, user.getUserId()));
        todoBBA.addEntry(todo);
      }
    }
  }
  /*
   * (non-Javadoc)
   * @see com.stratelia.silverpeas.silverStatisticsPeas.control.AbstractPieChartBuilder
   * #getChartTitle()
   */
  @Override
  public String getChartTitle() {
    String title = message.getString("silverStatisticsPeas.VolumeDocsNumber") + " ";

    try {
      if (StringUtil.isDefined(this.spaceId) && (!"WA0".equals(this.spaceId))) {
        SpaceInstLight space = AdminReference.getAdminService().getSpaceInstLightById(this.spaceId);
        title += message.getString("silverStatisticsPeas.FromSpace") + " [" + space.getName() + "]";
      }
    } catch (Exception e) {
      SilverTrace.error(
          "silverStatisticsPeas",
          "DocPieChartBuilder.getChartTitle()",
          "root.EX_SQL_QUERY_FAILED",
          e);
    }
    return title;
  }