How can I delete a task?

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
alekseybykov
Established Member II

How can I delete a task?

Jump to solution

Or more precisely - is there a way to remove a participant from subsequent iterations if it was "deleted" at the first iteration? 

What I mean...

For example, there is a some parallel workflow. I choose 10 participants and initiated this business process. Thus, 10 tasks were created, one for each participant. But then I decided to delete a few participants.

I can to complete these tasks by using WebScript. For example, as follows. I know the workflowId and taskId:

...
Map<String, String> templateArgs = req.getServiceMatch().getTemplateVars();

// String workflowId = templateArgs.get("workflowId");
String taskId = templateArgs.get("taskId");

Map<QName, Serializable> updatedProperties = new HashMap<>();
updatedProperties.put(TaskRemoverModel.REVIEW_OUTCOME_PROPERTY, "Approve");    
updatedProperties.put(TaskRemoverModel.COMMENT_PROPERTY,
     "The user was excluded from the list of participants.");

workflowService.updateTask(taskId, updatedProperties, null, null);
workflowService.endTask(taskId, null);
...
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

But the problem is that at the stage of revise, the initiator can send for approval again. Then tasks for excluded participants will be created again.

Is there a way to "mark" these participants, so that no tasks were created for them in the future?

For example, in the WebScript I added a new property to the task that should not be generated after resubmited.

...
updatedProperties.put(TaskRemoverModel.TASK_WAS_EXCLUDED_SIGN, "exclude");

workflowService.updateTask(taskId, updatedProperties, null, null);
workflowService.endTask(taskId, null);
...

Where:

...
public static final String BPM_MODEL_URI = "http://www.alfresco.org/model/bpm/1.0";
public static final QName TASK_WAS_EXCLUDED_SIGN = QName.createQName(BPM_MODEL_URI, "taskWasExcluded");
...

In the listener that listens for the complete event for a task, I can get the following set of variables:

