田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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<Boolean>{   
  
    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<String> 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<String, Object> map = new HashMap<String, Object>();
                //map.put("OUTCOME", outcom);
                 taskService.completeTask(joinTask.getId()); 
                 List<Task> 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<String, Object> map = new HashMap<String, Object>();
                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<String> outcomeList = (List<String>)taskService.getVariable(parentTask.getId(), VAR_OUTCOME_IN_COUNTER);
             if(outcomeList == null){
                 outcomeList = new ArrayList<String>();
             }
             outcomeList.add(sign);
             Map<String,Object> varMap = new HashMap<String, Object>();
             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<String>());
                 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);
        }
    }
}