利用JBPM4.4的AssignmentHandler實現用戶角色整合另外一種構思

Jbpm4提供的IdentitySession接口並非一種很好的處理方式,鑑於咱們每一個業務系統都有一套本身的用戶及權限認證管理機制,須要與jbpm4.4集成的話,就比較周折了,咱們常常須要查詢的就是用戶本身的任務。jbpm4的任務裏有一個比較好的任務人員指派定義方式,就是使用AssignmentHandler接口,其定義以下所示: java

Java代碼
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2.   
  3. <process name="TaskAssignmentHandler" xmlns="http://jbpm.org/4.4/jpdl">   
  4.   
  5.   <start g="20,20,48,48">   
  6.     <transition to="review" />   
  7.   </start>   
  8.   
  9.   <task name="review" g="96,16,127,52">   
  10.     <assignment-handler class="org.jbpm.examples.task.assignmenthandler.AssignTask">   
  11.       <field name="assignee">   
  12.         <string value="johndoe" />   
  13.       </field>   
  14.     </assignment-handler>   
  15.     <transition to="wait" />   
  16.   </task>   
  17.   
  18.   <state name="wait" g="255,16,88,52" />   
  19.   
  20. </process>  
<?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>

 

Java代碼
  1. package org.jbpm.examples.task.assignmenthandler;   
  2.   
  3. import org.jbpm.api.model.OpenExecution;   
  4. import org.jbpm.api.task.Assignable;   
  5. import org.jbpm.api.task.AssignmentHandler;   
  6.   
  7.   
  8. /**  
  9.  * @author Tom Baeyens  
  10.  */  
  11. public class AssignTask implements AssignmentHandler {   
  12.      
  13.   private static final long serialVersionUID = 1L;   
  14.   
  15.   String assignee;   
  16.   
  17.   public void assign(Assignable assignable, OpenExecution execution) {   
  18.     assignable.setAssignee(assignee);   
  19.   }   
  20. }  
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

 

Java代碼
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2.   
  3. <process name="TaskAssignmentHandler" xmlns="http://jbpm.org/4.4/jpdl">   
  4.   
  5.   <start g="20,20,48,48">   
  6.     <transition to="review" />   
  7.   </start>   
  8.   
  9.   <task name="review" g="96,16,127,52">   
  10.     <assignment-handler class="com.htsoft.core.jbpm.AssignmentHandler">   
  11.       <field name="userIds">   
  12.         <string value="1" />   
  13.       </field>   
  14.      <field name="roleIds">   
  15.         <string value="1,2" />   
  16.       </field>   
  17.     </assignment-handler>   
  18.     <transition to="wait" />   
  19.   </task>   
  20.   
  21.   <state name="wait" g="255,16,88,52" />   
  22.   
  23. </process>  
<?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

 

Java代碼
  1. /**  
  2.      * 爲流程定義加上任務的指派人員接口  
  3.      * @param deployId  
  4.      */  
  5.     public void addAssignHandler(ProUserAssign proUserAssign){   
  6.         ProcessDefinitionImpl pd=(ProcessDefinitionImpl)repositoryService.createProcessDefinitionQuery().deploymentId(proUserAssign.getDeployId()).uniqueResult();   
  7.          EnvironmentFactory environmentFactory = (EnvironmentFactory) processEngine;   
  8.          EnvironmentImpl env=null;   
  9.          try {   
  10.              env = environmentFactory.openEnvironment();   
  11.              //找到任務的定義   
  12.              TaskDefinitionImpl taskDef=pd.getTaskDefinition(proUserAssign.getActivityName());   
  13.              UserCodeReference userCodeReference = new UserCodeReference();   
  14.              ObjectDescriptor descriptor = new ObjectDescriptor();   
  15.              //加上任務的人員動態指派   
  16.              descriptor.setClassName("com.htsoft.core.jbpm.UserAssignHandler");   
  17.              //動態加參數   
  18.              FieldOperation userIdsFo = new FieldOperation();   
  19.              userIdsFo.setFieldName("userIds");    
  20.              userIdsFo.setDescriptor(new StringDescriptor(proUserAssign.getUserId()));   
  21.                 
  22.              FieldOperation groupIdsFo=new FieldOperation();   
  23.              groupIdsFo.setFieldName("groupIds");   
  24.              groupIdsFo.setDescriptor(new StringDescriptor(proUserAssign.getRoleId()));   
  25.                 
  26.              List<Operation> listOp=new ArrayList<Operation>();   
  27.              listOp.add(userIdsFo);   
  28.              listOp.add(groupIdsFo);   
  29.              descriptor.setOperations(listOp);   
  30.                 
  31.              userCodeReference.setCached(false);   
  32.              userCodeReference.setDescriptor(descriptor);   
  33.              taskDef.setAssignmentHandlerReference(userCodeReference);   
  34.                 
  35.          }catch(Exception ex){   
  36.              logger.error("ADD AssignHandler Error:" + ex.getMessage());   
  37.          }finally{   
  38.              if(env!=null){   
  39.                  env.close();   
  40.              }   
  41.          }   
  42.     }  
/**
     * 爲流程定義加上任務的指派人員接口
     * @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

 

 

Java代碼
  1. package com.htsoft.core.jbpm;   
  2.   
  3. import org.apache.commons.lang.StringUtils;   
  4. import org.apache.commons.logging.Log;   
  5. import org.apache.commons.logging.LogFactory;   
  6. import org.jbpm.api.model.OpenExecution;   
  7. import org.jbpm.api.task.Assignable;   
  8. import org.jbpm.api.task.AssignmentHandler;   
  9.   
  10. import com.htsoft.core.Constants;   
  11.   
  12. /**  
  13.  * 還沒有開始使用  
  14.  * <B><P>Joffice -- http://www.jee-soft.cn</P></B>  
  15.  * <B><P>Copyright (C) 2008-2010 GuangZhou HongTian Software Company (廣州宏天軟件有限公司)</P></B>   
  16.  * <B><P>description:</P></B>  
  17.  * <P></P>  
  18.  * <P>product:joffice</P>  
  19.  * <P></P>   
  20.  * @see com.htsoft.core.jbpm.UserAssignHandler  
  21.  * <P></P>  
  22.  * @author   
  23.  * @version V1  
  24.  * @create : 2010-11-23下午02:58:01  
  25.  */  
  26. public class UserAssignHandler implements AssignmentHandler{   
  27.     private Log logger=LogFactory.getLog(UserAssignHandler.class);   
  28.     //授予用戶ID   
  29.     String userIds;   
  30.     //受權角色ID   
  31.     String groupIds;   
  32.        
  33.     @Override  
  34.     public void assign(Assignable assignable, OpenExecution execution) throws Exception {   
  35.            
  36.         String assignId=(String)execution.getVariable(Constants.FLOW_ASSIGN_ID);   
  37.            
  38.         logger.info("assignId:===========>" + assignId);   
  39.            
  40.         //在表單提交中指定了固定的執行人員   
  41.         if(StringUtils.isNotEmpty(assignId)){   
  42.             assignable.setAssignee(assignId);   
  43.             return;   
  44.         }   
  45.            
  46.         //在表單中指定了執行的角色TODO   
  47.            
  48.         //在表單中指定了會籤人員   
  49.         String signUserIds=(String)execution.getVariable(Constants.FLOW_SIGN_USERIDS);   
  50.            
  51.         if(signUserIds!=null){   
  52.             //TODO 取到該任務,進行會籤設置   
  53.         }   
  54.            
  55.         logger.debug("Enter UserAssignHandler assign method~~~~");   
  56.            
  57.         if(userIds!=null){//若用戶不爲空   
  58.             String[]uIds=userIds.split("[,]");   
  59.             if(uIds!=null && uIds.length>1){//多於一我的的   
  60.                 for(String uId:uIds){   
  61.                     assignable.addCandidateUser(uId);   
  62.                 }   
  63.             }else{   
  64.                 assignable.setAssignee(userIds);   
  65.             }   
  66.         }   
  67.            
  68.         if(groupIds!=null){//若角色組不爲空   
  69.             String[]gIds=userIds.split("[,]");   
  70.             if(gIds!=null&& gIds.length>1){//多於一個角色的   
  71.                 for(String gId:gIds){   
  72.                     assignable.addCandidateGroup(gId);   
  73.                 }   
  74.             }else{   
  75.                 assignable.addCandidateGroup(groupIds);   
  76.             }   
  77.         }   
  78.   
  79.     }   
  80.   
  81. }  
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);
			}
		}

	}

}
相關文章
相關標籤/搜索