/**
   * Publishes a message to qpid that describes what action a user has just performed on a task.
   * These actions may be either TaskTaken or TaskCompleted. This calls checkInitialized to see if
   * there is a valid connection to qpid.
   *
   * @param task the task that has been acted upon
   * @param exchangeName the exchange name for messaging
   * @param eventName the event that correlates with the action
   * @param taskId the id of the task that was acted upon
   */
  public void amqpTaskPublish(Task task, String exchangeName, String eventName, String taskId) {
    String info = "";
    if (checkInitialized()) {
      if (eventName.equals("TaskTaken")) {

        try {
          info =
              "eventType="
                  + eventName
                  + ";"
                  + "processID="
                  + task.getProcessInstanceId()
                  + ";"
                  + "taskID="
                  + task.getId()
                  + ";"
                  + "assignee="
                  + task.getAssignee()
                  + ";";
        } catch (Exception e) {
          log.debug(e.getMessage());
        }
      } else if (eventName.equals("TaskCompleted")) {
        try {
          org.jbpm.task.Task ht = taskRepo.findById(Long.decode(taskId));
          String processName = ht.getTaskData().getProcessId();
          String processId = Long.toString(ht.getTaskData().getProcessInstanceId());
          String assignee = ht.getTaskData().getActualOwner().getId();
          info =
              "eventType="
                  + eventName
                  + ";"
                  + "processID="
                  + processName
                  + "."
                  + processId
                  + ";"
                  + "taskID="
                  + taskId
                  + ";"
                  + "assignee="
                  + assignee
                  + ";";
        } catch (Exception e) {
          log.debug(e.getMessage());
        }
      }
      sendMessage(info, exchangeName);
    }
  }
  // TODO: Check if you can update a task. Can you update task by just adding a task with the same
  // ID?
  private org.jbpm.task.Task createOrUpdateTask(Task source) {

    org.jbpm.task.Task target;
    boolean newTask = false;
    if (source.getId() == null) {
      newTask = true;
      target = new org.jbpm.task.Task();
    } else {
      BlockingGetTaskResponseHandler getTaskResponseHandler = new BlockingGetTaskResponseHandler();
      taskClient.getTask(Long.valueOf(source.getId()), getTaskResponseHandler);
      target = getTaskResponseHandler.getTask();
    }
    if (target == null) {
      return null;
    }
    if (source.getAssignee() != null) {
      PeopleAssignments pa = new PeopleAssignments();
      List<OrganizationalEntity> orgEnt = new ArrayList<OrganizationalEntity>();
      org.jbpm.task.User oe = new org.jbpm.task.User();
      oe.setId(source.getAssignee());
      pa.setTaskInitiator(oe);
      orgEnt.add(oe);
      pa.setPotentialOwners(orgEnt);
      target.setPeopleAssignments(pa);
    }
    if (source.getDescription() != null) {
      List<I18NText> desc = new ArrayList<I18NText>();
      desc.add(new I18NText("en-UK", source.getDescription()));
      target.setDescriptions(desc);
    }
    if (source.getDueDate() != null) {
      Deadlines deadlines = new Deadlines();
      List<Deadline> dls = new ArrayList<Deadline>();
      Deadline dl = new Deadline();
      dl.setDate(this.convert(source.getDueDate()));
      dls.add(dl);
      deadlines.setEndDeadlines(dls);
      target.setDeadlines(deadlines);
    }
    if (source.getName() != null) {
      List<I18NText> names = new ArrayList<I18NText>();
      names.add(new I18NText("en-UK", source.getName()));
      target.setNames(names);
    }
    if (source.getPriority() != null) {
      target.setPriority(source.getPriority());
    }

    TaskData td = new TaskData();
    target.setTaskData(td);
    /*
     * if (source.getProgress() != null) {
     * target.setProgress(source.getProgress()); }
     */

    // convert variables
    /*
     * if (source.getVariables() != null &&
     * source.getVariables().getVariables().size() > 0) { Map<String,
     * Object> variables = new HashMap<String, Object>(); for (Variable
     * variable : source.getVariables().getVariables()) {
     * variables.put(variable.getName(), variable.getValue()); }
     * this.taskService.setVariables(target.getId(), variables); }
     */

    if (newTask) {
      BlockingAddTaskResponseHandler addTaskResponseHandler = new BlockingAddTaskResponseHandler();
      taskClient.addTask(target, null, addTaskResponseHandler);
    }

    return target;
  }