田源
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
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
package com.vci.server.base.utility;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
 
import com.vci.omd.objects.OtherInfo;
import com.vci.server.cache.OMCacheProvider;
import com.vci.common.qt.object.ChildrenInfo;
import com.vci.common.qt.object.Condition;
import com.vci.common.qt.object.ConditionItem;
import com.vci.common.qt.object.Connector;
import com.vci.common.qt.object.LeafInfo;
import com.vci.common.qt.object.LeafValue;
import com.vci.common.qt.object.Operator;
import com.vci.common.qt.object.OrderInfo;
import com.vci.common.qt.object.PageInfo;
import com.vci.common.qt.object.QTConstants;
import com.vci.common.qt.object.QueryTemplate;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.atm.AttribItem;
 
public class OQueryHelper {
 
    /**
     * 在根节点上循环增加qtNode
     * 
     * @param doc
     * @return
     */
    public static Element addDoc(Element rootNode, QueryTemplate qt) {
        Element qtNode = rootNode.addElement(QTConstants.QUERYTEMPLATE);
        qtNode.addAttribute(QTConstants.ID, qt.getId());
        qtNode.addAttribute(QTConstants.TYPE, qt.getType());
        // 设置clauseList
        List<String> clauseList = qt.getClauseList();
        String claValue = QTConstants.BLANK;
        for (int i = 0; i < clauseList.size(); i++) {
            claValue += clauseList.get(i);
            if (i < clauseList.size() - 1) {
                claValue += ",";
            }
        }
        Element claListNode = qtNode.addElement(QTConstants.CLAUSELIST);
        claListNode.setText(claValue);
 
        // 设置查询的linkType
        String linkType = qt.getLinkType();
        Element ltNode = qtNode.addElement(QTConstants.LINKTYPE);
        if (linkType != null) {
            ltNode.setText(linkType);
        }
        // 设置查询的btmType
        String btmType = qt.getBtmType();
        Element btmNode = qtNode.addElement(QTConstants.BTMTYPE);
        if (btmType != null) {
            btmNode.setText(btmType);
        }
        // 查询业务对象时,是否查询子类型
        if (qt.getType().equals(QTConstants.TYPE_BTM)) {
            boolean queryChildrenFlag = qt.isQueryChildren();
            Element queryChildrenFlagNode = qtNode.addElement(QTConstants.QUERYCHILDRENFLAG);
            queryChildrenFlagNode.setText(String.valueOf(queryChildrenFlag));
        }
        // 是否查询是否包含下一级(链接查询)
        boolean isQueryIsLeaf = qt.isQueryISLeaf();
        Element queryIsLeafNode = qtNode.addElement(QTConstants.QUERYISLEAF);
        queryIsLeafNode.setText(String.valueOf(isQueryIsLeaf));
        // 查询时是否加入权限,默认加权限
        boolean rightFlag = qt.isRightFlag();
        Element rightFlagNode = qtNode.addElement(QTConstants.RIGHTFlag);
        rightFlagNode.setText(String.valueOf(rightFlag));
 
        // weidy@2018-06-19 增加密级控制
        boolean secretFlag = qt.isSecretFlag();
        Element secretFlagNode = qtNode.addElement(QTConstants.SECRETFLAG);
        secretFlagNode.setText(String.valueOf(secretFlag));// end weidy
        // 设置版本版次信息
        int version = qt.getVersion();
        Element versionNode = qtNode.addElement(QTConstants.VERSION);
        versionNode.setText(String.valueOf(version));
        // 设置链接查询的递归层次
        int levelRec = qt.getLevel();
        Element levelRecNode = qtNode.addElement(QTConstants.LEVELREC);
        levelRecNode.setText(String.valueOf(levelRec));
        // 递归返回数据模式
        int recReturnMode = qt.getRecReturnMode();
        Element recReturnModeNode = qtNode.addElement(QTConstants.RECRETURNMODE);
        recReturnModeNode.setText(String.valueOf(recReturnMode));
 
        if (qt.getType().equals(QTConstants.TYPE_LINK)) {
            // 设置链接查询方向
            String direction = qt.getDirection();
            Element directionNode = qtNode.addElement(QTConstants.DIRECTION);
            directionNode.setText(direction);
        }
        // 设置PageInfo
        PageInfo pageInfo = qt.getPageInfo();
        if (pageInfo != null) {
            Element pageInfoNode = qtNode.addElement(QTConstants.PAGEINFO);
            Element pageNONode = pageInfoNode.addElement(QTConstants.PAGENO);
            int pageNO = pageInfo.getPageNO();
            pageNONode.setText(String.valueOf(pageNO));
            Element rowCountNode = pageInfoNode.addElement(QTConstants.ROWCOUNT);
            int rowCount = pageInfo.getRowCount();
            rowCountNode.setText(String.valueOf(rowCount));
        }
        // 设置ORDER BY
        List<OrderInfo> orderInfoList = qt.getOrderInfoList();
        if (orderInfoList != null && orderInfoList.size() > 0) {
            Element orderInfosNode = qtNode.addElement(QTConstants.ORDERINFOS);
            for (int i = 0; i < orderInfoList.size(); i++) {
                OrderInfo o = orderInfoList.get(i);
                if (o == null) {
                    continue;
                }
                Element orderInfoNode = orderInfosNode.addElement(QTConstants.ORDERINFO);
                String orderField = o.getOrderField();
                Element orderFieldNode = orderInfoNode.addElement(QTConstants.ORDERFIELD);
                orderFieldNode.setText(orderField == null ? QTConstants.BLANK : orderField);
                String orderMode = o.getOrderMode();
                Element orderModeNode = orderInfoNode.addElement(QTConstants.ORDERMODE);
                orderModeNode.setText(orderMode == null ? QTConstants.BLANK : orderMode);
                int level = o.getLevel();
                Element levelNode = orderInfoNode.addElement(QTConstants.LEVEL);
                levelNode.setText(String.valueOf(level));
            }
        }
        // 设置Condition
        Condition condition = qt.getCondition();
        if (condition != null) {
            Element conditionNode = qtNode.addElement(QTConstants.CONDITION);
            conditionNode.addAttribute(QTConstants.ROOTCINAME, condition.getRootCIName());
            Map<String, ConditionItem> ciMap = condition.getCIMap();
            if (ciMap != null) {
                for (Iterator<String> i = ciMap.keySet().iterator(); i.hasNext();) {
                    String ciId = i.next();
                    // 设置conditionItem
                    ConditionItem cItem = ciMap.get(ciId);
                    Element cItemNode = conditionNode.addElement(QTConstants.CONDITIONITEM);
                    cItemNode.addAttribute(QTConstants.ID, cItem.getId());
                    // 设置是否是叶子节点
                    boolean isLeaf = cItem.isLeaf();
                    Element leafFlagNode = cItemNode.addElement(QTConstants.LEAFFLAG);
                    leafFlagNode.setText(String.valueOf(cItem.isLeaf()));
                    // 设置conditionItem的leafData
                    if (isLeaf) {
                        LeafInfo leafInfo = cItem.getLeafInfo();
                        Element leafInfoNode = cItemNode.addElement(QTConstants.LEAFINFO);
                        Element clauseNode = leafInfoNode.addElement(QTConstants.CLAUSE);
                        clauseNode.setText(leafInfo.getClause());
                        Element operatorNode = leafInfoNode.addElement(QTConstants.OPERATOR);
                        String operator = leafInfo.getOperator();
                        /**
                         * 因<, <= 在xml中不能正确存储, 故将查询模板的<, <=存为LT, LTE; 从xml读取operator为LT, LTE时,
                         * 再转换成查询模板的<, <=. 将>, >= 做相同处理.
                         */
                        if (operator.equals(Operator.LT)) {
                            operator = Operator.LTTEXT;
                        } else if (operator.equals(Operator.LTE)) {
                            operator = Operator.LTETEXT;
                        } else if (operator.equals(Operator.GT)) {
                            operator = Operator.GTTEXT;
                        } else if (operator.equals(Operator.GTE)) {
                            operator = Operator.GTETEXT;
                        }
                        operatorNode.setText(operator);
                        // 设置conditionItem的leafData的LDValue
                        LeafValue lDValue = leafInfo.getValue();
                        Element valueNode = leafInfoNode.addElement(QTConstants.VALUE);
                        Element ordinaryValueNode = valueNode.addElement(QTConstants.ORDINARYVALUE);
                        String ordinaryValue = lDValue.getOrdinaryValue();
                        if (ordinaryValue != null) {
                            ordinaryValueNode.setText(ordinaryValue);
                        }
                        Element queryTemplateNode = valueNode.addElement(QTConstants.REFQT);
                        QueryTemplate refQt = lDValue.getQueryTemplate();
                        if (refQt != null) {
                            addDoc(rootNode, refQt);
                            queryTemplateNode.setText(lDValue.getQueryTemplate().getId());
                        }
                    } else {
                        // 设置conditionItem的childrenData
                        Element childrenInfoNode = cItemNode.addElement(QTConstants.CHILDRENINFO);
                        ChildrenInfo cdInfo = cItem.getChildrenInfo();
                        Element leftNode = childrenInfoNode.addElement(QTConstants.LEFT);
                        leftNode.setText(cdInfo.getLeftCIName());
                        Element rightNode = childrenInfoNode.addElement(QTConstants.RIGHT);
                        rightNode.setText(cdInfo.getRightCIName());
                        Element conncectorNode = childrenInfoNode.addElement(QTConstants.CONNECTOR);
                        conncectorNode.setText(cdInfo.getConnector());
                    }
                }
            }
 
        }
 
        return rootNode;
    }
 
