@Override
  public void claim(Context context, BasicWorkflowItem workflowItem, EPerson e)
      throws SQLException, IOException, AuthorizeException {
    int taskstate = workflowItem.getState();

    switch (taskstate) {
      case WFSTATE_STEP1POOL:

        // authorize DSpaceActions.SUBMIT_REVIEW
        doState(context, workflowItem, WFSTATE_STEP1, e);

        break;

      case WFSTATE_STEP2POOL:

        // authorize DSpaceActions.SUBMIT_STEP2
        doState(context, workflowItem, WFSTATE_STEP2, e);

        break;

      case WFSTATE_STEP3POOL:

        // authorize DSpaceActions.SUBMIT_STEP3
        doState(context, workflowItem, WFSTATE_STEP3, e);

        break;

        // if we got here, we weren't pooled... error?
        // FIXME - log the error?
    }

    log.info(
        LogManager.getHeader(
            context,
            "claim_task",
            "workflow_item_id="
                + workflowItem.getID()
                + "item_id="
                + workflowItem.getItem().getID()
                + "collection_id="
                + workflowItem.getCollection().getID()
                + "newowner_id="
                + workflowItem.getOwner().getID()
                + "old_state="
                + taskstate
                + "new_state="
                + workflowItem.getState()));
  }
  @Override
  public void unclaim(Context context, BasicWorkflowItem workflowItem, EPerson e)
      throws SQLException, IOException, AuthorizeException {
    int taskstate = workflowItem.getState();

    switch (taskstate) {
      case WFSTATE_STEP1:

        // authorize DSpaceActions.STEP1
        doState(context, workflowItem, WFSTATE_STEP1POOL, e);

        break;

      case WFSTATE_STEP2:

        // authorize DSpaceActions.APPROVE
        doState(context, workflowItem, WFSTATE_STEP2POOL, e);

        break;

      case WFSTATE_STEP3:

        // authorize DSpaceActions.STEP3
        doState(context, workflowItem, WFSTATE_STEP3POOL, e);

        break;

        // error handling? shouldn't get here
        // FIXME - what to do with error - log it?
    }

    log.info(
        LogManager.getHeader(
            context,
            "unclaim_workflow",
            "workflow_item_id="
                + workflowItem.getID()
                + ",item_id="
                + workflowItem.getItem().getID()
                + ",collection_id="
                + workflowItem.getCollection().getID()
                + ",old_state="
                + taskstate
                + ",new_state="
                + workflowItem.getState()));
  }
  protected void notifyGroupOfTask(
      Context c, BasicWorkflowItem wi, Group mygroup, List<EPerson> epa)
      throws SQLException, IOException {
    // check to see if notification is turned off
    // and only do it once - delete key after notification has
    // been suppressed for the first time
    UUID myID = wi.getItem().getID();

    if (noEMail.containsKey(myID)) {
      // suppress email, and delete key
      noEMail.remove(myID);
    } else {
      try {
        // Get the item title
        String title = getItemTitle(wi);

        // Get the submitter's name
        String submitter = getSubmitterName(wi);

        // Get the collection
        Collection coll = wi.getCollection();

        String message = "";

        for (EPerson anEpa : epa) {
          Locale supportedLocale = I18nUtil.getEPersonLocale(anEpa);
          Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "submit_task"));
          email.addArgument(title);
          email.addArgument(coll.getName());
          email.addArgument(submitter);

          ResourceBundle messages = ResourceBundle.getBundle("Messages", supportedLocale);
          switch (wi.getState()) {
            case WFSTATE_STEP1POOL:
              message = messages.getString("org.dspace.workflow.WorkflowManager.step1");

              break;

            case WFSTATE_STEP2POOL:
              message = messages.getString("org.dspace.workflow.WorkflowManager.step2");

              break;

            case WFSTATE_STEP3POOL:
              message = messages.getString("org.dspace.workflow.WorkflowManager.step3");

              break;
          }
          email.addArgument(message);
          email.addArgument(getMyDSpaceLink());
          email.addRecipient(anEpa.getEmail());
          email.send();
        }
      } catch (MessagingException e) {
        String gid = (mygroup != null) ? String.valueOf(mygroup.getID()) : "none";
        log.warn(
            LogManager.getHeader(
                c,
                "notifyGroupofTask",
                "cannot email user group_id="
                    + gid
                    + " workflow_item_id="
                    + wi.getID()
                    + ":  "
                    + e.getMessage()));
      }
    }
  }
  @Override
  public WorkspaceItem sendWorkflowItemBackSubmission(
      Context context,
      BasicWorkflowItem workflowItem,
      EPerson ePerson,
      String provenancePrefix,
      String rejection_message)
      throws SQLException, AuthorizeException, IOException {

    int oldState = workflowItem.getState();
    // authorize a DSpaceActions.REJECT
    // stop workflow
    taskListItemService.deleteByWorkflowItem(context, workflowItem);

    // rejection provenance
    Item myitem = workflowItem.getItem();

    // Get current date
    String now = DCDate.getCurrent().toString();

    // Get user's name + email address
    String usersName = getEPersonName(ePerson);

    // Here's what happened
    String provDescription =
        "Rejected by " + usersName + ", reason: " + rejection_message + " on " + now + " (GMT) ";

    // Add to item as a DC field
    itemService.addMetadata(
        context,
        myitem,
        MetadataSchema.DC_SCHEMA,
        "description",
        "provenance",
        "en",
        provDescription);
    itemService.update(context, myitem);

    // convert into personal workspace
    WorkspaceItem wsi = returnToWorkspace(context, workflowItem);

    // notify that it's been rejected
    notifyOfReject(context, workflowItem, ePerson, rejection_message);

    log.info(
        LogManager.getHeader(
            context,
            "reject_workflow",
            "workflow_item_id="
                + workflowItem.getID()
                + "item_id="
                + workflowItem.getItem().getID()
                + "collection_id="
                + workflowItem.getCollection().getID()
                + "eperson_id="
                + ePerson.getID()));

    logWorkflowEvent(
        context,
        wsi.getItem(),
        workflowItem,
        ePerson,
        WFSTATE_SUBMIT,
        null,
        wsi.getCollection(),
        oldState,
        null);

    return wsi;
  }
  // returns true if archived
  protected boolean doState(
      Context context, BasicWorkflowItem workflowItem, int newstate, EPerson newowner)
      throws SQLException, IOException, AuthorizeException {
    Collection mycollection = workflowItem.getCollection();
    Group mygroup = null;
    boolean archived = false;

    // Gather our old data for launching the workflow event
    int oldState = workflowItem.getState();

    workflowItem.setState(newstate);

    switch (newstate) {
      case WFSTATE_STEP1POOL:

        // any reviewers?
        // if so, add them to the tasklist
        workflowItem.setOwner(null);

        // get reviewers (group 1 )
        mygroup = collectionService.getWorkflowGroup(mycollection, 1);

        if ((mygroup != null) && !(groupService.isEmpty(mygroup))) {
          // get a list of all epeople in group (or any subgroups)
          List<EPerson> epa = groupService.allMembers(context, mygroup);

          // there were reviewers, change the state
          //  and add them to the list
          createTasks(context, workflowItem, epa);
          workflowItemService.update(context, workflowItem);

          // email notification
          notifyGroupOfTask(context, workflowItem, mygroup, epa);
        } else {
          // no reviewers, skip ahead
          workflowItem.setState(WFSTATE_STEP1);
          archived = advance(context, workflowItem, null, true, false);
        }

        break;

      case WFSTATE_STEP1:

        // remove reviewers from tasklist
        // assign owner
        taskListItemService.deleteByWorkflowItem(context, workflowItem);
        workflowItem.setOwner(newowner);

        break;

      case WFSTATE_STEP2POOL:

        // clear owner
        // any approvers?
        // if so, add them to tasklist
        // if not, skip to next state
        workflowItem.setOwner(null);

        // get approvers (group 2)
        mygroup = collectionService.getWorkflowGroup(mycollection, 2);

        if ((mygroup != null) && !(groupService.isEmpty(mygroup))) {
          // get a list of all epeople in group (or any subgroups)
          List<EPerson> epa = groupService.allMembers(context, mygroup);

          // there were approvers, change the state
          //  timestamp, and add them to the list
          createTasks(context, workflowItem, epa);

          // email notification
          notifyGroupOfTask(context, workflowItem, mygroup, epa);
        } else {
          // no reviewers, skip ahead
          workflowItem.setState(WFSTATE_STEP2);
          archived = advance(context, workflowItem, null, true, false);
        }

        break;

      case WFSTATE_STEP2:

        // remove admins from tasklist
        // assign owner
        taskListItemService.deleteByWorkflowItem(context, workflowItem);
        workflowItem.setOwner(newowner);

        break;

      case WFSTATE_STEP3POOL:

        // any editors?
        // if so, add them to tasklist
        workflowItem.setOwner(null);
        mygroup = collectionService.getWorkflowGroup(mycollection, 3);

        if ((mygroup != null) && !(groupService.isEmpty(mygroup))) {
          // get a list of all epeople in group (or any subgroups)
          List<EPerson> epa = groupService.allMembers(context, mygroup);

          // there were editors, change the state
          //  timestamp, and add them to the list
          createTasks(context, workflowItem, epa);

          // email notification
          notifyGroupOfTask(context, workflowItem, mygroup, epa);
        } else {
          // no editors, skip ahead
          workflowItem.setState(WFSTATE_STEP3);
          archived = advance(context, workflowItem, null, true, false);
        }

        break;

      case WFSTATE_STEP3:

        // remove editors from tasklist
        // assign owner
        taskListItemService.deleteByWorkflowItem(context, workflowItem);
        workflowItem.setOwner(newowner);

        break;

      case WFSTATE_ARCHIVE:

        // put in archive in one transaction
        // remove workflow tasks
        taskListItemService.deleteByWorkflowItem(context, workflowItem);

        mycollection = workflowItem.getCollection();

        Item myitem = archive(context, workflowItem);

        // now email notification
        notifyOfArchive(context, myitem, mycollection);
        archived = true;

        break;
    }

    logWorkflowEvent(
        context,
        workflowItem.getItem(),
        workflowItem,
        context.getCurrentUser(),
        newstate,
        newowner,
        mycollection,
        oldState,
        mygroup);

    if (!archived) {
      workflowItemService.update(context, workflowItem);
    }

    return archived;
  }
  @Override
  public boolean advance(
      Context context, BasicWorkflowItem workflowItem, EPerson e, boolean curate, boolean record)
      throws SQLException, IOException, AuthorizeException {
    int taskstate = workflowItem.getState();
    boolean archived = false;

    // perform curation tasks if needed
    if (curate && workflowCuratorService.needsCuration(workflowItem)) {
      if (!workflowCuratorService.doCuration(context, workflowItem)) {
        // don't proceed - either curation tasks queued, or item rejected
        log.info(
            LogManager.getHeader(
                context,
                "advance_workflow",
                "workflow_item_id="
                    + workflowItem.getID()
                    + ",item_id="
                    + workflowItem.getItem().getID()
                    + ",collection_id="
                    + workflowItem.getCollection().getID()
                    + ",old_state="
                    + taskstate
                    + ",doCuration=false"));
        return false;
      }
    }

    switch (taskstate) {
      case WFSTATE_SUBMIT:
        archived = doState(context, workflowItem, WFSTATE_STEP1POOL, e);

        break;

      case WFSTATE_STEP1:

        // authorize DSpaceActions.SUBMIT_REVIEW
        // Record provenance
        if (record) {
          recordApproval(context, workflowItem, e);
        }
        archived = doState(context, workflowItem, WFSTATE_STEP2POOL, e);

        break;

      case WFSTATE_STEP2:

        // authorize DSpaceActions.SUBMIT_STEP2
        // Record provenance
        if (record) {
          recordApproval(context, workflowItem, e);
        }
        archived = doState(context, workflowItem, WFSTATE_STEP3POOL, e);

        break;

      case WFSTATE_STEP3:

        // authorize DSpaceActions.SUBMIT_STEP3
        // We don't record approval for editors, since they can't reject,
        // and thus didn't actually make a decision
        archived = doState(context, workflowItem, WFSTATE_ARCHIVE, e);

        break;

        // error handling? shouldn't get here
    }

    log.info(
        LogManager.getHeader(
            context,
            "advance_workflow",
            "workflow_item_id="
                + workflowItem.getID()
                + ",item_id="
                + workflowItem.getItem().getID()
                + ",collection_id="
                + workflowItem.getCollection().getID()
                + ",old_state="
                + taskstate
                + ",new_state="
                + workflowItem.getState()));
    return archived;
  }