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