ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
package com.vci.server.query.delegate;
 
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
 
import org.apache.commons.lang3.ArrayUtils;
 
import com.vci.corba.omd.atm.AttribItem;
import com.vci.corba.omd.btm.BtmItem;
import com.vci.corba.omd.ltm.LinkType;
import com.vci.corba.query.data.RefPath;
import com.vci.corba.query.data.RefValue;
import com.vci.omd.objects.OtherInfo;
import com.vci.server.base.utility.ServerServiceProvider;
import com.vci.server.cache.OMCacheProvider;
import com.vci.server.query.refquery.objects.FromItem;
import com.vci.server.query.refquery.objects.MetaData;
import com.vci.server.query.refquery.objects.PLMRefPathInfo;
import com.vci.server.query.refquery.objects.PLMRefQueryPath;
import com.vci.server.query.refquery.utils.PLMRefSQLHelper;
import com.vci.server.query.refquery.utils.RefQueryUtils;
import com.vci.common.exception.VciExceptionTool;
import com.vci.common.log.ServerWithLog4j;
import com.vci.corba.common.VCIError;
 
public class RefQueryDelegate  {
    public static final String Other_Key = "btm";
    public static final String Root_key = "A_1_OID";
    public static final String ToOid = "t_oid";
    public static final String FromOid = "f_oid";
 
 
    /**
     * 依据数据库最大的限制OID的数目做分割查询
     * 
     * @param RefPath
     *            [] 参照路径数组
     * @return RefPath [] 参照路径数组
     * @throws Exception
     * */
    public RefPath[] limitNumQuery(RefPath[] paths, int limit)
            throws Throwable {
        List<RefPath> results = new ArrayList<RefPath>();
        // parse path
        PLMRefQueryPath[] refPaths = toQueryPath(paths);
        // to mode(a subtree of type tree )
        Map<String, List<PLMRefQueryPath>> queryConditions = toQuerymode(refPaths);
        results = queryEachMode(queryConditions, limit);
        return results.toArray(new RefPath[results.size()]);
    }
 
    public RefPath[] limitNumQuery(RefPath[] paths, String toType, int limit)
            throws Throwable {
        // 结果集合
        List<RefPath> results = new ArrayList<RefPath>();
        // 生成查询路径 parse path
        PLMRefQueryPath[] refPaths = toQueryPath(paths, toType);
        // 分割查询模式 to mode(a subtree of type tree )
        Map<String, List<PLMRefQueryPath>> queryConditions = toQuerymode(refPaths);
        // 查询每一个模式
        results = queryEachMode(queryConditions, limit);
        // 返回结果集合
        return results.toArray(new RefPath[results.size()]);
    }
    
    public RefPath[] limitNumQuery(RefPath[] paths, String[] toTypes, int limit)
            throws Throwable {
        
        // 结果集合
        List<RefPath> results = new ArrayList<RefPath>();
        // 查询每一个模式
        results = mulTypeQueury(paths, toTypes, limit);
        // 返回结果集合
        return results.toArray(new RefPath[results.size()]);
    }
    
