From 660690bbd80b32c24fbaf66e74635f0b0737521d Mon Sep 17 00:00:00 2001 From: Roy Wetherall Date: Mon, 15 Aug 2011 05:41:15 +0000 Subject: [PATCH] Workflow Notification working for Activiti workflows git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29737 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../workflow/WorkflowNotificationUtils.java | 154 +++++++++++------- .../tasklistener/TaskCreateListener.java | 23 ++- 2 files changed, 118 insertions(+), 59 deletions(-) diff --git a/source/java/org/alfresco/repo/workflow/WorkflowNotificationUtils.java b/source/java/org/alfresco/repo/workflow/WorkflowNotificationUtils.java index 6802355125..867ab91874 100644 --- a/source/java/org/alfresco/repo/workflow/WorkflowNotificationUtils.java +++ b/source/java/org/alfresco/repo/workflow/WorkflowNotificationUtils.java @@ -30,45 +30,46 @@ public abstract class WorkflowNotificationUtils { /** Send EMail notifications property */ public static final String PROP_SEND_EMAIL_NOTIFICATIONS = "bpm_sendEMailNotifications"; + public static final String PROP_PACKAGE = "bpm_package"; /** I18N */ public static final String MSG_ASSIGNED_TASK = "assigned-task"; public static final String MSG_NEW_POOLED_TASK = "new-pooled-task"; + + /** Args value names */ + public static final String ARG_WF_ID = "workflowId"; + public static final String ARG_WF_TITLE = "workflowTitle"; + public static final String ARG_WF_DESCRIPTION = "workflowDescription"; + public static final String ARG_WF_DUEDATE = "workflowDueDate"; + public static final String ARG_WF_PRIORITY = "workflowPriority"; + public static final String ARG_WF_POOLED = "workflowPooled"; + public static final String ARG_WF_DOCUMENTS = "workflowDocuments"; /** Standard workflow assigned template */ private static final NodeRef WF_ASSIGNED_TEMPLATE = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "wf-email-html-ftl"); /** - * Send workflow assigned email notification. * - * @param services service registry - * @param taskId workflow global task id - * @param assignedAuthority assigned authority - * @param pooled true if pooled task, false otherwise + * @param services + * @param taskId + * @param title + * @param description + * @param dueDate + * @param priority + * @param workflowPackage + * @param assignedAuthorites + * @param pooled */ public static void sendWorkflowAssignedNotificationEMail(ServiceRegistry services, - String taskId, - String assignedAuthority, - boolean pooled) + String taskId, + String title, + String description, + Date dueDate, + Integer priority, + NodeRef workflowPackage, + String[] assignedAuthorites, + boolean pooled) { - sendWorkflowAssignedNotificationEMail(services, taskId, new String[]{assignedAuthority}, pooled); - } - - /** - * Send workflow assigned email notification. - * - * @param services service registry - * @param taskId workflow global task id - * @param assignedAuthorites assigned authorities - * @param pooled true if pooled task, false otherwise - */ - public static void sendWorkflowAssignedNotificationEMail(ServiceRegistry services, - String taskId, - String[] assignedAuthorites, - boolean pooled) - { - WorkflowTask workflowTask = services.getWorkflowService().getTaskById(taskId); - Map props = workflowTask.getProperties(); NotificationContext notificationContext = new NotificationContext(); // Determine the subject of the notification @@ -88,47 +89,36 @@ public abstract class WorkflowNotificationUtils // Build the template args MaptemplateArgs = new HashMap(7); - templateArgs.put("workflowId", workflowTask.getId()); - templateArgs.put("workflowTitle", workflowTask.getTitle()); - - // Get the description - String description = (String)props.get(WorkflowModel.PROP_DESCRIPTION); - if (description == null) - { - description = workflowTask.getDescription(); - } - templateArgs.put("workflowDescription", description); - - // Get the due date - Date dueDate = (Date)props.get(WorkflowModel.PROP_DUE_DATE); + templateArgs.put(ARG_WF_ID, taskId); + templateArgs.put(ARG_WF_TITLE, title); + templateArgs.put(ARG_WF_DESCRIPTION, description); if (dueDate != null) { - templateArgs.put("workflowDueDate", dueDate); + templateArgs.put(ARG_WF_DUEDATE, dueDate); } - - // Get the workflow priority - Integer priority = (Integer)props.get(WorkflowModel.PROP_PRIORITY); if (priority != null) { - templateArgs.put("workflowPriority", priority); + templateArgs.put(ARG_WF_PRIORITY, priority); } // Indicate whether this is a pooled workflow item or not - templateArgs.put("workflowPooled", pooled); + templateArgs.put(ARG_WF_POOLED, pooled); - // Add details of associated content - NodeRef workflowPackage = workflowTask.getPath().getInstance().getWorkflowPackage(); - List assocs = services.getNodeService().getChildAssocs(workflowPackage); - NodeRef[] docs = new NodeRef[assocs.size()]; - if (assocs.size() != 0) + if (workflowPackage != null) { - int index = 0; - for (ChildAssociationRef assoc : assocs) + // Add details of associated content + List assocs = services.getNodeService().getChildAssocs(workflowPackage); + NodeRef[] docs = new NodeRef[assocs.size()]; + if (assocs.size() != 0) { - docs[index] = assoc.getChildRef(); - index++; + int index = 0; + for (ChildAssociationRef assoc : assocs) + { + docs[index] = assoc.getChildRef(); + index++; + } + templateArgs.put(ARG_WF_DOCUMENTS, docs); } - templateArgs.put("workflowDocuments", docs); } // Set the template args @@ -141,6 +131,58 @@ public abstract class WorkflowNotificationUtils } // Send email notification - services.getNotificationService().sendNotification(EMailNotificationProvider.NAME, notificationContext); + services.getNotificationService().sendNotification(EMailNotificationProvider.NAME, notificationContext); + } + + /** + * Send workflow assigned email notification. + * + * @param services service registry + * @param taskId workflow global task id + * @param assignedAuthorites assigned authorities + * @param pooled true if pooled task, false otherwise + */ + public static void sendWorkflowAssignedNotificationEMail(ServiceRegistry services, + String taskId, + String[] assignedAuthorites, + boolean pooled) + { + // Get the workflow task + WorkflowTask workflowTask = services.getWorkflowService().getTaskById(taskId); + + // Get the workflow properties + Map props = workflowTask.getProperties(); + + // Get the title and description + String title = workflowTask.getTitle(); + String description = (String)props.get(WorkflowModel.PROP_DESCRIPTION); + if (description == null) + { + description = workflowTask.getDescription(); + } + + // Get the duedate, priority and workflow package + Date dueDate = (Date)props.get(WorkflowModel.PROP_DUE_DATE); + Integer priority = (Integer)props.get(WorkflowModel.PROP_PRIORITY); + NodeRef workflowPackage = workflowTask.getPath().getInstance().getWorkflowPackage(); + + // Send notification + sendWorkflowAssignedNotificationEMail(services, taskId, title, description, dueDate, priority, workflowPackage, assignedAuthorites, pooled); + } + + /** + * Send workflow assigned email notification. + * + * @param services service registry + * @param taskId workflow global task id + * @param assignedAuthority assigned authority + * @param pooled true if pooled task, false otherwise + */ + public static void sendWorkflowAssignedNotificationEMail(ServiceRegistry services, + String taskId, + String assignedAuthority, + boolean pooled) + { + sendWorkflowAssignedNotificationEMail(services, taskId, new String[]{assignedAuthority}, pooled); } } diff --git a/source/java/org/alfresco/repo/workflow/activiti/tasklistener/TaskCreateListener.java b/source/java/org/alfresco/repo/workflow/activiti/tasklistener/TaskCreateListener.java index a698e0d99b..70e8734831 100644 --- a/source/java/org/alfresco/repo/workflow/activiti/tasklistener/TaskCreateListener.java +++ b/source/java/org/alfresco/repo/workflow/activiti/tasklistener/TaskCreateListener.java @@ -27,8 +27,10 @@ import org.activiti.engine.impl.persistence.entity.ExecutionEntity; import org.activiti.engine.impl.persistence.entity.TaskEntity; import org.alfresco.repo.workflow.WorkflowNotificationUtils; import org.alfresco.repo.workflow.activiti.ActivitiConstants; +import org.alfresco.repo.workflow.activiti.ActivitiScriptNode; import org.alfresco.repo.workflow.activiti.properties.ActivitiPropertyConverter; import org.alfresco.service.ServiceRegistry; +import org.alfresco.service.cmr.repository.NodeRef; /** * Tasklistener that is notified when a task is created. This will set all @@ -65,18 +67,33 @@ public class TaskCreateListener implements TaskListener { task.setVariableLocal(ActivitiConstants.PROP_TASK_FORM_KEY, taskFormKey); } - + // Determine whether we need to send the workflow notification or not ExecutionEntity executionEntity = ((ExecutionEntity)task.getExecution()).getProcessInstance(); Boolean value = (Boolean)executionEntity.getVariable(WorkflowNotificationUtils.PROP_SEND_EMAIL_NOTIFICATIONS); if (Boolean.TRUE.equals(value) == true) { + NodeRef workflowPackage = null; + ActivitiScriptNode scriptNode = (ActivitiScriptNode)executionEntity.getVariable(WorkflowNotificationUtils.PROP_PACKAGE); + if (scriptNode != null) + { + workflowPackage = scriptNode.getNodeRef(); + } + + // TODO how do we identify a pooled task? + boolean pooled = false; + // Send email notification WorkflowNotificationUtils.sendWorkflowAssignedNotificationEMail( services, "activiti$" + task.getId(), - task.getAssignee(), - false); + task.getName(), + task.getDescription(), + task.getDueDate(), + Integer.valueOf(task.getPriority()), + workflowPackage, + new String[]{task.getAssignee()}, + pooled); } }