Guest User

Untitled

a guest
May 5th, 2012
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment