Skip to main content
12-Amethyst
May 10, 2013
Solved

How to retrieve all objects(WTpart,document,epm) from a Particular context?

  • May 10, 2013
  • 7 replies
  • 7607 views

I need to get all the objects including WTPart,WTDocument,EPMDocument from a particular context.Any give me some code or API for this to do.

Thanks

Best answer by KD_01

HI Vigneshwaran,

Don't know whether It's a efficient way or not.But it works in `10.1

package ext.customization;

import wt.doc.WTDocument;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.part.WTPart;
import wt.pdmlink.PDMLinkProduct;
import wt.pds.StatementSpec;
import wt.query.CompositeQuerySpec;
import wt.query.QueryException;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTException;

public class ObjectFromContainer {
public static void main(String[] args) {
try{
CompositeQuerySpec cqs = new CompositeQuerySpec();
cqs.addComponent(statementspecGenerator(WTPart.class,"<product name>"));
cqs.addComponent(statementspecGenerator(WTDocument.class,"<product name>"));

QueryResult queryResult2 = PersistenceHelper.manager.find((StatementSpec)cqs);
System.out.println(queryResult2.size());
}
catch(QueryException e)
{
e.printStackTrace();
} catch (WTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static StatementSpec statementspecGenerator(Class c,String contName) throws QueryException
{
QuerySpec queryspec = new QuerySpec(c);


queryspec.setAdvancedQueryEnabled(true);
int contIndex = queryspec.appendClassList(PDMLinkProduct.class,false);
SearchCondition sc1 = new SearchCondition(c, "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,contName,true);
queryspec.appendWhere(scSWPart, new int[] { contIndex });

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

return (StatementSpec)queryspec;
}

}

7 replies

KD_0112-AmethystAnswer
12-Amethyst
May 13, 2013

HI Vigneshwaran,

Don't know whether It's a efficient way or not.But it works in `10.1

package ext.customization;

import wt.doc.WTDocument;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.part.WTPart;
import wt.pdmlink.PDMLinkProduct;
import wt.pds.StatementSpec;
import wt.query.CompositeQuerySpec;
import wt.query.QueryException;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTException;

public class ObjectFromContainer {
public static void main(String[] args) {
try{
CompositeQuerySpec cqs = new CompositeQuerySpec();
cqs.addComponent(statementspecGenerator(WTPart.class,"<product name>"));
cqs.addComponent(statementspecGenerator(WTDocument.class,"<product name>"));

QueryResult queryResult2 = PersistenceHelper.manager.find((StatementSpec)cqs);
System.out.println(queryResult2.size());
}
catch(QueryException e)
{
e.printStackTrace();
} catch (WTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static StatementSpec statementspecGenerator(Class c,String contName) throws QueryException
{
QuerySpec queryspec = new QuerySpec(c);


queryspec.setAdvancedQueryEnabled(true);
int contIndex = queryspec.appendClassList(PDMLinkProduct.class,false);
SearchCondition sc1 = new SearchCondition(c, "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,contName,true);
queryspec.appendWhere(scSWPart, new int[] { contIndex });

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

return (StatementSpec)queryspec;
}

}

VINO12-AmethystAuthor
12-Amethyst
May 13, 2013

Hi kaushik das,

thanks for your response.The code you have given is working fine but it is giving the query size only.I need to get all the objects with their lifecycle state(each object may have different LC).I tried to get object from query result using queryresult2.nextelement().But it is showing error.

what I'm trying to achieve is to change the Lifecycle state of each objects.

thank you

12-Amethyst
May 13, 2013

Change your main method to the following and it will give you the object .

public static void main(String[] args) {

try{

WTPart part = null;

WTDocument doc = null;

Persistable[] pt = null;

CompositeQuerySpec cqs = new CompositeQuerySpec();

cqs.addComponent(statementspecGenerator(WTPart.class,"newMPM"));

cqs.addComponent(statementspecGenerator(WTDocument.class,"newMPM"));

QueryResult queryResult2 = PersistenceHelper.manager.find((StatementSpec)cqs);

System.out.println(queryResult2.size());

while(queryResult2.hasMoreElements())

{

pt = (Persistable[])queryResult2.nextElement();

for(Persistable p:pt)

{

if(p instanceof WTPart)

{

part = (WTPart)p;

System.out.println("Part name :- " + part.getName());

}

else if(p instanceof WTDocument)

{

doc = (WTDocument)p;

System.out.println("Doc name " + doc.getName());

}

}

}

}

catch(QueryException e)

{

e.printStackTrace();

} catch (WTException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}