KickJava   Java API By Example, From Geeks To Geeks.

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


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.AssociationRef;
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.alfresco.service.namespace.RegexQNamePattern;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /**
39  * Implementation of a QuerySession that stores the results from a query for
40  * associations
41  *
42  * @author Roy Wetherall
43  */

44 public class AssociatedQuerySession extends AbstractQuerySession
45 {
46     private static final long serialVersionUID = 6488008047324436124L;
47
48     private transient static Log logger = LogFactory
49             .getLog(AssociatedQuerySession.class);
50
51     private Reference node;
52
53     /**
54      * Constructs a AssociatedQuerySession
55      *
56      * @param batchSize
57      * The batch size to use for this session
58      * @param node
59      * The node to retrieve the associations
60      */

61     public AssociatedQuerySession(int batchSize, Reference node)
62     {
63         super(batchSize);
64
65         this.node = node;
66     }
67
68     /**
69      * @see org.alfresco.repo.webservice.repository.QuerySession#getNextResultsBatch(org.alfresco.service.cmr.search.SearchService,
70      * org.alfresco.service.cmr.repository.NodeService,
71      * org.alfresco.service.namespace.NamespaceService)
72      */

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

156     @Override JavaDoc
157     public String JavaDoc toString()
158     {
159         StringBuilder JavaDoc builder = new StringBuilder JavaDoc(super.toString());
160         builder.append(" (id=").append(getId());
161         builder.append(" batchSize=").append(this.batchSize);
162         builder.append(" position=").append(this.position);
163         builder.append(" node-id=").append(this.node.getUuid()).append(")");
164         return builder.toString();
165     }
166 }
167
Popular Tags