田源
2025-01-16 cfd8d4c470cc6db6f6689ebf01eae07e47a46990
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package plm.portal.utility;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
import org.dom4j.DocumentException;
 
import com.vci.base.server.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
 
import plm.bs.bom.common.BusinessObject;
import plm.bs.bom.common.LinkObject;
import plm.bs.bom.common.constants.ObjectTool;
import plm.bs.omd.linktype.client.LinkTypeStart;
import plm.bs.omd.statepool.client.StatePoolStart;
import plm.corba.attribpool.AttribItem;
import plm.corba.btm.BtmAndApName;
import plm.corba.enumManager.EnumChild;
import plm.corba.enumManager.EnumItem;
import plm.corba.enumManager.EnumItemFilter;
import plm.corba.linkTypeManager.LinkType;
import plm.corba.refquery.RefPath;
import plm.corba.statePoolManager.StatePool;
import plm.oq.objectQuery.common.OrderInfo;
import plm.oq.objectQuery.common.PageInfo;
import plm.portal.custom.ICustomDefine;
import plm.portal.utility.DataModelProcessor;
import plm.portal.utility.PRMItem;
import plm.uif.engine.common.CBOHelper;
import plm.vo.omd.attribpool.ui.APClient;
import plm.vo.omd.btm.ui.BtmClient;
import plm.vo.omd.enumManager.ui.EnumClient;
 
public class TableDataUtil {
    
    public final String LCSTATUS = "lcstatus";
    private HashMap<String, HashMap<String, String>> typeAttrEnumValueToNameMap = new HashMap<String,HashMap<String,String>>();
    private HashMap<String, HashMap<String, String>> typeAttrEnumNameToValueMap = new HashMap<String,HashMap<String,String>>();
    
    /**
     * 查询业务对象的详细信息
     * @param qtName, 查询模版
     * @param replaceMap, 替换map
     * @param conditionMap, 附加条件
     * @param pageInfo, 翻页信息
     * @param orderInfos, 排序信息
     * @param showType, 显示类型
     * @param queryColumnsMap, 查询列, key为查询列名,value为显示名称
     * @param customMap, 自定义列
     * @return
     * @throws VCIError
     * @throws DocumentException
     */
    public List<Map<String, String>> getObjectDataList(String qtName, Map<String, String> replaceMap, 
            Map<String, String> conditionMap, PageInfo pageInfo, List<OrderInfo> orderInfos, String showType,
            Map<String, String> queryColumnsMap, Map<String, PRMItem> customMap, Map<String, PRMItem> fileMap) throws VCIError, DocumentException {
        DataModelProcessor processor = new DataModelProcessor();
        BusinessObject[] objs = processor.getBusinessObjectByQueryTemplate(
                qtName, replaceMap, conditionMap, pageInfo, orderInfos);
        List<Map<String, String>> datas = getObjectDataSecondList(processor, objs, showType, queryColumnsMap);
        if (customMap != null && customMap.size() > 0) {
            getCustomValue(customMap, datas);
        }
        if (fileMap != null && fileMap.size() > 0) {
            getFileColValue(fileMap, datas);
        }
        return datas;
    }
    