    /**
     * 将查询模板结构转化成查询模板text
     * 
     * @param qt
     * @return
     */
    public static String getQTTextByQT(QueryTemplate qt) {
        return qtTOXMl(qt).asXML();
    }
 
    /**
     * 将查询模板, 以及其关联的查询模板
     * 
     * @param qt
     * @return
     */
    private static Document qtTOXMl(QueryTemplate qt) {
        Document document = DocumentHelper.createDocument();
        Element rootNode = document.addElement(QTConstants.DATA);
        addDoc(rootNode, qt);
        return document;
    }
 
 
    /**
     * 将查询模板text转化成查询模板结构
     * 
     * @param qtName : 查询模板name
     * @param qtText : 查询模板text
     * @return
     * @throws DocumentException
     * @throws VCIError
     */
    public static QueryTemplate getQTByQTText(String qtName, String qtText) throws VCIError, DocumentException {
        
        return getQTByDoc(DocumentHelper.parseText(qtText), qtName);
    }
 
    /**
     * 根据Document, Document中的模板名获取查询模板
     * 
     * @throws VCIError
     */
    private static QueryTemplate getQTByDoc(Document doc, String name) throws VCIError {
        QueryTemplate qt = new QueryTemplate();
 
        Element rootNode = doc.getRootElement();
        List<Element> qtNodes = rootNode.elements(QTConstants.QUERYTEMPLATE);
        for (Iterator<Element> i = qtNodes.iterator(); i.hasNext();) {
            Element qtNode = i.next();
            if (qtNode.attributeValue(QTConstants.ID).equals(name)) {
                // 设置Id
                qt.setId(name);
                String type = qtNode.attributeValue(QTConstants.TYPE);
                if (type == null || (!type.equals(QTConstants.TYPE_BTM) && !type.equals(QTConstants.TYPE_LINK))) {
                    throw new VCIError("P0010QT-00011", new String[] { "没有正确设置查询模板的类型" });
                } else {
                    qt.setType(type);
                }
                // 设置clauseList
                List<String> clauseList = new ArrayList<String>();
                String[] clauses = qtNode.elementText(QTConstants.CLAUSELIST).split(",");
                for (int k = 0; k < clauses.length; k++) {
                    clauseList.add(clauses[k]);
                }
                qt.setClauseList(clauseList);
                // 设置查询的linkType
                String linkType = qtNode.elementText(QTConstants.LINKTYPE);
                qt.setLinkType(linkType == null ? QTConstants.BLANK : linkType);
                // 设置查询的btmType
                String btmType = qtNode.elementText(QTConstants.BTMTYPE);
                qt.setBtmType(btmType == null ? QTConstants.BLANK : btmType);
                if (qt.getType().equals(QTConstants.TYPE_BTM)) {
                    // 是否查询业务类型的子类型
                    String queryChildrenFlag = qtNode.elementText(QTConstants.QUERYCHILDRENFLAG);
                    qt.setQueryChildrenFlag(Boolean.valueOf(queryChildrenFlag));
                }
                // 是否查询是否包含下一级(链接查询)
                String queryIsLeaf = qtNode.elementText(QTConstants.QUERYISLEAF);
                if (queryIsLeaf == null || queryIsLeaf.equals("")) {
                    queryIsLeaf = String.valueOf(Boolean.FALSE);
                }
                qt.setQueryISLeaf(Boolean.valueOf(queryIsLeaf));
                // 查询时是否加入权限,默认加权限
                String rightFlag = qtNode.elementText(QTConstants.RIGHTFlag);
                if (rightFlag == null || rightFlag.equals("")) {
                    rightFlag = String.valueOf(Boolean.TRUE);
                }
                qt.setRightFlag(Boolean.valueOf(rightFlag));
                // weidy@2018-06-19 增加密级控制
                String secretFlag = qtNode.elementText(QTConstants.SECRETFLAG);
                if (secretFlag == null || secretFlag.equals("")) {
                    secretFlag = String.valueOf(Boolean.TRUE);
                }
                qt.setSecretFlag(Boolean.valueOf(secretFlag));
                // 设置版本版次信息
                String versionStr = qtNode.elementText(QTConstants.VERSION);
                int version = Integer.valueOf(versionStr == null ? "0" : versionStr);
                qt.setVersion(version);
 
                // 设置链接查询递归的层次
                String levelRecStr = qtNode.elementText(QTConstants.LEVELREC);
                int levelRec = Integer.valueOf(levelRecStr == null ? "1" : levelRecStr);
                qt.setLevel(levelRec);
 
                // 递归返回数据模式
                String recReturnModeStr = qtNode.elementText(QTConstants.RECRETURNMODE);
                int recReturnMode = Integer.valueOf(recReturnModeStr == null ? "1" : recReturnModeStr);
                qt.setRecReturnMode(recReturnMode);
 
                // 设置链接查询方向
                if (qt.getType().equals(QTConstants.TYPE_LINK)) {
                    String direction = qtNode.elementText(QTConstants.DIRECTION);
                    if (direction == null || (!direction.equals(QTConstants.DIRECTION_POSITIVE)
                            && !direction.equals(QTConstants.DIRECTION_OPPOSITE))) {
                        qt.setDirection(QTConstants.DIRECTION_POSITIVE);
                    } else {
                        qt.setDirection(direction);
                    }
                }
                // 设置PageInfo
                Element pageInfoNode = qtNode.element(QTConstants.PAGEINFO);
                if (pageInfoNode != null && pageInfoNode.element(QTConstants.PAGENO) != null
                        && pageInfoNode.element(QTConstants.ROWCOUNT) != null) {
                    PageInfo pageInfo = new PageInfo();
                    String pageNO = pageInfoNode.element(QTConstants.PAGENO).getText();
                    String rowCount = pageInfoNode.element(QTConstants.ROWCOUNT).getText();
                    if (pageNO != null && !pageNO.equals("")) {
                        pageInfo.setPageNO(Integer.parseInt(pageNO));
                    }
                    if (rowCount != null && !rowCount.equals("")) {
                        pageInfo.setRowCount(Integer.parseInt(rowCount));
                    }
                    qt.setPageInfo(pageInfo);
                }
                // 设置orderInfoList
                Element orderInfosNode = qtNode.element(QTConstants.ORDERINFOS);
                if (orderInfosNode != null && orderInfosNode.elements(QTConstants.ORDERINFO) != null) {
                    List<OrderInfo> orderInfoList = new ArrayList<OrderInfo>();
                    List<Element> orderInfoNodes = orderInfosNode.elements(QTConstants.ORDERINFO);
                    for (Iterator<Element> ite = orderInfoNodes.iterator(); ite.hasNext();) {
                        Element orderInfoNode = ite.next();
                        if (orderInfoNode.element(QTConstants.ORDERFIELD) != null
                                && orderInfoNode.element(QTConstants.ORDERMODE) != null
                                && orderInfoNode.element(QTConstants.LEVEL) != null) {
                            OrderInfo obj = new OrderInfo();
                            obj.setOrderField(orderInfoNode.elementText((QTConstants.ORDERFIELD)));
                            obj.setOrderMode(orderInfoNode.elementText((QTConstants.ORDERMODE)));
                            if (orderInfoNode.elementText((QTConstants.LEVEL)) != null
                                    && !orderInfoNode.elementText((QTConstants.LEVEL)).equals("")) {
                                obj.setLevel(Integer.valueOf(orderInfoNode.elementText((QTConstants.LEVEL))));
                            }
                            orderInfoList.add(obj);
                        }
                    }
                    qt.setOrderInfoList(orderInfoList);
                }
                Element conditionNode = qtNode.element(QTConstants.CONDITION);
                // 设置condition
                if (conditionNode != null
                        && !conditionNode.attributeValue(QTConstants.ROOTCINAME).equals(QTConstants.BLANK)) {
                    Condition condition = new Condition();
                    qt.setCondition(condition);
                    condition.setRootCIName(conditionNode.attributeValue(QTConstants.ROOTCINAME));
                    HashMap<String, ConditionItem> cIMap = new HashMap<String, ConditionItem>();
                    condition.setCIMap(cIMap);
                    List<Element> ciNodes = conditionNode.elements(QTConstants.CONDITIONITEM);
                    for (Iterator<Element> ite = ciNodes.iterator(); ite.hasNext();) {
                        Element ciNode = ite.next();
                        // 设置conditionItem
                        ConditionItem conditionItem = new ConditionItem();
                        conditionItem.setId(ciNode.attributeValue(QTConstants.ID));
                        conditionItem.setLeafFlag(Boolean.valueOf(ciNode.elementText(QTConstants.LEAFFLAG)));
 
                        // 设置conditionItem的leafInfo
                        Element ldNode = ciNode.element(QTConstants.LEAFINFO);
                        if (ldNode != null && ldNode.element(QTConstants.CLAUSE) != null) {
                            LeafInfo leafData = new LeafInfo();
                            conditionItem.setLeafInfo(leafData);
                            leafData.setClause(ldNode.elementText(QTConstants.CLAUSE));
                            String operator = ldNode.elementText(QTConstants.OPERATOR);
                            /**
                             * 因<, <= 在xml中不能正确存储, 故将查询模板的<, <=存为LT, LTE; 从xml读取operator为LT, LTE时,
                             * 再转换成查询模板的<, <=. 将>, >= 做相同处理.
                             */
                            if (operator.equals(Operator.LTTEXT)) {
                                operator = Operator.LT;
                            } else if (operator.equals(Operator.LTETEXT)) {
                                operator = Operator.LTE;
                            } else if (operator.equals(Operator.GTTEXT)) {
                                operator = Operator.GT;
                            } else if (operator.equals(Operator.GTETEXT)) {
                                operator = Operator.GTE;
                            }
                            leafData.setOperator(operator);
                            // 设置conditionItem的leafData的LDValue
                            Element valueNode = ldNode.element(QTConstants.VALUE);
                            LeafValue ldValue = new LeafValue();
                            leafData.setValue(ldValue);
                            ldValue.setOrdinaryValue(valueNode.elementText(QTConstants.ORDINARYVALUE));
                            String qtName = valueNode.elementText(QTConstants.REFQT);
                            if (qtName != null && !qtName.equals(QTConstants.BLANK)) {
                                ldValue.setQueryTemplate(getQTByDoc(doc, qtName));
                            }
                        }
 
                        // 设置conditionItem的childrenInfo
                        Element cdNode = ciNode.element(QTConstants.CHILDRENINFO);
                        if (cdNode != null) {
                            ChildrenInfo cdData = new ChildrenInfo();
                            conditionItem.setChildrenInfo(cdData);
                            cdData.setLeftCIName(cdNode.elementText((QTConstants.LEFT)));
                            cdData.setRightCIName(cdNode.elementText((QTConstants.RIGHT)));
                            cdData.setConnector(cdNode.elementText((QTConstants.CONNECTOR)));
                        }
 
                        cIMap.put(conditionItem.getId(), conditionItem);
                    }
                }
            }
        }
        return qt;
    }
 
 
 
