田源
2025-01-09 8a166a60cfd1a2e593ffa103d10c0dc224fc8628
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package com.vci.client.uif.actions.client;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
 
import javax.swing.JFrame;
 
import org.apache.commons.lang3.StringUtils;
 
import com.vci.client.bof.ClientBusinessObject;
import com.vci.client.bof.ClientLinkObject;
import com.vci.client.common.objects.UserEntityObject;
import com.vci.client.common.oq.OQTool;
import com.vci.client.oq.QTClient;
import com.vci.client.ui.exception.VCIException;
import com.vci.client.ui.process.QANProcessBar;
import com.vci.client.ui.process.QANProcessBarFrame;
import com.vci.client.uif.engine.common.IDataNode;
import com.vci.client.workflow.delegate.ProcessCustomClientDelegate;
import com.vci.client.workflow.launch.LaunchDialog;
import com.vci.common.qt.object.Condition;
import com.vci.common.qt.object.QueryTemplate;
import com.vci.common.qt.object.Version;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.corba.workflow.data.FlowInstanceInfo;
import com.vci.corba.workflow.data.MapTransfersInfo;
import com.vci.corba.workflow.data.ProcessTaskInfo;
import com.vci.mw.ClientContextVariable;
 
 
/**
 * 启动工作流程 Action
 * <p>
 * 1、基于选定数据进行的操作
 * </p>
 * <p>
 * 2、可以选择多条数据进行此操作
 * </p>
 * 
 * @author xiongchao
 * 
 */
public class LaunchWorkFlowAction extends AbstractBatchBusionessOperationAction {
 
    /**
     * 按钮参数
     */
    private Map<String, String> paramsMap = null;
 
    /**
     * 按钮配置的dialog列头。
     */
    private String headers = "";
    
    /**
     * 按钮配置的dialog列头对应的mapvalue中的key。
     */
    private String keys = "";
    
    /**
     * 登陆人员信息
     */
    private String loginUserName = "";
 
    private String processType = "";
    
    private String boType = "";
 
    private String[] objId = null;
    
    /**
     * dialog窗口数据。
     */
    private Map<String, Map<String, String>> values = new HashMap<String, Map<String, String>>();
    
    private String workflowStartState = "Working";
    
    /**
     * dialog窗口table列头。 
     */
    private String[] heads = null;
    
    private boolean errorNameFlag = false;//用于标记当对象为已发布,但无法取其标示时为true;
    
//    private String[] ids;
    
    LaunchDialog dialog = null;
    @Override
    public String getKey() {
        return "launchworkflow";
    }
 
