KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webservice > repository > ChildrenQuerySession


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.repo.webservice.repository;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.alfresco.repo.webservice.Utils;
24 import org.alfresco.repo.webservice.types.NamedValue;
25 import org.alfresco.repo.webservice.types.Reference;
26 import org.alfresco.repo.webservice.types.ResultSetRow;
27 import org.alfresco.repo.webservice.types.ResultSetRowNode;
28 import org.alfresco.service.cmr.repository.ChildAssociationRef;
29 import org.alfresco.service.cmr.repository.NodeRef;
30 import org.alfresco.service.cmr.repository.NodeService;
31 import org.alfresco.service.cmr.search.SearchService;
32 import org.alfresco.service.namespace.NamespaceService;
33 import org.alfresco.service.namespace.QName;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 /**
38  * Implementation of a QuerySession that stores the results from a query for children
39  *
40  * @author gavinc
41  */

42 public class ChildrenQuerySession extends AbstractQuerySession
43 {
44    private static final long serialVersionUID = -5347036309571057074L;
45
46    private transient static Log logger = LogFactory.getLog(ChildrenQuerySession.class);
47    
48    private Reference node;
49    
50    /**
51     * Constructs a ChildrenQuerySession
52     *
53     * @param batchSize The batch size to use for this session
54     * @param node The node to retrieve the parents
55     */

56    public ChildrenQuerySession(int batchSize, Reference node)
57    {
58       super(batchSize);
59       
60       this.node = node;
61    }
62    
63    /**
64     * @see org.alfresco.repo.webservice.repository.QuerySession#getNextResultsBatch(org.alfresco.service.cmr.search.SearchService, org.alfresco.service.cmr.repository.NodeService, org.alfresco.service.namespace.NamespaceService)
65     */

66    public QueryResult getNextResultsBatch(SearchService searchService, NodeService nodeService, NamespaceService namespaceService)
67    {
68       QueryResult queryResult = null;
69       
70       if (this.position != -1)
71       {
72          if (logger.isDebugEnabled())
73             logger.debug("Before getNextResultsBatch: " + toString());
74          
75          // create the node ref and get the children from the repository
76
NodeRef nodeRef = Utils.convertToNodeRef(this.node, nodeService, searchService, namespaceService);
77          List JavaDoc<ChildAssociationRef> kids = nodeService.getChildAssocs(nodeRef);
78          
79          int totalRows = kids.size();
80          int lastRow = calculateLastRowIndex(totalRows);
81          int currentBatchSize = lastRow - this.position;
82          
83          if (logger.isDebugEnabled())
84             logger.debug("Total rows = " + totalRows + ", current batch size = " + currentBatchSize);
85          
86          org.alfresco.repo.webservice.types.ResultSet batchResults = new org.alfresco.repo.webservice.types.ResultSet();
87          org.alfresco.repo.webservice.types.ResultSetRow[] rows = new org.alfresco.repo.webservice.types.ResultSetRow[currentBatchSize];
88             
89          int arrPos = 0;
90          for (int x = this.position; x < lastRow; x++)
91          {
92             ChildAssociationRef assoc = kids.get(x);
93             NodeRef childNodeRef = assoc.getChildRef();
94             ResultSetRowNode rowNode = new ResultSetRowNode(childNodeRef.getId(), nodeService.getType(childNodeRef).toString(), null);
95             
96             // create columns for all the properties of the node
97
// get the data for the row and build up the columns structure
98
Map JavaDoc<QName, Serializable JavaDoc> props = nodeService.getProperties(childNodeRef);
99             NamedValue[] columns = new NamedValue[props.size()];
100             int col = 0;
101             for (QName propName : props.keySet())
102             {
103                String JavaDoc value = null;
104                Serializable JavaDoc valueObj = props.get(propName);
105                if (valueObj != null)
106                {
107                   value = valueObj.toString();
108                }
109                columns[col] = new NamedValue(propName.toString(), value);
110                col++;
111             }
112             
113             ResultSetRow row = new ResultSetRow();
114             row.setRowIndex(x);
115             row.setNode(rowNode);
116             row.setColumns(columns);
117             
118             // add the row to the overall results
119
rows[arrPos] = row;
120             arrPos++;
121          }
122          
123          // add the rows to the result set and set the total row count
124
batchResults.setRows(rows);
125          batchResults.setTotalRowCount(totalRows);
126          
127          queryResult = new QueryResult(getId(), batchResults);
128          
129          // move the position on
130
updatePosition(totalRows, queryResult);
131          
132          if (logger.isDebugEnabled())
133             logger.debug("After getNextResultsBatch: " + toString());
134       }
135       
136       return queryResult;
137    }
138    
139    /**
140     * @see java.lang.Object#toString()
141     */

142    @Override JavaDoc
143    public String JavaDoc toString()
144    {
145       StringBuilder JavaDoc builder = new StringBuilder JavaDoc(super.toString());
146       builder.append(" (id=").append(getId());
147       builder.append(" batchSize=").append(this.batchSize);
148       builder.append(" position=").append(this.position);
149       builder.append(" node-id=").append(this.node.getUuid()).append(")");
150       return builder.toString();
151    }
152 }
153
Popular Tags