    /**
     * 将查询模板中查询条件的值参数, 替换为map中给定的值
     * 
     * @param qt
     * @param map
     * @throws VCIError
     */
    public static QueryTemplate replaceQTValues(QueryTemplate qt, Map<String, String> map) throws VCIError {
        try {
            if (qt == null || map == null) {
                return qt;
            }
            Condition condition = qt.getCondition();
            if (condition == null) {
                return qt;
            }
            Map<String, ConditionItem> ciMap = condition.getCIMap();
            if (ciMap == null) {
                return qt;
            }
            for (Iterator<String> i = ciMap.keySet().iterator(); i.hasNext();) {
                String ciId = i.next();
                ConditionItem ci = ciMap.get(ciId);
                // 当ConditionItem是叶子结点, 并且值参数在map中时, 替换。
                if (ci.isLeaf()) {
                    LeafInfo lInfo = ci.getLeafInfo();
                    String value = lInfo.getValue().getOrdinaryValue();
                    if (value != null && !value.equals("")) {
                        if (map.containsKey(value)) {
                            String value_ = map.get(value);
                            lInfo.getValue().setOrdinaryValue(value_);
                            // 替换自定义查询(非嵌套)in (select oid from ff where #val1# #val2# )
                        } else if (value.contains("#")) {
                            while (value.contains("#")) {
                                int start = value.indexOf('#');
                                int end = value.indexOf('#', start + 1);
                                String key = value.substring(start + 1, end);
                                String val = map.get(key);
                                value = value.replace('#' + key + '#', val == null ? "null" : val);
                            }
                            lInfo.getValue().setOrdinaryValue(value);
                        }
 
                    } else {
                        QueryTemplate refQT = lInfo.getValue().getQueryTemplate();
                        QueryTemplate refQT_ = replaceQTValues(refQT, map);
                        lInfo.getValue().setQueryTemplate(refQT_);
                    }
                }
            }
            return qt;
        } catch (Throwable e) {
            e.printStackTrace();
            throw new VCIError("P0010QT-00008", new String[] { e.getMessage() });
        }
    }
 
 
 