    /*
     * (non-Javadoc)
     * 
     * @see plm.uif.actions.client.BusinessOperationAction#doPost()
     */
    @SuppressWarnings("deprecation")
    @Override
    public boolean doPost() {
        preLoadMessage();
        try {
            if(dialog == null){
                return false;
            }
            if(!dialog.isSaveFlag()){
                return false;
            }
            String processName = dialog.getProcessName();
            String processDec = dialog.getProcessDec();
            while(dialog.isSaveFlag() && checkProcessNameIsExist(dialog)){
                dialog.setSaveFlag(false);
                dialog.setVisible(true);
                processName = dialog.getProcessName();
            }
            if(!dialog.isSaveFlag()){
                return false;
            }
            Map<String, List<String>> taskAndUserMap = dialog
                    .getTaskAndUserMap();
            // 流程名称:加版本号
            String processDefinitionKey = dialog.getProcessTemplateName();
            int taskIndex = 0;
            // 任务名称数组
            String[] tasknames = new String[taskAndUserMap.size()];
            // 任务对应的用户数组
            String[][] taskUserNames = new String[taskAndUserMap.size()][];
            for (Entry<String, List<String>> entrySet : taskAndUserMap
                    .entrySet()) {
                tasknames[taskIndex] = entrySet.getKey();
                List<String> list = entrySet.getValue();
                String[] array = list.toArray(new String[] {});
                String[] userNames = new String[array.length];
                for (int userIndex = 0; userIndex < array.length; userIndex++) {
                    userNames[userIndex] = array[userIndex];
                }
                taskUserNames[taskIndex] = userNames;
                taskIndex++;
            }
            // 用户名
            String[] userName = new String[] {};
            ProcessCustomClientDelegate delegate = createProcessCustomClientDelegate();
            String deploymentID = delegate
                    .getDeploymentID(processDefinitionKey);
            ProcessTaskInfo firstProcessTask = delegate
                    .getFirstProcessTask(deploymentID);
            for (int i = 0; i < tasknames.length; i++) {
                if (firstProcessTask.taskName.equals(tasknames[i])) {
                    userName = new String[taskUserNames[i].length];
                    for (int j = 0; j < taskUserNames[i].length; j++) {
                        userName[j] = taskUserNames[i][j];
                    }
                }
            }
            // 流程实例对象
            FlowInstanceInfo flowInstanceInfo = new FlowInstanceInfo();
            flowInstanceInfo.creator = loginUserName;
            flowInstanceInfo.templatePuid = "";
            flowInstanceInfo.clsfOid = "";
            flowInstanceInfo.tableName = processType;
            flowInstanceInfo.applicant = loginUserName;
            flowInstanceInfo.desc = processDec == null ? "" : processDec;
            flowInstanceInfo.processType = boType;
            flowInstanceInfo.templateName = "";
            flowInstanceInfo.partList = "";
            flowInstanceInfo.processName = processName == null ? ""
                    : processName;
 
            MapTransfersInfo[] mapTransfersInfos = new MapTransfersInfo[1];
            MapTransfersInfo info = new MapTransfersInfo();
            info.key = "boType";
            info.value = boType; 
            mapTransfersInfos[0] = info;
            String outcome = "";
            return startPocess(delegate, processDefinitionKey, flowInstanceInfo, objId, 
                    userName, outcome, tasknames, taskUserNames, 
                    mapTransfersInfos);
                
        } catch (VCIError e) {
            e.printStackTrace();
            UIFUtils.showErrorMessage(ClientContextVariable.getFrame(), e);
            return false;
        } catch (VCIException e) {
            e.printStackTrace();
            UIFUtils.showErrorMessage(ClientContextVariable.getFrame(), e);
            return false;
        }
    }
 
    private void preLoadMessage() {
        final QANProcessBarFrame frame  = new QANProcessBarFrame();
        final QANProcessBar bar = new QANProcessBar(new Thread(new Runnable() {
            @SuppressWarnings("deprecation")
            @Override
            public void run() {
                try {
                    frame.setContent("正在校验数据......");
                    // 获取选择的数据
                    // 数据的实际类型一般为 IDataNode
                    Object[] objs = getDataModel().getSelectObjects();
                    paramsMap = getButtonParams();
                    String cWorkflowStartState = paramsMap.get("workflowStartState");
                    if (cWorkflowStartState != null && !cWorkflowStartState.equals("")) {
                        workflowStartState = cWorkflowStartState;
                    }
                    if(paramsMap.containsKey("header")){
                        headers = paramsMap.get("header");// 获取设置列头
                    }
                    if(paramsMap.containsKey("keys")){
                        keys = paramsMap.get("keys"); // 获取列头对应的属性值。
                    }
                    // 获取
                    if (headers.equals("")) {
                        headers = "编码##代号##名称";
                    }
                    if(keys.equals("")){
                        keys = "code##id##name";
                    }
                    setLaunchDialogMessage(objs);
                    dialog = new LaunchDialog(processType, heads, values);
                } catch (VCIError e) {
                    e.printStackTrace();
                    frame.setProcessBarCancel(true);
                    UIFUtils.showErrorMessage(ClientContextVariable.getFrame(), e);
                    return;
                }finally{
                    frame.setProcessBarCancel(true);
                }
            }
        }), (JFrame) ClientContextVariable.getFrame(), frame, "启动流程", false);
        bar.setVisible(true);
        if(dialog != null){
            dialog.setVisible(true);
        }
    }
 
    private boolean startPocess(
            final ProcessCustomClientDelegate delegate,
            final String processDefinitionKey, final FlowInstanceInfo flowInstanceInfo,
            final String[] objId, final String[] userName, final String outcome, final String[] tasknames,
            final String[][] taskUserNames, final MapTransfersInfo[] mapTransfersInfos) {
        final boolean[] ress = {false};
        final QANProcessBarFrame frame  = new QANProcessBarFrame();
        final QANProcessBar bar = new QANProcessBar(new Thread(new Runnable() {
            @SuppressWarnings("deprecation")
            @Override
            public void run() {
                try{
                    frame.setContent("正在提交流程,请稍等...");
                    String processInstansId = delegate.startPocessByPLM(
                            processDefinitionKey, flowInstanceInfo, objId,
                            userName, outcome, tasknames, taskUserNames,
                            mapTransfersInfos);
                    ress[0] = true;
                    frame.setProcessBarCancel(true);
                    UIFUtils.showMessage(ClientContextVariable.getFrame(),
                            "流程启动成功!\n【流程执行ID:" + processInstansId + "】");
                }catch(VCIError e){
                    frame.setProcessBarCancel(true);
                    UIFUtils.showErrorMessage(ClientContextVariable.getFrame(), e);
                } finally{
                    frame.setProcessBarCancel(true);
                }
            }
        }), (JFrame) ClientContextVariable.getFrame(), frame, "启动流程", false);
        bar.setVisible(true);
        return ress[0];
    }
    
    private void setLaunchDialogMessage(Object[] objs) throws VCIError {
        heads = headers.split("##");
        String[] key = keys.split("##");
        if(heads.length != key.length){
            throw new VCIError("plm.uif.actions.client.LaunchWorkFlowAction.headerNoEqualsKey", new String[]{});
        }
        int len = key.length;
        ArrayList<String[]> vals = new ArrayList<String[]>();
        objId = new String[objs.length];
        String boOid = "";
        String boLcstatus = "";
        StringBuffer processDataSb = new StringBuffer();
        for (int objectIndex = 0; objectIndex < objs.length; objectIndex++) {
            IDataNode idn = (IDataNode) objs[objectIndex];
            Map<String, String> valueMap = idn.getValueMap();
            String[] val = new String[len];
            String keyNotExist = "";
            for (int i = 0; i < len; i++) {
                if(valueMap.containsKey(key[i])){
                    val[i] = valueMap.get(key[i]);
                }else{
                    keyNotExist +="["+ key[i]+"]";
                }
            }
            if(!keyNotExist.equals("")){
                //抛出异常,key值不存在
                throw new VCIError("plm.uif.actions.client.LaunchWorkFlowAction.keyPropertyIsNull", new String[]{"{"+keyNotExist+"}"});
            }
            vals.add(val);
            boType = getBoType(idn);
            boOid = getBoOid(idn);
            boLcstatus = getBoLcStatus(boLcstatus, idn);
            if(StringUtils.isNotEmpty(workflowStartState)){
                if (!boLcstatus.toLowerCase().equals(workflowStartState.toLowerCase())) {
                    processDataSb.append(idn.toString()).append(",");
                }
            }
            objId[objectIndex] = boOid;
        }
        checkDataIfInProcess(processDataSb);
        createDatasForDialog(objId, heads, vals);
        // 分类名称
        processType = boType;
    }
 
    /**
     * 判断是否发起过流程
     * 
     * @param processDataSb
     *            发起国流程的对象名称字符串。
     * @throws VCIError 
     */
    private void checkDataIfInProcess(StringBuffer processDataSb) throws VCIError {
        if(errorNameFlag){
            throw new VCIError("所选数据中存在处于不能发起流程状态的数据,请重新选择!", new String[]{});
        }else if (processDataSb != null && processDataSb.length() > 0) {
            String mes = processDataSb.substring(0, processDataSb.length()-1);
            throw new VCIError("所选数据[ "+mes+" ]的状态不能启动流程,请重新选择!", new String[]{});
        }
    }
 
