首页>代码>JBPM实例demo>/JbpmSource/src/com/ktvoa/process/action/JbpmAction.java
package com.ktvoa.process.action;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.history.HistoryProcessInstance;
import org.jbpm.api.model.ActivityCoordinates;
import org.jbpm.api.task.Task;
import com.ktv.process.bean.DptTreeModel;
import com.ktv.process.bean.Orgnazie;
import com.ktv.process.bean.ProcessNameBean;
import com.ktv.process.image.JpdlModel;
import com.ktv.process.image.JpdlModelDrawer;
import com.ktv.util.StringUtil;
import com.ktv.util.ZipUtil;
import com.opensymphony.xwork2.ActionContext;
/**
 * jbpm4.4流程控制的action
 * @author LYK
 *
 */
public class JbpmAction extends BasicAction {
	
	protected final Log logger = LogFactory.getLog(getClass());
	private static final long serialVersionUID = 1L;
	// 流程定义集合
	private List<ProcessDefinition> processDefinitions;
	// 流程实例集合
	private List<ProcessInstance> processInstances;
	// 流程参数集合
	private Map<String, Object> map;
	// 历史流程
	private List<HistoryProcessInstance> historyProcessInstances;
	// 当前用户任务集合
	private List<Task> tasks;

	// 数据传输域模型对象
	private ProcessDto processDto;

	// 用于接收经理审批结果的字符串,批注和驳回
	private String result;

	private JbpmManager jbpmManager;
	
	private List<String> deployProcessInstanceName;

	// 用于接收前台传过来的id参数
	private String id;
	// 流程图活动坐标
	private ActivityCoordinates activityCoordinates;

	public String initProcess() {
	
	/*	String roleName = (String) ActionContext.getContext().getSession().get(
				"role");*/
		processDefinitions = jbpmManager.getProcessDefinitions();
		processInstances = jbpmManager.getProcessInstances();
		historyProcessInstances = jbpmManager.getHistoryProcessInstances();
		deployProcessInstanceName = jbpmManager.getDeployProcessInstanceName();
		tasks = jbpmManager.getTasks("李大山");

		return SUCCESS;
	}

	/**
	 * 发布新流程
	 * 
	 * @return
	 */
	public String deploy() {
		String deployKey = getValueByKey(ActionContext.getContext().getParameters(),"deployKey");
		jbpmManager.deploy(deployKey);
		return SUCCESS;
	}

	//循环遍历Map中的数据
	public String getValueByMap(Map<String,Object> map){
		Set<Map.Entry<String, Object>> set = map.entrySet();
		for(Iterator<Map.Entry<String,Object>> iter = set.iterator();iter.hasNext();){
			Map.Entry<String, Object> entry = iter.next();
			
		}
		
		return null;
	}
	