    private List<RefPath> mulTypeQueury(RefPath[] paths, String[] toTypes, int limit) throws Throwable {
        RefValue[] rvs = paths[0].values;
        HashMap<String, List<RefValue>> map = new LinkedHashMap<String, List<RefValue>>();
        HashMap<String, List<Integer>> seqMap = new LinkedHashMap<String, List<Integer>>();
        
        for (int i = 0; i < rvs.length; i++) {
            String type = toTypes[i];
            List<RefValue> list = map.get(type);
            if (list == null) {
                list = new ArrayList<RefValue>();
                map.put(type, list);
            }
            list.add(rvs[i]);
            List<Integer> seqList = seqMap.get(type);
            if (seqList == null) {
                seqList = new ArrayList<Integer>();
                seqMap.put(type, seqList);
            }
            seqList.add(i);
        }
        Iterator<String> itor = map.keySet().iterator();
        int count = 0;
        List<List<RefPath>> returnList = new ArrayList<List<RefPath>>();
        Map<Integer, Integer> posMap = new LinkedHashMap<Integer, Integer>();
        Map<ArrayList<RefValue>, String> queryMap = new LinkedHashMap<ArrayList<RefValue>, String>();
        while (itor.hasNext()) {
            String cType = itor.next();
            List<RefValue> cList = map.get(cType);
            List<Integer> cSeqList = seqMap.get(cType);
            ArrayList<RefValue> midList = new ArrayList<RefValue>();
            for (int i = 0; i < cList.size(); i++) {
                midList.add(cList.get(i));
                posMap.put(count, cSeqList.get(i));
                if (count + 1 % limit == 0) {
                    queryMap.put(midList, cType);
                    returnList.add(multiTypeQuery(queryMap, paths));
                    queryMap = new HashMap<ArrayList<RefValue>, String>();
                    midList = new ArrayList<RefValue>();
                }
                count++;
            }
            queryMap.put(midList, cType);
            midList = new ArrayList<RefValue>();
        }
        if (queryMap.size() > 0) {
            returnList.add(multiTypeQuery(queryMap, paths));
        }
        List<RefPath> wrsList = null;
        for (int i = 0; i < returnList.size(); i++) {
            List<RefPath> rsList = returnList.get(i);
            if (i == 0) {
                wrsList = rsList;
                continue;
            }
            for (int j = 0; j < rsList.size(); j++) {
                ArrayUtils.addAll(wrsList.get(i).values, rsList.get(i).values);
            }
        }
        for (int i = 0; i < wrsList.size(); i++) {
            RefValue[] values = wrsList.get(i).values;
            RefValue[] newValues = new RefValue[values.length];
            for (int j = 0; j < values.length; j++) {
                newValues[posMap.get(j)] = values[j];
                newValues[posMap.get(j)].row = posMap.get(j);
            }
            wrsList.get(i).values = newValues;
        }
        
        return wrsList;
    }
    
    public List<RefPath> multiTypeQuery(Map<ArrayList<RefValue>, String> queryMap, RefPath[] paths) throws Throwable {
        List<RefPath> list = null;
        Iterator<ArrayList<RefValue>> itor = queryMap.keySet().iterator();
        String allSql = "";
        boolean isFirst = true;
        
        PLMRefQueryPath[] crefPaths = null;
        while (itor.hasNext()) {
            ArrayList<RefValue> next = itor.next();
            String type = queryMap.get(next);
            RefPath[] cpaths = new RefPath[paths.length];
            for (int i = 0; i < paths.length; i++) {
                RefPath cPath = new RefPath();
                cPath.path = paths[i].path;
                cPath.values = copyRefValues(next.toArray(new RefValue[next.size()]));
                cpaths[i] = cPath;
            }
            PLMRefQueryPath[] refPaths = toQueryPath(cpaths, type);
            String sql = PLMRefSQLHelper.generateSQL(refPaths);
            if (isFirst) {
                allSql = sql;
                crefPaths = refPaths;
                isFirst = false;
            } else {
                allSql += " union " + sql;
                for (int i = 0; i < crefPaths.length; i++) {
                    RefValue[] rfs1 = crefPaths[i].getPath().values;
                    RefValue[] rfs2 = refPaths[i].getPath().values;
                    RefValue[] trfs = new RefValue[rfs1.length + rfs2.length];
                    System.arraycopy(rfs1, 0, trfs, 0, rfs1.length);
                    System.arraycopy(rfs2, 0, trfs, rfs1.length, rfs2.length);
                    crefPaths[i].getPath().values = trfs;
                }
            }
        }
        list = getMultiQueryResult(crefPaths, allSql);
        return list;
    }
    
    private RefValue[] copyRefValues(RefValue[] refValues) {
        RefValue[] copyRefValues = new RefValue[refValues.length];
        for (int i = 0; i < refValues.length; i++) {
            RefValue copyRefValue = new RefValue();
            copyRefValue.row = refValues[i].row;
            copyRefValue.value = refValues[i].value;
            copyRefValues[i] = copyRefValue;
        }
        
        return copyRefValues;
    }
    
