View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.util;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.ItemType;
38  import info.magnolia.cms.core.search.Query;
39  import info.magnolia.cms.core.search.QueryManager;
40  import info.magnolia.cms.core.search.QueryResult;
41  import info.magnolia.context.MgnlContext;
42  import org.apache.commons.lang.time.DateFormatUtils;
43  import org.apache.commons.lang.time.DateUtils;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  import javax.jcr.RepositoryException;
48  import java.util.Calendar;
49  import java.util.Collection;
50  import java.util.Collections;
51  
52  /**
53   * Util to execute queries as simple as possible.
54   * @author Philipp Bracher
55   * @version $Id: QueryUtil.java 32667 2010-03-13 00:37:06Z gjoseph $
56   *
57   */
58  public class QueryUtil {
59      private static Logger log = LoggerFactory.getLogger(QueryUtil.class);
60  
61      /**
62       * Executes a query.
63       * @param repository
64       * @param statement
65       * @return
66       */
67      public static Collection<Content> query(String repository, String statement){
68          return query(repository,statement, Query.SQL);
69      }
70  
71      /**
72       * Executes a query.
73       * @param repository
74       * @param statement
75       * @param language
76       * @return
77       */
78      public static Collection<Content> query(String repository, String statement, String language){
79          return query(repository,statement,language, ItemType.NT_BASE);
80      }
81  
82      /**
83       * Executes a query, throwing any exceptions that arise as a result.
84       */
85      public static Collection<Content> exceptionThrowingQuery(String repository, String statement, String language, String returnItemType) throws RepositoryException {
86          QueryManager qm = MgnlContext.getQueryManager(repository);
87          Query query= qm.createQuery(statement, language);
88          QueryResult result = query.execute();
89          return result.getContent(returnItemType);
90      }
91  
92      /**
93       * Executes a query - if an exception is thrown, it is logged and an empty collection is returned.
94       * @param repository
95       * @param statement
96       * @param language
97       * @param returnItemType
98       * @return
99       */
100     @SuppressWarnings("unchecked") //Collections.EMPTY_LIST;
101     public static Collection<Content> query(String repository, String statement, String language, String returnItemType) {
102         try {
103             return exceptionThrowingQuery(repository, statement, language, returnItemType);
104         } catch (Exception e) {
105             log.error("can't execute query [" + statement  + "], will return empty collection", e);
106         }
107         return Collections.EMPTY_LIST;
108     }
109 
110     /**
111      * @param month 1-12 (as opposed to java.util.Calendar 0-11 notation)
112      */
113     public static String createDateExpression(int year, int month, int day) {
114         Calendar cal = Calendar.getInstance();
115         cal.set(year, month - 1, day);
116         return createDateExpression(cal);
117     }
118 
119     /**
120      * Expression representing a date
121      */
122     public static String createDateExpression(Calendar calendar) {
123         return "DATE '" + DateFormatUtils.format(calendar.getTimeInMillis(), "yyyy-MM-dd", calendar.getTimeZone()) + "'";
124     }
125 
126     /**
127      * @param month 1-12 (as opposed to java.util.Calendar 0-11 notation)
128      */
129     public static String createDateTimeExpression(int year, int month, int day, int hour, int minutes, int seconds) {
130         Calendar cal = Calendar.getInstance();
131         cal.set(year, month - 1, day, hour, minutes, seconds);
132         return createDateTimeExpression(cal);
133     }
134 
135     /**
136      * Expression representing a date and time
137      */
138     public static String createDateTimeExpression(Calendar calendar) {
139         calendar.set(Calendar.MILLISECOND, 0);
140         StringBuffer str = new StringBuffer("TIMESTAMP '");
141         str.append(DateFormatUtils.format(calendar.getTime(), "yyyy-MM-dd'T'HH:mm:ss.SSSZ", calendar.getTimeZone()));
142         str.insert(str.length()-2, ":");
143         str.append("'");
144         return str.toString();
145     }
146 
147     /**
148      * @param month 1-12 (as opposed to java.util.Calendar 0-11 notation)
149      */
150     public static String createDateTimeExpressionIgnoreTimeZone(int year, int month, int day, int hour, int minutes, int seconds) {
151         Calendar cal = Calendar.getInstance(DateUtils.UTC_TIME_ZONE);
152         cal.set(year, month - 1, day, hour, minutes, seconds);
153         return createDateTimeExpression(cal);
154     }
155 
156     /**
157      * Do not consider the timezone.
158      */
159     public static String createDateTimeExpressionIgnoreTimeZone(Calendar calendar) {
160         Calendar utc = Calendar.getInstance(DateUtils.UTC_TIME_ZONE);
161         utc.setTimeInMillis(calendar.getTimeInMillis() + calendar.getTimeZone().getRawOffset());
162         return createDateTimeExpression(utc);
163     }
164 }