KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > search > filters > RolesFilter


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.search.filters;
17
18 import org.apache.lucene.search.Filter;
19 import org.apache.lucene.index.IndexReader;
20 import org.apache.lucene.document.Document;
21
22 import java.util.BitSet JavaDoc;
23 import java.util.List JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import com.blandware.atleap.search.SearchManager;
27 import com.blandware.atleap.common.util.ConvertUtil;
28
29 /**
30  * <p>This class to filter search results by roles</p>
31  * <p/>
32  * <p><a HREF="RolesFilter.java.htm"><i>View Source</i></a></p>
33  *
34  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
35  * @version $Revision: 1.2 $ $Date: 2006/03/16 11:09:39 $
36  */

37 public class RolesFilter extends Filter {
38
39     private String JavaDoc userRoles = null;
40
41     /**
42      * Creates new instance of RolesFilter
43      *
44      * @param userRoles comma-separated list of user roles
45      */

46     public RolesFilter(String JavaDoc userRoles) {
47         if (userRoles == null) {
48             this.userRoles = "";
49         } else {
50             this.userRoles = userRoles.trim();
51         }
52     }
53
54     /**
55      * {@link Filter#bits}.
56      */

57     public BitSet JavaDoc bits(IndexReader reader) throws IOException JavaDoc
58     {
59         BitSet JavaDoc bits = new BitSet JavaDoc(reader.maxDoc());
60
61         for (int i = 0; i < reader.maxDoc(); i++) {
62             Document document = reader.document(i);
63             String JavaDoc docRoles = document.get(SearchManager.ROLES_FIELD);
64
65             if (docRoles == null) {
66                 docRoles = "";
67             } else {
68                 docRoles = docRoles.trim();
69             }
70
71             if (docRoles.length() == 0) {
72                 bits.set(i);
73             } else if (userRoles.length() == 0) {
74                 continue;
75             } else {
76                 List JavaDoc userRolesList = ConvertUtil.convertStringToList(userRoles, ",", true);
77                 List JavaDoc docRolesList = ConvertUtil.convertStringToList(docRoles, ",", true);
78                 for (int j = 0; j < docRolesList.size(); j++) {
79                     String JavaDoc docRole = (String JavaDoc) docRolesList.get(j);
80                     if (userRolesList.contains(docRole)) {
81                         bits.set(i);
82                         break;
83                     }
84                 }
85             }
86         }
87         return bits;
88     }
89
90 }
91
Popular Tags