1 17 package org.alfresco.repo.security.permissions.impl.acegi; 18 19 import java.util.BitSet ; 20 import java.util.List ; 21 import java.util.ListIterator ; 22 23 import org.alfresco.repo.search.ResultSetRowIterator; 24 import org.alfresco.service.cmr.repository.ChildAssociationRef; 25 import org.alfresco.service.cmr.repository.NodeRef; 26 import org.alfresco.service.cmr.repository.Path; 27 import org.alfresco.service.cmr.search.ResultSet; 28 import org.alfresco.service.cmr.search.ResultSetMetaData; 29 import org.alfresco.service.cmr.search.ResultSetRow; 30 31 public class FilteringResultSet extends ACLEntryAfterInvocationProvider implements ResultSet 32 { 33 private ResultSet unfiltered; 34 35 private BitSet inclusionMask; 36 37 private ResultSetMetaData resultSetMetaData; 38 39 FilteringResultSet(ResultSet unfiltered) 40 { 41 super(); 42 this.unfiltered = unfiltered; 43 inclusionMask = new BitSet (unfiltered.length()); 44 } 45 46 ResultSet getUnFilteredResultSet() 47 { 48 return unfiltered; 49 } 50 51 void setIncluded(int i, boolean excluded) 52 { 53 inclusionMask.set(i, excluded); 54 } 55 56 boolean getIncluded(int i) 57 { 58 return inclusionMask.get(i); 59 } 60 61 public Path[] getPropertyPaths() 62 { 63 return unfiltered.getPropertyPaths(); 64 } 65 66 public int length() 67 { 68 return inclusionMask.cardinality(); 69 } 70 71 private int translateIndex(int n) 72 { 73 if (n > length()) 74 { 75 throw new IndexOutOfBoundsException (); 76 } 77 int count = -1; 78 for (int i = 0, l = unfiltered.length(); i < l; i++) 79 { 80 if (inclusionMask.get(i)) 81 { 82 count++; 83 } 84 if (count == n) 85 { 86 return i; 87 } 88 89 } 90 throw new IndexOutOfBoundsException (); 91 } 92 93 public NodeRef getNodeRef(int n) 94 { 95 return unfiltered.getNodeRef(translateIndex(n)); 96 } 97 98 public float getScore(int n) 99 { 100 return unfiltered.getScore(translateIndex(n)); 101 } 102 103 public void close() 104 { 105 unfiltered.close(); 106 } 107 108 public ResultSetRow getRow(int i) 109 { 110 return unfiltered.getRow(translateIndex(i)); 111 } 112 113 public List <NodeRef> getNodeRefs() 114 { 115 List <NodeRef> answer = unfiltered.getNodeRefs(); 116 for (int i = unfiltered.length() - 1; i >= 0; i--) 117 { 118 if (!inclusionMask.get(i)) 119 { 120 answer.remove(i); 121 } 122 } 123 return answer; 124 } 125 126 public List <ChildAssociationRef> getChildAssocRefs() 127 { 128 List <ChildAssociationRef> answer = unfiltered.getChildAssocRefs(); 129 for (int i = unfiltered.length() - 1; i >= 0; i--) 130 { 131 if (!inclusionMask.get(i)) 132 { 133 answer.remove(i); 134 } 135 } 136 return answer; 137 } 138 139 public ChildAssociationRef getChildAssocRef(int n) 140 { 141 return unfiltered.getChildAssocRef(translateIndex(n)); 142 } 143 144 public ListIterator <ResultSetRow> iterator() 145 { 146 return new FilteringIterator(); 147 } 148 149 class FilteringIterator implements ResultSetRowIterator 150 { 151 int underlyingPosition = -1; 153 154 public boolean hasNext() 155 { 156 return inclusionMask.nextSetBit(underlyingPosition + 1) != -1; 157 } 158 159 public ResultSetRow next() 160 { 161 underlyingPosition = inclusionMask.nextSetBit(underlyingPosition + 1); 162 if( underlyingPosition == -1) 163 { 164 throw new IllegalStateException (); 165 } 166 return unfiltered.getRow(underlyingPosition); 167 } 168 169 public boolean hasPrevious() 170 { 171 if (underlyingPosition <= 0) 172 { 173 return false; 174 } 175 else 176 { 177 for (int i = underlyingPosition - 1; i >= 0; i--) 178 { 179 if (inclusionMask.get(i)) 180 { 181 return true; 182 } 183 } 184 } 185 return false; 186 } 187 188 public ResultSetRow previous() 189 { 190 if (underlyingPosition <= 0) 191 { 192 throw new IllegalStateException (); 193 } 194 for (int i = underlyingPosition - 1; i >= 0; i--) 195 { 196 if (inclusionMask.get(i)) 197 { 198 underlyingPosition = i; 199 return unfiltered.getRow(underlyingPosition); 200 } 201 } 202 throw new IllegalStateException (); 203 } 204 205 public int nextIndex() 206 { 207 return inclusionMask.nextSetBit(underlyingPosition+1); 208 } 209 210 public int previousIndex() 211 { 212 if (underlyingPosition <= 0) 213 { 214 return -1; 215 } 216 for (int i = underlyingPosition - 1; i >= 0; i--) 217 { 218 if (inclusionMask.get(i)) 219 { 220 return i; 221 } 222 } 223 return -1; 224 } 225 226 229 230 public void remove() 231 { 232 throw new UnsupportedOperationException (); 234 } 235 236 public void set(ResultSetRow o) 237 { 238 throw new UnsupportedOperationException (); 240 } 241 242 public void add(ResultSetRow o) 243 { 244 throw new UnsupportedOperationException (); 246 } 247 248 } 249 250 public ResultSetMetaData getResultSetMetaData() 251 { 252 return resultSetMetaData; 253 } 254 255 public void setResultSetMetaData(ResultSetMetaData resultSetMetaData) 256 { 257 this.resultSetMetaData = resultSetMetaData; 258 } 259 260 261 } 262 | Popular Tags |