KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > core > search > QueryResultImpl


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.core.search;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ItemType;
17 import info.magnolia.cms.core.Path;
18 import info.magnolia.cms.security.AccessManager;
19 import info.magnolia.cms.security.Permission;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.jcr.Node;
27 import javax.jcr.NodeIterator;
28 import javax.jcr.RepositoryException;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.log4j.Logger;
32
33
34 /**
35  * @author Sameer Charles
36  * @author Fabrizio Giustina
37  */

38 public class QueryResultImpl implements QueryResult {
39
40     /**
41      * Logger.
42      */

43     private static Logger log = Logger.getLogger(QueryResultImpl.class);
44
45     /**
46      * Unfiltered result object
47      */

48     private javax.jcr.query.QueryResult result;
49
50     /**
51      * caches all previously queried objects
52      */

53     private Map JavaDoc objectStore = new Hashtable JavaDoc();
54
55     private AccessManager accessManager;
56
57     private Map JavaDoc dirtyHandles = new Hashtable JavaDoc();
58
59     protected QueryResultImpl(javax.jcr.query.QueryResult result, AccessManager accessManager) {
60         this.result = result;
61         this.accessManager = accessManager;
62     }
63
64     /**
65      * Build required result objects
66      */

67     private void build(String JavaDoc nodeType, Collection JavaDoc collection) throws RepositoryException {
68         this.objectStore.put(nodeType, collection);
69         NodeIterator nodeIterator = this.result.getNodes();
70         while (nodeIterator.hasNext()) {
71             Node node = nodeIterator.nextNode();
72             try {
73                 build(node, nodeType, collection);
74             }
75             catch (RepositoryException re) {
76                 log.error(re.getMessage());
77                 log.debug(re);
78             }
79         }
80     }
81
82     /**
83      * Build required result objects
84      */

85     private void build(Node node, String JavaDoc nodeType, Collection JavaDoc collection) throws RepositoryException {
86         /**
87          * All custom node types
88          */

89         if (node.isNodeType(nodeType)) {
90             if (this.dirtyHandles.get(node.getPath()) == null) {
91                 boolean isAllowed = this.accessManager.isGranted(Path.getAbsolutePath(node.getPath()), Permission.READ);
92                 if (isAllowed) {
93                     collection.add(new Content(node, this.accessManager));
94                     this.dirtyHandles.put(node.getPath(), StringUtils.EMPTY);
95                 }
96             }
97             return;
98         }
99         if (node.getDepth() > 0) {
100             this.build(node.getParent(), nodeType, collection);
101         }
102     }
103
104     /**
105      * @see info.magnolia.cms.core.search.QueryResult#getContent()
106      */

107     public Collection JavaDoc getContent() {
108         return getContent(ItemType.CONTENT.getSystemName());
109     }
110
111     /**
112      * @see info.magnolia.cms.core.search.QueryResult#getContent(java.lang.String)
113      */

114     public Collection JavaDoc getContent(String JavaDoc nodeType) {
115         Collection JavaDoc resultSet = (Collection JavaDoc) this.objectStore.get(nodeType);
116         if (resultSet == null) {
117             /* build it first time */
118             resultSet = new ArrayList JavaDoc();
119             try {
120                 this.build(nodeType, resultSet);
121             }
122             catch (RepositoryException re) {
123                 log.error(re.getMessage());
124             }
125         }
126         return resultSet;
127     }
128
129 }
130
Popular Tags