    private List<RefPath> getMultiQueryResult(PLMRefQueryPath[] refpaths, String sql)
            throws SQLException {
        // reference SQL
        String[] cnames = PLMRefSQLHelper.generateResultSetMetaData(refpaths);
        // reference data
        Map<String, Object> datas = RefQueryUtils.executeQuery(sql, cnames);
        //如果没有查到,判断是否为业务类型,判读是否有继承关系的属性。如果有继承的业务对象,则继续查找。
        try {
            if(datas == null || datas.isEmpty()){
                datas = queryAllChildBtmData(refpaths, cnames);
            }
        } catch (Exception e) {
            //子查询时出现异常
            e.printStackTrace();
        }
        
        List<RefPath> returnRefPaths = new ArrayList<RefPath>();
 
        for (PLMRefQueryPath o : refpaths) {
            // root node
            if (o.getSelectKey() == null) {
                returnRefPaths.add(o.getPath());
                continue;
            }
            // 查询数据库,数据不存在
            List<Object> values = (List<Object>) datas.get(o.getSelectKey()
                    .toUpperCase());
 
            RefValue[] refValues = (RefValue[]) o.getPath().values;
 
            for (int i = 0; i < refValues.length; i++) {
                if (values == null || values.size() == 0) {
                    // 非主键
                    if (!o.getSelectKey().toUpperCase().equals(Root_key)) {
                        refValues[i].value = "";
                        continue;
                    }
                }
                int real = mapToDbIndex(refValues[i].value, datas);
                if (real == -1) {
                    // 非主键
                    if (!o.getSelectKey().toUpperCase().equals(Root_key)) {
                        refValues[i].value = "";
                    }
                } else {
                    refValues[i].value = values.get(real) instanceof String ? (String) values
                            .get(real) : String.valueOf(values.get(real));
                }
            }
            returnRefPaths.add(o.getPath());
        }
        return returnRefPaths;
    }
 
    /**
     * 将二次查询的属性进行分组
     * eg. gpr.usepart.id
     *     gpr.usepart.code
     *     gpr.usepart.name
     *     gpr.useppart.id
     *     gpr.useppart.code
     *     gpr.useppart.name
     * @param refPaths
     * @return Map<usepart, List<PLMRefQueryPath>>
     *            <useppart,List<PLMRefQueryPath>>
     */
    private Map<String, List<PLMRefQueryPath>> toQuerymode(
            PLMRefQueryPath[] refPaths) {
        Map<String, List<PLMRefQueryPath>> queryItems = new HashMap<String, List<PLMRefQueryPath>>();
        for (int i = 0; i < refPaths.length; i++) {
            MetaData firstLevelO = refPaths[i].getLevel(1);
            if (queryItems.get(firstLevelO.getAttributeDef().name) == null) {
                List<PLMRefQueryPath> levelRefPaths = new ArrayList<PLMRefQueryPath>();
                levelRefPaths.add(refPaths[i]);
                queryItems.put(firstLevelO.getAttributeDef().name,
                        levelRefPaths);
            } else {
 
                List<PLMRefQueryPath> levelRefPaths = queryItems
                        .get(firstLevelO.getAttributeDef().name);
                levelRefPaths.add(refPaths[i]);
            }
        }
        return queryItems;
 
    }
 
    private List<RefPath> queryEachMode(
            Map<String, List<PLMRefQueryPath>> queryConditions, int limit)
            throws VCIError {
        List<RefPath> results = new ArrayList<RefPath>();
        // query each of mode
        Iterator<Entry<String, List<PLMRefQueryPath>>> itrator = queryConditions
                .entrySet().iterator();
        while (itrator.hasNext()) {
            List<PLMRefQueryPath> refPathLists = itrator.next().getValue();
            try {
                // 限制数目是1000
                int maxNum = 0;
                // 所有的Id
                if (refPathLists != null && refPathLists.size() != 0) {
                    maxNum = refPathLists.get(0).getPath().values.length;
                }
                if (maxNum < limit) {
                    results.addAll(query(refPathLists));
                    results.toArray(new RefPath[results.size()]);
                } else {
                    results.addAll(splitValuesQuery(maxNum, refPathLists, limit));
                }
            } catch (SQLException e) {
                ServerWithLog4j.logger.error(e.getMessage(), e);
                throw  new VCIError(RefQueryDelegate.class.getName(), new String[]{VciExceptionTool.getExceptionStr(e), VciExceptionTool.getExceptionDetail(e)});
                
            }
 
        }
        return results;
    }
 
