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