1 17 package org.alfresco.repo.webservice.repository; 18 19 import java.io.Serializable ; 20 import java.util.List ; 21 import java.util.Map ; 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 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 61 public AssociatedQuerySession(int batchSize, Reference node) 62 { 63 super(batchSize); 64 65 this.node = node; 66 } 67 68 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 NodeRef nodeRef = Utils.convertToNodeRef(this.node, nodeService, 85 searchService, namespaceService); 86 List <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 Map <QName, Serializable > 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 value = null; 118 Serializable 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 rows[arrPos] = row; 134 arrPos++; 135 } 136 137 batchResults.setRows(rows); 139 batchResults.setTotalRowCount(totalRows); 140 141 queryResult = new QueryResult(getId(), batchResults); 142 143 updatePosition(totalRows, queryResult); 145 146 if (logger.isDebugEnabled()) 147 logger.debug("After getNextResultsBatch: " + toString()); 148 } 149 150 return queryResult; 151 } 152 153 156 @Override 157 public String toString() 158 { 159 StringBuilder builder = new StringBuilder (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 |