windchill开发简单入门之高级条件查询

源:原创 作者:云淡天晴 时间:2008-10-17

       在上一篇文章中,我们介绍了windchill开发简单入门之条件查询,可以说是查询中必备的能力。但是现实中,我们的查询将更加复杂,我们平时能够进行sql语句的多表查询,那么windchill本身是不是也有这样的简单但是方便功能强大的查询呢?答案是肯定的,但是相关资料比较少,或者说也比较复杂,在这里,我们对此举一个实例。

   以下是我们的源代码:

 import wt.clients.prodmgmt.WTPartHelper;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.part.WTPartUsageLink;
import wt.query.CompositeWhereExpression;
import wt.query.ConstantExpression;
import wt.query.LogicalOperator;
import wt.query.QueryException;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.query.TableColumn;
import wt.util.WTException;


public class Demo3
{

 /**
  * @param args
  * @throws WTException
  */
 public static void main(String[] args) throws WTException
 {
  // 这里在本机windchill系统中查询固定编码的part
  WTPart[] parts=WTPartHelper.findPartByNumber("0000000001");
  WTPart[] parts2=WTPartHelper.findPartByNumber("0000000002");
  WTPartMaster master=null;
  if(parts2[0]!=null)
  {
   //获取该part对应的WTPartMaster
      master=(WTPartMaster)parts2[0].getMaster();
  }
  WTPartUsageLink link=queryPartusageLink(parts[1], master);
  System.out.println("WTPartUsageLink:"+link);
 }
 
 /**
  * 查询part与另一个part是否已经有WTPartUsageLink的关系
  * @param part   父节点
  * @param master  子节点
  * @return  
  */
 public static WTPartUsageLink queryPartusageLink(WTPart part, WTPartMaster master)
 {
  //如果part或者master任一为空,则返回空值
  if (part == null || master == null)
  {
   return null;
  }
  long poid = part.getPersistInfo().getObjectIdentifier().getId();
  long moid = master.getPersistInfo().getObjectIdentifier().getId();
  try
  {
   QuerySpec queryspec = new QuerySpec();
   //该关系的类是WTPartUsageLink
   int a = queryspec.appendClassList(WTPartUsageLink.class, true);
   //此处一定要设置为true,通知系统为高级查询
   queryspec.setAdvancedQueryEnabled(true);
   String[] aliases = new String[1];
   aliases[0] = queryspec.getFromClause().getAliasAt(a);
   //IDA3A5为数据表中字段,存贮的就是part的id
   TableColumn tc1 = new TableColumn(aliases[0], "IDA3A5");
   //IDA3B5为数据表中字段,存贮的就是master的id
   TableColumn tc2 = new TableColumn(aliases[0], "IDA3B5");
   //此处查询的sql语句是与的关系
   CompositeWhereExpression andExpression = new CompositeWhereExpression(LogicalOperator.AND);
   //设置条件
   andExpression.append(new SearchCondition(tc1, "=", new ConstantExpression(new Long(poid))));
   andExpression.append(new SearchCondition(tc2, "=", new ConstantExpression(new Long(moid))));
   queryspec.appendWhere(andExpression, null);
   QueryResult qr1 = PersistenceHelper.manager.find(queryspec);
   if (qr1.hasMoreElements())
   {
    Object obj[] = (Object[]) qr1.nextElement();
    WTPartUsageLink usage = (WTPartUsageLink) obj[0];
    return usage;
   }
  }
  catch (QueryException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  catch (WTException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
 }
 
}

发布了24 篇原创文章 · 获赞 4 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/wangxiaomei2008/article/details/4990938