Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment