KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
21
22 import org.alfresco.repo.webservice.Utils;
23 import org.alfresco.repo.webservice.types.NamedValue;
24 import org.alfresco.repo.webservice.types.Query;
25 import org.alfresco.repo.webservice.types.ResultSetRowNode;
26 import org.alfresco.repo.webservice.types.Store;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.service.cmr.repository.NodeService;
29 import org.alfresco.service.cmr.repository.Path;
30 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
31 import org.alfresco.service.cmr.search.ResultSet;
32 import org.alfresco.service.cmr.search.ResultSetRow;
33 import org.alfresco.service.cmr.search.SearchService;
34 import org.alfresco.service.namespace.NamespaceService;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /**
39  * Implementation of a QuerySession that retrieves results from a repository ResultSet
40  *
41  * @author gavinc
42  */

43 public class ResultSetQuerySession extends AbstractQuerySession
44 {
45     private static final long serialVersionUID = -9154514445963635138L;
46
47    private transient static Log logger = LogFactory.getLog(ResultSetQuerySession.class);
48    
49    private Store store;
50    private Query query;
51    private boolean includeMetaData;
52    
53    /**
54     * Constructs a ResultSetQuerySession
55     *
56     * @param batchSize The batch size to use for this session
57     * @param store The repository store to query against
58     * @param query The query to execute
59     * @param includeMetaData Whether to include metadata in the query results
60     */

61    public ResultSetQuerySession(int batchSize, Store store, Query query, boolean includeMetaData)
62    {
63       super(batchSize);
64       
65       this.store = store;
66       this.query = query;
67       this.includeMetaData = includeMetaData;
68    }
69    
70    /**
71     * @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)
72     */

73    public QueryResult getNextResultsBatch(SearchService searchService, NodeService nodeService, NamespaceService namespaceService)
74    {
75       QueryResult queryResult = null;
76       
77       if (this.position != -1)
78       {
79          if (logger.isDebugEnabled())
80             logger.debug("Before getNextResultsBatch: " + toString());
81          
82          // handle the special search string of * meaning, get everything
83
String JavaDoc statement = query.getStatement();
84          if (statement.equals("*"))
85          {
86             statement = "ISNODE:*";
87          }
88          ResultSet searchResults = null;
89          try
90          {
91             searchResults = searchService.query(Utils.convertToStoreRef(this.store),
92                   this.query.getLanguage().getValue(), statement);
93          
94             int totalRows = searchResults.length();
95             int lastRow = calculateLastRowIndex(totalRows);
96             int currentBatchSize = lastRow - this.position;
97          
98             if (logger.isDebugEnabled())
99                logger.debug("Total rows = " + totalRows + ", current batch size = " + currentBatchSize);
100          
101             org.alfresco.repo.webservice.types.ResultSet batchResults = new org.alfresco.repo.webservice.types.ResultSet();
102             org.alfresco.repo.webservice.types.ResultSetRow[] rows = new org.alfresco.repo.webservice.types.ResultSetRow[currentBatchSize];
103          
104             int arrPos = 0;
105             for (int x = this.position; x < lastRow; x++)
106             {
107                ResultSetRow origRow = searchResults.getRow(x);
108                NodeRef nodeRef = origRow.getNodeRef();
109                ResultSetRowNode rowNode = new ResultSetRowNode(nodeRef.getId(), nodeService.getType(nodeRef).toString(), null);
110             
111                // get the data for the row and build up the columns structure
112
Map JavaDoc<Path, Serializable JavaDoc> values = origRow.getValues();
113                NamedValue[] columns = new NamedValue[values.size()];
114                int col = 0;
115                for (Path path : values.keySet())
116                {
117                   String JavaDoc value = null;
118                   try
119                   {
120                       value = DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, values.get(path));
121                   }
122                   catch (Throwable JavaDoc exception)
123                   {
124                       value = values.get(path).toString();
125                   }
126                
127                   // Get the attribute QName from the result path
128
String JavaDoc attributeName = path.last().toString();
129                   if (attributeName.startsWith("@") == true)
130                   {
131                       attributeName = attributeName.substring(1);
132                   }
133                
134                   columns[col] = new NamedValue(attributeName, value);
135                   col++;
136                }
137
138             
139                org.alfresco.repo.webservice.types.ResultSetRow row = new org.alfresco.repo.webservice.types.ResultSetRow();
140                row.setColumns(columns);
141                row.setScore(origRow.getScore());
142                row.setRowIndex(x);
143                row.setNode(rowNode);
144             
145                // add the row to the overall results
146
rows[arrPos] = row;
147                arrPos++;
148             }
149          
150             // TODO: build up the meta data data structure if we've been asked to
151

152             // add the rows to the result set and set the total row count
153
batchResults.setRows(rows);
154             batchResults.setTotalRowCount(totalRows);
155          
156             queryResult = new QueryResult(getId(), batchResults);
157          
158             // move the position on
159
updatePosition(totalRows, queryResult);
160          
161             if (logger.isDebugEnabled())
162                logger.debug("After getNextResultsBatch: " + toString());
163          }
164          finally
165          {
166              if (searchResults != null)
167              {
168                  searchResults.close();
169              }
170          }
171       }
172       
173       return queryResult;
174    }
175
176    /**
177     * @see java.lang.Object#toString()
178     */

179    @Override JavaDoc
180    public String JavaDoc toString()
181    {
182       StringBuilder JavaDoc builder = new StringBuilder JavaDoc(super.toString());
183       builder.append(" (id=").append(getId());
184       builder.append(" batchSize=").append(this.batchSize);
185       builder.append(" position=").append(this.position);
186       builder.append(" store=").append(this.store.getScheme().getValue()).append(":").append(this.store.getAddress());
187       builder.append(" language=").append(this.query.getLanguage().getValue());
188       builder.append(" statement=").append(this.query.getStatement());
189       builder.append(")");
190       return builder.toString();
191    }
192 }
193
Popular Tags