1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.rssaggregator.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.util.QueryUtil;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import javax.jcr.RepositoryException;
44 import java.util.ArrayList;
45 import java.util.Collection;
46 import static java.util.Collections.*;
47 import java.util.List;
48
49
50
51
52
53
54
55
56 public class MagnoliaTemplate implements MagnoliaQueryOperations {
57
58
59 protected Logger logger = LoggerFactory.getLogger(getClass());
60
61
62 public <T> List<T> queryForList(String repository, String query, String language, ItemType type, ContentMapper<T> mapper) {
63 return query(repository, query, language, type, mapper);
64 }
65
66
67 public <T> T queryForObject(String repository, String query, String language, ItemType type, ContentMapper<T> mapper) {
68 List<T> results = query(repository, query, language, type, mapper);
69 return singleResult(results);
70 }
71
72
73 public <T> List<T> xpathQueryForList(String repository, String query, ItemType type, ContentMapper<T> mapper) throws DataAccessException {
74 return queryForList(repository, query, Query.XPATH, type, mapper);
75 }
76
77
78 public <T> T xpathQueryForObject(String repository, String query, ItemType type, ContentMapper<T> mapper) throws DataAccessException {
79 return queryForObject(repository, query, Query.XPATH, type, mapper);
80 }
81
82
83 public Content xpathQueryForContent(String repository, String query, ItemType type) throws DataAccessException {
84 return queryForObject(repository, query, Query.XPATH, type, new ContentMapper<Content>() {
85 public Content map(Content content) throws RepositoryException {
86 return content;
87 }
88 });
89 }
90
91
92
93 protected <T> List<T> query(String repository, String query, String language, ItemType type, ContentMapper<T> mapper) throws DataAccessException {
94 Collection<Content> contents = queryInternal(repository, query, language, type);
95 if (contents.isEmpty()) {
96 return emptyList();
97 }
98 List<T> result = new ArrayList<T>(contents.size());
99 for (Content content : contents) {
100 try {
101 result.add(mapper.map(content));
102 } catch (RepositoryException re) {
103 throw new DataAccessException(String.format("Failed to map content '%s' using mapper '%s'", content, mapper.getClass()));
104 }
105 }
106 return result;
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 protected <T> Collection<T> queryInternal(String repository, String query, String language, ItemType type) {
125 try {
126 return doExceptionThrowingQuery(repository, query, language, type);
127 } catch (RepositoryException re) {
128 throw new DataAccessException("A repository exception occurred", re);
129 }
130 }
131
132 @SuppressWarnings("unchecked")
133 protected <T> Collection<T> doExceptionThrowingQuery(String repository, String query, String language, ItemType type) throws RepositoryException {
134 logger.debug("Executing {} query for '{}' in repository '{}': \"{}\"", new Object[] {language,
135 type.getSystemName(), repository, query}
136 );
137 Collection<T> results = (Collection<T>) QueryUtil.exceptionThrowingQuery(repository, query, language, type.getSystemName());
138 logger.debug("Query returned %s result(s)", (results == null) ? null : results.size());
139 return results;
140 }
141
142
143
144
145
146
147
148
149
150 protected static <T> T singleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException {
151 int size = (results == null ? 0 : results.size());
152 if (size == 0) {
153 return null;
154 }
155 if (results.size() > 1) {
156 throw new IncorrectResultSizeDataAccessException(1, size);
157 }
158 return results.iterator().next();
159 }
160
161 }