	//根据value取得Map中所有的key
	public List<String> getKeyByValue(Map<String,Object> map, Object value) {
		List<String> keys = new ArrayList<String>();//根据value取得所有的key
		Iterator<Map.Entry<String,Object>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String,Object> entry = it.next();
			Object obj = entry.getValue();
			if (obj != null && obj.equals(value)) {
				keys.add(entry.getKey());
			}
		}
		return keys;
	} 
	
	/**
	 * 通过key值获取value
	 * @param map
	 * @param key
	 * @return
	 */
	public String getValueByKey(Map<String,Object> map,String key){
		String value = "";
		for (Object oo : map.entrySet()) {
			Entry<String, String[]> entry = (Entry<String, String[]>) oo;
			Object obj = entry.getKey();
			if (obj != null && obj.equals(key)) {
				value =  entry.getValue()[0].toString();
			}
		}
		return value;
	}
	
	
	/**
	 * 删除不需的以部署流程
	 * 
	 * @return
	 */
	public String undeploy() {
		jbpmManager.undeploy(id);
		return SUCCESS;
	}

	/**
	 * 启动一个流程并关联到用户
	 * 
	 * @return
	 * @throws Exception 
	 */
	public String start() throws Exception {
		map = new HashMap<String, Object>();
		map.put("owner", ActionContext.getContext().getSession().get(
				"role"));
		if(id.contains("decision")){
			map.put("sineeJbpm", "科长");
		}
		id=new String(id.getBytes("GBK"));
		jbpmManager.start(id, map);
		return SUCCESS;
	}

	/**
	 * 申请请求
	 * 
	 * @return
	 */
	public String request() {
		return SUCCESS;
	}

	/**
	 * 处理用户申请请求
	 * 
	 * @return
	 */
	public String submit() {
		map = new HashMap<String, Object>();
		map.put("owner", processDto.getOwner());
		map.put("day", Integer.parseInt(processDto.getDay()));
		map.put("reason", processDto.getReason());
		map.put("name", processDto.getOwner());
		jbpmManager.complate(processDto.getTaskId(), map);
		return SUCCESS;
	}

	/**
	 * 经理审批请求
	 * 
	 * @return
	 */
	public String managerRequerst() {
		map = jbpmManager.manager(id);
		return SUCCESS;
	}

	/**
	 * 经理审批
	 * 
	 * @return
	 */
	public String submitManager() {
		jbpmManager.complateByManager(id, result);
		return SUCCESS;
	}

	/**
	 * 老板请求
	 * 
	 * @return
	 */
	public String bossRequest() {
		map = jbpmManager.boss(id);
		return SUCCESS;
	}

	/**
	 * 老板处理
	 * 
	 * @return
	 */
	public String submitBoss() {
		jbpmManager.complateByBoss(id);
		return SUCCESS;
	}
	 /**
	  * 
	  * @throws IOException
	 *@author LYK
	 *@date 2013-7-5 下午03:44:52
	 *@descriptions:()
	  */
	public void dpyProByUserDefine() throws IOException{
		String s="D:/process/myProcess.jpdl.xml";
		//s=ServletActionContext.getServletContext().getRealPath("com/jbpm/source/1111.jpdl.xml");
		String xml=getRequestValue("xml");
		File f =new File(s);
		boolean needCreate=false;
	     if(!f.exists()){
	f.mkdir();
	needCreate=true;
	     }
	     if(needCreate)
	     f.createNewFile();
	   	     
           //写出jbpmxml文件
	     OutputStreamWriter  output = new OutputStreamWriter(new FileOutputStream(f),"UTF-8");
	     output.write(xml);
	     output.flush();
	     output.close();
	     //第二步将xml文件转换为png图片
	     InputStream is = new FileInputStream(f);    
		 JpdlModel jpdlModel;
		 File f1=new File("D:/process/myProcess.png");
		try {
			jpdlModel = new JpdlModel (is);
			 ImageIO.write(new JpdlModelDrawer().draw(jpdlModel), "png",
					 f1);  
		} catch (Exception e) {
			e.printStackTrace();
		}      
		
	
		//第三步将xml文件和png图片压缩为zip文件
		ZipUtil.zipFile("D:/process/myProcess.jpdl.xml", "D:/process/myProcess.png", "D:/process/myProcess.zip");
		//第四步部署流程
		jbpmManager.deployJbpmFromOutSrc("myProcess");
		
	     
	     
	     
	     
	     
			
	     
		
	}
	
	 public static String getPath() {
		  URL url =JbpmAction.class.getResource("");
		  File file = new File(url.getFile());
		  String path = file.getParent();
		  while (-1 != path.lastIndexOf("bin") || -1 != path.lastIndexOf(".jar")) {
		   file = new File(path);
		   path = file.getParent();
		  }
		  if (path.startsWith("file")) {
		   path = path.replaceAll("file:", "");
		  }

		  path = path + File.separator+"JmailConfig.xml";
		  return path;
		 }

	/**
	 * 请求显示流程图片
	 * 
	 * @return
	 */
	public String view() {
		//activityCoordinates = jbpmManager.findActivityCoordinates(id);
		String myId=getId();
		myId=StringUtil.ISOtoGB(myId);
		activityCoordinates = jbpmManager.findActivityCoordinates(myId);
		return SUCCESS;
	}
	
	
  /**
   * 
   * @return
  *@author LYK
  *@date 2013-7-1 下午06:40:04
  *@descriptions:(显示流程图的方法)
   */
	public String showProcessView() {
		activityCoordinates = jbpmManager.findActivityCoordinates(id);
		InputStream inputStream = jbpmManager.findPicInputStream(id);
		setActivityCoordinates(activityCoordinates);
		PrintWriter pw = null;
	    HttpServletResponse response=	ServletActionContext.getResponse();
		if (inputStream == null) {
			try {
				pw = response.getWriter();
				pw.write("error");
			} catch (IOException e) {
				//e.printStackTrace();
				org.jbpm.internal.log.Log.getLog("读取不到图片文件!");
			} finally {
				pw.close();
			}
		} else {
			byte[] b = new byte[1024];
			int len = -1;
			ServletOutputStream sos = null;
			try {
				sos = response.getOutputStream();
				while ((len = inputStream.read(b)) != -1) {
					sos.write(b, 0, len);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (sos != null) {
					try {
						sos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			}
			

		}
	/*	String id=getId();
	       List<Task> list=jbpmManager.getTasks("李永克");
	        for (Task task : list) {  
	            System.out.println("任务活动名称:"+  
	                    task.getActivityName());  
	            System.out.println("流程实例Id:"+  
	                    task.getExecutionId());  
	            System.out.println("任务活动Id:"+  
	                    task.getId());  
	            System.out.println("任务活动创建时间:"+  
	                    task.getCreateTime());  
	            System.out.println("任务活动进度:"+  
	                    task.getProgress());  
	            System.out.println("任务活动分配给:"+  
	                    task.getAssignee());  
	            System.out.println(id);
	            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");  
	        } 
	        setId("250001");
	        complateTaskByProcessId();*/
		
		return null;
	}
	
	/**
	 * 
	 * 
	*@author LYK
	*@date 2013-7-6 下午04:54:55
	*@descriptions:(根据流程实例的id删除这个流程)
	 */
	public void delteProcessById(){
		jbpmManager.delteProcessById(id);
	}
	/**
	 * 
	 * 
	*@author LYK
	*@date 2013-7-2 下午06:27:56
	*@descriptions:(注释类的作用)
	 */
	public void showView(){

		InputStream inputStream = jbpmManager.findPicInputStream(id);
		activityCoordinates = jbpmManager.findActivityCoordinates(id);
		PrintWriter pw = null;
	    HttpServletResponse response=	ServletActionContext.getResponse();
		if (inputStream == null) {
			try {
				pw = response.getWriter();
				pw.write("error");
			} catch (IOException e) {
				//e.printStackTrace();
				org.jbpm.internal.log.Log.getLog("读取不到图片文件!");
			} finally {
				pw.close();
			}
		} else {
			byte[] b = new byte[1024];
			int len = -1;
			ServletOutputStream sos = null;
			try {
				sos = response.getOutputStream();
				while ((len = inputStream.read(b)) != -1) {
					sos.write(b, 0, len);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (sos != null) {
					try {
						sos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			}
			

		}
	
	}
	/**
	 * 
	 * 
	*@author LYK
	 * @throws IOException 
	*@date 2013-7-3 下午04:28:41
	*@descriptions:(显示组织结构的树,已便于用户点击发布流程时选择对应的人事)
	*{"allowChildren", "allowEdit", "dpt", "draggable", "iconCls", "id", "leaf", "parentId", "text"}
	*
	*dptname;
	private String dptnode;
	private String parentnode;
	 */
	@SuppressWarnings("unchecked")
	public void showDptNamesList() throws IOException{/*
		List<Orgnazie> organizeList=(List<Orgnazie>) queryToList(Orgnazie.class);
		String []os={"dptname","dptnode","parentnode"};
		String []ts={"text","id","parentId"};
		List<EmployeeBean> empList=(List<EmployeeBean>) super.queryToList(EmployeeBean.class);
		DptTreeModel parentNode=null;
		for(Orgnazie o:organizeList){
			if(o.getDptnode().length()==1){
				parentNode=(DptTreeModel) MyBeanHelpers.invokeBeanToAnthoer(o, os, DptTreeModel.class, ts);
				parentNode.setAllowChildren(true);
				parentNode.setAllowEdit(false);
				parentNode.setIconCls("org-dept");
				parentNode.setLeaf(false);
				parentNode.setDraggable(true);
				parentNode.setChecked(false);
			}
		}
		List<DptTreeModel>childernList=new ArrayList<DptTreeModel>();
	
		for(Orgnazie o:organizeList){
			if(o.getDptnode().length()>1){
			DptTreeModel d=(DptTreeModel) MyBeanHelpers.invokeBeanToAnthoer(o, os, DptTreeModel.class, ts);
			d.setAllowChildren(true);
			d.setAllowEdit(false);
			d.setIconCls("org-dept");
			d.setLeaf(false);
			d.setDraggable(true);
			if(empList.size()>=1){
				List<DptTreeModel>childernList1=new ArrayList<DptTreeModel>();
			for(EmployeeBean e:empList){
				if(e.getDepartmentName().trim().equals(o.getDptname())){
				DptTreeModel d1=new DptTreeModel();
				d1.setAllowChildren(false);
				d1.setAllowEdit(false);
				d1.setIconCls("org-user");
				d1.setLeaf(true);
				d1.setDraggable(true);
				d1.setId(e.getEmployeeId()+"");
				d1.setText(e.getEmployeeName());
				d1.setParentId(d.getId());
		      	childernList1.add(d1);
		   
				}
				
			}
			d.setChildren(childernList1);
			childernList.add(d);
			
			}
			}
		}
		
		parentNode.setChildren(childernList);
		String json=JSONArray.fromObject(parentNode).toString();
		super.sendJsonByObject(json);
		
	*/}
	/**
	 * 
	 * 
	*@author LYK
	*@date 2013-7-6 下午04:36:30
	*@descriptions:(执行流程任务的方法)
	 */
	public void complateTaskByProcessId(){
		
		jbpmManager.complateTaskByProcessId(id);
		
		
	}
	 
	
	/**
	 * 
	 * 
	*@author LYK
	*@date 2013-7-4 下午03:43:11
	*@descriptions:(展现流程名称的action)
	 */
	public void showProcessNameList(){
	ProcessNameBean p=new ProcessNameBean();
	p.setCode("1");
	p.setId("101");
	p.setName("请假流程");
	List<ProcessNameBean> l=new ArrayList<ProcessNameBean>();

		l.add(p);
		p=new ProcessNameBean();
		p.setCode("2");
		p.setId("102");
		p.setName("收文审批流程");
		l.add(p);
		p=new ProcessNameBean();
		p.setName("发文审批流程");
		p.setCode("3");
		p.setId("103");
		l.add(p);
	
	
	super.sendJsonToAJAX(JSONArray.fromObject(l).toString());
		
		
	}

	public List<ProcessDefinition> getProcessDefinitions() {
		return processDefinitions;
	}

	public void setProcessDefinitions(List<ProcessDefinition> processDefinitions) {
		this.processDefinitions = processDefinitions;
	}

	public List<ProcessInstance> getProcessInstances() {
		return processInstances;
	}

	public void setProcessInstances(List<ProcessInstance> processInstances) {
		this.processInstances = processInstances;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public List<Task> getTasks() {
		return tasks;
	}

	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}


	public void setJbpmManager(JbpmManager jbpmManager) {
		this.jbpmManager = jbpmManager;
	}

	public ProcessDto getProcessDto() {
		return processDto;
	}

	public void setProcessDto(ProcessDto processDto) {
		this.processDto = processDto;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<HistoryProcessInstance> getHistoryProcessInstances() {
		return historyProcessInstances;
	}

	public void setHistoryProcessInstances(
			List<HistoryProcessInstance> historyProcessInstances) {
		this.historyProcessInstances = historyProcessInstances;
	}

	public ActivityCoordinates getActivityCoordinates() {
		return activityCoordinates;
	}

	public void setActivityCoordinates(ActivityCoordinates activityCoordinates) {
		this.activityCoordinates = activityCoordinates;
	}
	
	public List<String> getDeployProcessInstanceName() {
		return deployProcessInstanceName;
	}

	public void setDeployProcessInstanceName(List<String> deployProcessInstanceName) {
		this.deployProcessInstanceName = deployProcessInstanceName;
	}
}
最近下载更多
胜过这首歌  LV2 2023年6月27日
17852250910  LV2 2021年12月27日
157554513749  LV11 2020年8月1日
xuyongff  LV24 2020年3月3日
chaoy_nx  LV8 2020年2月25日
三生石sh1  LV13 2019年10月21日
逛逛逛  LV2 2019年8月13日
alreadyhome  LV10 2019年6月6日
hanl  LV12 2019年2月22日
wj024835  LV8 2019年1月25日
最近浏览更多
胜过这首歌  LV2 2023年6月27日
liuzejuncn  LV6 2022年3月27日
17852250910  LV2 2021年12月27日
qwqqwq123 2021年7月14日
暂无贡献等级
hollwomanc  LV1 2021年4月9日
pxqtsht  LV16 2021年4月1日
zhaoyu_2016  LV2 2021年1月12日
chenliuyang 2020年10月13日
暂无贡献等级
随便取个名字_哈哈  LV27 2020年10月5日
157554513749  LV11 2020年8月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友