KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > controlx > search > SimpleSearchUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

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 JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.commons.lang.StringUtils;
26
27
28 /**
29  * @author Philipp Bracher
30  * @version $Revision: 6341 $ ($Author: philipp $)
31  */

32 public class SimpleSearchUtil {
33
34     public static SearchQuery getSimpleSearchQuery(String JavaDoc searchStr, SearchConfig config) {
35         SearchQuery query = new SearchQuery();
36         query.setRootExpression(getSimpleSearchExpression(searchStr, config));
37         return query;
38     }
39
40     /**
41      * Return the expression build by a simple search
42      */

43     public static SearchQueryExpression getSimpleSearchExpression(String JavaDoc searchStr, SearchConfig config) {
44         if (StringUtils.isNotEmpty(searchStr)) {
45             List JavaDoc expressions = new ArrayList JavaDoc();
46             for (Iterator JavaDoc 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 JavaDoc expressions, String JavaDoc operator) {
60         SearchQueryExpression expr = null;
61         for (Iterator JavaDoc 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