    /**
     * 获取业务对象二次查询的结果
     * @param processor
     * @param objs, 业务对象
     * @param showType, 显示类型
     * @param formColumnsMap,key为内部主键,value为显示名称(在此过程中暂不使用,可以传入空值)
     */
    private List<Map<String, String>> getObjectDataSecondList(
            DataModelProcessor processor,
            BusinessObject[] objs, String showType,
            Map<String, String> queryColumnsMap){
        @SuppressWarnings("unchecked")
        Map<String, String>[] res = new LinkedHashMap[0];
        res = CBOHelper.getBOValueMap(objs);
        Map<String, List<String>>  referenceKeyMap = processor.getReferenceCol(queryColumnsMap);
        if (referenceKeyMap != null && referenceKeyMap.size() > 0) {
            Iterator<String> referenceKey = referenceKeyMap.keySet().iterator();
            List<String> referenceList = new LinkedList<String>();
            while (referenceKey.hasNext()) {
                referenceList.add(referenceKey.next());
            }
            Map<String, Map<String, String>> rowRefMap = new LinkedHashMap<String, Map<String, String>>();
            Map<String, Boolean> emptyCheckMap = new LinkedHashMap<String, Boolean>();
            for (BusinessObject obj : objs) {
                String boOid = obj.oid;
                Map<String, String> refMap = new LinkedHashMap<String, String>();
                for (int j = 0; j < referenceList.size(); j++) {
                    String refKey = referenceList.get(j);
                    String refVal = ObjectTool.getBOAttributeValue(obj, refKey);
                    if (refVal == null) {
                        refVal = "";
                        emptyCheckMap.put(refVal, true);
                    } else {
                        emptyCheckMap.put(refKey, false);
                    }
                    refMap.put(refKey, refVal);
                }
                rowRefMap.put(boOid, refMap);
            }
            
            Map<String, String> emptyMap = new HashMap<String, String>();
            Iterator<String> emptyCheckItor = emptyCheckMap.keySet().iterator();
            List<String> delKeyList = new ArrayList<String>();
            while (emptyCheckItor.hasNext()) {
                String emptyCheckKey = emptyCheckItor.next();
                boolean  emptyCheckVal = emptyCheckMap.get(emptyCheckKey);
                if (!emptyCheckVal) {
                    continue;
                }
                List<String> list  = referenceKeyMap.get(emptyCheckKey);
                emptyMap.put(emptyCheckKey, "");
                for (int j = 0; j < list.size(); j++) {
                    emptyMap.put(list.get(j), "");
                }
                referenceKeyMap.remove(emptyCheckKey);
                delKeyList.add(emptyCheckKey);
            }
            if (delKeyList.size() > 0) {
                Iterator<String> itor = rowRefMap.keySet().iterator();
                while (itor.hasNext()) {
                    Map<String, String> crowMap = rowRefMap.get(itor.next());
                    for (int i = 0; i < delKeyList.size(); i++) {
                        crowMap.remove(delKeyList.get(i));
                    }
                }
            }
            
            Map<String, String> secQueryMap = processor.getQueryBusinessObjectCondition(showType, rowRefMap, referenceKeyMap);
            RefPath[] refPaths = processor.queryReference(secQueryMap, showType);
            connectSecondQueryResult(res, refPaths, secQueryMap, referenceKeyMap);
        }
        
        List<Map<String, String>> listRes = new LinkedList<Map<String, String>>();
        for (Map<String, String> resMap : res) {
            listRes.add(resMap);
        }
        return listRes;
    }
    
    /**
     * 将二次查询结果拼接到一次查询结果中
     * @param res
     * @param refPaths
     * @param secQueryMap
     * @param referenceKeyMap
     */
    private void connectSecondQueryResult(Map<String, String>[] res, RefPath[] refPaths, Map<String, String> secQueryMap, Map<String, List<String>>  referenceKeyMap) {
        if (refPaths != null) {
            int colNum = secQueryMap.size();
            for (int i = 0; i < res.length; i++) {
                Map<String, String> rowVal = res[i];
                for (int j = 0; j < colNum; j++) {
                    String key = secQueryMap.keySet().toArray(new String[colNum])[j];
                    if (referenceKeyMap.containsKey(key)) {
                        continue;
                    }
                    if (refPaths[j].values.length <= i) {
                        continue;
                    }
                    String cVal = refPaths[j].values[i].value;
                    rowVal.put(secQueryMap.keySet().toArray(new String[colNum])[j], cVal);
                }
            }
        }
    }
    
    /**
     * 获取自定义列的值
     * @param customMap, 自定义查询列
     * @param datas, 查询结果
     * @throws VCIError
     */
    private void getCustomValue(Map<String, PRMItem> customMap, List<Map<String, String>> datas) throws VCIError {
        Iterator<String> itor = customMap.keySet().iterator();
        while (itor.hasNext()) {
            String key = itor.next();
            PRMItem prm = customMap.get(key);
            String customCls = prm.getItemCustomClass();
            if(customCls == null || "".equals(customCls)){
                continue;
            }
            executeSetCustomValue(customCls, key, customMap, datas);
        }
    }
    
