import java.util.*;
import org.joget.apps.app.service.*;
import org.joget.workflow.model.*;
import org.joget.workflow.model.service.*;
import org.joget.workflow.model.dao.WorkflowProcessLinkDao;

String processInstanceId = "#assignment.processId#";
String activityDefId = "waitingForResponse";

//utility bean
WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");  
WorkflowUserManager workflowUserManager = (WorkflowUserManager) AppUtil.getApplicationContext().getBean("workflowUserManager");

//get current user username and temporary set current user to roleAnonymous to get the assignment
String username = workflowUserManager.getCurrentUsername();

workflowUserManager.setCurrentThreadUser("roleAnonymous");

WorkflowProcessLinkDao dao = (WorkflowProcessLinkDao) AppUtil.getApplicationContext().getBean("workflowProcessLinkDao");

//get assignment
Collection assignments = workflowManager.getAssignmentList(null, null, processInstanceId, activityDefId, null, null, null, 1);

if (assignments == null || assignments.isEmpty()) {
    Collection processLinks = dao.getLinks(processInstanceId);
    if (!processLinks.isEmpty()) {
        for (WorkflowProcessLink l : processLinks) {
            assignments = workflowManager.getAssignmentList(null, null, l.getProcessId(), activityDefId, null, null, null, 1);
            if (assignments != null && !assignments.isEmpty()) {
                break;
            }
        }
    }
}
if (assignments != null && !assignments.isEmpty()) {
    WorkflowAssignment ass = (WorkflowAssignment) assignments.iterator().next();
    String actId = ass.getActivityId();
    //accept and complete assignment
    workflowManager.assignmentAccept(actId);
    workflowManager.assignmentComplete(actId, new HashMap());
}

//set the current user back to original
workflowUserManager.setCurrentThreadUser(username);
  • No labels