    /**
     * 把key, Value 封装成查询模板的Condition
     * 
     * @param map
     * @return
     */
    public static Condition getCondition(Map<String, String> map) {
        Condition cond = new Condition();
        Map<String, ConditionItem> ciMap = new HashMap<String, ConditionItem>();
        Iterator<String> ite = map.keySet().iterator();
        List<ConditionItem> list = new ArrayList<ConditionItem>();
        int idFlag = 1;
        while (ite.hasNext()) {
            String key = ite.next();
            String value = map.get(key);
            if (value == null || value.equals("")) {
                continue;
            }
            List<ConditionItem> list_ = getCIList(key, value, idFlag);
            if (list.size() > 0) {
                ConditionItem lCI = list.get(list.size() - 1);
                ConditionItem rCI = list_.get(list_.size() - 1);
                list.addAll(list_);
                idFlag = idFlag + list_.size();
                ConditionItem pCI = new ConditionItem();
                pCI.setId("ci" + idFlag);
                pCI.setLeafFlag(false);
                pCI.setLeafInfo(null);
                ChildrenInfo cInfo = new ChildrenInfo();
                cInfo.setLeftCIName(lCI.getId());
                cInfo.setConnector(Connector.AND);
                cInfo.setRightCIName(rCI.getId());
                pCI.setChildrenInfo(cInfo);
                list.add(pCI);
                idFlag++;
            } else {
                list.addAll(list_);
                idFlag = idFlag + list_.size();
            }
        }
 
        for (int i = 0; i < list.size(); i++) {
            ConditionItem ci = list.get(i);
            ciMap.put(ci.getId(), ci);
        }
        if (list.size() > 0) {
            ConditionItem rootCI = list.get(list.size() - 1);
            cond.setRootCIName(rootCI.getId());
            cond.setCIMap(ciMap);
            return cond;
        } else {
            return null;
        }
    }
 
