cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

We are happy to announce the new Windchill Customization board! Learn more.

how to write query to get Documents in a particular product?

grainger-3
1-Newbie

how to write query to get Documents in a particular product?

Hello,

I am new in windchill customization, i am to write a query to get all the documents( say type "x") in a product(say "y"). Can somebody help???

1 ACCEPTED SOLUTION

Accepted Solutions
BhushanNehe
14-Alexandrite
(To:grainger-3)

Well, it is returning array, use the following code to get pass the error

QuerySpec queryspec = new QuerySpec(WTDocumentMaster.class);

  queryspec.setAdvancedQueryEnabled(true);

  int contIndex = queryspec.appendClassList(PDMLinkProduct.class,false);

  SearchCondition sc1 = new SearchCondition(WTDocumentMaster.class, "containerReference.key.id",PDMLinkProduct.class, wt.util.WTAttributeNameIfc.ID_NAME);

  queryspec.appendWhere(sc1, new int[] { 0, contIndex });

  queryspec.appendAnd();

  SearchCondition scSWPart = new SearchCondition(PDMLinkProduct.class ,"containerInfo.name",SearchCondition.EQUAL,"TestProduct1",true);

  queryspec.appendWhere(scSWPart,new int[] { contIndex });

  QueryResult queryResult = PersistenceHelper.manager.find(queryspec);

  System.out.println("Size :- " + queryResult.size());

  int size = queryResult.size();

  System.out.println("WTDocument query result size: " + size);

while (queryResult.hasMoreElements()) {

  wt.fc.Persistable persisArray []= (Persistable[]) queryResult.nextElement();

  wt.doc.WTDocumentMaster doc=(WTDocumentMaster) persisArray[0];

  System.out.println(doc);

  }

Regards,

Bhushan

View solution in original post

12 REPLIES 12
bsindelar
6-Contributor
(To:grainger-3)

Assuming by "query" you mean an SQL query, then you need to join the correct context type DB table (PDMLinkProduct, WTLibrary, or Project2 are the table names) for the table you are trying to get to the wtdocument DB table, and note that for the wtdocument table the ida3containerreference column contains the same value as the context DB table's ida2a2 value.

Out of curiosity, what type of functionality are you looking to customize?  My company is very experienced in all things Windchill and I'd be happy to lend a helping hand or provide additional options to achieve a certain functionality depending upon the need.

I meant by queryspec.

kpritchard
4-Participant
(To:grainger-3)

Add Context and create a Join with WTDocument, set the Criteria to Context:Name... You can use a Parameter here so that the User can enter the Context Name when performing the query

Thanks for your reply, Could you please give me queryspec ... ?

the query i am using is as below, but it's giving exception.

QuerySpec qss = new QuerySpec(WTDocumentMaster.class);

  qss.appendWhere(new SearchCondition(WTDocumentMaster.class,WTDocumentMaster.CONTAINER_NAME,SearchCondition.EQUAL,"TestProduct1"),new int[] { 0 });

exception is saying

Attribute "containerReference.name" is not a member of class "class wt.doc.WTDocumentMaster.

BhushanNehe
14-Alexandrite
(To:grainger-3)

Hi Gajendra

Following example is for Cad Document, you can modify to use if to WTDocuments

QuerySpec queryspec =

new QuerySpec(EPMDocument.

class);

queryspec.setAdvancedQueryEnabled(

true);

int contIndex = queryspec.appendClassList(PDMLinkProduct.class,false);

SearchCondition sc1 =

new SearchCondition(EPMDocument.class, "containerReference.key.id",PDMLinkProduct.class, wt.util.WTAttributeNameIfc.ID_NAME);

queryspec.appendWhere(sc1,

new int[] { 0, contIndex });

queryspec.appendAnd();

SearchCondition scSWPart =

new SearchCondition(PDMLinkProduct.class ,"containerInfo.name",SearchCondition.EQUAL,"<your product name>",true);

queryspec.appendWhere(scSWPart,

new int[] { contIndex });

QueryResult queryResult = PersistenceHelper.

manager.find((StatementSpec)queryspec);

System.

out.println("Size :- " + queryResult.size());

Regards,

Bhushan

hello Bhushan,

Thank you for reply,

used modified the query as below,

