Jbpm4提供的IdentitySession接口並非一種很好的處理方式,鑑於咱們每一個業務系統都有一套本身的用戶及權限認證管理機制,須要與jbpm4.4集成的話,就比較周折了,咱們常常須要查詢的就是用戶本身的任務。jbpm4的任務裏有一個比較好的任務人員指派定義方式,就是使用AssignmentHandler接口,其定義以下所示: java
<?xml version="1.0" encoding="UTF-8"?> <process name="TaskAssignmentHandler" xmlns="http://jbpm.org/4.4/jpdl"> <start g="20,20,48,48"> <transition to="review" /> </start> <task name="review" g="96,16,127,52"> <assignment-handler class="org.jbpm.examples.task.assignmenthandler.AssignTask"> <field name="assignee"> <string value="johndoe" /> </field> </assignment-handler> <transition to="wait" /> </task> <state name="wait" g="255,16,88,52" /> </process>
package org.jbpm.examples.task.assignmenthandler; import org.jbpm.api.model.OpenExecution; import org.jbpm.api.task.Assignable; import org.jbpm.api.task.AssignmentHandler; /** * @author Tom Baeyens */ public class AssignTask implements AssignmentHandler { private static final long serialVersionUID = 1L; String assignee; public void assign(Assignable assignable, OpenExecution execution) { assignable.setAssignee(assignee); } }
這要求咱們在設計流程定義後,任務的處理人已經必須定下來了,但若咱們在流程發佈後,還須要手工改這裏的任務執行人員(而且人員是咱們系統的用戶),甚至人員可能在流程運行過程當中,由用戶在任務表單或計算過程當中動態指定,以上的方式並不能知足咱們的要求。 apache
基於這種想法,應該設計另外一種容許用戶修改流程定義中的人員,而且跟咱們的系統用戶角色結合起來。 api
jbpm4以後的版本,啓動流程及運行流程時,都會去讀取流程定義,所以,咱們能夠動態修改以上配置文件,讓其生成相似以下的配置格式便可以知足咱們的要求: ide
<?xml version="1.0" encoding="UTF-8"?> <process name="TaskAssignmentHandler" xmlns="http://jbpm.org/4.4/jpdl"> <start g="20,20,48,48"> <transition to="review" /> </start> <task name="review" g="96,16,127,52"> <assignment-handler class="com.htsoft.core.jbpm.AssignmentHandler"> <field name="userIds"> <string value="1" /> </field> <field name="roleIds"> <string value="1,2" /> </field> </assignment-handler> <transition to="wait" /> </task> <state name="wait" g="255,16,88,52" /> </process>
以上的userIds的1,以及roleIds的1,2則表明咱們系統中的用戶id與角色的id,其值由後臺用戶在後面經過界面來設置。 spa
其設置後,就生成以上的代碼寫至jbpm4_lob表中的blobvalue字段中去則可,這是持久化的處理。 .net
也能夠臨時調用相似如下的代碼動態實現以上效果: debug
/** * 爲流程定義加上任務的指派人員接口 * @param deployId */ public void addAssignHandler(ProUserAssign proUserAssign){ ProcessDefinitionImpl pd=(ProcessDefinitionImpl)repositoryService.createProcessDefinitionQuery().deploymentId(proUserAssign.getDeployId()).uniqueResult(); EnvironmentFactory environmentFactory = (EnvironmentFactory) processEngine; EnvironmentImpl env=null; try { env = environmentFactory.openEnvironment(); //找到任務的定義 TaskDefinitionImpl taskDef=pd.getTaskDefinition(proUserAssign.getActivityName()); UserCodeReference userCodeReference = new UserCodeReference(); ObjectDescriptor descriptor = new ObjectDescriptor(); //加上任務的人員動態指派 descriptor.setClassName("com.htsoft.core.jbpm.UserAssignHandler"); //動態加參數 FieldOperation userIdsFo = new FieldOperation(); userIdsFo.setFieldName("userIds"); userIdsFo.setDescriptor(new StringDescriptor(proUserAssign.getUserId())); FieldOperation groupIdsFo=new FieldOperation(); groupIdsFo.setFieldName("groupIds"); groupIdsFo.setDescriptor(new StringDescriptor(proUserAssign.getRoleId())); List<Operation> listOp=new ArrayList<Operation>(); listOp.add(userIdsFo); listOp.add(groupIdsFo); descriptor.setOperations(listOp); userCodeReference.setCached(false); userCodeReference.setDescriptor(descriptor); taskDef.setAssignmentHandlerReference(userCodeReference); }catch(Exception ex){ logger.error("ADD AssignHandler Error:" + ex.getMessage()); }finally{ if(env!=null){ env.close(); } } }
不過該方式沒有持久久,重啓系統後,保存的用戶及角色設置並不會生效。 設計
UserAssignHandler類代碼以下: code
package com.htsoft.core.jbpm; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jbpm.api.model.OpenExecution; import org.jbpm.api.task.Assignable; import org.jbpm.api.task.AssignmentHandler; import com.htsoft.core.Constants; /** * 還沒有開始使用 * <B><P>Joffice -- http://www.jee-soft.cn</P></B> * <B><P>Copyright (C) 2008-2010 GuangZhou HongTian Software Company (廣州宏天軟件有限公司)</P></B> * <B><P>description:</P></B> * <P></P> * <P>product:joffice</P> * <P></P> * @see com.htsoft.core.jbpm.UserAssignHandler * <P></P> * @author * @version V1 * @create: 2010-11-23下午02:58:01 */ public class UserAssignHandler implements AssignmentHandler{ private Log logger=LogFactory.getLog(UserAssignHandler.class); //授予用戶ID String userIds; //受權角色ID String groupIds; @Override public void assign(Assignable assignable, OpenExecution execution) throws Exception { String assignId=(String)execution.getVariable(Constants.FLOW_ASSIGN_ID); logger.info("assignId:===========>" + assignId); //在表單提交中指定了固定的執行人員 if(StringUtils.isNotEmpty(assignId)){ assignable.setAssignee(assignId); return; } //在表單中指定了執行的角色TODO //在表單中指定了會籤人員 String signUserIds=(String)execution.getVariable(Constants.FLOW_SIGN_USERIDS); if(signUserIds!=null){ //TODO 取到該任務,進行會籤設置 } logger.debug("Enter UserAssignHandler assign method~~~~"); if(userIds!=null){//若用戶不爲空 String[]uIds=userIds.split("[,]"); if(uIds!=null && uIds.length>1){//多於一我的的 for(String uId:uIds){ assignable.addCandidateUser(uId); } }else{ assignable.setAssignee(userIds); } } if(groupIds!=null){//若角色組不爲空 String[]gIds=userIds.split("[,]"); if(gIds!=null&& gIds.length>1){//多於一個角色的 for(String gId:gIds){ assignable.addCandidateGroup(gId); } }else{ assignable.addCandidateGroup(groupIds); } } } }