KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > QueryUtil


1 /**
2  *
3  */

4 package info.magnolia.cms.util;
5
6 import info.magnolia.cms.core.ItemType;
7 import info.magnolia.cms.core.search.Query;
8 import info.magnolia.cms.core.search.QueryManager;
9 import info.magnolia.cms.core.search.QueryResult;
10 import info.magnolia.context.MgnlContext;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19
20 /**
21  * Util to execute queries as simple as possible.
22  * @author Philipp Bracher
23  * @version $Id: QueryUtil.java 6381 2006-09-15 08:10:59Z philipp $
24  *
25  */

26 public class QueryUtil {
27     private static Logger log = LoggerFactory.getLogger(QueryUtil.class);
28
29     /**
30      * Execute a query
31      * @param repository
32      * @param statement
33      * @return
34      */

35     public static Collection JavaDoc query(String JavaDoc repository, String JavaDoc statement){
36         return query(repository,statement, Query.SQL);
37     }
38
39     /**
40      * Execute a query
41      * @param repository
42      * @param statement
43      * @param language
44      * @return
45      */

46     public static Collection JavaDoc query(String JavaDoc repository, String JavaDoc statement, String JavaDoc language){
47         return query(repository,statement,language, ItemType.NT_BASE);
48     }
49
50     /**
51      * Execute a query
52      * @param repository
53      * @param statement
54      * @param language
55      * @param returnItemType
56      * @return
57      */

58     public static Collection JavaDoc query(String JavaDoc repository, String JavaDoc statement, String JavaDoc language, String JavaDoc returnItemType){
59         try {
60             QueryManager qm = MgnlContext.getQueryManager(repository);
61             Query query= qm.createQuery(statement, language);
62             QueryResult result = query.execute();
63             return result.getContent(returnItemType);
64         }
65         catch (Exception JavaDoc e) {
66             log.error("can't query, will return empty collection", e);
67         }
68         
69         return Collections.EMPTY_LIST;
70     }
71     
72 }
73
Popular Tags