Querying Objects Using the QuerySpec, QueryResult and SearchCondition 

1. Querying link classes Querying link classes is slightly different, 
since it is usually not desired to perform a query on all link objects 
of particular type. Instead, you want to query for all link objects 
that exist for one or two given objects. An example for a link object 
is WTPartUsageLink. 

This link holds the information for a uses-usedBy relation which 
describes dependencies between parts (An assembly is dependent on 
its components eg.). Link objects can be identified in the Rational 
Rose Model as being a link attributes of an association between two 
persistable objects. See the examples for more information. 

2. Events Find queries dispatch a PersistenceManagerEvent.POST_FIND event, 
navigate queries dispatch a PersistenceManagerEvent.POST_NAVIGATE event. 

3. Implicite filtering Objects which are not allowed to be viewed by the 
user are automatically removed from the result set which is returned. 

Example 1 (simple find query): 

We want to search for all objects of type wt.doc.WTDocument which have 
the title "talk" or "presentation", while excluding all documents that 
have the strings "not" and "public" in their description. 

These query directly corresponds to the SQL statement select title from 

wtdocument where (title = 'talk' or title = 'presentation' ) and not 
description like '%not%public%'; 

// create a query specification which allows searching for all objects 
//of type WTDocument 

QuerySpec mySpec = new QuerySpec( wt.doc.WTDocument.class );

// Create the search conditions 

SearchCondition searchCond1 = new SearchCondition( wt.doc.WTDocument.class, "title", SearchCondition.EQUAL, "talk" ); 
SearchCondition searchCond2 = new SearchCondition( wt.doc.WTDocument.class, "title", SearchCondition.EQUAL, "presentation" );
SearchCondition searchCond3 = new SearchCondition( wt.doc.WTDocument.class, "description", SearchCondition.LIKE, "%not%public%" ); 

// Append the search conditions and apply the necessary boolean operations 

mySpec.appendOpenParen(); mySpec.appendSearchCondition(searchCond1); 
mySpec.appendOr(); mySpec.appendSearchCondition(searchCond2); 
mySpec.appendCloseParen(); 
mySpec.appendAnd(); 
mySpec.appendNot(); 
mySpec.appendSearchCondition(searchCond3); 

// Do the query 

QueryResult myResult = PersistenceHelper.manager.find( mySpec ); 

Example 2 (find query on link objects): 

We want to find all link objects of type WTPartUsageLink between the given 
objects thePartAssembly and thePartCompMaster. 

If thePartAssembly contains a "uses" relation to thePartCompMaster, 
the query will return one or more objects. In this case, a query 
specification is not necessary. 

WTPart thePartAssembly = ... // 

Your initialization 

WTPartMaster thePartCompMaster = ... // Your initialization 

QueryResult myResult = PersistenceHelper.manager.find( WTPartUsageLink.class, thePartCompMaster, WTPartUsageLink.USES_ROLE, thePartAssembly ); 

Example 3 (navigate query): 

Here we want to find all objects of type WTPartMaster which are used by the 
given part thePartAssembly. 

Actually, the query finds link objects of type WTPartUsageLink. 
But since we use the navigate method, these links are traversed before the 
result is returned. Note, that if we set the last argument of the navigate 
method to false, the links would not be traversed and WTPartUsageLink 
objects would be returned. Note also that additional search conditions 
could be applied to WTPartMaster and WTPartUsageLink objects. 

WTPart thePartAssembly = ... // 

Your initialization 

mySpec = new QuerySpec( wt.part.WTPartMaster.class, wt.part.WTPartUsageLink.class ); 
myResult = PersistenceHelper.manager.navigate( thePartAssembly, WTPartUsageLink.USES_ROLE, mySpec, true ); 