    /**
     * 根据value中包含的AND/OR解析出conditionItem组
     * 
     * @param key
     * @param value
     * @param idFlag
     * @return
     */
    public static List<ConditionItem> getCIList(String key, String value, int idFlag) {
        List<ConditionItem> list = new ArrayList<ConditionItem>();
        String connector = Connector.AND;
        while (value.length() > 0) {
            int indexAnd = value.indexOf("\\AND");
            int indexOr = value.indexOf("\\OR");
            ConditionItem ci = new ConditionItem();
            if (indexAnd > 0 || indexOr > 0) {
                String nextConnector = Connector.AND;
                String value_ = null;
                if ((indexAnd > 0 && indexAnd < indexOr) || indexOr < 0) {
                    value_ = value.substring(0, indexAnd);
                    nextConnector = Connector.AND;
                    value = value.substring(indexAnd + 4);
                } else {
                    value_ = value.substring(0, indexOr);
                    nextConnector = Connector.OR;
                    value = value.substring(indexOr + 3);
                }
                ci = getConditionItem(key, value_, idFlag);
                if (list.size() > 0) {
                    ConditionItem lCI = list.get(list.size() - 1);
                    list.add(ci);
                    idFlag++;
                    ConditionItem pCI = new ConditionItem();
                    pCI.setId("ci" + idFlag);
                    pCI.setLeafFlag(false);
                    pCI.setLeafInfo(null);
                    ChildrenInfo cInfo = new ChildrenInfo();
                    cInfo.setLeftCIName(lCI.getId());
                    cInfo.setConnector(connector);
                    cInfo.setRightCIName(ci.getId());
                    pCI.setChildrenInfo(cInfo);
                    list.add(pCI);
                    idFlag++;
                } else {
                    list.add(ci);
                    idFlag++;
                }
                connector = nextConnector;
            } else {
                ci = getConditionItem(key, value, idFlag);
                if (list.size() > 0) {
                    ConditionItem lCI = list.get(list.size() - 1);
                    list.add(ci);
                    idFlag++;
                    ConditionItem pCI = new ConditionItem();
                    pCI.setId("ci" + idFlag);
                    pCI.setLeafFlag(false);
                    pCI.setLeafInfo(null);
                    ChildrenInfo cInfo = new ChildrenInfo();
                    cInfo.setLeftCIName(lCI.getId());
                    cInfo.setConnector(connector);
                    cInfo.setRightCIName(ci.getId());
                    pCI.setChildrenInfo(cInfo);
                    list.add(pCI);
                    idFlag++;
                } else {
                    list.add(ci);
                    idFlag++;
                }
                value = "";
            }
        }
        return list;
    }
 
