@Override public Void execute(Context context) { TaskContext ctx = (TaskContext) context; TaskPersistenceContext persistenceContext = ctx.getPersistenceContext(); TaskDeadlinesService deadlineService = ctx.getTaskDeadlinesService(); try { long now = System.currentTimeMillis(); List<DeadlineSummary> resultList = persistenceContext.queryInTransaction( "UnescalatedStartDeadlines", ClassUtil.<List<DeadlineSummary>>castClass(List.class)); for (DeadlineSummary summary : resultList) { long delay = summary.getDate().getTime() - now; deadlineService.schedule( summary.getTaskId(), summary.getDeadlineId(), delay, DeadlineType.START); } resultList = persistenceContext.queryInTransaction( "UnescalatedEndDeadlines", ClassUtil.<List<DeadlineSummary>>castClass(List.class)); for (DeadlineSummary summary : resultList) { long delay = summary.getDate().getTime() - now; deadlineService.schedule( summary.getTaskId(), summary.getDeadlineId(), delay, DeadlineType.END); } } catch (Exception e) { logger.error("Error when executing deadlines", e); } return null; }
@Override public void afterTaskAddedEvent(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 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())); } else if (!ti.getPeopleAssignments().getPotentialOwners().isEmpty()) { StringBuilder sb = new StringBuilder(); for (OrganizationalEntity o : ti.getPeopleAssignments().getPotentialOwners()) { sb.append(o.getId()).append("|"); } 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())); } persistenceContext.persist( new TaskEventImpl( ti.getId(), org.kie.internal.task.api.model.TaskEvent.TaskEventType.ADDED, userId, new Date())); }
@Override public Context getContext() { final TaskPersistenceContext persistenceContext = tpm.getPersistenceContext(); persistenceContext.joinTransaction(); return new TaskContext() { @Override public void set(String identifier, Object value) { txm.putResource(identifier, value); } @Override public void remove(String identifier) {} @Override public String getName() { return null; } @Override public World getContextManager() { return null; } @Override public Object get(String identifier) { return txm.getResource(identifier); } @Override public void setPersistenceContext(TaskPersistenceContext context) {} @Override public TaskPersistenceContext getPersistenceContext() { return persistenceContext; } @Override public UserGroupCallback getUserGroupCallback() { return null; } @Override public Task loadTaskVariables(Task task) { return task; } }; }
public List<Content> getAllContentByTaskId(long taskId) { Task task = persistenceContext.findTask(taskId); long inputContentId = task.getTaskData().getDocumentContentId(); long outputContentId = task.getTaskData().getOutputContentId(); long faultContentId = task.getTaskData().getFaultContentId(); List<Content> allContent = new ArrayList<Content>(); allContent.add(persistenceContext.findContent(inputContentId)); allContent.add(persistenceContext.findContent(outputContentId)); allContent.add(persistenceContext.findContent(faultContentId)); return allContent; }
@SuppressWarnings("unchecked") private Map<String, Object> getVariables( TaskContext ctx, TaskPersistenceContext persistenceContext, Task task, TaskData taskData) { Map<String, Object> variables; Content content = persistenceContext.findContent(taskData.getDocumentContentId()); if (content != null) { ContentMarshallerContext mContext = ctx.getTaskContentService().getMarshallerContext(task); Object objectFromBytes = ContentMarshallerHelper.unmarshall( content.getContent(), mContext.getEnvironment(), mContext.getClassloader()); if (objectFromBytes instanceof Map) { variables = (Map<String, Object>) objectFromBytes; } else { variables = new HashMap<String, Object>(); variables.put("content", objectFromBytes); } } else { variables = Collections.emptyMap(); } return variables; }
private Void executedeadLine( TaskContext ctx, TaskPersistenceContext persistenceContext, Task task, DeadlineSummaryImpl deadlineSummaryImpl, TaskData taskData) { Deadline deadline = persistenceContext.findDeadline(deadlineSummaryImpl.getDeadlineId()); if (task == null || deadline == null) { return null; } if (taskData != null) { // check if task is still in valid status if (DeadlineType.START.isValidStatus(taskData.getStatus()) || DeadlineType.END.isValidStatus(taskData.getStatus())) { Map<String, Object> variables = getVariables(ctx, persistenceContext, task, taskData); if (deadline == null || deadline.getEscalations() == null) { return null; } for (Escalation escalation : deadline.getEscalations()) { for (Notification notification : escalation.getNotifications()) { if (notification.getNotificationType() == NotificationType.Email) { logger.debug("Sending an Email"); notificationListener.onNotification( new NotificationEvent(notification, task, variables)); } } } } } return null; }
@Override public void afterTaskActivatedEvent(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.ACTIVATED, userId, new Date())); UserAuditTask task = persistenceContext.find(UserAuditTaskImpl.class, ti.getId()); task.setStatus(ti.getTaskData().getStatus().name()); persistenceContext.persist(task); }
private List<DeadlineSummaryImpl> getAlldeadlines( TaskPersistenceContext persistenceContext, TaskData taskData) { List<DeadlineSummaryImpl> resultList; // get no-completed notification if (Status.InProgress == taskData.getStatus() || Status.Suspended == taskData.getStatus()) { resultList = persistenceContext.queryWithParametersInTransaction( "UnescalatedEndDeadlinesByTaskIdForReminder", persistenceContext.addParametersToMap("taskId", taskId), ClassUtil.<List<DeadlineSummaryImpl>>castClass(List.class)); } else { // get no-started notification resultList = persistenceContext.queryWithParametersInTransaction( "UnescalatedStartDeadlinesByTaskIdForReminder", persistenceContext.addParametersToMap("taskId", taskId), ClassUtil.<List<DeadlineSummaryImpl>>castClass(List.class)); } return resultList; }
@Override public Void execute(Context context) { TaskContext ctx = (TaskContext) context; if (notificationListener == null) { this.notificationListener = new EmailNotificationListener((UserInfo) context.get(EnvironmentName.TASK_USER_INFO)); } TaskPersistenceContext persistenceContext = ctx.getPersistenceContext(); try { Task task = persistenceContext.findTask(taskId); TaskData taskData = task.getTaskData(); List<DeadlineSummaryImpl> resultList = null; resultList = getAlldeadlines(persistenceContext, taskData); if (resultList == null || resultList.size() == 0) { if (taskData.getActualOwner() == null) return null; if (taskData != null) { // check if task is still in valid status if (DeadlineType.START.isValidStatus(taskData.getStatus()) || DeadlineType.END.isValidStatus(taskData.getStatus())) { logger.debug("Sending an Email"); Map<String, Object> variables = getVariables(ctx, persistenceContext, task, taskData); Notification notification = buildDefaultNotification(taskData, task); notificationListener.onNotification( new NotificationEvent(notification, task, variables)); } } } else { for (DeadlineSummaryImpl deadlineSummaryImpl : resultList) { executedeadLine(ctx, persistenceContext, task, deadlineSummaryImpl, taskData); } } } catch (Exception e) { logger.error("Error when executing deadlines", e); } return null; }
@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); } }
@SuppressWarnings("unchecked") public long addOutputContent(long taskId, Map<String, Object> params) { Task task = persistenceContext.findTask(taskId); long outputContentId = task.getTaskData().getOutputContentId(); Content outputContent = persistenceContext.findContent(outputContentId); long contentId = -1; if (outputContent == null) { ContentMarshallerContext context = getMarshallerContext(task); ContentData outputContentData = ContentMarshallerHelper.marshal(task, params, context.getEnvironment()); Content content = TaskModelProvider.getFactory().newContent(); ((InternalContent) content).setContent(outputContentData.getContent()); persistenceContext.persistContent(content); ((InternalTaskData) task.getTaskData()).setOutput(content.getId(), outputContentData); contentId = content.getId(); } else { // I need to merge it if it already exist ContentMarshallerContext context = getMarshallerContext(task); Object unmarshalledObject = ContentMarshallerHelper.unmarshall( outputContent.getContent(), context.getEnvironment(), context.getClassloader()); if (unmarshalledObject != null && unmarshalledObject instanceof Map) { ((Map<String, Object>) unmarshalledObject).putAll(params); } ContentData outputContentData = ContentMarshallerHelper.marshal(task, unmarshalledObject, context.getEnvironment()); ((InternalContent) outputContent).setContent(outputContentData.getContent()); persistenceContext.persistContent(outputContent); contentId = outputContentId; } ((InternalTaskData) task.getTaskData()).setTaskOutputVariables(params); taskEventSupport.fireAfterTaskOutputVariablesChanged(task, context, params); return contentId; }
public void taskOperation( final Operation operation, final long taskId, final String userId, final String targetEntityId, final Map<String, Object> data, List<String> groupIds, OrganizationalEntity... entities) throws TaskException { try { final List<OperationCommand> commands = operations.get(operation); Task task = persistenceContext.findTask(taskId); if (task == null) { String errorMessage = "Task '" + taskId + "' not found"; throw new PermissionDeniedException(errorMessage); } User user = persistenceContext.findUser(userId); OrganizationalEntity targetEntity = null; if (targetEntityId != null && !targetEntityId.equals("")) { targetEntity = persistenceContext.findOrgEntity(targetEntityId); } switch (operation) { case Activate: { taskEventSupport.fireBeforeTaskActivated(task, context); break; } case Claim: { taskEventSupport.fireBeforeTaskClaimed(task, context); break; } case Complete: { taskEventSupport.fireBeforeTaskCompleted(task, context); break; } case Delegate: { taskEventSupport.fireBeforeTaskDelegated(task, context); break; } case Exit: { taskEventSupport.fireBeforeTaskExited(task, context); break; } case Fail: { if (data != null) { FaultData faultData = ContentMarshallerHelper.marshalFault(data, null); Content content = TaskModelProvider.getFactory().newContent(); ((InternalContent) content).setContent(faultData.getContent()); persistenceContext.persistContent(content); ((InternalTaskData) task.getTaskData()).setFault(content.getId(), faultData); } taskEventSupport.fireBeforeTaskFailed(task, context); break; } case Forward: { taskEventSupport.fireBeforeTaskForwarded(task, context); break; } case Nominate: { taskEventSupport.fireBeforeTaskNominated(task, context); break; } case Release: { taskEventSupport.fireBeforeTaskReleased(task, context); break; } case Resume: { taskEventSupport.fireBeforeTaskResumed(task, context); break; } case Skip: { taskEventSupport.fireBeforeTaskSkipped(task, context); break; } case Start: { taskEventSupport.fireBeforeTaskStarted(task, context); break; } case Stop: { taskEventSupport.fireBeforeTaskStopped(task, context); break; } case Suspend: { taskEventSupport.fireBeforeTaskSuspended(task, context); break; } } evalCommand(operation, commands, task, user, targetEntity, groupIds, entities); switch (operation) { case Activate: { taskEventSupport.fireAfterTaskActivated(task, context); break; } case Claim: { taskEventSupport.fireAfterTaskClaimed(task, context); break; } case Complete: { if (data != null) { taskContentService.addOutputContent(taskId, data); } taskEventSupport.fireAfterTaskCompleted(task, context); break; } case Delegate: { // This is a really bad hack to execut the correct behavior ((InternalTaskData) task.getTaskData()).setStatus(Status.Reserved); taskEventSupport.fireAfterTaskDelegated(task, context); break; } case Exit: { taskEventSupport.fireAfterTaskExited(task, context); break; } case Fail: { taskEventSupport.fireAfterTaskFailed(task, context); break; } case Forward: { taskEventSupport.fireAfterTaskForwarded(task, context); break; } case Nominate: { taskEventSupport.fireAfterTaskNominated(task, context); break; } case Release: { taskEventSupport.fireAfterTaskReleased(task, context); break; } case Resume: { taskEventSupport.fireAfterTaskResumed(task, context); break; } case Start: { taskEventSupport.fireAfterTaskStarted(task, context); break; } case Skip: { taskEventSupport.fireAfterTaskSkipped(task, context); break; } case Stop: { taskEventSupport.fireAfterTaskStopped(task, context); break; } case Suspend: { taskEventSupport.fireAfterTaskSuspended(task, context); break; } } } catch (RuntimeException re) { throw re; } }
public Content getContentById(long contentId) { return persistenceContext.findContent(contentId); }
public void deleteDocumentContent(long taskId, long contentId) { Task task = persistenceContext.findTask(taskId); ((InternalTaskData) task.getTaskData()).setDocumentContentId(-1); Content content = persistenceContext.findContent(contentId); persistenceContext.removeContent(content); }
// TODO: if there's an existing document content entity, we lose all link to that through this! public long setDocumentContent(long taskId, Content content) { Task task = persistenceContext.findTask(taskId); persistenceContext.persistContent(content); ((InternalTaskData) task.getTaskData()).setDocumentContentId(content.getId()); return content.getId(); }