    private ProcessCustomClientDelegate createProcessCustomClientDelegate() {
        UserEntityObject userEntityObject = new UserEntityObject();
        String ip = ClientContextVariable.getInvocationInfo().clientIPInfo;
        loginUserName = ClientContextVariable.getInvocationInfo().userName;
        userEntityObject.setIp(ip);
        userEntityObject.setUserName(loginUserName);
        userEntityObject.setModules("");
        return new ProcessCustomClientDelegate(userEntityObject);
    }
 
    private void createDatasForDialog(
            String[] objId, String[] headers, ArrayList<String[]> vals) {
        for (int i = 0; i < objId.length; i++) {
            Map<String, String> oneValues = new HashMap<String, String>();
            for (int j = 0; j < headers.length; j++) {
                oneValues.put(headers[j], vals.get(i)[j]);
            }
            values.put(objId[i], oneValues);
        }
    }
 
    private String getBoType(IDataNode idn) {
        String boType = "";
        if (idn.getMaterObject() instanceof ClientBusinessObject) {
            boType = ((ClientBusinessObject) idn.getMaterObject()).getBtmName();
        } else if (idn.getMaterObject() instanceof ClientLinkObject) {
            if (idn.isForward()) {
                boType = ((ClientLinkObject) idn.getMaterObject())
                        .getToBTMName();
            } else {
                boType = ((ClientLinkObject) idn.getMaterObject())
                        .getFromBTMName();
            }
        }
        return boType;
    }
 
    private String getBoOid(IDataNode idn) {
        String boOid = "";
        if (idn.getMaterObject() instanceof ClientBusinessObject) {
            boOid = ((ClientBusinessObject) idn.getMaterObject()).getOid();
        } else if (idn.getMaterObject() instanceof ClientLinkObject) {
            if (idn.isForward()) {
                boOid = ((ClientLinkObject) idn.getMaterObject()).getToOid();
            } else {
                boOid = ((ClientLinkObject) idn.getMaterObject()).getFromOid();
            }
        }
        return boOid;
    }
 
    private String getBoLcStatus(String boLcstatus, IDataNode idn) {
        if (idn.getMaterObject() instanceof ClientBusinessObject) {
            boLcstatus = ((ClientBusinessObject) idn.getMaterObject())
                    .getLcStatus();
        } else if (idn.getMaterObject() instanceof ClientLinkObject) {
            if (idn.isForward()) {
                boLcstatus = idn.getValueMap().get("t_oid.lcstatus");
            } else {
                boLcstatus = idn.getValueMap().get("f_oid.lcstatus");
            }
        }
        return boLcstatus;
    }
    
    @SuppressWarnings("deprecation")
    private boolean checkProcessNameIsExist(LaunchDialog dialog) throws VCIError{
        if(!dialog.isSaveFlag()) return false;
        String processName = dialog.getProcessName();
        boolean res = false;
        QueryTemplate qt9 = new QueryTemplate();
        List<String> clauseList = new ArrayList<String>();
        clauseList.add("*");
        qt9.setClauseList(clauseList);
        qt9.setVersion(Version.lastRevLastVer);
        qt9.setBtmType("workitem");
        qt9.setRightFlag(false);
        qt9.setId("d");
        qt9.setType("btm");
        
        Map<String, String> conditions = new HashMap<String, String>();
        conditions.put("name", "*" + processName + "-*");
        conditions.put("workType", "2");
        conditions.put("businesstype", "5");
        Condition cond = OQTool.getCondition(conditions);
        qt9.setCondition(cond);
        BusinessObject[] d = QTClient.getService().findBTMObjects(qt9.getId(), OQTool.qtTOXMl(qt9).asXML());
        res = d.length > 0;
        if(res){
            UIFUtils.showMessage(ClientContextVariable.getFrame(), 
                    "流程名称:‘" + processName + "’已经存在,不允许出现重复的流程名称!");
        }
        return res;
    }
}