    private List<RefPath> splitValuesQuery(int maxNum,
            List<PLMRefQueryPath> refPathLists, int limit) throws SQLException {
        List<RefPath> results = new ArrayList<RefPath>();
        // 保存一份数据
        RefValue[] valuesCopy = new RefValue[maxNum];
        RefValue[] valuesNew = new RefValue[maxNum];
        System.arraycopy(refPathLists.get(0).getPath().values, 0, valuesNew, 0,
                maxNum);
        getCopy(maxNum, valuesCopy, valuesNew);
        // 分批次查询
        Map<RefPath, List<RefValue>> rsMaps = new HashMap<RefPath, List<RefValue>>();
        for (int i = 0; i < (maxNum / limit) + 1; i++) {
            int length = (maxNum - ((i) * (limit - 1)));
            if (length < (limit - 1)) {
                // 重新赋值
                for (int j = 0; j < refPathLists.size(); j++) {
                    refPathLists.get(j).getPath().values = getEachSplitValueCopy(
                            length, (limit - 1), i, valuesNew);
                }
            } else {
                // 重新赋值
                for (int j = 0; j < refPathLists.size(); j++) {
                    refPathLists.get(j).getPath().values = getEachSplitValueCopy(
                            (limit - 1), (limit - 1), i, valuesNew);
                }
            }
            // 执行查询
            query(refPathLists);
            // 分段查询结束
            getEachSplitResult(refPathLists, rsMaps);
        }// end for
        // 全部查询结束
        for (int n = 0; n < refPathLists.size(); n++) {
            RefPath key = refPathLists.get(n).getPath();
            // 非主键
            if (refPathLists.get(n).getLevel() != 1) {
                key.values = rsMaps.get(key).toArray(new RefValue[maxNum]);
            } else {
                refPathLists.get(n).getPath().values = valuesCopy;
            }
            // 返回结果
            results.add(refPathLists.get(n).getPath());
        }
        return results;
    }
 
    @SuppressWarnings("unchecked")
    private List<RefPath> query(List<PLMRefQueryPath> refPathLists)
            throws SQLException {
        PLMRefQueryPath[] refpaths = refPathLists
                .toArray(new PLMRefQueryPath[refPathLists.size()]);
        // reference SQL
        String sql = PLMRefSQLHelper.generateSQL(refpaths);
        String[] cnames = PLMRefSQLHelper.generateResultSetMetaData(refpaths);
        // reference data
        Map<String, Object> datas = RefQueryUtils.executeQuery(sql, cnames);
        //如果没有查到,判断是否为业务类型,判读是否有继承关系的属性。如果有继承的业务对象,则继续查找。
        try {
            if(datas == null || datas.isEmpty()){
                datas = queryAllChildBtmData(refpaths, cnames);
            }
        } catch (Exception e) {
            //子查询时出现异常
            e.printStackTrace();
        }
        
        List<RefPath> returnRefPaths = new ArrayList<RefPath>();
 
        for (PLMRefQueryPath o : refpaths) {
            // root node
            if (o.getSelectKey() == null) {
                returnRefPaths.add(o.getPath());
                continue;
            }
            // 查询数据库,数据不存在
            List<Object> values = (List<Object>) datas.get(o.getSelectKey()
                    .toUpperCase());
 
            RefValue[] refValues = (RefValue[]) o.getPath().values;
 
            for (int i = 0; i < refValues.length; i++) {
                if (values == null || values.size() == 0) {
                    // 非主键
                    if (!o.getSelectKey().toUpperCase().equals(Root_key)) {
                        refValues[i].value = "";
                        continue;
                    }
                }
                int real = mapToDbIndex(refValues[i].value, datas);
                if (real == -1) {
                    // 非主键
                    if (!o.getSelectKey().toUpperCase().equals(Root_key)) {
                        refValues[i].value = "";
                    }
                } else {
                    refValues[i].value = values.get(real) instanceof String ? (String) values
                            .get(real) : String.valueOf(values.get(real));
                }
            }
            returnRefPaths.add(o.getPath());
        }
        return returnRefPaths;
    }
    
