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.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 42 public class ParentsQuerySession extends AbstractQuerySession 43 { 44 private static final long serialVersionUID = 2539375863409175463L; 45 46 private transient static Log logger = LogFactory.getLog(ParentsQuerySession.class); 47 48 private Reference node; 49 50 56 public ParentsQuerySession(int batchSize, Reference node) 57 { 58 super(batchSize); 59 60 this.node = node; 61 } 62 63 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 NodeRef nodeRef = Utils.convertToNodeRef(this.node, nodeService, searchService, namespaceService); 77 List <ChildAssociationRef> parents = nodeService.getParentAssocs(nodeRef); 78 79 int totalRows = parents.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 = parents.get(x); 93 NodeRef parentNodeRef = assoc.getParentRef(); 94 ResultSetRowNode rowNode = new ResultSetRowNode(parentNodeRef.getId(), nodeService.getType(parentNodeRef).toString(), null); 95 96 Map <QName, Serializable > props = nodeService.getProperties(parentNodeRef); 99 NamedValue[] columns = new NamedValue[props.size()]; 100 int col = 0; 101 for (QName propName : props.keySet()) 102 { 103 String value = null; 104 Serializable 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 rows[arrPos] = row; 120 arrPos++; 121 } 122 123 batchResults.setRows(rows); 125 batchResults.setTotalRowCount(totalRows); 126 127 queryResult = new QueryResult(getId(), batchResults); 128 129 updatePosition(totalRows, queryResult); 131 132 if (logger.isDebugEnabled()) 133 logger.debug("After getNextResultsBatch: " + toString()); 134 } 135 136 return queryResult; 137 } 138 139 142 @Override 143 public String toString() 144 { 145 StringBuilder builder = new StringBuilder (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 |