    /**
     * 将原子value(不包含and/or)解析成单个ConditionItem
     * 
     * @param key
     * @param value
     * @param idFlag
     * @return
     */
    private static ConditionItem getConditionItem(String key, String value, int idFlag) {
        ConditionItem ci = new ConditionItem();
        ci.setId("ci" + idFlag);
        ci.setChildrenInfo(null);
        ci.setLeafFlag(true);
        LeafInfo lInfo = new LeafInfo();
        String operator = null;
 
        if (value.startsWith("\\NOTLIKE")) {
            operator = Operator.NOTLIKE;
            value = value.substring(8);
        } else if (value.startsWith(Operator.STAR) || value.endsWith(Operator.STAR)) {
            operator = Operator.LIKE;
        } else if (value.startsWith(Operator.UNEQUAL)) {
            operator = Operator.UNEQUAL;
            value = value.substring(2);
        } else if (value.startsWith(Operator.GT)) { // add by caill 2016.4.7 为操作添加“大于”的判断 并在此判断中添加“大于等于”的判断
            operator = Operator.GT;
            if (value.startsWith(Operator.GTE)) {
                operator = Operator.GTE;
                value = value.substring(2);
            } else {
                value = value.substring(1);
            }
 
        } else if (value.startsWith(Operator.LT)) { // add by caill 2016.4.7 为操作添加“小于”的判断 并在此判断中添加“小于等于”的判断
            operator = Operator.LT;
            if (value.startsWith(Operator.LTE)) {
                operator = Operator.LTE;
                value = value.substring(2);
            } else {
                value = value.substring(1);
            }
        } /*
             * else if(value.startsWith(Operator.LTE)){ operator = Operator.LTE; value =
             * value.substring(2); }else if(value.startsWith(Operator.GTE)){ operator =
             * Operator.GTE; value = value.substring(2); }
             */else if (value.startsWith("\\IN")) {
            operator = Operator.IN;
            value = value.substring(3);
        } else if (value.startsWith("\\NOTIN")) {
            operator = Operator.NOTIN;
            value = value.substring(6);
        } else {
            operator = Operator.EQUAL;
        }
        lInfo.setOperator(operator);
        if (key.toUpperCase().contains("T_OID.") || key.toUpperCase().contains("F_OID.")) {
            // 去掉T_OID.或者F_OID.
            String clause_ = key.substring(6);
            // 属性为参照属性
            if (clause_.contains(".")) {
                int fpIndex = clause_.indexOf(".");
                String refAbName = key.substring(0, fpIndex + 6);
                key = key.substring(fpIndex + 6 + 1);
                lInfo.setClause(refAbName);
                // 去掉T_OID.或者F_OID.
                refAbName = refAbName.substring(6);
                lInfo.setOperator(Operator.IN);
                QueryTemplate qt = getRefQT(refAbName, key, operator, value);
                LeafValue lValue = new LeafValue();
                lValue.setQueryTemplate(qt);
                lInfo.setValue(lValue);
                // 属性为非参照属性
            } else {
                lInfo.setClause(key);
                LeafValue lValue = new LeafValue();
                lInfo.setValue(lValue);
                lValue.setOrdinaryValue(value);
            }
        } else {
            // 属性为参照属性
            if (key.contains(".")) {
                int fpIndex = key.indexOf(".");
                String refAbName = key.substring(0, fpIndex);
                key = key.substring(fpIndex + 1);
                lInfo.setClause(refAbName);
                lInfo.setOperator(Operator.IN);
                QueryTemplate qt = getRefQT(refAbName, key, operator, value);
                LeafValue lValue = new LeafValue();
                lValue.setQueryTemplate(qt);
                lInfo.setValue(lValue);
                // 属性为非参照属性
            } else {
                lInfo.setClause(key);
                LeafValue lValue = new LeafValue();
                lInfo.setValue(lValue);
                lValue.setOrdinaryValue(value);
            }
        }
        ci.setLeafInfo(lInfo);
        return ci;
    }
 
