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
package com.vci.test.qttest;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
import com.vci.common.qt.object.Condition;
import com.vci.common.qt.object.ConditionItem;
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.QTConstants;
import com.vci.common.qt.object.QueryTemplate;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.atm.AttribItem;
import com.vci.corba.omd.btm.BtmItem;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.corba.omd.data.LinkObject;
import com.vci.omd.objects.OtherInfo;
import com.vci.server.base.utility.ServerServiceProvider;
import com.vci.server.query.service.QueryObjectService;
 
/**
 * @Title        :服务器端QT查询API使用示例
 * @Description    : 
 * @Copyright    :宏博远达科技有限公司
 * @Author        :平台与规划部/ZhongGY/E-mail:zhonggy@vci-tech.com
 * @Date        :2015-7-6
 * @Version        :1
 * @Other        :产生注释:Alt+Shift+J
 */
public class TestQTAPI {
    /**
     * 业务对象QT嵌套查询
     * @Title        :testBOQt
     * @Description    :
     * @return
     */
    public static BusinessObject[] testBOQt() {
        //主查询QT对象
        QueryTemplate qt = new QueryTemplate();
        //QT临时Id
        qt.setId("qt1");
        //业务类型查询
        qt.setType(QTConstants.TYPE_BTM);
        //查询的业务类型
        qt.setBtmType("part");
        qt.setQueryChildrenFlag(true);//true表示查询子孙业务类型
        //qt.setVersion(?);//业务对象版本规则(最新版本版次、最新发布版本?)
        //查询列
        List<String> clauseList = new ArrayList<String>();
        //默认查询返回所有列
        clauseList.add("*");
        qt.setClauseList(clauseList);
        //查询条件(条件中可嵌套QT对象)
        Condition con = new Condition();
        qt.setCondition(con);//设置查询条件
        HashMap<String, ConditionItem> ciMap = new HashMap<String, ConditionItem>();
        //查询条件项
        ConditionItem ci = new ConditionItem();
        ci.setId("test1");
        ci.setLeafFlag(true);
        LeafInfo leafInfo = new LeafInfo();
        leafInfo.setClause("ownproduct");//查询条件属性
        leafInfo.setOperator(Operator.IN);//查询条件操作符(IN 中可嵌套QT对象)
        //嵌套查询QT对象
        QueryTemplate qtse = getRefQT("ownproduct", "code", "=", "test");        
        LeafValue lValue = new LeafValue();//查询条件值
        lValue.setQueryTemplate(qtse);//查询条件值可嵌套QT对象
        leafInfo.setValue(lValue);//设置查询条件值
        ci.setLeafInfo(leafInfo);//查询条件项
        ciMap.put(ci.getId(), ci);//设置查询条件项(通过集合可设置多查询条件项)
        con.setCIMap(ciMap);//QT查询条件定义
        con.setRootCIName(con.getRootCINameByCIMap(ciMap));
        List<BusinessObject> result = null;
         try {
             //执行查询
             try {
                result = QueryObjectService.getInstance().findBtmObjects(qt);
            } catch (Throwable e) {
            }
                if(qt.isQueryChildren()){
                    //查询子类型
                    BtmItem[] btms = null;
                    try {
                        btms = ServerServiceProvider.getOMDService().getBTMService().getChildrenBtms(qt.getBtmType());
                    } catch (Exception e) {
                    } catch (Throwable e) {
                    }
                    for(int i = 0; i < btms.length; i++){
                        qt.setBtmType(btms[i].name);
                        try {
                            result.addAll(QueryObjectService.getInstance().findBtmObjects(qt));
                        } catch (Throwable e) {
                        }
                    }
                }
                return result.toArray(new BusinessObject[0]);
        } catch (Throwable e) {
        }
        return new BusinessObject[]{};
        
    }
    
    /**
     * @Title        :构建嵌套查询模板
     * @Description    :
     * @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();
        try {
            List<String> clauseList = new ArrayList<String>();
            clauseList.add("OID");
            qt.setClauseList(clauseList);
            // 根据refAbName获取业务类型
            AttribItem refAb;
            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);
        } catch (VCIError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return qt;
    }
    
    public static void main(String[] args) {
    //    System.err.println(testBOQt().length);
        System.err.println(testLinkQt().length);
    }
    
    /**
     * @Title        :多链接类型(嵌套)查询示例:获取某产品下所有子孙零部件对象 [产品to根零部件link关系 + ebom关系]
     * @Description    :
     * @return
     */
    private static LinkObject[] testLinkQt() {
        // 主查询模板对象
        QueryTemplate qt = new QueryTemplate(); //示例ebom关系
        qt.setId("qt1");
        qt.setType(QTConstants.TYPE_LINK);
        qt.setLinkType("ebom");
        //设置正反向查询
        //qt.setDirection("positiva");
        qt.setBtmType("part");
        //qt.setBtmType("*");//也支持所有业务类型查询
        //设置查询多少层
        qt.setLevel(5);
        List<String> clauseList = new ArrayList<String>();
        //查询列
        clauseList.add("*");
        qt.setClauseList(clauseList);
        Condition con = new Condition();
        qt.setCondition(con);
        HashMap<String, ConditionItem> ciMap = new HashMap<String, ConditionItem>();
        //设置嵌套查询条件
        ConditionItem ci = new ConditionItem();        
        ci.setId("test1");
        ci.setLeafFlag(true);
        LeafInfo leafInfo = new LeafInfo();
        leafInfo.setClause("f_oid");
        leafInfo.setOperator(Operator.IN);
        LeafValue lValue = new LeafValue();
        leafInfo.setValue(lValue);
        try {
            //获取嵌套条件的查询模板
            /*QTWrapper qtm = QTClient.getService().getQT("productTreeQueryTemplate");
            String qtText = qtm.qtText;
            QueryTemplate qt_ = Tool.getQTByQTText(qtm.qtName, qtText);*/
            QueryTemplate qt_ = new QueryTemplate();  //示例"产品to根零部件link关系"
            //TODO:构件一个查询模板对象qt_
            
            List<String> clauseListse = new ArrayList<String>();
            clauseListse.add("t_oid");
            qt_.setClauseList(clauseListse);
            lValue.setQueryTemplate(qt_);//设置嵌套查询模板对象
        } catch (Throwable e) {
        }
        ci.setLeafInfo(leafInfo);
        ciMap.put(ci.getId(), ci);
        con.setCIMap(ciMap);
        con.setRootCIName(con.getRootCINameByCIMap(ciMap));
        List<LinkObject> result = null;
        try {
            /*
             * result = QTClient.getService().findLTObjects(qt.getId(),
             * Tool.qtTOXMl(qt).asXML()); QueryResultDialog dialog = new
             * QueryResultDialog(result, qt.getClauseList());
             * dialog.setVisible(true);
             */
            // 执行查询
            // 查询一层, 分查询是否有有下一级和不查询是否有有下一级
            if (qt.getLevel() == 1) {
                try {
                    result = QueryObjectService.getInstance().findLinkObjectOneLevel(qt);
                } catch (Throwable e) {
                }
                // 查询多层, 或者没有层数限制
            } else {
                try {
                    result = QueryObjectService.getInstance().findLinkObject(qt);
                } catch (Throwable e) {
                }
                int recReturnMode = qt.getRecReturnMode();
                // 对查询结果进行过滤
                if (recReturnMode == QTConstants.RECRETURNMODE_FILTER) {
                    result = QueryObjectService.getInstance().filterLos(result);
                }
            }
            return result.toArray(new LinkObject[0]);
        } catch (Throwable e) {
        }
        return new LinkObject[]{};
    }
}