我们要设计一个审批流程,这将需要一个以上的人的批准审批过程将要求批准组的每一个人都批准。 当其中有一个拒绝时,整个批准过程将立即被拒绝。而且,正如预期的那样,他们每个人都需要批准才能获得批准。

对于流程设计,我们需要2个独立的工作流程。


图1:主流程 - 应用流程


图2:个人审批流程

图3:ApproverGroup的映射

按照设计,需要先启动“应用”过程并提交“应用”活动。然后,“生成批准”工具将根据“批准者组”参与者映射中返回的用户数量生成尽可能多的“批准”流程。

每个“批准者”流程实例都会从尊敬的批准者那里收集决策。然后执行“更新应用程序”触发父进程中的“等待响应”活动(“应用”进程),然后执行“进程批准”工具来评估是否足以作出最终决定并前进。

生成审批

import org.joget.workflow.model.service.WorkflowManager;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.app.service.AppService;
import org.joget.workflow.model.WorkflowAssignment;
import org.joget.workflow.util.WorkflowUtil;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.joget.workflow.model.WorkflowProcess;
import org.joget.workflow.model.WorkflowProcessResult;

//constant value
String processDefKey = "approve";
String rowCountVariableName = "approvalCount";
String approverGroupParticipantId = "approverGroup";
String approvalIdsVariableName = "approvalIds";

//utility bean
WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");
AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");

//get processDefId
WorkflowProcess processDef = appService.getWorkflowProcessForApp(appDef.getId(), appDef.getVersion().toString(), processDefKey);
String processDefId = processDef.getId();

//get foreign key
String processId = workflowAssignment.getProcessId();

int rowCount = 0;
Collection userList = null;
userList = WorkflowUtil.getAssignmentUsers(appDef.getAppId(), workflowAssignment.getProcessDefId(), workflowAssignment.getProcessId(), workflowAssignment.getProcessVersion(), workflowAssignment.getActivityId(), "", approverGroupParticipantId);

String approvalInstanceIds = "";
for(String user : userList){
	System.out.println(user);
	Map variables = new HashMap();
	variables.put("apply_id", processId);
	variables.put("approver", user);
	WorkflowProcessResult result = workflowManager.processStart(processDefId, null, variables, "admin", null, false);
	approvalInstanceIds += result.getProcess().getInstanceId() + ",";
    rowCount++;
}

//set row count to workflow variable
workflowManager.processVariable(processId, rowCountVariableName, Integer.toString(rowCount));

//keep the list of approval instances
workflowManager.processVariable(processId, approvalIdsVariableName, approvalInstanceIds);

更新应用程序

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

//constant value
String processInstanceId = "#variable.apply_id#";
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");

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

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, null);
}

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

流程审批

import org.joget.apps.app.service.*;
import org.joget.apps.form.dao.*;
import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import org.joget.workflow.model.*;
import org.joget.workflow.model.service.*;

//constant value
String foreignKey = "customProperties.apply_id";
String formDefId = "approveForm";
String tableName = "multiApproval_approvals";
String rowCountVariableName = "approvalCount";
String statusVariableName = "status";
int approvalCount = Integer.parseInt("#variable.approvalCount#");
String approvalIds = "#variable.approvalIds#";

//utility bean
FormDataDao formDataDao = (FormDataDao) AppUtil.getApplicationContext().getBean("formDataDao");
WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");
AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");

//get foreign key
String processId = workflowAssignment.getProcessId();

//build condition
String condition = " WHERE " + foreignKey + " = ?";
Object[] paramsArray = new Object[]{processId};

//get approval data
FormRowSet rows = new FormRowSet();
rows = formDataDao.find(formDefId, tableName, condition, paramsArray, "dateCreated", false, null, null);

int rowCount = 0;
String status = "";
String recordId = "";
for (FormRow r : rows) {
    recordId = r.getId();
    String recordStatus = r.get("status");

	if(recordStatus.equalsIgnoreCase("Rejected")){
		status = "Rejected";
		break;
	}
    rowCount++;
}

if(status.equalsIgnoreCase("Rejected")){
	workflowManager.processVariable(processId, statusVariableName, "Rejected");
	//terminate any remaining approval instances
	String[] approvalIdsSplit = approvalIds.split(",");
	for(String approvalId : approvalIdsSplit){
		if(!approvalId.equalsIgnoreCase("") && !approvalId.equalsIgnoreCase(recordId)){
			try{
				workflowManager.processAbort(approvalId);
			}catch(Exception e){
			}
		}
	}
}else if(rowCount >= approvalCount){
	workflowManager.processVariable(processId, statusVariableName, "Approved");
}


图4:提交“申请”活动并生成所有“批准”流程后的实例视图


图5:应用程序的结果

KB:Download the App

  • No labels