    /**
     * 获得传入数组的备份
     * */
    private RefValue[] getCopy(int maxNum, RefValue[] valuesCopy,
            RefValue[] valuesNew) {
        int b = 0;
        for (RefValue v : valuesNew) {
            RefValue temp = new RefValue();
            temp.row = v.row;
            temp.value = v.value;
            valuesCopy[b] = temp;
            ++b;
        }
        return valuesCopy;
    }
 
    /**
     * 分割后,每一个批次的result
     * */
    private Map<RefPath, List<RefValue>> getEachSplitResult(
            List<PLMRefQueryPath> refPathLists,
            Map<RefPath, List<RefValue>> rsMaps) {
        for (int n = 0; n < refPathLists.size(); n++) {
            RefPath key = refPathLists.get(n).getPath();
            if (rsMaps.get(key) == null) {
                List<RefValue> valueObjects = new ArrayList<RefValue>();
                valueObjects.addAll(Arrays.asList(key.values));
                rsMaps.put(key, valueObjects);
            } else {
                rsMaps.get(key).addAll(Arrays.asList(key.values));
            }
        }
        return rsMaps;
    }
 
    /**
     * 分割后,每一个批次的Value
     * */
    private RefValue[] getEachSplitValueCopy(int length, int limit, int batch,
            RefValue[] valuesNew) {
        RefValue[] eachbatch = new RefValue[(length)];
        for (int c = 0; c < (length); c++) {
            RefValue temp = new RefValue();
            temp.row = valuesNew[batch * (limit - 1) + c].row;
            temp.value = valuesNew[batch * (limit - 1) + c].value;
            eachbatch[c] = temp;
        }
        return eachbatch;
    }
 
    @SuppressWarnings("unchecked")
    private int mapToDbIndex(String oid, Map<String, Object> dbdatas) {
        List<Object> dbvalues = (List<Object>) dbdatas.get(Root_key);
        return dbvalues.indexOf(oid);
    }
 
    private PLMRefPathInfo generatePathInfo(RefPath path,
            Map<String, String> toFromTypes) throws Throwable {
        // 根类型的所有的属性信息
        AttribItem[] leveloneattrs = getAllAttributeByTypeName(path.path
                .split("\\.")[0]);
        PLMRefPathInfo pathInfo = new PLMRefPathInfo();
        List<MetaData> metas = new ArrayList<MetaData>();
        String[] cascade = path.path.split("\\.");
 
        for (int i = 0; i < cascade.length; i++) {
            if (i == 0 || (i > 1 && i == cascade.length - 1)) {
                continue;
            }
            AttribItem currentAttribute = null;
            if (i == 1) {
                for (AttribItem a : leveloneattrs) {
                    if (a.name.equals(cascade[i])) {
                        currentAttribute = a;
                        break;
                    }
                }
            }
            if (i > 1) {
                currentAttribute = getCurrentAttribute(cascade[i]);
            }
            // 查询类型
            getCurrentType(currentAttribute, null, path, toFromTypes, i, metas);
            // generateMetaData(metas, currentAttribute, currentType, i);
        }
        pathInfo.setMd(metas.toArray(new MetaData[metas.size()]));
        return pathInfo;
    }
 
    private void generateMetaData(List<MetaData> metas, AttribItem currentAttr,
            Object type, int level, OtherInfo info) {
        MetaData meta = new MetaData();
        meta.setAttribItem(currentAttr);
        if (type instanceof BtmItem) {
            meta.setRefType((BtmItem) type);
            meta.setType(true);
        } else {
            if (info != null) {
                meta.setQueryEnum(info.getRefFlag());
            }
            meta.setRefLink((LinkType) type);
            meta.setType(false);
        }
        meta.setLevel(level);
        metas.add(meta);
    }
 
