Here is a full sample from blogbox:
/**
- entry search returns struct with keys [entries,count]
*/
struct function search(search="",isPublished,category,author,max=0,offset=0){
var results = {};
// get Hibernate Restrictions class
var restrictions = getRestrictions();
// criteria queries
var criteria = [];
// isPublished filter
if( structKeyExists(arguments,“isPublished”) AND arguments.isPublished NEQ “any”){
arrayAppend(criteria, restrictions.eq(“isPublished”, javaCast(“boolean”,arguments.isPublished)) );
}
// Author Filter
if( structKeyExists(arguments,“author”) AND arguments.author NEQ “all”){
arrayAppend(criteria, restrictions.eq(“author.authorID”, javaCast(“int”,arguments.author)) );
}
// Search Criteria
if( len(arguments.search) ){
// like disjunctions
var orCriteria = [];
arrayAppend(orCriteria, restrictions.like(“title”,"%#arguments.search#%"));
arrayAppend(orCriteria, restrictions.like(“content”,"%#arguments.search#%"));
// append disjunction to main criteria
arrayAppend( criteria, restrictions.disjunction( orCriteria ) );
}
// Category Filter
if( structKeyExists(arguments,“category”) AND arguments.category NEQ “all”){
// Uncategorized?
if( arguments.category eq “none” ){
arrayAppend(criteria, restrictions.isEmpty(“categories”) );
}
// With categories
else{
// create association criteria, by passing a simple value the method will inflate.
arrayAppend(criteria, “categories”);
// add the association criteria to the main search
arrayAppend(criteria, restrictions.in(“categories.categoryID”,JavaCast(“java.lang.Integer[]”,[arguments.category])));
}
}
// run criteria query and projections count
results.entries = criteriaQuery(criteria=criteria,offset=arguments.offset,max=arguments.max,sortOrder=“publishedDate DESC”,asQuery=false);
results.count = criteriaCount(criteria=criteria);
return results;
}
Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com
Social: twitter.com/lmajano facebook.com/lmajano