public class TaskCompleteListener implements TaskListener {
     ...
     public void notify(DelegateTask delegateTask) {

          Map<String, Object> exVars = execution.getVariables();
          Iterator iterator = exVars.entrySet().iterator();
          while(iterator.hasNext()) {
               Map.Entry exVar = (Map.Entry)iterator.next();
               if(exVar.getKey() != null) {
                    System.out.println("var == " +
                         exVar.getKey().toString() + ",
                    value == " + exVar.getValue());
               }
          }
     ...
     }
}

I get the following:

var == bpm_packageActionGroup, value == add_package_item_actions
var == bpm_reassignable, value == true
var == tkbwf_reviewerCount, value == 4.0
var == bpm_priority, value == 2
var == initiator, value == Node Type: {http://www.alfresco.org/model/content/1.0}person, Node Aspects: [{http://www.alfresco.org/model/content/1.0}ownable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/system/1.0}localized, {http://www.alfresco.org/model/content/1.0}preferences]
var == bpm_sendEMailNotifications, value == false
var == bpm_percentComplete, value == 0
var == tkbwf_rejectCount, value == 0.0
var == workflowinstanceid, value == activiti$4806
var == bpm_hiddenTransitions, value ==
var == bpm_workflowPriority, value == 2
var == bpm_workflowDueDate, value == null
var == bpm_package, value == Node Type: {http://www.alfresco.org/model/bpm/1.0}package, Node Aspects: [{http://www.alfresco.org/model/bpm/1.0}workflowPackage, {http://www.alfresco.org/model/content/1.0}auditable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/system/1.0}localized]
var == tkbwf_approveCount, value == 1.0
var == tkbwf_tpApproved, value == false
var == nrOfActiveInstances, value == 4
var == bpm_workflowDescription, value == Another check
var == bpm_packageItemActionGroup, value == start_package_item_actions
var == _startTaskCompleted, value == Mon Dec 11 19:26:58 GMT+04:00 2017
var == companyhome, value == Node Type: {http://www.alfresco.org/model/content/1.0}folder, Node Aspects: [{http://www.alfresco.org/model/content/1.0}titled, {http://www.alfresco.org/model/content/1.0}auditable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/system/1.0}localized, {http://www.alfresco.org/model/application/1.0}uifacets]
var == tkbwf_package, value == null
var == bpm_assignees, value == [Node Type: {http://www.alfresco.org/model/content/1.0}person, Node Aspects: [{http://www.alfresco.org/model/content/1.0}ownable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/system/1.0}localized, {http://www.alfresco.org/model/content/1.0}preferences], Node Type: {http://www.alfresco.org/model/content/1.0}person, Node Aspects: [{http://www.alfresco.org/model/content/1.0}ownable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/system/1.0}localized], Node Type: {http://www.alfresco.org/model/content/1.0}person, Node Aspects: [{http://www.alfresco.org/model/content/1.0}ownable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/system/1.0}localized], Node Type: {http://www.alfresco.org/model/content/1.0}person, Node Aspects: [{http://www.alfresco.org/model/content/1.0}ownable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/system/1.0}localized]]
var == loopCounter, value == 1
var == bpm_description, value == Another check
var == nrOfInstances, value == 4
var == initiatorhome, value == Node Type: {http://www.alfresco.org/model/content/1.0}folder, Node Aspects: [{http://www.alfresco.org/model/content/1.0}titled, {http://www.alfresco.org/model/content/1.0}auditable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/system/1.0}localized, {http://www.alfresco.org/model/application/1.0}uifacets]
var == cancelled, value == false
var == bpm_comment, value ==
var == bpm_status, value == Not Yet Started
var == reviewAssignee, value == Node Type: {http://www.alfresco.org/model/content/1.0}person, Node Aspects: [{http://www.alfresco.org/model/content/1.0}ownable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/system/1.0}localized]
var == nrOfCompletedInstances, value == 0

As far as I understand, I need this pair (for example, I assigned 4 participants), but there are no references to the nodes:

var == bpm_assignees, 
value ==
     [Node Type:
          {http://www.alfresco.org/model/content/1.0}person,
          Node Aspects:
               [{http://www.alfresco.org/model/content/1.0}ownable,
               {http://www.alfresco.org/model/system/1.0}referenceable,
               {http://www.alfresco.org/model/system/1.0}localized,
               {http://www.alfresco.org/model/content/1.0}preferences],
     Node Type:
          {http://www.alfresco.org/model/content/1.0}person,
          Node Aspects:
               [{http://www.alfresco.org/model/content/1.0}ownable,
               {http://www.alfresco.org/model/system/1.0}referenceable,
               {http://www.alfresco.org/model/system/1.0}localized],
     Node Type:
          {http://www.alfresco.org/model/content/1.0}person,
          Node Aspects:
               [{http://www.alfresco.org/model/content/1.0}ownable,
               {http://www.alfresco.org/model/system/1.0}referenceable,
               {http://www.alfresco.org/model/system/1.0}localized],
     Node Type:
          {http://www.alfresco.org/model/content/1.0}person,
          Node Aspects:
               [{http://www.alfresco.org/model/content/1.0}ownable,
               {http://www.alfresco.org/model/system/1.0}referenceable,
               {http://www.alfresco.org/model/system/1.0}localized]
     ]

In the task properties by using the WorkflowService I can find the following sequence:

key == {http://www.alfresco.org/model/bpm/1.0}assignees, 
value == [workspace://SpacesStore/bf7db7b8-5678-4481-9131-3ee7ae062a27,
       workspace://SpacesStore/5e6dad71-fc14-420f-a038-180d8bebf45c,
       workspace://SpacesStore/f1f5f39d-289c-4a56-9cfb-0790535b3a31,
       workspace://SpacesStore/84237c79-d2a6-408a-9837-8a55697e83f8
]

Can I assume that these sequences coincide? For example, I deleted the participant with:

nodeRef == workspace://SpacesStore/5e6dad71-fc14-420f-a038-180d8bebf45c 

(second in the list). Then I should delete the second value in the first list:

Node Type: 
     {http://www.alfresco.org/model/content/1.0}person,
Node Aspects:
     [{http://www.alfresco.org/model/content/1.0}ownable,
     {http://www.alfresco.org/model/system/1.0}referenceable,
     {http://www.alfresco.org/model/system/1.0}localized],

Is this the right approach?...

I would be very grateful for the information. Thanks to all.

1 Solution

Accepted Solutions
alekseybykov
Established Member II

Re: How can I delete a task?

Jump to solution

The solution can be represented as follows.

In the WebScript it's need to update the task properties and complete it. In one of the new properties of the completed task it's needed to write the reference to the excluded participant:

public class TaskRemover extends DeclarativeWebScript {
     private WorkflowService workflowService;
     private PersonService personService;

     protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
          Map<String, Object> model = new HashMap<>();
          Map<String, String> templateArgs = req.getServiceMatch().getTemplateVars();
          
          String taskId = templateArgs.get("taskId");
          
          Map<QName, Serializable> updatedProperties = new HashMap<>();
          updatedProperties.put(TaskRemoverModel.WORKFLOW_CONFIRM_MODEL_CONFIRMOUTCOME_PROPERTY, "Approve");     
          updatedProperties.put(TaskRemoverModel.WORKFLOW_MODEL_LASTCOMMENT_PROPERTY, "The user was excluded from the list of participants.");
          
          WorkflowTask workflowTask = workflowService.getTaskById(taskId);
          Iterator taskProperties = workflowTask.getProperties().entrySet().iterator();
        while(taskProperties.hasNext()) {
            Map.Entry taskProperty = (Map.Entry)taskProperties.next();           
               if(TaskRemoverModel.CONTENT_MODEL_OWNER_PROPERTY.toString().equals(taskProperty.getKey().toString())) {
                    updatedProperties.put(TaskRemoverModel.TASK_WAS_EXCLUDED_SIGN,
                              personService.getPerson(taskProperty.getValue().toString()));
               }           
        }
         
          workflowService.updateTask(taskId, updatedProperties, null, null);
         workflowService.endTask(taskId, null);

          ...
     }
     ...     
}

In the listener it's needed to get the list of bpm_assignees and delete the reference to the participant that was written in the WebScript:

public class TaskCompleteListener implements TaskListener {
     private Map<Object, Object> registeredBeans = Context.getProcessEngineConfiguration().getBeans();
     
     private ServiceRegistry registry = (ServiceRegistry) registeredBeans.get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
     private NodeService nodeService = registry.getNodeService();
     private WorkflowService workflowService = registry.getWorkflowService(); 
     
     @Override
     public void notify(DelegateTask delegateTask) {
        DelegateExecution execution = delegateTask.getExecution();
       
          WorkflowTask workflowTask = workflowService.getTaskById("activiti$" + delegateTask.getId());
        Map<QName, Serializable> taskProperties = workflowTask.getProperties();
         
          Iterator iterator = taskProperties.entrySet().iterator();
        while(iterator.hasNext()) {
            Map.Entry taskProperty = (Map.Entry)iterator.next();
               if(taskProperty.getKey().toString().equals(TaskRemoverModel.TASK_WAS_EXCLUDED_SIGN)) {
                    ActivitiScriptNodeList assignees = (ActivitiScriptNodeList) delegateTask.getVariable("bpm_assignees");
                    for(ActivitiScriptNode personNode : assignees) {
                         if(personNode.getNodeRef().toString().equals(taskProperty.getValue().toString())) {                    
                              assignees.remove(personNode);
                         }
                    }
                    delegateTask.setVariable("bpm_assignees", assignees);
               }           
        }
     }
}

After that it becomes possible to delete the tasks of the participants in runtime and after the resubmit for these participants the tasks are not generated.

This solved my issue.

View solution in original post

1 Reply
alekseybykov
Established Member II

Re: How can I delete a task?

Jump to solution

The solution can be represented as follows.

In the WebScript it's need to update the task properties and complete it. In one of the new properties of the completed task it's needed to write the reference to the excluded participant:

public class TaskRemover extends DeclarativeWebScript {
     private WorkflowService workflowService;
     private PersonService personService;

     protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
          Map<String, Object> model = new HashMap<>();
          Map<String, String> templateArgs = req.getServiceMatch().getTemplateVars();
          
          String taskId = templateArgs.get("taskId");
          
          Map<QName, Serializable> updatedProperties = new HashMap<>();
          updatedProperties.put(TaskRemoverModel.WORKFLOW_CONFIRM_MODEL_CONFIRMOUTCOME_PROPERTY, "Approve");     
          updatedProperties.put(TaskRemoverModel.WORKFLOW_MODEL_LASTCOMMENT_PROPERTY, "The user was excluded from the list of participants.");
          
          WorkflowTask workflowTask = workflowService.getTaskById(taskId);
          Iterator taskProperties = workflowTask.getProperties().entrySet().iterator();
        while(taskProperties.hasNext()) {
            Map.Entry taskProperty = (Map.Entry)taskProperties.next();           
               if(TaskRemoverModel.CONTENT_MODEL_OWNER_PROPERTY.toString().equals(taskProperty.getKey().toString())) {
                    updatedProperties.put(TaskRemoverModel.TASK_WAS_EXCLUDED_SIGN,
                              personService.getPerson(taskProperty.getValue().toString()));
               }           
        }
         
          workflowService.updateTask(taskId, updatedProperties, null, null);
         workflowService.endTask(taskId, null);

          ...
     }
     ...     
}

In the listener it's needed to get the list of bpm_assignees and delete the reference to the participant that was written in the WebScript:

public class TaskCompleteListener implements TaskListener {
     private Map<Object, Object> registeredBeans = Context.getProcessEngineConfiguration().getBeans();
     
     private ServiceRegistry registry = (ServiceRegistry) registeredBeans.get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
     private NodeService nodeService = registry.getNodeService();
     private WorkflowService workflowService = registry.getWorkflowService(); 
     
     @Override
     public void notify(DelegateTask delegateTask) {
        DelegateExecution execution = delegateTask.getExecution();
       
          WorkflowTask workflowTask = workflowService.getTaskById("activiti$" + delegateTask.getId());
        Map<QName, Serializable> taskProperties = workflowTask.getProperties();
         
          Iterator iterator = taskProperties.entrySet().iterator();
        while(iterator.hasNext()) {
            Map.Entry taskProperty = (Map.Entry)iterator.next();
               if(taskProperty.getKey().toString().equals(TaskRemoverModel.TASK_WAS_EXCLUDED_SIGN)) {
                    ActivitiScriptNodeList assignees = (ActivitiScriptNodeList) delegateTask.getVariable("bpm_assignees");
                    for(ActivitiScriptNode personNode : assignees) {
                         if(personNode.getNodeRef().toString().equals(taskProperty.getValue().toString())) {                    
                              assignees.remove(personNode);
                         }
                    }
                    delegateTask.setVariable("bpm_assignees", assignees);
               }           
        }
     }
}

After that it becomes possible to delete the tasks of the participants in runtime and after the resubmit for these participants the tasks are not generated.

This solved my issue.

View solution in original post