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 }
119 catch (RepositoryException re) {
120 log.error("{} caught while iterating on query results: {}", re.getClass().getName(), re.getMessage());
121 log.debug("{} caught while iterating on query results: {}", re.getClass().getName(), re.getMessage(), re);
122 }
123 }
124 }
125
126
127
128
129 protected void build(Node node, String[] nodeType, Collection<Content> collection) throws RepositoryException {
130
131
132
133 if ((nodeType == null || nodeType.length == 0) || isNodeType(node, nodeType) && !node.isNodeType(ItemType.NT_RESOURCE)) {
134 if (this.dirtyHandles.get(node.getPath()) == null) {
135
136 boolean isAllowed = PermissionUtil.isGranted(node.getSession(), Path.getAbsolutePath(node.getPath()), Session.ACTION_READ);
137 if (isAllowed) {
138 collection.add(new DefaultContent(node));
139 this.dirtyHandles.put(node.getPath(), StringUtils.EMPTY);
140 }
141 }
142 return;
143 }
144 if (node.getDepth() > 0) {
145 this.build(node.getParent(), nodeType, collection);
146 }
147 }
148
149 @Override
150 public Collection<Content> getContent() {
151 return getContent(ItemType.CONTENT.getSystemName());
152 }
153
154 @Override
155 public Collection<Content> getContent(String nodeType) {
156 Collection<Content> resultSet = this.objectStore.get(nodeType);
157 if (resultSet == null) {
158
159 resultSet = new ArrayList<Content>();
160 try {
161 this.build(nodeType, resultSet);
162 }
163 catch (RepositoryException re) {
164 log.error(re.getMessage());
165 }
166 }
167 return resultSet;
168 }
169
170 private boolean isNodeType(Node node, String[] nodeType) throws RepositoryException {
171
172 for (String nt : nodeType) {
173 if (node.isNodeType(nt)) {
174 return true;
175 }
176 }
177 return false;
178 }
179 }