info.magnolia.module.rssaggregator.util
Class MagnoliaTemplate

java.lang.Object
  extended by info.magnolia.module.rssaggregator.util.MagnoliaTemplate
All Implemented Interfaces:
MagnoliaQueryOperations

public class MagnoliaTemplate
extends Object
implements MagnoliaQueryOperations

Convenience class to simplify use of Magnolia queries and the mapping of their results to types.

Note that all queries performed by this class are logged at DEBUG level for convenient debugging when necessary.

Author:
Rob van der Linden Vooren
See Also:
ContentMapper

Field Summary
protected  org.slf4j.Logger logger
          Logger for this instance.
 
Constructor Summary
MagnoliaTemplate()
           
 
Method Summary
protected
<T> Collection<T>
doExceptionThrowingQuery(String repository, String query, String language, String type)
           
protected
<T> List<T>
query(String repository, String query, String language, String type, ContentMapper<T> mapper)
           
<T> List<T>
queryForList(String repository, String query, String language, String type, ContentMapper<T> mapper)
          Execute a query for a result list of type T, for the given query.
<T> T
queryForObject(String repository, String query, String language, String type, ContentMapper<T> mapper)
          Execute a query for a single or null result object, for the given query.
protected
<T> Collection<T>
queryInternal(String repository, String query, String language, String type)
          Performs the actual query execution.
protected static
<T> T
singleResult(Collection<T> results)
          Return a single result object from the given collection or null if the collection is empty or null.
 javax.jcr.Node xpathQueryForContent(String repository, String query, String type)
          Execute an XPath query for a Content node.
<T> List<T>
xpathQueryForList(String repository, String query, String type, ContentMapper<T> mapper)
          Execute an XPath query for a result list of type T, for the given query.
<T> T
xpathQueryForObject(String repository, String query, String type, ContentMapper<T> mapper)
          Execute an XPath query for a single or null result object, for the given query.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected org.slf4j.Logger logger
Logger for this instance.

Constructor Detail

MagnoliaTemplate

public MagnoliaTemplate()
Method Detail

queryForList

public <T> List<T> queryForList(String repository,
                                String query,
                                String language,
                                String type,
                                ContentMapper<T> mapper)
Execute a query for a result list of type T, for the given query.

Specified by:
queryForList in interface MagnoliaQueryOperations
Parameters:
repository - the repository to execute query against
query - query to execute
language - query language (either XPATH or SQL).
type - the type of the item to query for
mapper - the mapper used
Returns:
the result object of the required type, or null in case of a null query

queryForObject

public <T> T queryForObject(String repository,
                            String query,
                            String language,
                            String type,
                            ContentMapper<T> mapper)
Execute a query for a single or null result object, for the given query.

This method is useful for running queries with a known outcome. The query is expected to be a single result query; the returned result will be directly mapped to the corresponding object type.

Specified by:
queryForObject in interface MagnoliaQueryOperations
Parameters:
repository - the repository to execute query against
query - query to execute
language - query language (either XPATH or SQL).
type - the type of the item to query for
mapper - the mapper used
Returns:
the result object of the required type, or null in case of a null query

xpathQueryForList

public <T> List<T> xpathQueryForList(String repository,
                                     String query,
                                     String type,
                                     ContentMapper<T> mapper)
                          throws DataAccessException
Execute an XPath query for a result list of type T, for the given query.

Specified by:
xpathQueryForList in interface MagnoliaQueryOperations
Parameters:
repository - the repository to execute query against
query - query to execute
type - the type of the item to query for
mapper - the mapper used
Returns:
the result object of the required type, or null in case of a null Xpath query
Throws:
DataAccessException - if there is any problem executing the query

xpathQueryForObject

public <T> T xpathQueryForObject(String repository,
                                 String query,
                                 String type,
                                 ContentMapper<T> mapper)
                      throws DataAccessException
Execute an XPath query for a single or null result object, for the given query.

This method is useful for running queries with a known outcome. The query is expected to be a single result query; the returned result will be directly mapped to the corresponding object type.

Specified by:
xpathQueryForObject in interface MagnoliaQueryOperations
Parameters:
repository - the repository to execute query against
query - query to execute
type - the type of the item to query for
mapper - the mapper used
Returns:
the result object of the required type, or null in case of a null query
Throws:
IncorrectResultSizeDataAccessException - if the query returns more than one results
DataAccessException - if there is any problem executing the query

xpathQueryForContent

public javax.jcr.Node xpathQueryForContent(String repository,
                                           String query,
                                           String type)
                                    throws DataAccessException
Execute an XPath query for a Content node.

Specified by:
xpathQueryForContent in interface MagnoliaQueryOperations
Parameters:
repository - the repository to execute query against
query - query to execute
type - the type of the item to query for
Returns:
the result Content node, or null if no such node exists
Throws:
DataAccessException - if there is any problem executing the query

query

protected <T> List<T> query(String repository,
                            String query,
                            String language,
                            String type,
                            ContentMapper<T> mapper)
                 throws DataAccessException
Throws:
DataAccessException

queryInternal

protected <T> Collection<T> queryInternal(String repository,
                                          String query,
                                          String language,
                                          String type)
Performs the actual query execution. Purposely delegates to QueryUtil.exceptionThrowingQuery(..) in order to provide flexibility with regards to error handling, by translating any RepositoryException into an unchecked DataAccessException. This allows for optional exception handling and logging, making it a better alternative then using QueryUtil.query(..) directly, which logs the error and then directly swallows the exception. Execute a query for a result list of type T, for the given query.

Parameters:
repository - the repository to execute query against
query - query to execute
language - query language (either or ).
type - the type of the item to query for
Returns:
the result object of the required type, or null in case of a null query
Throws:
DataAccessException - if there is any problem executing the query

doExceptionThrowingQuery

protected <T> Collection<T> doExceptionThrowingQuery(String repository,
                                                     String query,
                                                     String language,
                                                     String type)
                                          throws javax.jcr.RepositoryException
Throws:
javax.jcr.RepositoryException

singleResult

protected static <T> T singleResult(Collection<T> results)
                         throws IncorrectResultSizeDataAccessException
Return a single result object from the given collection or null if the collection is empty or null.

Parameters:
results - the result collection (may be null)
Returns:
the single result object, or null when no result
Throws:
IncorrectResultSizeDataAccessException - if collection contains more than one element


Copyright © 2014 Magnolia International Ltd.. All Rights Reserved.