protected void getAllTasks() { error = null; errorMessage = null; userTasks = new ArrayList<>(); CoreSession coreSession = getCoreSession(); boolean filterTrashDocs = getFilterDocumentsInTrash(); NuxeoPrincipal pal = (NuxeoPrincipal) coreSession.getPrincipal(); TaskService taskService = Framework.getService(TaskService.class); List<Task> tasks = taskService.getAllCurrentTaskInstances(coreSession, getSortInfos()); if (tasks != null) { for (Task task : tasks) { List<String> targetDocumentsIds = task.getTargetDocumentsIds(); boolean hasTargetDocuments = targetDocumentsIds != null && !targetDocumentsIds.isEmpty(); if (task.hasEnded() || task.isCancelled() || !hasTargetDocuments) { continue; } DocumentModel doc = taskService.getTargetDocumentModel(task, coreSession); if (doc != null) { if (filterTrashDocs && LifeCycleConstants.DELETED_STATE.equals(doc.getCurrentLifeCycleState())) { continue; } else { userTasks.add(new DashBoardItemImpl(task, doc, getLocale())); } } else { log.warn( String.format( "User '%s' has a task of type '%s' on a missing or deleted document", pal.getName(), task.getName())); } } } }
@Override public int compare(Task o1, Task o2) { try { // return o1.getCreated().compareTo(o2.getCreated()); // stupid MySQL doesn't have subsecond resolution // sorting by first actor is enough for this test String a1 = o1.getActors().get(0); String a2 = o2.getActors().get(0); return a1.compareTo(a2); } catch (ClientException e) { throw new ClientRuntimeException(); } }
@Override public String endTask( CoreSession coreSession, NuxeoPrincipal principal, Task task, String comment, String eventName, boolean isValidated) throws ClientException { // put user comment on the task if (!StringUtils.isEmpty(comment)) { task.addComment(principal.getName(), comment); } // end the task, adding boolean marker that task was validated or // rejected task.setVariable(TaskService.VariableName.validated.name(), String.valueOf(isValidated)); task.end(coreSession); coreSession.saveDocument(task.getDocument()); // notify Map<String, Serializable> eventProperties = new HashMap<String, Serializable>(); ArrayList<String> notificationRecipients = new ArrayList<String>(); notificationRecipients.add(task.getInitiator()); notificationRecipients.addAll(task.getActors()); eventProperties.put(NotificationConstants.RECIPIENTS_KEY, notificationRecipients); // try to resolve document when notifying DocumentModel document = null; String docId = task.getVariable(TaskService.VariableName.documentId.name()); String docRepo = task.getVariable(TaskService.VariableName.documentRepositoryName.name()); if (coreSession.getRepositoryName().equals(docRepo)) { try { document = coreSession.getDocument(new IdRef(docId)); } catch (Exception e) { log.error( String.format( "Could not fetch document with id '%s:%s' for notification", docRepo, docId), e); } } else { log.error( String.format( "Could not resolve document for notification: " + "document is on repository '%s' and given session is on " + "repository '%s'", docRepo, coreSession.getRepositoryName())); } TaskEventNotificationHelper.notifyEvent( coreSession, document, principal, task, eventName, eventProperties, comment, null); String seamEventName = isValidated ? TaskEventNames.WORKFLOW_TASK_COMPLETED : TaskEventNames.WORKFLOW_TASK_REJECTED; return seamEventName; }
@Override public void handleEvent(Event event) { EventContext eventContext = event.getContext(); Serializable property = eventContext.getProperty(TaskService.TASK_INSTANCE_EVENT_PROPERTIES_KEY); if (property == null || !(property instanceof Task)) { // do nothing return; } Task task = (Task) property; Boolean validated = Boolean.valueOf(task.getVariable(TaskService.VariableName.validated.name())); String chain; if (validated) { chain = task.getVariable(OperationTaskVariableName.acceptOperationChain.name()); } else { chain = task.getVariable(OperationTaskVariableName.rejectOperationChain.name()); } if (!StringUtils.isEmpty(chain)) { try { // run the given operation AutomationService os = Framework.getService(AutomationService.class); OperationContext ctx = new OperationContext(eventContext.getCoreSession()); if (eventContext instanceof DocumentEventContext) { ctx.setInput(((DocumentEventContext) eventContext).getSourceDocument()); ctx.put(OperationTaskVariableName.taskDocument.name(), task.getDocument()); } try { os.run(ctx, chain); } catch (InvalidChainException e) { log.error("Unknown chain: " + chain); } } catch (OperationException t) { log.error(t, t); } } }
public static void notifyEvent( CoreSession coreSession, DocumentModel document, NuxeoPrincipal principal, Task task, String eventId, Map<String, Serializable> properties, String comment, String category) throws ClientException { // Default category if (category == null) { category = DocumentEventCategories.EVENT_DOCUMENT_CATEGORY; } if (properties == null) { properties = new HashMap<String, Serializable>(); } EventContext eventContext = null; if (document != null) { properties.put(CoreEventConstants.REPOSITORY_NAME, document.getRepositoryName()); properties.put(CoreEventConstants.SESSION_ID, coreSession.getSessionId()); properties.put(CoreEventConstants.DOC_LIFE_CYCLE, document.getCurrentLifeCycleState()); eventContext = new DocumentEventContext(coreSession, principal, document); } else { eventContext = new EventContextImpl(coreSession, principal); } properties.put(DocumentEventContext.COMMENT_PROPERTY_KEY, comment); properties.put(DocumentEventContext.CATEGORY_PROPERTY_KEY, category); properties.put(TaskService.TASK_INSTANCE_EVENT_PROPERTIES_KEY, task); String disableNotif = task.getVariable(TaskEventNames.DISABLE_NOTIFICATION_SERVICE); if (disableNotif != null && Boolean.TRUE.equals(Boolean.valueOf(disableNotif))) { properties.put(TaskEventNames.DISABLE_NOTIFICATION_SERVICE, Boolean.TRUE); } eventContext.setProperties(properties); Event event = eventContext.newEvent(eventId); getEventProducer().fireEvent(event); }
@Test public void testCreateSingleTaskChain() throws Exception { OperationContext ctx = new OperationContext(coreSession); ctx.setInput(document); List<Task> tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession); assertNotNull(tasks); assertEquals(0, tasks.size()); automationService.run(ctx, "createSingleTaskChain"); tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession); assertEquals(1, tasks.size()); Task task = tasks.get(0); assertEquals("single test task", task.getName()); List<String> pooledActorIds = task.getActors(); assertEquals(3, pooledActorIds.size()); assertTrue(pooledActorIds.contains(SecurityConstants.MEMBERS)); assertTrue(pooledActorIds.contains("myuser")); assertTrue(pooledActorIds.contains(SecurityConstants.ADMINISTRATOR)); List<TaskComment> comments = task.getComments(); assertEquals(1, comments.size()); TaskComment comment = comments.get(0); assertEquals(SecurityConstants.ADMINISTRATOR, comment.getAuthor()); assertEquals("test comment", comment.getText()); Calendar calendar = Calendar.getInstance(); calendar.set(2006, 6, 6, 15, 10, 15); calendar.set(Calendar.MILLISECOND, 0); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); assertEquals(calendar.getTime(), task.getDueDate()); // task status assertTrue(task.isOpened()); assertFalse(task.isCancelled()); assertFalse(task.hasEnded()); assertEquals(6, task.getVariables().size()); assertEquals( document.getRepositoryName(), task.getVariable(TaskService.VariableName.documentRepositoryName.name())); assertEquals(document.getId(), task.getVariable(TaskService.VariableName.documentId.name())); assertEquals("test directive", task.getVariable(TaskService.VariableName.directive.name())); assertEquals( "true", task.getVariable(OperationTaskVariableName.createdFromCreateTaskOperation.name())); assertEquals("true", task.getVariable(TaskService.VariableName.createdFromTaskService.name())); assertEquals(SecurityConstants.ADMINISTRATOR, task.getInitiator()); // accept task taskService.acceptTask( coreSession, (NuxeoPrincipal) coreSession.getPrincipal(), task, "ok i'm in"); coreSession.save(); // test task again tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession); // ended tasks are filtered assertEquals(0, tasks.size()); // check document metadata assertNull(document.getPropertyValue("dc:description")); }
@Test public void testCreateSeveralTasksChain() throws Exception { OperationContext ctx = new OperationContext(coreSession); ctx.setInput(document); List<Task> tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession); assertNotNull(tasks); assertEquals(0, tasks.size()); automationService.run(ctx, "createSeveralTasksChain"); tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession); Collections.sort(tasks, new TaskInstanceComparator()); assertEquals(3, tasks.size()); Task task1 = tasks.get(0); assertEquals("several test tasks", task1.getName()); List<String> pooledActorIds = task1.getActors(); assertEquals(1, pooledActorIds.size()); assertEquals(SecurityConstants.ADMINISTRATOR, pooledActorIds.get(0)); List<TaskComment> comments = task1.getComments(); assertEquals(0, comments.size()); // task status assertTrue(task1.isOpened()); assertFalse(task1.isCancelled()); assertFalse(task1.hasEnded()); assertEquals(5, task1.getVariables().size()); assertEquals( document.getRepositoryName(), task1.getVariable(TaskService.VariableName.documentRepositoryName.name())); assertEquals(document.getId(), task1.getVariable(TaskService.VariableName.documentId.name())); assertNull(task1.getVariable(TaskService.VariableName.directive.name())); assertEquals( "true", task1.getVariable(OperationTaskVariableName.createdFromCreateTaskOperation.name())); assertEquals("true", task1.getVariable(TaskService.VariableName.createdFromTaskService.name())); assertEquals(SecurityConstants.ADMINISTRATOR, task1.getInitiator()); // accept task taskService.acceptTask( coreSession, (NuxeoPrincipal) coreSession.getPrincipal(), task1, "ok i'm in"); coreSession.save(); // test task again tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession); // ended tasks are filtered assertEquals(2, tasks.size()); Collections.sort(tasks, new TaskInstanceComparator()); // check other tasks Task task2 = tasks.get(0); assertEquals("several test tasks", task2.getName()); pooledActorIds = task2.getActors(); assertEquals(1, pooledActorIds.size()); assertEquals(SecurityConstants.MEMBERS, pooledActorIds.get(0)); comments = task2.getComments(); assertEquals(0, comments.size()); // task status assertTrue(task2.isOpened()); assertFalse(task2.isCancelled()); assertFalse(task2.hasEnded()); assertEquals(5, task2.getVariables().size()); assertEquals( document.getRepositoryName(), task2.getVariable(TaskService.VariableName.documentRepositoryName.name())); assertEquals(document.getId(), task2.getVariable(TaskService.VariableName.documentId.name())); assertNull(task2.getVariable(TaskService.VariableName.directive.name())); assertEquals( "true", task2.getVariable(OperationTaskVariableName.createdFromCreateTaskOperation.name())); assertEquals("true", task2.getVariable(TaskService.VariableName.createdFromTaskService.name())); assertEquals(SecurityConstants.ADMINISTRATOR, task2.getInitiator()); Task task3 = tasks.get(1); assertEquals("several test tasks", task3.getName()); pooledActorIds = task3.getActors(); assertEquals(1, pooledActorIds.size()); assertEquals("myuser", pooledActorIds.get(0)); comments = task3.getComments(); assertEquals(0, comments.size()); // task status assertTrue(task3.isOpened()); assertFalse(task3.isCancelled()); assertFalse(task3.hasEnded()); assertEquals(5, task3.getVariables().size()); assertEquals( document.getRepositoryName(), task3.getVariable(TaskService.VariableName.documentRepositoryName.name())); assertEquals(document.getId(), task3.getVariable(TaskService.VariableName.documentId.name())); assertNull(task3.getVariable(TaskService.VariableName.directive.name())); assertEquals( "true", task3.getVariable(OperationTaskVariableName.createdFromCreateTaskOperation.name())); assertEquals("true", task3.getVariable(TaskService.VariableName.createdFromTaskService.name())); assertEquals(SecurityConstants.ADMINISTRATOR, task3.getInitiator()); // check document metadata assertNull(document.getPropertyValue("dc:description")); }