public KaleoTaskInstanceToken addKaleoTaskInstanceToken(
      long kaleoInstanceTokenId,
      long kaleoTaskId,
      String kaleoTaskName,
      Collection<KaleoTaskAssignment> kaleoTaskAssignments,
      Date dueDate,
      Map<String, Serializable> workflowContext,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    KaleoInstanceToken kaleoInstanceToken =
        kaleoInstanceTokenPersistence.findByPrimaryKey(kaleoInstanceTokenId);

    User user = userPersistence.findByPrimaryKey(serviceContext.getGuestOrUserId());
    Date now = new Date();

    long kaleoTaskInstanceTokenId = counterLocalService.increment();

    KaleoTaskInstanceToken kaleoTaskInstanceToken =
        kaleoTaskInstanceTokenPersistence.create(kaleoTaskInstanceTokenId);

    long groupId = StagingUtil.getLiveGroupId(serviceContext.getScopeGroupId());

    kaleoTaskInstanceToken.setGroupId(groupId);

    kaleoTaskInstanceToken.setCompanyId(user.getCompanyId());
    kaleoTaskInstanceToken.setUserId(user.getUserId());
    kaleoTaskInstanceToken.setUserName(user.getFullName());
    kaleoTaskInstanceToken.setCreateDate(now);
    kaleoTaskInstanceToken.setModifiedDate(now);
    kaleoTaskInstanceToken.setDueDate(dueDate);
    kaleoTaskInstanceToken.setKaleoDefinitionId(kaleoInstanceToken.getKaleoDefinitionId());
    kaleoTaskInstanceToken.setKaleoInstanceId(kaleoInstanceToken.getKaleoInstanceId());
    kaleoTaskInstanceToken.setKaleoInstanceTokenId(kaleoInstanceTokenId);

    kaleoTaskInstanceToken.setKaleoTaskId(kaleoTaskId);
    kaleoTaskInstanceToken.setKaleoTaskName(kaleoTaskName);

    if (workflowContext != null) {
      kaleoTaskInstanceToken.setClassName(
          (String) workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME));

      if (workflowContext.containsKey(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK)) {

        kaleoTaskInstanceToken.setClassPK(
            GetterUtil.getLong(
                (String) workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK)));
      }
    }

    kaleoTaskInstanceToken.setCompleted(false);
    kaleoTaskInstanceToken.setWorkflowContext(WorkflowContextUtil.convert(workflowContext));

    kaleoTaskInstanceTokenPersistence.update(kaleoTaskInstanceToken, false);

    kaleoTaskAssignmentInstanceLocalService.addTaskAssignmentInstances(
        kaleoTaskInstanceToken, kaleoTaskAssignments, workflowContext, serviceContext);

    return kaleoTaskInstanceToken;
  }
  protected KaleoTaskInstanceToken addKaleoTaskInstanceToken() throws Exception {
    long pk = RandomTestUtil.nextLong();

    KaleoTaskInstanceToken kaleoTaskInstanceToken = _persistence.create(pk);

    kaleoTaskInstanceToken.setGroupId(RandomTestUtil.nextLong());

    kaleoTaskInstanceToken.setCompanyId(RandomTestUtil.nextLong());

    kaleoTaskInstanceToken.setUserId(RandomTestUtil.nextLong());

    kaleoTaskInstanceToken.setUserName(RandomTestUtil.randomString());

    kaleoTaskInstanceToken.setCreateDate(RandomTestUtil.nextDate());

    kaleoTaskInstanceToken.setModifiedDate(RandomTestUtil.nextDate());

    kaleoTaskInstanceToken.setKaleoDefinitionId(RandomTestUtil.nextLong());

    kaleoTaskInstanceToken.setKaleoInstanceId(RandomTestUtil.nextLong());

    kaleoTaskInstanceToken.setKaleoInstanceTokenId(RandomTestUtil.nextLong());

    kaleoTaskInstanceToken.setKaleoTaskId(RandomTestUtil.nextLong());

    kaleoTaskInstanceToken.setKaleoTaskName(RandomTestUtil.randomString());

    kaleoTaskInstanceToken.setClassName(RandomTestUtil.randomString());

    kaleoTaskInstanceToken.setClassPK(RandomTestUtil.nextLong());

    kaleoTaskInstanceToken.setCompletionUserId(RandomTestUtil.nextLong());

    kaleoTaskInstanceToken.setCompleted(RandomTestUtil.randomBoolean());

    kaleoTaskInstanceToken.setCompletionDate(RandomTestUtil.nextDate());

    kaleoTaskInstanceToken.setDueDate(RandomTestUtil.nextDate());

    kaleoTaskInstanceToken.setWorkflowContext(RandomTestUtil.randomString());

    _kaleoTaskInstanceTokens.add(_persistence.update(kaleoTaskInstanceToken));

    return kaleoTaskInstanceToken;
  }
  public KaleoTaskInstanceToken updateDueDate(
      long kaleoTaskInstanceTokenId, Date dueDate, ServiceContext serviceContext)
      throws PortalException, SystemException {

    KaleoTaskInstanceToken kaleoTaskInstance =
        kaleoTaskInstanceTokenPersistence.findByPrimaryKey(kaleoTaskInstanceTokenId);

    kaleoTaskInstance.setModifiedDate(new Date());

    if (dueDate != null) {
      Calendar cal = CalendarFactoryUtil.getCalendar(LocaleUtil.getDefault());

      cal.setTime(dueDate);

      kaleoTaskInstance.setDueDate(cal.getTime());
    }

    kaleoTaskInstanceTokenPersistence.update(kaleoTaskInstance, false);

    return kaleoTaskInstance;
  }
  public KaleoTaskInstanceToken assignKaleoTaskInstanceToken(
      long kaleoTaskInstanceTokenId,
      String assigneeClassName,
      long assigneeClassPK,
      Map<String, Serializable> workflowContext,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    KaleoTaskInstanceToken kaleoTaskInstanceToken =
        kaleoTaskInstanceTokenPersistence.findByPrimaryKey(kaleoTaskInstanceTokenId);

    kaleoTaskInstanceToken.setModifiedDate(new Date());
    kaleoTaskInstanceToken.setWorkflowContext(WorkflowContextUtil.convert(workflowContext));

    kaleoTaskInstanceTokenPersistence.update(kaleoTaskInstanceToken, false);

    kaleoTaskAssignmentInstanceLocalService.assignKaleoTaskAssignmentInstance(
        kaleoTaskInstanceToken, assigneeClassName,
        assigneeClassPK, serviceContext);

    return kaleoTaskInstanceToken;
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    KaleoTaskInstanceToken newKaleoTaskInstanceToken = _persistence.create(pk);

    newKaleoTaskInstanceToken.setGroupId(RandomTestUtil.nextLong());

    newKaleoTaskInstanceToken.setCompanyId(RandomTestUtil.nextLong());

    newKaleoTaskInstanceToken.setUserId(RandomTestUtil.nextLong());

    newKaleoTaskInstanceToken.setUserName(RandomTestUtil.randomString());

    newKaleoTaskInstanceToken.setCreateDate(RandomTestUtil.nextDate());

    newKaleoTaskInstanceToken.setModifiedDate(RandomTestUtil.nextDate());

    newKaleoTaskInstanceToken.setKaleoDefinitionId(RandomTestUtil.nextLong());

    newKaleoTaskInstanceToken.setKaleoInstanceId(RandomTestUtil.nextLong());

    newKaleoTaskInstanceToken.setKaleoInstanceTokenId(RandomTestUtil.nextLong());

    newKaleoTaskInstanceToken.setKaleoTaskId(RandomTestUtil.nextLong());

    newKaleoTaskInstanceToken.setKaleoTaskName(RandomTestUtil.randomString());

    newKaleoTaskInstanceToken.setClassName(RandomTestUtil.randomString());

    newKaleoTaskInstanceToken.setClassPK(RandomTestUtil.nextLong());

    newKaleoTaskInstanceToken.setCompletionUserId(RandomTestUtil.nextLong());

    newKaleoTaskInstanceToken.setCompleted(RandomTestUtil.randomBoolean());

    newKaleoTaskInstanceToken.setCompletionDate(RandomTestUtil.nextDate());

    newKaleoTaskInstanceToken.setDueDate(RandomTestUtil.nextDate());

    newKaleoTaskInstanceToken.setWorkflowContext(RandomTestUtil.randomString());

    _kaleoTaskInstanceTokens.add(_persistence.update(newKaleoTaskInstanceToken));

    KaleoTaskInstanceToken existingKaleoTaskInstanceToken =
        _persistence.findByPrimaryKey(newKaleoTaskInstanceToken.getPrimaryKey());

    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getKaleoTaskInstanceTokenId(),
        newKaleoTaskInstanceToken.getKaleoTaskInstanceTokenId());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getGroupId(), newKaleoTaskInstanceToken.getGroupId());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getCompanyId(), newKaleoTaskInstanceToken.getCompanyId());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getUserId(), newKaleoTaskInstanceToken.getUserId());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getUserName(), newKaleoTaskInstanceToken.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingKaleoTaskInstanceToken.getCreateDate()),
        Time.getShortTimestamp(newKaleoTaskInstanceToken.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingKaleoTaskInstanceToken.getModifiedDate()),
        Time.getShortTimestamp(newKaleoTaskInstanceToken.getModifiedDate()));
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getKaleoDefinitionId(),
        newKaleoTaskInstanceToken.getKaleoDefinitionId());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getKaleoInstanceId(),
        newKaleoTaskInstanceToken.getKaleoInstanceId());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getKaleoInstanceTokenId(),
        newKaleoTaskInstanceToken.getKaleoInstanceTokenId());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getKaleoTaskId(),
        newKaleoTaskInstanceToken.getKaleoTaskId());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getKaleoTaskName(),
        newKaleoTaskInstanceToken.getKaleoTaskName());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getClassName(), newKaleoTaskInstanceToken.getClassName());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getClassPK(), newKaleoTaskInstanceToken.getClassPK());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getCompletionUserId(),
        newKaleoTaskInstanceToken.getCompletionUserId());
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getCompleted(), newKaleoTaskInstanceToken.getCompleted());
    Assert.assertEquals(
        Time.getShortTimestamp(existingKaleoTaskInstanceToken.getCompletionDate()),
        Time.getShortTimestamp(newKaleoTaskInstanceToken.getCompletionDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingKaleoTaskInstanceToken.getDueDate()),
        Time.getShortTimestamp(newKaleoTaskInstanceToken.getDueDate()));
    Assert.assertEquals(
        existingKaleoTaskInstanceToken.getWorkflowContext(),
        newKaleoTaskInstanceToken.getWorkflowContext());
  }