KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > query > NodeRefListQueryResultImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.jcr.query;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.jcr.NodeIterator;
25 import javax.jcr.RepositoryException;
26 import javax.jcr.nodetype.NodeType;
27 import javax.jcr.nodetype.PropertyDefinition;
28 import javax.jcr.query.Query;
29 import javax.jcr.query.QueryResult;
30 import javax.jcr.query.RowIterator;
31
32 import org.alfresco.jcr.item.NodeRefNodeIteratorImpl;
33 import org.alfresco.jcr.session.SessionImpl;
34 import org.alfresco.jcr.util.JCRProxyFactory;
35 import org.alfresco.service.cmr.repository.NodeRef;
36 import org.alfresco.service.cmr.repository.NodeService;
37 import org.alfresco.service.namespace.QName;
38
39
40 /**
41  * Query Result based a NodeRef List
42  *
43  * @author David Caruana
44  */

45 public class NodeRefListQueryResultImpl implements QueryResult
46 {
47     /** Session */
48     private SessionImpl session;
49
50     /** The node refs in the result set */
51     private List JavaDoc<NodeRef> nodeRefs;
52     
53     /** Node Service */
54     private NodeService nodeService;
55     
56     /** Column Names */
57     private Map JavaDoc<QName, PropertyDefinition> columns = null;
58     
59     /** Proxy */
60     private QueryResult proxy = null;
61     
62     
63     /**
64      * Construct
65      *
66      * @param nodeRefs list of node references
67      */

68     public NodeRefListQueryResultImpl(SessionImpl session, List JavaDoc<NodeRef> nodeRefs)
69     {
70         this.session = session;
71         this.nodeRefs = nodeRefs;
72         this.nodeService = session.getRepositoryImpl().getServiceRegistry().getNodeService();
73     }
74
75     /**
76      * Get proxied JCR Query Result
77      *
78      * @return proxy
79      */

80     public QueryResult getProxy()
81     {
82         if (proxy == null)
83         {
84             proxy = (QueryResult)JCRProxyFactory.create(this, QueryResult.class, session);
85         }
86         return proxy;
87     }
88
89     /* (non-Javadoc)
90      * @see javax.jcr.query.QueryResult#getColumnNames()
91      */

92     public String JavaDoc[] getColumnNames() throws RepositoryException
93     {
94         Map JavaDoc<QName, PropertyDefinition> columns = getColumnDefinitions();
95         String JavaDoc[] names = new String JavaDoc[columns.size()];
96         int i = 0;
97         for (QName columnName : columns.keySet())
98         {
99             names[i++] = columnName.toPrefixString(session.getNamespaceResolver());
100         }
101         return names;
102     }
103
104     /* (non-Javadoc)
105      * @see javax.jcr.query.QueryResult#getRows()
106      */

107     public RowIterator getRows() throws RepositoryException
108     {
109         return new NodeRefRowIteratorImpl(session, getColumnDefinitions(), nodeRefs).getProxy();
110     }
111
112     /* (non-Javadoc)
113      * @see javax.jcr.query.QueryResult#getNodes()
114      */

115     public NodeIterator getNodes() throws RepositoryException
116     {
117         return new NodeRefNodeIteratorImpl(session, nodeRefs);
118     }
119
120     
121     /**
122      * Get list of column definitions
123      *
124      * @return list of column definitions
125      */

126     private Map JavaDoc<QName, PropertyDefinition> getColumnDefinitions()
127         throws RepositoryException
128     {
129         if (columns == null)
130         {
131             columns = new HashMap JavaDoc<QName, PropertyDefinition>();
132             
133             // build list of column names from result set
134
if (nodeRefs.size() > 0)
135             {
136                 // Base column list on first node ref
137
// TODO: determine on a more formal basis
138
QName type = nodeService.getType(nodeRefs.get(0));
139                 NodeType nodeType = session.getTypeManager().getNodeType(type.toPrefixString(session.getNamespaceResolver()));
140                 PropertyDefinition[] propDefs = nodeType.getPropertyDefinitions();
141                 for (PropertyDefinition propDef : propDefs)
142                 {
143                     if (!propDef.isMultiple())
144                     {
145                         columns.put(QName.createQName(propDef.getName(), session.getNamespaceResolver()), propDef);
146                     }
147                 }
148                 Set JavaDoc<QName>aspects = nodeService.getAspects(nodeRefs.get(0));
149                 for (QName aspect : aspects)
150                 {
151                     NodeType nodeAspect = session.getTypeManager().getNodeType(aspect.toPrefixString(session.getNamespaceResolver()));
152                     propDefs = nodeAspect.getPropertyDefinitions();
153                     for (PropertyDefinition propDef : propDefs)
154                     {
155                         if (!propDef.isMultiple())
156                         {
157                             columns.put(QName.createQName(propDef.getName(), session.getNamespaceResolver()), propDef);
158                         }
159                     }
160                 }
161             }
162             
163             // add JCR required columns
164
columns.put(QueryManagerImpl.JCRPATH_COLUMN, null);
165             columns.put(QueryManagerImpl.JCRSCORE_COLUMN, null);
166         }
167         return columns;
168     }
169     
170 }
171
Popular Tags