    /**
     * 获取参照的查询模板
     * 
     * @param refAbName:    参照属性名
     * @param clause:       属性参照的业务类型中的属性
     * @param operator
     * @param ordinaryValue
     * @return
     */
    private static QueryTemplate getRefQT(String refAbName, String clause, String operator, String ordinaryValue) {
        QueryTemplate qt = new QueryTemplate();
        List<String> clauseList = new ArrayList<String>();
        clauseList.add("OID");
        qt.setClauseList(clauseList);
        AttribItem refAb = OMCacheProvider.getAttribute(refAbName);
        //refAb = ServerServiceProvider.getOMDService().getAttributeService().getAttribItemByName(refAbName);
        OtherInfo otherInfo = OtherInfo.getOtherInfoByText(refAb.other);
        int refFlag = otherInfo.getRefFlag();
        String type = otherInfo.getRefTypeName();
        if (refFlag == 0) {
            qt.setType(QTConstants.TYPE_BTM);
            qt.setBtmType(type);
        } else if (refFlag == 1) {
            qt.setType(QTConstants.TYPE_LINK);
            qt.setLinkType(type);
        }
        Condition condition = new Condition();
        qt.setCondition(condition);
        condition.setRootCIName("ci1");
        HashMap<String, ConditionItem> ciMap = new HashMap<String, ConditionItem>();
        condition.setCIMap(ciMap);
        ConditionItem ci = new ConditionItem();
        ci.setId("ci1");
        ciMap.put(ci.getId(), ci);
        ci.setLeafFlag(true);
        LeafInfo leafInfo = new LeafInfo();
        if (clause.contains(".")) {
            int fpIndex = clause.indexOf(".");
            String refAbName_ = clause.substring(0, fpIndex);
            clause = clause.substring(fpIndex + 1);
            leafInfo.setClause(refAbName_);
            leafInfo.setOperator(Operator.IN);
            QueryTemplate qt_ = getRefQT(refAbName_, clause, operator, ordinaryValue);
            LeafValue lValue = new LeafValue();
            lValue.setQueryTemplate(qt_);
            leafInfo.setValue(lValue);
            qt.setId("qt_" + refAbName + "_" + refAbName_);
        } else {
            leafInfo.setClause(clause);
            leafInfo.setOperator(operator);
            LeafValue lValue = new LeafValue();
            lValue.setOrdinaryValue(ordinaryValue);
            leafInfo.setValue(lValue);
            qt.setId("qt_" + refAbName + "_" + clause);
        }
        ci.setLeafInfo(leafInfo);
        condition.setCIMap(ciMap);
        return qt;
    }
 
