View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 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.module.rssaggregator.util;
35  
36  import static java.util.Collections.emptyList;
37  
38  import info.magnolia.cms.core.Content;
39  import info.magnolia.cms.core.search.Query;
40  import info.magnolia.cms.util.QueryUtil;
41  
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.List;
45  
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  /**
53   * Convenience class to simplify use of Magnolia queries and the mapping of their results to types. <p/> Note that all
54   * queries performed by this class are logged at DEBUG level for convenient debugging when necessary.
55   *
56   * @author Rob van der Linden Vooren
57   * @see ContentMapper
58   */
59  public class MagnoliaTemplate implements MagnoliaQueryOperations {
60  
61      /** Logger for this instance. */
62      protected Logger logger = LoggerFactory.getLogger(getClass());
63  
64      /** {@inheritDoc} */
65      @Override
66      public <T> List<T> queryForList(String repository, String query, String language, String type, ContentMapper<T> mapper) {
67          return query(repository, query, language, type, mapper);
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public <T> T queryForObject(String repository, String query, String language, String type, ContentMapper<T> mapper) {
73          List<T> results = query(repository, query, language, type, mapper);
74          return singleResult(results);
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public <T> List<T> xpathQueryForList(String repository, String query, String type, ContentMapper<T> mapper) throws DataAccessException {
80          return queryForList(repository, query, Query.XPATH, type, mapper);
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public <T> T xpathQueryForObject(String repository, String query, String type, ContentMapper<T> mapper) throws DataAccessException {
86          return queryForObject(repository, query, Query.XPATH, type, mapper);
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public Node xpathQueryForContent(String repository, String query, String type) throws DataAccessException {
92          return queryForObject(repository, query, Query.XPATH, type, new ContentMapper<Node>() {
93              @Override
94              public Node map(Node content) throws RepositoryException {
95                  return content;
96              }
97          });
98      }
99  
100     // Helper methods
101 
102     protected <T> List<T> query(String repository, String query, String language, String type, ContentMapper<T> mapper) throws DataAccessException {
103         Collection<Content> contents = queryInternal(repository, query, language, type);
104         if (contents.isEmpty()) {
105             return emptyList();
106         }
107         List<T> result = new ArrayList<T>(contents.size());
108         for (Content content : contents) {
109             try {
110                 result.add(mapper.map(content.getJCRNode()));
111             } catch (RepositoryException re) {
112                 logger.error(String.format("Failed to map content '%s' using mapper '%s'", content, mapper.getClass()), re);
113             }
114         }
115         return result;
116     }
117 
118     /**
119      * Performs the actual query execution. Purposely delegates to {@link QueryUtil#exceptionThrowingQuery(String, String, String, String) QueryUtil.exceptionThrowingQuery(..)} in order to provide flexibility with regards to
120      * error handling, by translating any {@link RepositoryException} into an unchecked {@link DataAccessException}.
121      * This allows for optional exception handling and logging, making it a better alternative then using {@link QueryUtil#query(String, String) QueryUtil.query(..)} directly, which logs the error and then directly swallows
122      * the {@link RepositoryException exception}. Execute a query for a result list of type T, for the given query.
123      * 
124      * @param repository the repository to execute query against
125      * @param query query to execute
126      * @param language query language (either {@value Query#XPATH} or {@value Query#SQL}).
127      * @param type the type of the item to query for
128      * @return the result object of the required type, or {@code null} in case of a {@code null} query
129      * @throws DataAccessException if there is any problem executing the query
130      */
131     protected <T> Collection<T> queryInternal(String repository, String query, String language, String type) {
132         try {
133             return doExceptionThrowingQuery(repository, query, language, type);
134         } catch (RepositoryException re) {
135             throw new DataAccessException("A repository exception occurred", re);
136 
137         }
138     }
139 
140     @SuppressWarnings("unchecked")
141     protected <T> Collection<T> doExceptionThrowingQuery(String repository, String query, String language, String type) throws RepositoryException {
142         logger.debug("Executing {} query for '{}' in repository '{}': \"{}\"", new Object[] {language,
143                 type, repository, query}
144                 );
145         Collection<T> results = (Collection<T>) QueryUtil.exceptionThrowingQuery(repository, query, language, type);
146         logger.debug("Query returned %s result(s)", (results == null) ? null : results.size());
147         return results;
148     }
149 
150     /**
151      * Return a single result object from the given collection or {@code null} if the collection is empty or null.
152      *
153      * @param results the result collection (may be {@code null})
154      * @return the single result object, or {@code null} when no result
155      * @throws IncorrectResultSizeDataAccessException
156      *          if collection contains more than one element
157      */
158     protected static <T> T singleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException {
159         int size = (results == null ? 0 : results.size());
160         if (size == 0) {
161             return null;
162         }
163         if (results.size() > 1) {
164             throw new IncorrectResultSizeDataAccessException(1, size);
165         }
166         return results.iterator().next();
167     }
168 
169 }