Windchill_二次开发新手入门常用的API
1.根据零件名称/编码 得到该零件
wt.clients.prodmgmt.WTPartHelper.findPartByName(name) ; wt.clients.prodmgmt.WTPartHelper.findPartByNumber(number);
2.根据WTpart得到WTparMaster
WtPart wtpart; WTPartMaster wtmaster=(WTPartMster)part.getMaster();
3.获取codebase下配置文件wt.properties属性信息
WTProperties wtproperties = WTProperties.getLocalProperties();
String wthome = wtproperties.getProperty("wt.home", ""); //codebase的文件夹路径
4.获取part被借用的所有父部件
QueryResult qr= wt.part.WTPartHelper.service.getUsedByWTParts(WTPartMster wtMaster); 注:此方法得到的结果为该part被使用情况的全部父部件,包括了Design视图及Manufacturing视图 更包括了父部件使用part的所有修订版 本,打印出来可以看到会有相同的部件编号,不同的修订版本.
5.根据OID 获取Wtpart
wt.fc.WTReference partRef = new wt.fc.ReferenceFactory().getReference( oid );
WTPart wtpart=(WTPart)partRef;
6.得到零件最新版本
WTPart wtpart= (WTPart) VersionControlHelper.getLatestIteration(part);
7.通过过滤得到零件最新版本
QuerySpec querysearch = new QuerySpec(WTPartMaster.class); //查询所有的WTPartMaster
QueryResult queryresult = PersistenceHelper.manager.find(querysearch);
LatestConfigSpec latestconfigspec = new LatestConfigSpec(); //根据WTPartMaster查询所有最新版本的零部件
QueryResult allWTPart = ConfigHelper.service.filteredIterationsOf(queryresult,latestconfigspec)
8.查询某用户某段时间范围内创建的零件
QuerySpec qs = new QuerySpec(WTPart.class);
qs.appendSearchCondition(new SearchCondition(WTPart.class,WTPart.CREATE_TIMESTAMP, true, new AttributeRange(begintime, endtime)));//删选条件 时间范围内
qs.appendAnd();//一定要加上 不然下一个条件不能删选
qs.appendSearchCondition(new SearchCondition(WTPart.class, "iterationInfo.creator.key", SearchCondition.EQUAL,PersistenceHelper.getObjectIdentifier(name)));//删选条件 用户
QueryResult qr = PersistenceHelper.manager.find(qs); //今后持续更新
9.根据用户名得到用户
/**
* 根据用户名得到用户
* @param name 用户名
* @throws WTException
* return WTUser
*/
public static WTUser getUserFromName(String name) throws WTException {
Enumeration enumUser = OrganizationServicesHelper.manager.findUser(WTUser.NAME, name);
WTUser user = null;
if (enumUser.hasMoreElements())
user = (WTUser) enumUser.nextElement();
if (user == null) {
enumUser = OrganizationServicesHelper.manager.findUser(WTUser.FULL_NAME, name);
if (enumUser.hasMoreElements())
user = (WTUser) enumUser.nextElement();
}
if (user == null) {
throw new WTException("系统中不存在用户名为'" + name + "'的用户!");
}
return user;
}
}
10.windchill 中查询,高级查询,基本查询
QuerySpec qs = new QuerySpec();//构造
Int index = qs.appendClassList(WTPart.class,true);//添加查询类型,获取类型索引,第2个参数表示“要查询的类型、表”
WhereExpression where = new SearchCondition(WTPart.class, WTPart.xx, “=”, xx);//泛型在WC API中的使用
//获取查询条件数目
If(qs.getConditionCount()>0 && qs.getWhere().endsWith(“"))
{
qs.appendAnd();
}
//添加查询条件
qs.appendWhere(where, new int[]{index});
//** 以下是联合查询的API范例。LINK关系//ROLEA、ROLEB的INDEX被使用到。
int linkIndex = qs.appendClassList(XXLink.class, false);
qs.appendJoin(linkIndex, xxLink.RoleA, index_A);
qs.appendJoin(linkIndex, xxLink.RoleB, index_B);
//添加“生命周期”查询条件
LifeCycleConfigSpec lcsp = new LifeCycleConfigSpec();
lcsp.setLifeCycleState(State.toState(state));
qs = lcsp.appendSearchCriteria(qs);
//执行查询
QueryResult qr = PersistenceHelper.manager.find(qs);
//过滤出最新小版本
LatestConfigSpec lcs = new LatestConfigSpec();
qr = lcs.process(qr);
11、根据WTPartMaster对象获得最新的WTPart
/**
* 根据WTPartMaster对象获得最新的WTPart
* @param partmaster WTPartMaster对象
* @return 最新的WTPart
* @throws WTException
*/
public static WTPart getLastPart(WTPartMaster partmaster) throws WTException{
WTPart part=null;
if(partmaster==null){
return part;
}
ConfigSpec configSpec=ConfigHelper.service.getDefaultConfigSpecFor(WTPart.class);
QueryResult qr=ConfigHelper.service.filteredIterationsOf(partmaster, configSpec);
if(qr!=null){
while(qr.hasMoreElements()){
part=(WTPart) qr.nextElement();
}
}
return part;
}
在windchill中很多查询都是非常类似的,方法也是非常之多,不过只要会两三中查询方式就可以应付windchill中几乎所有的查询需要,
本次将再次提及一种非常方便的查询,不过对于这种查询方式需要对于数据库表有一定的了解。此实例中需要注意的是对于时间的比较查询。
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.projmgmt.admin.Project2;
import wt.projmgmt.execution.ProjectPlan;
import wt.query.CompositeWhereExpression;
import wt.query.ConstantExpression;
import wt.query.DateExpression;
import wt.query.LogicalOperator;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.query.TableColumn;
import wt.util.WTException;
public class Demo
{
/**
* @param args
* @throws WTException
* @throws ParseException
*/
public static void main(String[] args) throws WTException, ParseException
{
String string="2009-05-01 00:00:00.000000";
Timestamp time=Timestamp.valueOf(string);
// TODO Auto-generated method stub
QuerySpec qs = new QuerySpec();
int a = qs.appendClassList(Project2.class, true);
int b = qs.appendClassList(ProjectPlan.class, true);
qs.setAdvancedQueryEnabled(true);
String[] aliases = new String[2];
aliases[0] = qs.getFromClause().getAliasAt(a);
aliases[1] = qs.getFromClause().getAliasAt(b);
TableColumn tc1 = new TableColumn(aliases[0], "IDA2A2"); //项目id
TableColumn tc2 = new TableColumn(aliases[0], "STATECONTAINERTEAMMANAGEDINF");//项目状态
TableColumn tc3 = new TableColumn(aliases[1], "IDA3B8"); //项目计划中引用项目id
TableColumn tc4 = new TableColumn(aliases[1], "NAME"); //项目名称
TableColumn tc5 = new TableColumn(aliases[1], "PERCENTCOMPLETE"); //项目完成进度
TableColumn tc6 = new TableColumn(aliases[1], "TIMETOSTART"); //项目实际开始时间
CompositeWhereExpression andExpression = new CompositeWhereExpression(LogicalOperator.AND);
andExpression.append(new SearchCondition(tc1, "=", tc3));
andExpression.append(new SearchCondition(tc2, "=", new ConstantExpression("RUNNING"))); //正在运行
andExpression.append(new SearchCondition(tc4, "=", new ConstantExpression("上雪堂")));
andExpression.append(new SearchCondition(tc5, ">=", new ConstantExpression(new Integer("89"))));
andExpression.append(new SearchCondition(tc6, SearchCondition.GREATER_THAN_OR_EQUAL, new ConstantExpression(time)));
qs.appendWhere(andExpression, null);
QueryResult qr=PersistenceHelper.manager.find(qs);
while (qr.hasMoreElements())
{
Object obj[] = (Object[]) qr.nextElement();
Project2 project2 = (Project2)obj[0];
System.out.println(project2.getName()); //项目名称
System.out.println(project2.getCtmState());//项目状态
ProjectPlan projectPlan = (ProjectPlan)obj[1];
System.out.println(projectPlan.getPercentComplete()); //项目完成进度
System.out.println(projectPlan.getStartTime()); //项目开始时间
}
}
}
高级查询一般用于有多种约束条件的数据查询.用高级查询主要用来减少数据查询的次数,提高查询的效率.
下面一个例子就是一个简单的高级查询,用于查询某一软属性为某一值的所有的文档:(CSDN好像贴源代码的功能并不强,不像JAVAEYE,这里将就一下)
QuerySpec qs = new QuerySpec();
qs.setAdvancedQueryEnabled(true);
int ibaHolderIndex = qs.appendClassList(WTDocument.class, true);
int ibaStringValueIndex = qs.appendClassList(StringValue.class, false);
int ibaStringDefinitionIndex = qs.appendClassList(StringDefinition.class, false);
// Latest Iteration
SearchCondition scLatestIteration = new SearchCondition(WTDocument.class, WTAttributeNameIfc.LATEST_ITERATION,SearchCondition.IS_TRUE);
// String Value With IBA Holder
SearchCondition scJoinStringValueIBAHolder = new SearchCondition(StringValue.class,
"theIBAHolderReference.key.id", WTDocument.class, WTAttributeNameIfc.ID_NAME);
// String Value With Definition
SearchCondition scJoinStringValueStringDefinition = new SearchCondition(StringValue.class,
"definitionReference.key.id", StringDefinition.class, WTAttributeNameIfc.ID_NAME);
// String Definition 软属性名称
SearchCondition scStringDefinitionName = new SearchCondition(StringDefinition.class, StringDefinition.NAME,
SearchCondition.EQUAL, ibaname);
// String Value 软属性值
SearchCondition scStringValueValue = new SearchCondition(StringValue.class, StringValue.VALUE,
SearchCondition.EQUAL, ibavalue.toUpperCase());
// documentmaster name = type
qs.appendWhere(scLatestIteration, ibaHolderIndex);
qs.appendAnd();
qs.appendWhere(scJoinStringValueIBAHolder, ibaStringValueIndex, ibaHolderIndex);
qs.appendAnd();
qs.appendWhere(scJoinStringValueStringDefinition,
qs.appendWhere(scJoinStringValueStringDefinition, ibaStringValueIndex, ibaStringDefinitionIndex);
qs.appendAnd();
qs.appendWhere(scStringDefinitionName, ibaStringDefinitionIndex);
qs.appendAnd();
qs.appendWhere(scStringValueValue, ibaStringValueIndex);
QueryResult qr = PersistenceHelper.manager.find(qs);
注意:
1)如果对查询没有把握,可以先试着用sql在数据库里操作一下
2)查询尽可能考虑条件的优化
3)减少查询次数并不是必然的,如果多个表关联查询,要考虑这些表的数据量的问题,必要时将不消耗资源的查询先做了
4)测试过程,可以建立一些临时表,用工具导入尽可能多的测试数据,这高级查询产生的SQL去执行,看一下执行的效率
标签:qs,wt,SearchCondition,查询,新手入门,API,二次开发,new,class From: https://www.cnblogs.com/onlywen/p/17035627.html