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 clauseList = new ArrayList(); //默认查询返回所有列 clauseList.add("*"); qt.setClauseList(clauseList); //查询条件(条件中可嵌套QT对象) Condition con = new Condition(); qt.setCondition(con);//设置查询条件 HashMap ciMap = new HashMap(); //查询条件项 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 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 clauseList = new ArrayList(); 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 ciMap = new HashMap(); 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 clauseList = new ArrayList(); //查询列 clauseList.add("*"); qt.setClauseList(clauseList); Condition con = new Condition(); qt.setCondition(con); HashMap ciMap = new HashMap(); //设置嵌套查询条件 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 clauseListse = new ArrayList(); 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 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[]{}; } }