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