QuerySpec queryspec = new QuerySpec(WTDocumentMaster.class); 

  queryspec.setAdvancedQueryEnabled(true);   

  int contIndex = queryspec.appendClassList(PDMLinkProduct.class,false); 

  SearchCondition sc1 = new SearchCondition(WTDocumentMaster.class, "containerReference.key.id",PDMLinkProduct.class, wt.util.WTAttributeNameIfc.ID_NAME); 

  queryspec.appendWhere(sc1, new int[] { 0, contIndex }); 

  queryspec.appendAnd(); 

   SearchCondition scSWPart = new SearchCondition(PDMLinkProduct.class ,"containerInfo.name",SearchCondition.EQUAL,"TestProduct1",true); 

  queryspec.appendWhere(scSWPart,new int[] { contIndex }); 

  QueryResult queryResult = PersistenceHelper.manager.find((StatementSpec)queryspec); 

  System.out.println("Size :- " + queryResult.size()); 

  int size = queryResult.size();

  System.out.println("WTDocument query result size: " + size);

  if (logger.isTraceEnabled()) {

  logger.trace("Query Size:" + size);

  }

  while (queryResult.hasMoreElements()) {

  WTDocumentMaster docMstr = (WTDocumentMaster) queryResult

  .nextElement();

  LatestConfigSpec ltSpec = new LatestConfigSpec();

this is throwing exception

Exception in thread "main" wt.util.WTRemoteException: Unable to invoke remote method; nested exception is:

  java.rmi.ServerRuntimeException: Server exception; nested exception is:

  java.lang.ClassCastException: [Lwt.fc.Persistable; cannot be cast to wt.doc.WTDocumentMaster

  at wt.method.RemoteMethodServer.invoke(RemoteMethodServer.java:788)

  at com.comdev.tools.ReassignLCRDDWithContainerInfo.main(ReassignLCRDDWithContainerInfo.java:99)

Caused by: java.lang.ClassCastException: [Lwt.fc.Persistable; cannot be cast to wt.doc.WTDocumentMaster

  ... 2 more

Could you please correct me where i am wrong?

Gajendra - any reason for passing WTDocumentMaster instead of WTDocument to QuerySpec?

No, not any specific reason, i found query somewhere so i tried.

My goal is to reassign Life cycle to all the WTDocuments in "TestProduct1" product.

BhushanNehe
14-Alexandrite
(To:grainger-3)

Well, it is returning array, use the following code to get pass the error

QuerySpec queryspec = new QuerySpec(WTDocumentMaster.class);

  queryspec.setAdvancedQueryEnabled(true);

  int contIndex = queryspec.appendClassList(PDMLinkProduct.class,false);

  SearchCondition sc1 = new SearchCondition(WTDocumentMaster.class, "containerReference.key.id",PDMLinkProduct.class, wt.util.WTAttributeNameIfc.ID_NAME);

  queryspec.appendWhere(sc1, new int[] { 0, contIndex });

  queryspec.appendAnd();

  SearchCondition scSWPart = new SearchCondition(PDMLinkProduct.class ,"containerInfo.name",SearchCondition.EQUAL,"TestProduct1",true);

  queryspec.appendWhere(scSWPart,new int[] { contIndex });

  QueryResult queryResult = PersistenceHelper.manager.find(queryspec);

  System.out.println("Size :- " + queryResult.size());

  int size = queryResult.size();

  System.out.println("WTDocument query result size: " + size);

while (queryResult.hasMoreElements()) {

  wt.fc.Persistable persisArray []= (Persistable[]) queryResult.nextElement();

  wt.doc.WTDocumentMaster doc=(WTDocumentMaster) persisArray[0];

  System.out.println(doc);

  }

Regards,

Bhushan

BhushanNehe
14-Alexandrite
(To:BhushanNehe)

Hi Gajendra,

Does the last suggestion help you to get the documents in particular product. If so can you mark the appropriate answer correct to help others.

Regards,

Bhushan

Bhushan Nehe The only real change I would make is to not hard code the attribute names.  I like your use of WTAttributeNameIfc.  Not enough people use that.

In this case,

"containerReference.key.id" == new StringBuilder().append(WTDocumentMaster.CONTAINER_REFERENCE).append('.').append(WTAttributeNameIfc.REF_OBJECT_ID).toString();

"containerInfo.name" = new StringBuilder().append(PDMLinkProduct.CONTAINER_INFO).append('.').append(WTContainerInfo.NAME).toString();

Top Tags