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 36903 2010-09-02 16:01:20Z pbaerfuss $
56   *
57   */
58  public class QueryUtil {
59      private static Logger log = LoggerFactory.getLogger(QueryUtil.class);
60  
61      /**
62       * Executes a query.
63       */
64      public static Collection<Content> query(String repository, String statement){
65          return query(repository,statement, Query.SQL);
66      }
67  
68      /**
69       * Executes a query.
70       */
71      public static Collection<Content> query(String repository, String statement, String language){
72          return query(repository,statement,language, ItemType.NT_BASE);
73      }
74  
75      /**
76       * Executes a query, throwing any exceptions that arise as a result.
77       */
78      public static Collection<Content> exceptionThrowingQuery(String repository, String statement, String language, String returnItemType) throws RepositoryException {
79          QueryManager qm = MgnlContext.getQueryManager(repository);
80          Query query= qm.createQuery(statement, language);
81          QueryResult result = query.execute();
82          return result.getContent(returnItemType);
83      }
84  
85      /**
86       * Executes a query - if an exception is thrown, it is logged and an empty collection is returned.
87       */
88      @SuppressWarnings("unchecked") //Collections.EMPTY_LIST;
89      public static Collection<Content> query(String repository, String statement, String language, String returnItemType) {
90          try {
91              return exceptionThrowingQuery(repository, statement, language, returnItemType);
92          } catch (Exception e) {
93              log.error("can't execute query [" + statement  + "], will return empty collection", e);
94          }
95          return Collections.EMPTY_LIST;
96      }
97  
98      /**
99       * @param month 1-12 (as opposed to java.util.Calendar 0-11 notation)
100      */
101     public static String createDateExpression(int year, int month, int day) {
102         Calendar cal = Calendar.getInstance();
103         cal.set(year, month - 1, day);
104         return createDateExpression(cal);
105     }
106 
107     /**
108      * Expression representing a date.
109      */
110     public static String createDateExpression(Calendar calendar) {
111         return "DATE '" + DateFormatUtils.format(calendar.getTimeInMillis(), "yyyy-MM-dd", calendar.getTimeZone()) + "'";
112     }
113 
114     /**
115      * @param month 1-12 (as opposed to java.util.Calendar 0-11 notation)
116      */
117     public static String createDateTimeExpression(int year, int month, int day, int hour, int minutes, int seconds) {
118         Calendar cal = Calendar.getInstance();
119         cal.set(year, month - 1, day, hour, minutes, seconds);
120         return createDateTimeExpression(cal);
121     }
122 
123     /**
124      * Expression representing a date and time.
125      */
126     public static String createDateTimeExpression(Calendar calendar) {
127         calendar.set(Calendar.MILLISECOND, 0);
128         StringBuffer str = new StringBuffer("TIMESTAMP '");
129         str.append(DateFormatUtils.format(calendar.getTime(), "yyyy-MM-dd'T'HH:mm:ss.SSSZ", calendar.getTimeZone()));
130         str.insert(str.length()-2, ":");
131         str.append("'");
132         return str.toString();
133     }
134 
135     /**
136      * @param month 1-12 (as opposed to java.util.Calendar 0-11 notation)
137      */
138     public static String createDateTimeExpressionIgnoreTimeZone(int year, int month, int day, int hour, int minutes, int seconds) {
139         Calendar cal = Calendar.getInstance(DateUtils.UTC_TIME_ZONE);
140         cal.set(year, month - 1, day, hour, minutes, seconds);
141         return createDateTimeExpression(cal);
142     }
143 
144     /**
145      * Do not consider the timezone.
146      */
147     public static String createDateTimeExpressionIgnoreTimeZone(Calendar calendar) {
148         Calendar utc = Calendar.getInstance(DateUtils.UTC_TIME_ZONE);
149         utc.setTimeInMillis(calendar.getTimeInMillis() + calendar.getTimeZone().getRawOffset());
150         return createDateTimeExpression(utc);
151     }
152 }