    /**
     * 
     * @param direction
     * @param oid
     * @param btmType
     * @param map
     * @return
     */
    public static Condition getConditionForLink(String direction, String oid, String btmType, Map<String, String> map) {
        Condition cond = new Condition();
        cond.setRootCIName("ci3");
        Map<String, ConditionItem> ciMap = new HashMap<String, ConditionItem>();
        if (direction.equals(QTConstants.DIRECTION_POSITIVE)) {
            ConditionItem ci1 = new ConditionItem();
            ci1.setId("ci1");
            ci1.setChildrenInfo(null);
            ci1.setLeafFlag(true);
            LeafInfo lInfo = new LeafInfo();
            lInfo.setClause("f_oid");
            lInfo.setOperator(Operator.EQUAL);
            LeafValue lValue = new LeafValue();
            lValue.setOrdinaryValue(oid);
            lInfo.setValue(lValue);
            ci1.setLeafInfo(lInfo);
            ciMap.put(ci1.getId(), ci1);
 
            ConditionItem ci2 = new ConditionItem();
            ci2.setId("ci2");
            ci2.setChildrenInfo(null);
            ci2.setLeafFlag(true);
            LeafInfo lInfo2 = new LeafInfo();
            lInfo2.setClause("t_nameOId");
            lInfo2.setOperator(Operator.IN);
            LeafValue lValue2 = new LeafValue();
            // 参照查询模板
            QueryTemplate refQT = new QueryTemplate();
            lValue2.setQueryTemplate(refQT);
            refQT.setId("refQT");
            refQT.setBtmType(btmType);
            refQT.setType(QTConstants.TYPE_BTM);
            List<String> list = new ArrayList<String>();
            list.add("nameOId");
            refQT.setClauseList(list);
            Condition cond2 = getCondition(map);
            refQT.setCondition(cond2);
            lInfo2.setValue(lValue2);
            ci2.setLeafInfo(lInfo2);
            ciMap.put(ci2.getId(), ci2);
 
            ConditionItem ci3 = new ConditionItem();
            ci3.setId("ci3");
            ci3.setLeafFlag(false);
            ci3.setLeafInfo(null);
            ChildrenInfo cInfo = new ChildrenInfo();
            ci3.setChildrenInfo(cInfo);
            cInfo.setLeftCIName(ci1.getId());
            cInfo.setConnector(Connector.AND);
            cInfo.setRightCIName(ci2.getId());
            ciMap.put(ci3.getId(), ci3);
        } else if (direction.equals(QTConstants.DIRECTION_OPPOSITE)) {
            ConditionItem ci1 = new ConditionItem();
            ci1.setId("ci1");
            ci1.setChildrenInfo(null);
            ci1.setLeafFlag(true);
            LeafInfo lInfo = new LeafInfo();
            lInfo.setClause("t_oid");
            lInfo.setOperator(Operator.EQUAL);
            LeafValue lValue = new LeafValue();
            lValue.setOrdinaryValue(oid);
            lInfo.setValue(lValue);
            ci1.setLeafInfo(lInfo);
            ciMap.put(ci1.getId(), ci1);
 
            ConditionItem ci2 = new ConditionItem();
            ci2.setId("ci2");
            ci2.setChildrenInfo(null);
            ci2.setLeafFlag(true);
            LeafInfo lInfo2 = new LeafInfo();
            lInfo2.setClause("f_oid");
            lInfo2.setOperator(Operator.IN);
            LeafValue lValue2 = new LeafValue();
            // 参照查询模板
            QueryTemplate refQT = new QueryTemplate();
            lValue2.setQueryTemplate(refQT);
            refQT.setId("refQT");
            refQT.setBtmType(btmType);
            refQT.setType(QTConstants.TYPE_BTM);
            List<String> list = new ArrayList<String>();
            list.add("oid");
            refQT.setClauseList(list);
            Condition cond2 = getCondition(map);
            refQT.setCondition(cond2);
            lInfo2.setValue(lValue2);
            ci2.setLeafInfo(lInfo2);
            ciMap.put(ci2.getId(), ci2);
 
            ConditionItem ci3 = new ConditionItem();
            ci3.setId("ci3");
            ci3.setLeafFlag(false);
            ci3.setLeafInfo(null);
            ChildrenInfo cInfo = new ChildrenInfo();
            ci3.setChildrenInfo(cInfo);
            cInfo.setLeftCIName(ci1.getId());
            cInfo.setConnector(Connector.AND);
            cInfo.setRightCIName(ci2.getId());
            ciMap.put(ci3.getId(), ci3);
        }
        cond.setCIMap(ciMap);
        return cond;
    }
 
 
 
    /**
     * 获取业务类型属性总集合
     * 
     * @param btNames
     * @return
     */
    private static Set<String> getUserAttrNameSet(String[] btNames) {
        Set<String> set = new LinkedHashSet<String>();
        try {
            for (String btName : btNames) {
                String[] abNames = ServerServiceProvider.getOMDService().getBTMService().getBtmApNameArray(btName);
                for (String abName : abNames) {
                    set.add(abName);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return set;
    }
 
 
    /**
     * 视图cols viewName.col as col2
     * 
     * @param ltName
     * @return
     */
    public static List<String> getViewColsWithAlias(String[] btNames, String viewName) {
        List<String> cols = new ArrayList<String>();
        String[] sysAbItems = OmdHelper.getBTSysANames();
        for (String sysAbItem : sysAbItems) {
            cols.add(viewName + "." + sysAbItem + " AS " + sysAbItem + "2");
        }
        Set<String> userAttrNameSet = getUserAttrNameSet(btNames);
        for (String userAttrName : userAttrNameSet) {
            cols.add(viewName + "." + userAttrName + " AS " + userAttrName + "2");
        }
        return cols;
    }
 
    /**
     * 获取查询条件的 值
     * 
     * @param con
     * @param key
     * @return
     */
    public static String getConditionItemVlaue(Condition con, String key) {
        if (con == null) {
            return null;
        }
        Map<String, ConditionItem> ciMap = con.getCIMap();
        for (String ciId : ciMap.keySet()) {
            ConditionItem ci = ciMap.get(ciId);
            if (ci.isLeaf()) {
                LeafInfo leafInfo = ci.getLeafInfo();
                String clause = leafInfo.getClause();
                if (clause.equalsIgnoreCase(key)) {
                    return leafInfo.getValue().getOrdinaryValue();
                }
            }
        }
        return null;
    }
 
 
}