/** 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);
      }
    }
  }
  public void removeProcessInstanceData(ProcessInstance instance) throws WorkflowException {
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_ENTER_METHOD");

    ForeignPK foreignPK = new ForeignPK(instance.getInstanceId(), instance.getModelId());

    // delete attachments
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_PARAM_VALUE",
        "Delete attachments foreignPK = " + foreignPK);
    AttachmentController.deleteAttachmentByCustomerPK(foreignPK);

    // delete versioning
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_PARAM_VALUE",
        "Delete versiong foreignPK = " + foreignPK);
    try {
      getVersioningBm().deleteDocumentsByForeignPK(foreignPK);
    } catch (Exception e) {
      throw new WorkflowException(
          "ProcessInstanceManagerImpl.removeProcessInstanceData",
          "EX_ERR_CANT_REMOVE_VERSIONNING_FILES",
          e);
    }

    // delete folder
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_PARAM_VALUE",
        "Delete folder");
    try {
      RecordSet folderRecordSet = instance.getProcessModel().getFolderRecordSet();
      folderRecordSet.delete(instance.getFolder());
    } catch (FormException e) {
      throw new WorkflowException(
          "ProcessInstanceManagerImpl.removeProcessInstanceData", "EX_ERR_CANT_REMOVE_FOLDER", e);
    }

    // delete history steps
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_PARAM_VALUE",
        "Delete history steps");
    HistoryStep[] steps = instance.getHistorySteps();
    for (int i = 0; steps != null && i < steps.length; i++) {
      if (!steps[i].getAction().equals("#question#") && !steps[i].getAction().equals("#response#"))
        steps[i].deleteActionRecord();
    }

    // delete associated todos
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_PARAM_VALUE",
        "Delete associated todos");
    TodoBackboneAccess tbba = new TodoBackboneAccess();
    tbba.removeEntriesFromExternal("useless", foreignPK.getInstanceId(), foreignPK.getId() + "##%");

    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_EXIT_METHOD");
  }