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;
|
}
|
|
|
}
|