// 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;
  }
  private void doCallbackOperationForTaskDeadlines(Deadlines deadlines) {
    if (deadlines != null) {
      if (deadlines.getStartDeadlines() != null) {
        List<Deadline> startDeadlines = deadlines.getStartDeadlines();
        for (Deadline startDeadline : startDeadlines) {
          List<Escalation> escalations = startDeadline.getEscalations();
          if (escalations != null) {
            for (Escalation escalation : escalations) {
              List<Notification> notifications = escalation.getNotifications();
              List<Reassignment> ressignments = escalation.getReassignments();
              if (notifications != null) {
                for (Notification notification : notifications) {
                  List<OrganizationalEntity> recipients = notification.getRecipients();
                  if (recipients != null) {
                    for (OrganizationalEntity recipient : recipients) {
                      if (recipient instanceof User) {
                        doCallbackUserOperation(recipient.getId());
                      }
                      if (recipient instanceof Group) {
                        doCallbackGroupOperation(recipient.getId());
                      }
                    }
                  }
                  List<OrganizationalEntity> administrators =
                      notification.getBusinessAdministrators();
                  if (administrators != null) {
                    for (OrganizationalEntity administrator : administrators) {
                      if (administrator instanceof User) {
                        doCallbackUserOperation(administrator.getId());
                      }
                      if (administrator instanceof Group) {
                        doCallbackGroupOperation(administrator.getId());
                      }
                    }
                  }
                }
              }
              if (ressignments != null) {
                for (Reassignment reassignment : ressignments) {
                  List<OrganizationalEntity> potentialOwners = reassignment.getPotentialOwners();
                  if (potentialOwners != null) {
                    for (OrganizationalEntity potentialOwner : potentialOwners) {
                      if (potentialOwner instanceof User) {
                        doCallbackUserOperation(potentialOwner.getId());
                      }
                      if (potentialOwner instanceof Group) {
                        doCallbackGroupOperation(potentialOwner.getId());
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }

      if (deadlines.getEndDeadlines() != null) {
        List<Deadline> endDeadlines = deadlines.getEndDeadlines();
        for (Deadline endDeadline : endDeadlines) {
          List<Escalation> escalations = endDeadline.getEscalations();
          if (escalations != null) {
            for (Escalation escalation : escalations) {
              List<Notification> notifications = escalation.getNotifications();
              List<Reassignment> ressignments = escalation.getReassignments();
              if (notifications != null) {
                for (Notification notification : notifications) {
                  List<OrganizationalEntity> recipients = notification.getRecipients();
                  if (recipients != null) {
                    for (OrganizationalEntity recipient : recipients) {
                      if (recipient instanceof User) {
                        doCallbackUserOperation(recipient.getId());
                      }
                      if (recipient instanceof Group) {
                        doCallbackGroupOperation(recipient.getId());
                      }
                    }
                  }
                  List<OrganizationalEntity> administrators =
                      notification.getBusinessAdministrators();
                  if (administrators != null) {
                    for (OrganizationalEntity administrator : administrators) {
                      if (administrator instanceof User) {
                        doCallbackUserOperation(administrator.getId());
                      }
                      if (administrator instanceof Group) {
                        doCallbackGroupOperation(administrator.getId());
                      }
                    }
                  }
                }
              }
              if (ressignments != null) {
                for (Reassignment reassignment : ressignments) {
                  List<OrganizationalEntity> potentialOwners = reassignment.getPotentialOwners();
                  if (potentialOwners != null) {
                    for (OrganizationalEntity potentialOwner : potentialOwners) {
                      if (potentialOwner instanceof User) {
                        doCallbackUserOperation(potentialOwner.getId());
                      }
                      if (potentialOwner instanceof Group) {
                        doCallbackGroupOperation(potentialOwner.getId());
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }