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.cms.core.search;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.DefaultContent;
38 import info.magnolia.cms.core.HierarchyManager;
39 import info.magnolia.cms.core.ItemType;
40 import info.magnolia.cms.core.Path;
41 import info.magnolia.cms.security.AccessManager;
42 import info.magnolia.cms.security.Permission;
43
44 import java.util.ArrayList;
45 import java.util.Collection;
46 import java.util.Hashtable;
47 import java.util.Map;
48
49 import javax.jcr.Node;
50 import javax.jcr.NodeIterator;
51 import javax.jcr.RepositoryException;
52
53 import org.apache.commons.lang.StringUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58
59
60
61
62 public class QueryResultImpl implements QueryResult {
63
64
65
66
67 private static Logger log = LoggerFactory.getLogger(QueryResultImpl.class);
68
69
70
71
72 protected javax.jcr.query.QueryResult result;
73
74
75
76
77 protected Map<String, Collection<Content>> objectStore = new Hashtable<String, Collection<Content>>();
78
79
80
81
82 private AccessManager accessManager;
83
84 protected HierarchyManager hm;
85
86 protected Map<String, String> dirtyHandles = new Hashtable<String, String>();
87
88 protected QueryResultImpl(javax.jcr.query.QueryResult result, HierarchyManager hm) {
89 this.result = result;
90 this.hm = hm;
91 this.accessManager = hm.getAccessManager();
92 }
93
94
95
96
97
98 public AccessManager getAccessManager() {
99 return accessManager;
100 }
101
102 public javax.jcr.query.QueryResult getJcrResult() {
103 return result;
104 }
105
106
107
108
109 protected void build(String nodeType, Collection<Content> collection) throws RepositoryException {
110 this.objectStore.put(nodeType, collection);
111 NodeIterator nodeIterator = this.result.getNodes();
112
113
114 String[] nodeTypes = StringUtils.split(nodeType);
115
116 while (nodeIterator.hasNext()) {
117 Node node = nodeIterator.nextNode();
118 try {
119 build(node, nodeTypes, collection);
120 }
121 catch (RepositoryException re) {
122 log.error("{} caught while iterating on query results: {}", re.getClass().getName(), re.getMessage());
123 if (log.isDebugEnabled()) {
124 log.debug(
125 re.getClass().getName() + " caught while iterating on query results: " + re.getMessage(),
126 re);
127 }
128 }
129 }
130 }
131
132
133
134
135 protected void build(Node node, String[] nodeType, Collection<Content> collection) throws RepositoryException {
136
137
138
139 if ((nodeType== null || nodeType.length == 0) || isNodeType(node, nodeType) && !node.isNodeType(ItemType.NT_RESOURCE)) {
140 if (this.dirtyHandles.get(node.getPath()) == null) {
141 boolean isAllowed = this.hm.getAccessManager().isGranted(Path.getAbsolutePath(node.getPath()), Permission.READ);
142 if (isAllowed) {
143 collection.add(new DefaultContent(node, this.hm));
144 this.dirtyHandles.put(node.getPath(), StringUtils.EMPTY);
145 }
146 }
147 return;
148 }
149 if (node.getDepth() > 0) {
150 this.build(node.getParent(), nodeType, collection);
151 }
152 }
153
154
155
156
157 public Collection<Content> getContent() {
158 return getContent(ItemType.CONTENT.getSystemName());
159 }
160
161
162
163
164 public Collection<Content> getContent(String nodeType) {
165 Collection<Content> resultSet = this.objectStore.get(nodeType);
166 if (resultSet == null) {
167
168 resultSet = new ArrayList<Content>();
169 try {
170 this.build(nodeType, resultSet);
171 }
172 catch (RepositoryException re) {
173 log.error(re.getMessage());
174 }
175 }
176 return resultSet;
177 }
178
179 private boolean isNodeType(Node node, String[] nodeType) throws RepositoryException {
180
181 for (String nt : nodeType) {
182 if (node.isNodeType(nt)) {
183 return true;
184 }
185 }
186 return false;
187 }
188 }