    /**
     * 生成查询路径
     * 
     * @param RefPath
     *            参照的路径信息 ,String 链接的to端类型
     * */
 
    private PLMRefPathInfo generatePathInfo(RefPath path, String toType,
            Map<String, String> toFromTypes) throws Throwable {
        PLMRefPathInfo pathInfo = new PLMRefPathInfo();
        List<MetaData> metas = new ArrayList<MetaData>();
        String[] cascade = path.path.split("\\.");
        for (int i = 0; i < cascade.length; i++) {
            if (i == 0 || (i > 1 && i == cascade.length - 1)) {
                continue;
            }
            AttribItem currentAttr = null;
            // 得到属性定义信息
            currentAttr = getCurrentAttribute(cascade[i]);
            getCurrentType(currentAttr, toType, path, toFromTypes, i, metas);
            // generateMetaData(metas, currentAttr, type, i);
        }
        pathInfo.setMd(metas.toArray(new MetaData[metas.size()]));
        return pathInfo;
    }
 
    /**
     * 生成查询路径集合
     * 
     * @param RefPath
     *            [] 参照的路径信息 ,String 链接的to端类型
     * */
    private PLMRefQueryPath[] toQueryPath(RefPath[] paths, String toType)
            throws Throwable {
        List<PLMRefQueryPath> refPaths = new ArrayList<PLMRefQueryPath>();
        // 收集to\from端类型
        Map<String, String> toFromTypes = new HashMap<String, String>();
        for (RefPath path : paths) {
            int level = path.path.split("\\.").length;
            PLMRefQueryPath refPath = new PLMRefQueryPath();
            refPath.setPath(path);
            refPath.setLevel(level - 1);
            PLMRefPathInfo info = generatePathInfo(path, toType, toFromTypes);
            refPath.setPathInfo(info);
            refPaths.add(refPath);
        }
        return refPaths.toArray(new PLMRefQueryPath[refPaths.size()]);
 
    }
    
//    private PLMRefQueryPath[] toQueryPath(RefPath[] paths, String[] toTypes)
//            throws Throwable {
//        List<PLMRefQueryPath> refPaths = new ArrayList<PLMRefQueryPath>();
//        // 收集to\from端类型
//        Map<String, String> toFromTypes = new HashMap<String, String>();
//        for (int i = 0; i < paths.length; i++) {
//            RefPath path = paths[i];
//            int level = path.path.split("\\.").length;
//            PLMRefQueryPath refPath = new PLMRefQueryPath();
//            refPath.setPath(path);
//            refPath.setLevel(level - 1);
//            PLMRefPathInfo info = generatePathInfo(path, toTypes[i], toFromTypes);
//            refPath.setPathInfo(info);
//            refPaths.add(refPath);
//        }
//        return refPaths.toArray(new PLMRefQueryPath[refPaths.size()]);
//
//    }
 
    private PLMRefQueryPath[] toQueryPath(RefPath[] paths) throws Throwable {
        List<PLMRefQueryPath> refPaths = new ArrayList<PLMRefQueryPath>();
        // 收集to\from端类型
        Map<String, String> toFromTypes = new HashMap<String, String>();
        for (RefPath path : paths) {
            int level = path.path.split("\\.").length;
            PLMRefQueryPath refPath = new PLMRefQueryPath();
            refPath.setPath(path);
            refPath.setLevel(level - 1);
            PLMRefPathInfo info = generatePathInfo(path, toFromTypes);
            refPath.setPathInfo(info);
            refPaths.add(refPath);
        }
        return refPaths.toArray(new PLMRefQueryPath[refPaths.size()]);
 
    }
 
