1 13 package info.magnolia.cms.gui.controlx.search; 14 15 import info.magnolia.cms.gui.query.SearchQuery; 16 import info.magnolia.cms.gui.query.SearchQueryExpression; 17 import info.magnolia.cms.gui.query.SearchQueryOperator; 18 import info.magnolia.cms.gui.query.StringSearchQueryParameter; 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.apache.commons.lang.StringUtils; 26 27 28 32 public class SimpleSearchUtil { 33 34 public static SearchQuery getSimpleSearchQuery(String searchStr, SearchConfig config) { 35 SearchQuery query = new SearchQuery(); 36 query.setRootExpression(getSimpleSearchExpression(searchStr, config)); 37 return query; 38 } 39 40 43 public static SearchQueryExpression getSimpleSearchExpression(String searchStr, SearchConfig config) { 44 if (StringUtils.isNotEmpty(searchStr)) { 45 List expressions = new ArrayList (); 46 for (Iterator iter = config.getControlDefinitions().iterator(); iter.hasNext();) { 47 SearchControlDefinition def = (SearchControlDefinition) iter.next(); 48 expressions.add(new StringSearchQueryParameter( 49 def.getColumn(), 50 searchStr, 51 StringSearchQueryParameter.CONTAINS)); 52 } 53 return chainExpressions(expressions, SearchQueryOperator.OR); 54 } 55 56 return null; 57 } 58 59 public static SearchQueryExpression chainExpressions(Collection expressions, String operator) { 60 SearchQueryExpression expr = null; 61 for (Iterator iter = expressions.iterator(); iter.hasNext();) { 62 SearchQueryExpression newExpr = (SearchQueryExpression) iter.next(); 63 if (expr == null) { 64 expr = newExpr; 65 } 66 else { 67 SearchQueryOperator opt = new SearchQueryOperator(operator); 68 opt.setleft(newExpr); 69 opt.setRight(expr); 70 expr = opt; 71 } 72 } 73 return expr; 74 } 75 76 } 77 | Popular Tags |