jbpm--(四)流程变量

package com.tgb.video;

import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.IdentityService;
import org.jbpm.api.ManagementService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;

import junit.framework.TestCase;

public class JbpmTestCase extends TestCase {
    
    protected RepositoryService repositoryService; //部署流程服务
    
    protected ExecutionService executionService; //流程执行服务 
    
    protected TaskService taskService ; //任务服务
    
    protected HistoryService historyServcie; //历史服务
    
    protected ManagementService managermentService; //流程管理服务
    
    protected IdentityService identityService; //身份认证服务
    
    
    
    protected void startUp(){
        ProcessEngine processEngine =  Configuration.getProcessEngine();
        repositoryService = processEngine.getRepositoryService();
        executionService = processEngine.getExecutionService();
        taskService = processEngine.getTaskService();
        historyServcie = processEngine.getHistoryService();
        managermentService = processEngine.getManagementService();
        identityService = processEngine.getIdentityService();
    }
    
    public void print(String name,String value){
        System.out.println(name + "=============" + value);
    }
}
package com.tgb.video;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.jbpm.api.ProcessInstance;

/**
 * 流程变量
 * @author Administrator
 *
 */
public class TestVariable extends JbpmTestCase implements JbpmUtil {

    @Override
    public void deploy() {
        // TODO Auto-generated method stub
        
    }
    
    /**
     * 创建流程实例,设置流程变量
     */
    @Override
    public void createInstance() {
        super.startUp();
        Map<String,Object> variable = new HashMap<String,Object>();
        variable.put("userId1", "001");
        variable.put("userName1", "tom");
        ProcessInstance processInstance = executionService.startProcessInstanceByKey("test", variable);
        
        super.print("流程实例ID", processInstance.getId());
        
    }

    @Override
    public void getCurrectActivity() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void getTask() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void completeTask() {
        super.startUp();
        taskService.completeTask("50001");
    }
    
    public void addTaskVariable(){
        super.startUp();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("taskUserID", "100");
        map.put("taskUserName", "jack");
        
        taskService.setVariables("50004", map);
    }
    
    public void getTaskVariable(){
        super.startUp();
        String taskUserID = taskService.getVariable("70001", "userId").toString();
        String taskUserName = taskService.getVariable("70001", "userName").toString();
        print("taskuserID",taskUserID);
        print("taskUserName",taskUserName);
        
    }
    
    /**
     * 获取流程变量
     */
    public void getMyVariable(){
        super.startUp();
        String userId = executionService.getVariable("test.40001", "userId1").toString();
        String userName = executionService.getVariable("test.40001", "userName1").toString();
        
        this.print("userId1", userId);
        this.print("userName1", userName);
    }
    
    public void updateMyVariable(){
        super.startUp();
        
        executionService.setVariable("test.50001", "userId", "002");
        
    }
    
    public void findMyVariable(){
        super.startUp();
        
        Set<String> set =  executionService.getVariableNames("test.50001");
        
        Iterator iter = set.iterator();
        while(iter.hasNext()){
            System.out.println(iter.next());
        }
        
        Map<String,Object> map = executionService.getVariables("test.50001", set);
        Iterator it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry m = (Map.Entry)it.next();
            this.print(m.getKey().toString(), m.getValue().toString());
        }
    }
    
}
package com.tgb.video;

public interface JbpmUtil {
    public void deploy(); //部署流程
    
    public void createInstance(); //创建流程实例
    
    public void getCurrectActivity(); //获取流程当前节点
    
    public void getTask(); //获取任务
    
    public void completeTask(); //完成任务
    
}
package com.tgb.video;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.jbpm.api.ProcessInstance;

/**
 * 流程变量
 * @author Administrator
 *
 */
public class TestVariable extends JbpmTestCase implements JbpmUtil {

    @Override
    public void deploy() {
        // TODO Auto-generated method stub
        
    }
    
    /**
     * 创建流程实例,设置流程变量
     */
    @Override
    public void createInstance() {
        super.startUp();
        Map<String,Object> variable = new HashMap<String,Object>();
        variable.put("userId1", "001");
        variable.put("userName1", "tom");
        ProcessInstance processInstance = executionService.startProcessInstanceByKey("test", variable);
        
        super.print("流程实例ID", processInstance.getId());
        
    }

    @Override
    public void getCurrectActivity() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void getTask() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void completeTask() {
        super.startUp();
        taskService.completeTask("50001");
    }
    
    public void addTaskVariable(){
        super.startUp();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("taskUserID", "100");
        map.put("taskUserName", "jack");
        
        taskService.setVariables("50004", map);
    }
    
    public void getTaskVariable(){
        super.startUp();
        String taskUserID = taskService.getVariable("70001", "userId").toString();
        String taskUserName = taskService.getVariable("70001", "userName").toString();
        print("taskuserID",taskUserID);
        print("taskUserName",taskUserName);
        
    }
    
    /**
     * 获取流程变量
     */
    public void getMyVariable(){
        super.startUp();
        String userId = executionService.getVariable("test.40001", "userId1").toString();
        String userName = executionService.getVariable("test.40001", "userName1").toString();
        
        this.print("userId1", userId);
        this.print("userName1", userName);
    }
    
    public void updateMyVariable(){
        super.startUp();
        
        executionService.setVariable("test.50001", "userId", "002");
        
    }
    
    public void findMyVariable(){
        super.startUp();
        
        Set<String> set =  executionService.getVariableNames("test.50001");
        
        Iterator iter = set.iterator();
        while(iter.hasNext()){
            System.out.println(iter.next());
        }
        
        Map<String,Object> map = executionService.getVariables("test.50001", set);
        Iterator it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry m = (Map.Entry)it.next();
            this.print(m.getKey().toString(), m.getValue().toString());
        }
    }
    
}

 

posted on 2017-07-15 16:40  code-java  阅读(264)  评论(0)    收藏  举报

导航