1 17 package org.alfresco.repo.webservice.repository; 18 19 import java.io.Serializable ; 20 import java.util.Map ; 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 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 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 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 String 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 Map <Path, Serializable > values = origRow.getValues(); 113 NamedValue[] columns = new NamedValue[values.size()]; 114 int col = 0; 115 for (Path path : values.keySet()) 116 { 117 String value = null; 118 try 119 { 120 value = DefaultTypeConverter.INSTANCE.convert(String .class, values.get(path)); 121 } 122 catch (Throwable exception) 123 { 124 value = values.get(path).toString(); 125 } 126 127 String 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 rows[arrPos] = row; 147 arrPos++; 148 } 149 150 152 batchResults.setRows(rows); 154 batchResults.setTotalRowCount(totalRows); 155 156 queryResult = new QueryResult(getId(), batchResults); 157 158 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 179 @Override 180 public String toString() 181 { 182 StringBuilder builder = new StringBuilder (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 |