Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 1.83 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2. * entry search returns struct with keys [entries,count]
  3. */
  4. struct function search(search="",isPublished,category,author,max=0,offset=0){
  5.         var results = {};
  6.         // get Hibernate Restrictions class
  7.         var restrictions = getRestrictions();  
  8.         // criteria queries
  9.         var criteria = [];
  10.        
  11.         // isPublished filter
  12.         if( structKeyExists(arguments,"isPublished") AND arguments.isPublished NEQ "any"){
  13.                 arrayAppend(criteria, restrictions.eq("isPublished", javaCast("boolean",arguments.isPublished)) );
  14.         }              
  15.         // Author Filter
  16.         if( structKeyExists(arguments,"author") AND arguments.author NEQ "all"){
  17.                 arrayAppend(criteria, restrictions.eq("author.authorID", javaCast("int",arguments.author)) );
  18.         }
  19.         // Search Criteria
  20.         if( len(arguments.search) ){
  21.                 // like disjunctions
  22.                 var orCriteria = [];
  23.                         arrayAppend(orCriteria, restrictions.like("title","%#arguments.search#%"));
  24.                         arrayAppend(orCriteria, restrictions.like("content","%#arguments.search#%"));
  25.                 // append disjunction to main criteria
  26.                 arrayAppend( criteria, restrictions.disjunction( orCriteria ) );
  27.         }
  28.         // Category Filter
  29.         if( structKeyExists(arguments,"category") AND arguments.category NEQ "all"){
  30.                
  31.                 // Uncategorized?
  32.                 if( arguments.category eq "none" ){
  33.                         arrayAppend(criteria, restrictions.isEmpty("categories") );
  34.                 }
  35.                 // With categories
  36.                 else{
  37.                         // create association criteria, by passing a simple value the method will inflate.
  38.                         arrayAppend(criteria, "categories");
  39.                         // add the association criteria to the main search
  40.                         arrayAppend(criteria, restrictions.in("categories.categoryID",JavaCast("java.lang.Integer[]",[arguments.category])));
  41.                 }                      
  42.         }      
  43.        
  44.         // run criteria query and projections count
  45.         results.entries = criteriaQuery(criteria=criteria,offset=arguments.offset,max=arguments.max,sortOrder="publishedDate DESC",asQuery=false);
  46.         results.count   = criteriaCount(criteria=criteria);
  47.        
  48.         return results;
  49. }