    /**
     * 查询路径在指定的层次的业务类型定义
     * 
     * @param AttribItem
     *            属性定义 ,String 链接的to端类型
     * */
    private Object getCurrentType(AttribItem attribute, String toType,
            RefPath path, Map<String, String> toFromTypes, int level,
            List<MetaData> metas) throws VCIError {
        Object retrun = null;
        BtmItem btmItem = null;
        LinkType linkItem = null;
        try {
            if (attribute.name.toLowerCase().trim().equals(ToOid)
                    || attribute.name.toLowerCase().trim().equals(FromOid)) {
                // t_oid or f_oid 处于第一层
                if (toType != null && !toType.equals("")) {
                    // query reference type
                    //btmItem = ServerServiceProvider.getOMDService().getBTMService().getBtmItemByName(toType);
                    btmItem = OMCacheProvider.getBizType(toType);
                    retrun = btmItem;
                    generateMetaData(metas, attribute, retrun, level, null);
                } else {
                    // t_oid or f_oid 处于非第一层
                    String key = getIndentifier((level), path);
                    if (toFromTypes.get(key) != null) {
                        //btmItem = ServerServiceProvider.getOMDService().getBTMService().getBtmItemByName(toFromTypes.get(key));
                        btmItem = OMCacheProvider.getBizType(toFromTypes.get(key));
                        retrun = btmItem;
                        generateMetaData(metas, attribute, retrun, level, null);
                        return retrun;
                    }
                    String linkTypeName = path.path.split("\\.")[level - 1];//
                    AttribItem attributeItem = getCurrentAttribute(linkTypeName);
                    OtherInfo info = OtherInfo.getOtherInfoByText(attributeItem.other);
                    String oid = path.values[0].value;
 
                    String typeName = attribute.name.toLowerCase().trim()
                            .equals(FromOid) ? searchType(info.getRefTypeName(),
                            oid, false) : searchType(info.getRefTypeName(), oid,
                            true);
                    //btmItem = ServerServiceProvider.getOMDService().getBTMService().getBtmItemByName(typeName);
                    btmItem = OMCacheProvider.getBizType(typeName);
                    toFromTypes.put(key, typeName);
                    retrun = btmItem;
                    generateMetaData(metas, attribute, retrun, level, info);
                }
            } else {
                // 普通属性:分为业务类型与链接类型
                // is reference Attribute
                OtherInfo info = OtherInfo.getOtherInfoByText(attribute.other);
                // 业务类型
                if (info.getRefFlag() == 0) {
                    btmItem = ServerServiceProvider.getOMDService().getBTMService().getBtmItemByName(
                            info.getRefTypeName());
                    retrun = btmItem;
                } else {
                    linkItem = ServerServiceProvider.getOMDService().getLinkTypeService().getLinkType(
                            info.getRefTypeName());
                    retrun = linkItem;
                }
                generateMetaData(metas, attribute, retrun, level, null);
            }
        } catch (Throwable e) {
            throw new VCIError("", new String[]{VciExceptionTool.getExceptionStr(e), VciExceptionTool.getExceptionDetail(e)});
            // TODO: handle exception
        }
        return retrun;
    }
 
    private String getIndentifier(int i, RefPath path) {
        String[] s = path.path.split("\\.");
        String results = "";
        for (int j = 0; j <= i; j++) {
 
            results += s[j];
            results += ".";
        }
        return results.substring(0, results.lastIndexOf("."));
 
    }
 
    private String searchType(String link, String oid, boolean to)
            throws SQLException {
        StringBuilder searchSQL = new StringBuilder();
        Object o = null;
        if (to) {
            searchSQL.append(" select t_btwname from ").append("platformlt_")
                    .append(link).append(" where oid in ('").append(oid)
                    .append("')");
 
            Map<String, Object> toTypes = RefQueryUtils.executeQuery(
                    searchSQL.toString(), new String[] { "t_btwname" });
 
            o = toTypes.get("t_btwname");
        } else {
            searchSQL.append(" select f_btwname from ").append("platformlt_")
                    .append(link).append(" where oid in ('").append(oid)
                    .append("')");
 
            Map<String, Object> toTypes = RefQueryUtils.executeQuery(
                    searchSQL.toString(), new String[] { "f_btwname" });
            o = toTypes.get("f_btwname");
        }
 
        return o.toString();
    }
 
    /**
     * 查询路径在指定的层次的属性定义
     * 
     * @param String
     *            属性名称
     * */
    private AttribItem getCurrentAttribute(String name) throws Throwable {
        AttribItem currentAttr = null;
        if (name.toLowerCase().trim().equals(ToOid)
                || name.toLowerCase().trim().equals(FromOid)) {
            AttribItem attribute = new AttribItem();
            attribute.name = name.toLowerCase().trim().equals(ToOid) ? ToOid
                    : FromOid;
            currentAttr = attribute;
        } else {
            currentAttr = getAttributeByName(name);
        }
        return currentAttr;
    }
 
    private AttribItem getAttributeByName(String name) throws  Throwable {
        return OMCacheProvider.getAttribute(name);
//        AttribItem[] arrays = ServerServiceProvider.getOMDService().getAttributeService().getAttribItemsByNames(new String[0]);
//        for (AttribItem a : arrays) {
//            if (a.name.equals(name)) {
//                return a;
//            }
//        }
//        return null;
    }
 
    /**
     * 依据业务类型的名称,获取所有的属性定义信息
     * 
     * @param String
     *            业务类型名称
     * */
    private AttribItem[] getAllAttributeByTypeName(String typeName)
            throws Throwable {
        //BtmItem typeItem = ServerServiceProvider.getOMDService().getBTMService().getBtmItemByName(typeName);
        BtmItem typeItem = OMCacheProvider.getBizType(typeName);
        String[] attributeNames = typeItem.apNameArray;
        List<AttribItem> attributes = new ArrayList<AttribItem>();
        for (String s : attributeNames) {
            attributes.add(getAttributeByName(s));
        }
        return attributes.toArray(new AttribItem[attributes.size()]);
    }
    
    /**
     * 一次从子业务类型中查找数据
     * 知道找到为止,如果全不包含返回空Map
     * @param refpaths 
     * @param cnames 
     * @return 
     */
    private Map<String, Object> queryAllChildBtmData(PLMRefQueryPath[] refpaths, String[] cnames){
        Map<String, Object> datas = new HashMap<String, Object>();
        try {
            //得到所有子业务类型
            List<FromItem> fromItemList = PLMRefSQLHelper.generateFromItemList(refpaths);
            BtmItem[] bits = getChildBtm(fromItemList);
            if(bits != null && bits.length > 0){
                for(BtmItem bit : bits) {
                    //重新设置refPaths和FormItem的信息
                    for(FromItem fi : fromItemList){
                        fi.getMeta().setRefType(bit);
                    }
                    //设置查询语句
                    String sql = PLMRefSQLHelper.generateSQL(refpaths, fromItemList);
                    //执行查询
                    datas = RefQueryUtils.executeQuery(sql, cnames);
                    if(datas == null || !datas.isEmpty()){
                        break;
                    }
                }
            }
        } catch (Exception e) {
            //System.out.println("二次查询时发生异常!");
            //e.printStackTrace();
            ServerWithLog4j.logger.error("二次查询时发生异常!", e);
        }
        return datas;
    }
    
    /**
     * 获得所有子业务类型
     * @return
     */
    /**
     * 得到业务对象名称
     * @param fromitems
     * @return
     */
    private static BtmItem[] getChildBtm(List<FromItem> fromitems) {
        String btmName = "";
        for (FromItem item : fromitems) {
            if (item.getLevel() == 1) {
                // 判断是业务对象还是link对象
                if (item.getMeta().isType()) {
                    btmName = item.getMeta().getRefType().name;
                } else {
                    btmName = item.getMeta().getRefLink().name;
                }
                break;
            }
        }
        try {
            BtmItem[] a1 = ServerServiceProvider.getOMDService().getBTMService().getChildrenBtms(btmName);
            return a1;
        } catch (Throwable e) {
            //System.out.println("查询子对象异常!");
            //e.printStackTrace();
            ServerWithLog4j.logger.error("查询子对象异常!", e);
            return null;
        }
    }
 
 
}