// Search for assets with subject subjects
// Acquire handles to managers
AssetManager assetManager = Connector.getAssetManager();
SubjectManager subjectManager = Connector.getSubjectManager();
Searcher searcher = Connector.getSearcher();
// Initialize parameters for EMS calls
Map metadata = new HashMap();
Collection metadataTemplateList = new ArrayList();
String assetVersionUuid;
// Create some asset placeholders
assetVersionUuid = assetManager.createAsset(m_userAuth,
AssetType.IMAGE,
"Test Asset Placeholder",
"JUnit created Asset with no file.",
m_userAuth.getOrgUuid(),
metadataTemplateList,
metadata);
cleanupAssetUuids.add(assetVersionUuid);
// First we create a subject tree. Here we use the form of createSubject that
// lets us also create a term at the same time we create the subject.
SubjectInfo rootSubject = subjectManager.getRootSubject(m_rootAuth, m_acmeCorpUuid);
String productsUuid = subjectManager.createSubject(m_rootAuth,
"product-subj", // subject 'code'
"Products", // term name
Language.US_ENGLISH.getLocale(), // language of term
SubjectTermType.AUTHORITATIVE_TERM, // term type
null, // domain -- null means the default domain
m_acmeCorpUuid); // the subject owner
subjectManager.addChild(m_rootAuth, rootSubject.getUuid(), productsUuid);
String industrialProductsUuid = subjectManager.createSubject(m_rootAuth,
"indust-product-subj", "Industrial Products",
Language.US_ENGLISH.getLocale(),
SubjectTermType.AUTHORITATIVE_TERM, null, m_acmeCorpUuid);
subjectManager.addChild(m_rootAuth, productsUuid, industrialProductsUuid);
String consumerProductsUuid = subjectManager.createSubject(m_rootAuth,
"consumer-product-subj", "Consumer Products",
Language.US_ENGLISH.getLocale(),
SubjectTermType.AUTHORITATIVE_TERM, null, m_acmeCorpUuid);
subjectManager.addChild(m_rootAuth, productsUuid, consumerProductsUuid);
String tricorderUuid = subjectManager.createSubject(m_rootAuth,
"tc-thx1138-subj", "Tricorder Model THX1138",
Language.US_ENGLISH.getLocale(),
SubjectTermType.AUTHORITATIVE_TERM, null, m_acmeCorpUuid);
subjectManager.addChild(m_rootAuth, consumerProductsUuid, tricorderUuid);
/* Now there's a subject hierarchy like this:
*
* Root
* |
* Products
* / \
* Industrial Consumer
* |
* Tricorder Model THX1138
*/
// Associate our asset with the Tricoder subject
subjectManager.applySubjectToObjects(m_userAuth, tricorderUuid, Collections.singletonList(assetVersionUuid));
// Now search for asset versions with the the Tricorder subject -- we'll get back our asset version.
//Create new Multi result specifier
Query query = new Query( ObjectType.ASSET );
// We want the asset name and the asset version UUID
query.addReturnAttribute("name");
query.addReturnAttribute("version.uuid");
//Add the condition that the asset version must be associated with tricorder subject
query.addCondition("subjects.uuid", Query.Operators.EQUALS, tricorderUuid);
// If you know the subjectId of the Subject object (its primary key), you
// can use it rather than the subject's UUID. This will elimate an additional
// join in the search query and subsequently increase performance:
// query.addCondition("subjects.subjectId", Query.Operators.EQUALS, subjectId);
// Execute the search
QueryResult subjectQueryResult = searcher.search(m_userAuth, query);
System.out.println("number of items found: "+subjectQueryResult.getResultCount());
Iterator iter = subjectQueryResult.rowIterator();
while (iter.hasNext()) {
Object fields[] = (Object[])iter.next();
System.err.println("subject query result: "+(String)fields[0]); // this will print out the asset name
}