    /**
     * 执行自定义列类获取具体的值
     * @param customCls, 自定义类
     * @param key, 自定义列主键
     * @param customMap, 自定义列对应的Map
     * @param datas, 数据
     */
    private void executeSetCustomValue(String customCls, String key, Map<String, PRMItem> customMap, List<Map<String, String>> datas){
        try {
            int paramIndex = customCls.indexOf("?");
            String param = "";
            if(paramIndex > 0){
                param = customCls.substring(paramIndex + 1);
                customCls = customCls.substring(0, paramIndex);
            }
            Class<?> cls = Class.forName(customCls);
            ICustomDefine custom = (ICustomDefine)cls.getConstructor().newInstance();
            if(param != null && !"".equals(param)){
                key = key + "?" + param;
            }
            custom.getCustomValue(key, customMap, datas);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    
    private void getFileColValue(Map<String, PRMItem> fileMap, List<Map<String, String>> datas) throws VCIError {
        try {
            Iterator<String> itor = fileMap.keySet().iterator();
            while (itor.hasNext()) {
                String key = itor.next();
                PRMItem prm = fileMap.get(key);
                String customCls = prm.getItemCustomClass();
                if(customCls == null || "".equals(customCls)){
                    customCls = "plm.portal.custom.CustomFileInterceptor";
                }
                executeSetCustomValue(customCls, key, fileMap, datas);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * 获取链接对象的详细信息
     * @param qtName, 查询模版
     * @param replaceMap, 替换map
     * @param conditionMap, 附加条件
     * @param linkType, 显示类型
     * @param isForward, 方向
     * @param queryColumnsMap, 查询列
     * @param customMap, 自定义列
     * @return
     * @throws VCIError
     * @throws DocumentException
     */
    public List<Map<String, String>> getLinkDataList(String qtName, Map<String, String> replaceMap, Map<String, String> conditionMap, boolean isForward, 
            String linkType, Map<String, String> queryColumnsMap, Map<String, PRMItem> customMap, Map<String, PRMItem> fileMap) throws VCIError, DocumentException {
        DataModelProcessor processor = new DataModelProcessor();
        LinkObject[] los = processor.getLinkObjectByQueryTemplate(qtName, replaceMap, conditionMap);
        List<Map<String, String>> datas = getLinkDataSecondList(processor, los, isForward, linkType, queryColumnsMap);
        if (customMap != null) {
            getCustomValue(customMap, datas);
        }
        if (fileMap != null && fileMap.size() > 0) {
            getFileColValue(fileMap, datas);
        }
        return datas;
    }
    
    /**
     * 返回LO二次查询结果
     * @param processor
     * @param los
     * @param isForward
     * @param linkType
     * @param formColumnsMap
     * @return
     */
    private List<Map<String, String>>  getLinkDataSecondList(
            DataModelProcessor processor,
            LinkObject[] los, boolean isForward, String linkType, 
            Map<String, String> queryColumnsMap){
        @SuppressWarnings("unchecked")
        Map<String, String>[] res = new LinkedHashMap[0];
        Map<String, List<String>>  referenceKeyMap = processor.getReferenceCol(queryColumnsMap);
        
        res = CBOHelper.getLOValueMap(los);
        if (referenceKeyMap.containsKey("t_oid")) {
            Map<String, String> rowRefMap = new LinkedHashMap<String, String>();
            Map<String, String> rowKeyMap = new LinkedHashMap<String, String>();
            String[] cTypes = new String[los.length];
            String cType = los[0].toBTMName;
            boolean isSingleType = true;
            for (int i = 0; i < los.length; i++) {
                String loOid = los[i].toOid;
                String refKey = "t_oid";
                rowRefMap.put(loOid, refKey);
                rowKeyMap.put(new ObjectUtility().getNewObjectID36(), loOid);
                if (!cType.equals(los[i].toBTMName)) {
                    isSingleType = false;
                }
                cTypes[i] = los[i].toBTMName;
            }
            Map<String, String> secQueryMap = processor.getQueryLinkObjectCondition(linkType, rowKeyMap, rowRefMap, referenceKeyMap);
            RefPath[] refPaths;
            if (isSingleType) {
                refPaths = processor.queryReference(secQueryMap, cType);
            } else {
                refPaths = processor.queryReference(secQueryMap, cTypes);
            }
            connectSecondQueryResult(res, refPaths, secQueryMap, referenceKeyMap);
            referenceKeyMap.remove("t_oid");
        }
        if (referenceKeyMap.containsKey("f_oid")) {
            Map<String, String> rowRefMap = new LinkedHashMap<String, String>();
            Map<String, String> rowKeyMap = new LinkedHashMap<String, String>();
            String[] cTypes = new String[los.length];
            String cType = los[0].fromBTMName;
            boolean isSingleType = true;
            for (int i = 0; i < los.length; i++) {
                String loOid = los[i].fromOid;
                String refKey = "f_oid";
                rowRefMap.put(loOid, refKey);
                rowKeyMap.put(new ObjectUtility().getNewObjectID36(), loOid);
                if (!cType.equals(los[i].fromBTMName)) {
                    isSingleType = false;
                }
                cTypes[i] = los[i].fromBTMName;
            }
            Map<String, String> secQueryMap = processor.getQueryLinkObjectCondition(linkType, rowKeyMap, rowRefMap, referenceKeyMap);
            RefPath[] refPaths;
            if (isSingleType) {
                refPaths = processor.queryReference(secQueryMap, cType);
            } else {
                refPaths = processor.queryReference(secQueryMap, cTypes);
            }
            connectSecondQueryResult(res, refPaths, secQueryMap, referenceKeyMap);
            referenceKeyMap.remove("f_oid");
        }
        
        Iterator<String> referenceKey = referenceKeyMap.keySet().iterator();
        List<String> referenceList = new LinkedList<String>();
        while (referenceKey.hasNext()) {
            referenceList.add(referenceKey.next());
        }
        if (referenceList.size() > 0) {
            Map<String, Map<String, String>> rowRefMap = new LinkedHashMap<String, Map<String, String>>();
            for (int i = 0; i < los.length; i++) {
                String boOid = los[i].oid;
                Map<String, String> refMap = new LinkedHashMap<String, String>();
                for (int j = 0; j < referenceList.size(); j++) {
                    String refKey = referenceList.get(j);
                    String refVal = ObjectTool.getLOAttributeValue(los[i], refKey);
                    if (refVal == null) {
                        refVal = "";
                    }
                    refMap.put(refKey, refVal);
                }
                rowRefMap.put(boOid, refMap);
            }
            Map<String, String> secQueryMap = processor.getQueryBusinessObjectCondition(linkType, rowRefMap, referenceKeyMap);
            RefPath[] refPaths = processor.queryReference(secQueryMap, "");
            connectSecondQueryResult(res, refPaths, secQueryMap, referenceKeyMap);
        }
        
        List<Map<String, String>> listRes = new LinkedList<Map<String, String>>();
        for (Map<String, String> resMap : res) {
            listRes.add(resMap);
        }
        return listRes;
    }
    
    /**
     * 国际化生命周期状态
     * @param datas
     */
    public void displayLifeCycleStatus(List<Map<String, String>> datas) {
        Map<String, StatePool> statePoolsMap = getAllStatusInfo();
        for (Map<String, String> data : datas) {
            Iterator<String> itor = data.keySet().iterator();
            while (itor.hasNext()) {
                String key = itor.next();
                if (!key.equalsIgnoreCase(LCSTATUS) && !key.toLowerCase().endsWith("." + LCSTATUS)) {
                    continue;
                }
                String value = data.get(key);
                if (statePoolsMap.containsKey(value)) {
                    data.put(key, statePoolsMap.get(value).tag);
                }
            }
        }
    }
    
    /**
     * 获取所有的生命周期状态
     * @return
     */
    private Map<String, StatePool> getAllStatusInfo() {
        Map<String, StatePool> statePoolsMap = new HashMap<String, StatePool>();
        try {
            StatePool[] statePools = StatePoolStart.getService().getStatePools();
            for(StatePool sp:statePools){
                statePoolsMap.put(sp.name, sp);
            }
        } catch (VCIError e) {
            e.printStackTrace();
        }
        return statePoolsMap;
    }
    
    /**
     * 显示链接类型数据的枚举值
     * @param datas
     * @param linkType
     */
    public void displayLinkObjectEnumValue(List<Map<String, String>> datas, String linkType) {
        String[] linktypes = new String[1];
        linktypes[0] = linkType;
        calcAttrEnumMapForLinkType(linktypes);
        getEnumValue(datas);
    }
    
    /**
     * 显示业务类型数据的枚举值
     * @param datas
     * @param type
     */
    public void displayBusinessObjectEnumValue(List<Map<String, String>> datas, String type) {
        String[] types = new String[1];
        types[0] = type;
        calcAttrEnumMapForBtmType(types);
        getEnumValue(datas);
    }
    
    private void getEnumValue(List<Map<String, String>> datas) {
        for (Map<String, String> data : datas) {
            Iterator<String> itor = data.keySet().iterator();
            while (itor.hasNext()) {
                String key = itor.next();
                String ckey = "";
                if (typeAttrEnumValueToNameMap.containsKey(key)) {
                    ckey = key;
                } else if (key.indexOf(".") > 0){
                    ckey = key.substring(key.lastIndexOf(".") + 1);
                    if (!typeAttrEnumValueToNameMap.containsKey(ckey)) {
                        continue;
                    }
                } else {
                    continue;
                }
                String value = data.get(key);
                HashMap<String, String> enumValue = typeAttrEnumValueToNameMap.get(ckey);
                if (enumValue.containsKey(value)) {
                    value = enumValue.get(value);
                }
                data.put(key, value);
            }
        }
    }
    
    /**
     * 计算指定链接类型 枚举属性的值map
     * @param types 业务类型名称集合
     */
    public void calcAttrEnumMapForLinkType(String[] linkTypes){
        try {
            LinkType[] lts = LinkTypeStart.getService().getLinkTypes();
            for(String type : linkTypes){
                for(LinkType lt : lts){
                    if(type.equals(lt.name)){
                        calcAttrEnumMapCommonLogic(lt.name, lt.attributes);
                        calcAttrEnumMapForBtmType(lt.btmItemsFrom);
                        calcAttrEnumMapForBtmType(lt.btmItemsTo);
                    }
                }
            }
        } catch (VCIError e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 计算指定业务类型 枚举属性的值map
     * @param types 业务类型名称集合
     */
    public void calcAttrEnumMapForBtmType(String[] btmTypes){
        try {
            for(String type : btmTypes){
                BtmAndApName[] baans = BtmClient.getService().getBtmAndApNameArray(type);
                for(BtmAndApName baan : baans) {
                    calcAttrEnumMapCommonLogic(baan.btmName, baan.apName);
                }
            }
        } catch (VCIError e) {
            e.printStackTrace();
        }
    }
    
    private void calcAttrEnumMapCommonLogic(String type, String[] attrNames) throws VCIError{
        AttribItem[] arItems = APClient.getService().getAttribItemsByNames(attrNames);
        for(AttribItem ai : arItems){
            //allowNull = yes;enumName = parttypeenum
            String[] others = ai.other.split(";");
            for(String other : others){
                String[] cvs = other.split("=");
                if(cvs[0].trim().equalsIgnoreCase("enumName")){
                    String enumName = cvs[1].trim();
                    HashMap<String, String> enumValueToName = getEnumValueOrNameMap(enumName, true);
                    HashMap<String, HashMap<String, String>> fieldToEnumValuetoNameMap = new HashMap<String, HashMap<String,String>>();
                    fieldToEnumValuetoNameMap.put(ai.name, enumValueToName);
                    typeAttrEnumValueToNameMap.putAll(fieldToEnumValuetoNameMap);
                    
                    HashMap<String, String> enumNameToValue = getEnumValueOrNameMap(enumName, false);
                    HashMap<String, HashMap<String, String>> fieldToEnumNameToValueMap = new HashMap<String, HashMap<String,String>>();
                    fieldToEnumNameToValueMap.put(ai.name, enumNameToValue);
                    typeAttrEnumNameToValueMap.putAll(fieldToEnumNameToValueMap);
                }
            }
        }
    }
    
    private HashMap<String, EnumItem> enumItemMap = new HashMap<String, EnumItem>();
    private HashMap<String, String> getEnumValueOrNameMap(String enumName, boolean valueToName){
        HashMap<String, String> map = new HashMap<String, String>();
        try {
            if(enumItemMap.size() == 0){
                EnumItemFilter filter = new EnumItemFilter();
                EnumItem[] enumItems = EnumClient.getService().getEmItems(filter, 1, 1);
                for(EnumItem ei : enumItems){
                    enumItemMap.put(ei.name, ei);
                }
            }
            if(enumItemMap.containsKey(enumName)){
                EnumItem ei = enumItemMap.get(enumName);
                for(EnumChild ec : ei.children){
                    if(valueToName){
                        map.put(ec.value, ec.name);
                    } else {
                        map.put(ec.name, ec.value);
                    }
                }
            }
        } catch (VCIError e) {
            e.printStackTrace();
        }
        return map;
    }
}