KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > permissions > impl > acegi > FilteringResultSet


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.security.permissions.impl.acegi;
18
19 import java.util.BitSet JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ListIterator JavaDoc;
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 JavaDoc inclusionMask;
36     
37     private ResultSetMetaData resultSetMetaData;
38
39     FilteringResultSet(ResultSet unfiltered)
40     {
41         super();
42         this.unfiltered = unfiltered;
43         inclusionMask = new BitSet JavaDoc(unfiltered.length());
44     }
45
46     /* package */ResultSet getUnFilteredResultSet()
47     {
48         return unfiltered;
49     }
50
51     /* package */void setIncluded(int i, boolean excluded)
52     {
53         inclusionMask.set(i, excluded);
54     }
55
56     /* package */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 JavaDoc();
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 JavaDoc();
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 JavaDoc<NodeRef> getNodeRefs()
114     {
115         List JavaDoc<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 JavaDoc<ChildAssociationRef> getChildAssocRefs()
127     {
128         List JavaDoc<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 JavaDoc<ResultSetRow> iterator()
145     {
146         return new FilteringIterator();
147     }
148
149     class FilteringIterator implements ResultSetRowIterator
150     {
151         // -1 at the start
152
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 JavaDoc();
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 JavaDoc();
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 JavaDoc();
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         /*
227          * Mutation is not supported
228          */

229
230         public void remove()
231         {
232             // TODO Auto-generated method stub
233
throw new UnsupportedOperationException JavaDoc();
234         }
235
236         public void set(ResultSetRow o)
237         {
238             // TODO Auto-generated method stub
239
throw new UnsupportedOperationException JavaDoc();
240         }
241
242         public void add(ResultSetRow o)
243         {
244             // TODO Auto-generated method stub
245
throw new UnsupportedOperationException JavaDoc();
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