Windchill 开发 根据实例对象查询关联的ECN
作者:互联网
Windchill 开发 根据实例对象查询关联的ECN
在Windchill二次开发时,通过ptc官方提供的api,我们可以很容易的获取ECN对象的 受影响对象和产生对象。例如:
// 获取变更后的对象
QueryResult qr = ChangeHelper2.service.getChangeablesAfter((WTChangeOrder2) obj)
现在有如下开发场景,需要在新的ECN创建时,校验受影响对象是否有其他未关闭的ECN。
这时候,我考虑从数据库查询,ECN的结构稍微复杂点,ECN与变更任务关联,变更任务再与产生对象关联。
根据部件编号查询所有有关联的ECN对象,数据库查询如下:
-- 根据部件编号,查询产生对象中包含该部件任意版本的ECN
select a1.IDA2A2,a1.name,a1.wtChgOrderNumber,a2.IDA2A2,a2.description,a2.statestate
from WTChangeOrder2Master a1 join WTChangeOrder2 a2 on a1.ida2a2=a2.ida3masterreference
where a2.branchidIterationInfo in
(
select branchida3a5 from IncludedIn2 where branchida3b5 in
(
select branchida3a5 from ChangeRecord2 where ida3b5 in
(
select IDA2A2 from wtpart where wtpart.ida3masterreference in
(select IDA2A2 from wtpartmaster where wtpartnumber='部件编号')
)
)
)
在实际开发中,一般并不需要用高级查询完整执行上面的sql。例如校验,只需要查出 WTChangeOrder2 表中的生命周期状态就可以了;然后将上面sql中最内层和次内层子查询,替换成文档或EPM对象,就可以查询他们相关的ECN了。
使用高级查询,代码实现如下:
import org.apache.log4j.Logger;
import wt.change2.ChangeRecord2;
import wt.change2.IncludedIn2;
import wt.change2.WTChangeOrder2;
import wt.doc.WTDocument;
import wt.epm.EPMDocument;
import wt.fc.Persistable;
import wt.fc.PersistenceServerHelper;
import wt.fc.QueryResult;
import wt.fc.ReferenceFactory;
import wt.fc.WTObject;
import wt.part.WTPart;
import wt.query.ClassAttribute;
import wt.query.QueryException;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.query.SubSelectExpression;
import wt.util.WTException;
/**
* @author 彭秉浪 PengBingLang@dingtalk.com
*/
public class TestUtil {
private static final Logger logger = Logger.getLogger(TestUtil.class.getName());
/**
* 在创建ECN后调用,校验受影响对象是否有其他未关闭的ECN
*/
public static void checkOldECN(WTObject newECN, WTObject obj, StringBuffer errorMsg) {
try {
QuerySpec qs1 = new QuerySpec();
qs1.setAdvancedQueryEnabled(true);
int a1 = 0;
SearchCondition sc1 = null;
if (obj instanceof WTPart) {
a1 = qs1.addClassList(WTPart.class, false);
qs1.appendSelectAttribute("thePersistInfo.theObjectIdentifier.id", a1, false);
sc1 = new SearchCondition(WTPart.class, WTPart.NUMBER, SearchCondition.EQUAL, ((WTPart) obj).getNumber());
} else if (obj instanceof WTDocument) {
a1 = qs1.addClassList(WTDocument.class, false);
qs1.appendSelectAttribute("thePersistInfo.theObjectIdentifier.id", a1, false);
sc1 = new SearchCondition(WTDocument.class, WTDocument.NUMBER, SearchCondition.EQUAL, ((WTDocument) obj).getNumber());
} else if (obj instanceof EPMDocument) {
a1 = qs1.addClassList(EPMDocument.class, false);
qs1.appendSelectAttribute("thePersistInfo.theObjectIdentifier.id", a1, false);
sc1 = new SearchCondition(EPMDocument.class, EPMDocument.NUMBER, SearchCondition.EQUAL, ((EPMDocument) obj).getNumber());
}
qs1.appendWhere(sc1, new int[] { a1 });
SubSelectExpression sse1 = new SubSelectExpression(qs1);
QuerySpec qs2 = new QuerySpec();
qs2.setAdvancedQueryEnabled(true);
int a2 = qs2.addClassList(ChangeRecord2.class, false);
qs2.appendSelectAttribute("roleAObjectRef.key.branchId", a2, false);
ClassAttribute ca2 = new ClassAttribute(ChangeRecord2.class, "roleBObjectRef.key.id");
SearchCondition sc2 = new SearchCondition(ca2, SearchCondition.IN, sse1);
qs2.appendWhere(sc2, new int[] { a2 });
SubSelectExpression sse2 = new SubSelectExpression(qs2);
QuerySpec qs3 = new QuerySpec();
qs3.setAdvancedQueryEnabled(true);
int a3 = qs3.addClassList(IncludedIn2.class, false);
qs3.appendSelectAttribute("roleAObjectRef.key.branchId", a3, false);
ClassAttribute ca3 = new ClassAttribute(IncludedIn2.class, "roleBObjectRef.key.branchId");
SearchCondition sc3 = new SearchCondition(ca3, SearchCondition.IN, sse2);
qs3.appendWhere(sc3, new int[] { a3 });
SubSelectExpression ssc3 = new SubSelectExpression(qs3);
QuerySpec qs4 = new QuerySpec();
qs4.setAdvancedQueryEnabled(true);
int a4 = qs4.addClassList(WTChangeOrder2.class, true);
qs4.appendSelectAttribute(WTChangeOrder2.BRANCH_IDENTIFIER, a4, true);
qs4.appendSelectAttribute(WTChangeOrder2.LIFE_CYCLE_STATE, a4, true);
ClassAttribute ca4 = new ClassAttribute(WTChangeOrder2.class, "iterationInfo.branchId");
SearchCondition sc4 = new SearchCondition(ca4, SearchCondition.IN, ssc3);
qs4.appendWhere(sc4, new int[] { a4 });
QueryResult qr = PersistenceServerHelper.manager.query(qs4);
// 查询oid
ReferenceFactory refefence = new ReferenceFactory();
String oid = refefence.getReferenceString((Persistable) newECN);
logger.debug("----------------------->WTObject newECN oid " + oid);
while (qr.hasMoreElements()) {
Object[] objectArr = (Object[]) qr.nextElement();
if (objectArr[0] instanceof WTChangeOrder2) {
WTChangeOrder2 ecn = (WTChangeOrder2) objectArr[0];
String lifeCycleState = ecn.getLifeCycleState().toString();
String branchId = String.valueOf(ecn.getBranchIdentifier());
logger.debug("----------------------->ECN State " + lifeCycleState + " " + branchId);
// 已解决 已取消
if ("RESOLVED".equals(lifeCycleState) || "CANCELLED".equals(lifeCycleState)) {
continue;
}
if (oid.indexOf(branchId) < 0) {
errorMsg.append(obj.getDisplayIdentity() + "对象被其他ECN占用:" + ecn.getDisplayIdentity() + "\n");
}
}
}
} catch (QueryException e) {
e.printStackTrace();
} catch (WTException e) {
e.printStackTrace();
}
}
}
标签:Windchill,wt,ECN,SearchCondition,实例,new,import,class 来源: https://blog.csdn.net/Peng_Bing_lang/article/details/112031795