@Override
 public void afterTaskDelegatedEvent(TaskEvent event) {
   String userId = "";
   Task ti = event.getTask();
   TaskPersistenceContext persistenceContext =
       ((TaskContext) event.getTaskContext()).getPersistenceContext();
   if (ti.getTaskData().getActualOwner() != null) {
     userId = ti.getTaskData().getActualOwner().getId();
   }
   persistenceContext.persist(
       new TaskEventImpl(
           ti.getId(),
           org.kie.internal.task.api.model.TaskEvent.TaskEventType.DELEGATED,
           userId,
           new Date()));
   UserAuditTask task = persistenceContext.find(UserAuditTaskImpl.class, ti.getId());
   if (task != null) {
     persistenceContext.remove(task);
   }
   StringBuilder sb = new StringBuilder();
   for (OrganizationalEntity o : ti.getPeopleAssignments().getPotentialOwners()) {
     sb.append(o.getId());
   }
   persistenceContext.persist(
       new GroupAuditTaskImpl(
           sb.toString(),
           ti.getId(),
           ti.getTaskData().getStatus().name(),
           ti.getTaskData().getActivationTime(),
           ti.getNames().get(0).getText(),
           (!ti.getDescriptions().isEmpty()) ? ti.getDescriptions().get(0).getText() : "",
           ti.getPriority(),
           (ti.getTaskData().getCreatedBy() == null)
               ? ""
               : ti.getTaskData().getCreatedBy().getId(),
           ti.getTaskData().getCreatedOn(),
           ti.getTaskData().getExpirationTime(),
           ti.getTaskData().getProcessInstanceId(),
           ti.getTaskData().getProcessId(),
           ti.getTaskData().getProcessSessionId(),
           ti.getTaskData().getParentId()));
 }
 @Override
 public void afterTaskClaimedEvent(TaskEvent event) {
   String userId = "";
   Task ti = event.getTask();
   TaskPersistenceContext persistenceContext =
       ((TaskContext) event.getTaskContext()).getPersistenceContext();
   if (ti.getTaskData().getActualOwner() != null) {
     userId = ti.getTaskData().getActualOwner().getId();
   }
   persistenceContext.persist(
       new TaskEventImpl(
           ti.getId(),
           org.kie.internal.task.api.model.TaskEvent.TaskEventType.CLAIMED,
           userId,
           new Date()));
   GroupAuditTaskImpl task = persistenceContext.find(GroupAuditTaskImpl.class, ti.getId());
   if (task != null) {
     persistenceContext.remove(task);
   }
   persistenceContext.persist(
       new UserAuditTaskImpl(
           userId,
           ti.getId(),
           ti.getTaskData().getStatus().name(),
           ti.getTaskData().getActivationTime(),
           ti.getNames().get(0).getText(),
           (!ti.getDescriptions().isEmpty()) ? ti.getDescriptions().get(0).getText() : "",
           ti.getPriority(),
           (ti.getTaskData().getCreatedBy() == null)
               ? ""
               : ti.getTaskData().getCreatedBy().getId(),
           ti.getTaskData().getCreatedOn(),
           ti.getTaskData().getExpirationTime(),
           ti.getTaskData().getProcessInstanceId(),
           ti.getTaskData().getProcessId(),
           ti.getTaskData().getProcessSessionId(),
           ti.getTaskData().getParentId()));
 }
 @Override
 public void afterTaskCompletedEvent(TaskEvent event) {
   String userId = "";
   Task ti = event.getTask();
   TaskPersistenceContext persistenceContext =
       ((TaskContext) event.getTaskContext()).getPersistenceContext();
   if (ti.getTaskData().getActualOwner() != null) {
     userId = ti.getTaskData().getActualOwner().getId();
   }
   persistenceContext.persist(
       new TaskEventImpl(
           ti.getId(),
           org.kie.internal.task.api.model.TaskEvent.TaskEventType.COMPLETED,
           userId,
           new Date()));
   UserAuditTask task = persistenceContext.find(UserAuditTaskImpl.class, ti.getId());
   if (task != null) {
     persistenceContext.remove(task);
     HistoryAuditTaskImpl historyAuditTaskImpl =
         new HistoryAuditTaskImpl(
             task.getActualOwner(),
             task.getTaskId(),
             ti.getTaskData().getStatus().name(),
             task.getActivationTime(),
             task.getName(),
             task.getDescription(),
             task.getPriority(),
             task.getCreatedBy(),
             task.getCreatedOn(),
             task.getDueDate(),
             task.getProcessInstanceId(),
             task.getProcessId(),
             task.getProcessSessionId(),
             task.getParentId());
     persistenceContext.persist(historyAuditTaskImpl);
   }
 }