package com.vci.server.workflow.server.event; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.sql.Connection; 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 org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.jbpm.api.ProcessEngine; import org.jbpm.api.RepositoryService; import org.jbpm.api.TaskService; import org.jbpm.api.cmd.Command; import org.jbpm.api.cmd.Environment; import org.jbpm.api.task.Task; import com.vci.common.resource.CommonProperties; import com.vci.corba.common.VCIError; import com.vci.server.base.persistence.dao.HibernateTemplate; import com.vci.server.workflow.common.FlowConstants; import com.vci.server.workflow.dao.PlflowapproveopinionDaoImpl; import com.vci.server.workflow.server.JbpmEngine; import com.vci.server.workflow.server.interfaces.PLMWorkItemInterface; public class Sign implements Command{ private static final long serialVersionUID = 1L; public static String VAR_SIGN="sign"; /** * 会签的处理意见 */ private static final String VAR_OUTCOME_IN_COUNTER = "outComeInCounter"; /** * 会签结果 */ public static final String VAR_SIGN_RESULT = "resultInCounter"; private String outcom; private String parentTaskId; private Task parentTask; private Task joinTask; String pid; String deploymentId; private String userName = ""; public Sign(String parentTaskId,Task joinTask,String outcom,String userName,String deploymentId){ this.parentTaskId=parentTaskId; this.outcom=outcom; this.userName=userName; this.joinTask=joinTask; this.deploymentId=deploymentId; } public String getPid(){ return pid; } private static byte[] InputStreamToByte(InputStream is) throws IOException { ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); int ch; while ((ch = is.read()) != -1) { bytestream.write(ch); } byte imgdata[] = bytestream.toByteArray(); bytestream.close(); return imgdata; } public byte[] getProcessResource(String jbpmDeploymentId, String resourceType) throws Exception { ProcessEngine processEngine = JbpmEngine.getProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); Set resourceNames = repositoryService.getResourceNames(jbpmDeploymentId); for (String resourceName : resourceNames) { if (resourceName.endsWith(resourceType)) { InputStream is = repositoryService.getResourceAsStream(jbpmDeploymentId, resourceName); byte[] result = InputStreamToByte(is); return result; } } return new byte[0]; } /** * 获取会签的比例 * @return 没有设置的时候返回null * @throws Exception */ private Integer getVotePercent(String parentTakName) throws Exception{ byte[] processXml; InputStream in = null; try { processXml = getProcessResource(this.deploymentId, ".xml"); in = new ByteArrayInputStream(processXml); BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8")); SAXReader saxr = new SAXReader(); Document doc = saxr.read(reader); Element root = doc.getRootElement(); for (Iterator a = root.elementIterator(); a.hasNext();) { Element element = (Element) a.next(); String cellType = element.getName(); String cellName = element.attributeValue(FlowConstants.XMLNAME); if (FlowConstants.XMLTASK.equals(cellType) && cellName.equals(parentTakName)) {// 任务节点 Attribute at = element.attribute(FlowConstants.SIGN_VOTE_PRCENT); if(at!=null){ return Integer.valueOf(at.getValue()); } break; } } }catch(Throwable w){ w.printStackTrace(); throw new Exception("获取流程模板中配置的会签比例出错了,请联系管理员检查"); }finally{ if (in != null) in.close(); } return null; } public Boolean execute(Environment environment) throws Exception { TaskService taskService=environment.get(TaskService.class); parentTask=taskService.getTask(parentTaskId); pid=parentTask.getExecutionId(); String sign=(String)taskService.getVariable(joinTask.getId(), VAR_SIGN); String delPartSql = "update jbpm4_task set ASSIGNEE_ = '' where dbid_='"+parentTaskId+"'"; Connection conn = new HibernateTemplate().getSessionConnection(); new PlflowapproveopinionDaoImpl().deleteEntityBySQL(conn, delPartSql, new String[]{}); Integer vote = getVotePercent(parentTask.getActivityName()); if(vote == null || vote == 0){ if(sign!=null&&sign.equals("不同意")){ //Map map = new HashMap(); //map.put("OUTCOME", outcom); taskService.completeTask(joinTask.getId()); List subTasks = taskService.getSubTasks(parentTaskId); for(Task subTask : subTasks){ taskService.completeTask(subTask.getId()); } for(Task s : subTasks){ deleteForkTask(s.getExecutionId()); } return true; } else if(sign!=null&&sign.equals("同意")){ Map map = new HashMap(); map.put("OUTCOME", outcom); taskService.setVariables(joinTask.getId(),map); System.out.println("joinTask.completeTask开始!"); taskService.completeTask(joinTask.getId()); System.out.println("joinTask.completeTask开始!"); //将当前的用户的协作关系清掉 避免待办任务里继续出现 String delSql = "delete jbpm4_participation where task_='"+parentTaskId+"' and userid_=(select t.assignee_ from jbpm4_task t where t.dbid_='"+joinTask.getId()+"' )"; new PlflowapproveopinionDaoImpl().deleteEntityBySQL(conn, delSql, new String[]{}); } String updateSupperTaskSql = "update jbpm4_hist_task t set t.supertask_ = '" + parentTaskId + "' where execution_ ='" + joinTask.getExecutionId() + "'"; new PlflowapproveopinionDaoImpl().deleteEntityBySQL(conn, updateSupperTaskSql, new String[]{}); if(taskService.getSubTasks(parentTaskId).size()==0){ return true; } return false; }else{ //设置了会签的比例的,我们要等到全部都执行完成了后判断 taskService.completeTask(joinTask.getId()); //modify by weidy@2022-05-06 //执行任务后,子任务的历史上没有supertask_的值,所以这里手动加一下 String updateSupperTaskSql = "update jbpm4_hist_task t set t.supertask_ = '" + parentTaskId + "' where execution_ ='" + joinTask.getExecutionId() + "'"; new PlflowapproveopinionDaoImpl().deleteEntityBySQL(conn, updateSupperTaskSql, new String[]{}); String delSql = "delete jbpm4_participation where task_='"+parentTaskId+"' and userid_=(select t.assignee_ from jbpm4_task t where t.dbid_='"+joinTask.getId()+"' )"; new PlflowapproveopinionDaoImpl().deleteEntityBySQL(conn, delSql, new String[]{}); List outcomeList = (List)taskService.getVariable(parentTask.getId(), VAR_OUTCOME_IN_COUNTER); if(outcomeList == null){ outcomeList = new ArrayList(); } outcomeList.add(sign); Map varMap = new HashMap(); varMap.put(VAR_OUTCOME_IN_COUNTER,outcomeList); taskService.setVariables(parentTask.getId(), varMap); if(taskService.getSubTasks(parentTaskId).size()==0){ //已经完成了,我们需要判断是同意还是不同意 //必须要清除,否则被退回后再执行到这里的时候,会把以前结果也读取到了 varMap.put(VAR_OUTCOME_IN_COUNTER,new ArrayList()); taskService.setVariables(parentTask.getId(), varMap); int allowCount = 0; for(int i = 0 ; i < outcomeList.size(); i++){ if(!outcomeList.get(i).equalsIgnoreCase("不同意")){ allowCount ++; } } boolean passFlag = ((float)allowCount/(float)outcomeList.size())*100>=vote; varMap.clear(); varMap.put(VAR_SIGN_RESULT, passFlag?"同意":"不同意"); taskService.setVariables(parentTask.getId(), varMap); return true; } return false; } } private void deleteForkTask(String excutionid) throws VCIError, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, ClassNotFoundException, InstantiationException { String workitemClass = CommonProperties.getStringProperty("workflow.plmworkitem"); if(workitemClass!=null&&!"".equals(workitemClass)){ PLMWorkItemInterface pwService = (PLMWorkItemInterface) (Class.forName(workitemClass).newInstance()); pwService.deleteForkTask(excutionid); } } }