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