KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > GTPrivMapDataSource


1 /**
2  * com.mckoi.database.GTPrivMapDataSource 26 Aug 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.util.ArrayList JavaDoc;
28 import com.mckoi.util.BigNumber;
29
30 /**
31  * A GTDataSource that maps a Privs 11-bit set to strings that represent the
32  * priv in human understandable string. Each 11-bit priv set contains 12
33  * entries for each bit that was set.
34  * <p>
35  * This table provides a convenient way to join the system grant table and
36  * 'expand' the privs that are allowed though it.
37  *
38  * @author Tobias Downer
39  */

40
41 public class GTPrivMapDataSource extends GTDataSource {
42
43   /**
44    * Number of bits.
45    */

46   private static int BIT_COUNT = Privileges.BIT_COUNT;
47   
48   
49   /**
50    * Constructor.
51    */

52   public GTPrivMapDataSource(DatabaseConnection connection) {
53     super(connection.getSystem());
54   }
55   
56   // ---------- Implemented from GTDataSource ----------
57

58   public DataTableDef getDataTableDef() {
59     return DEF_DATA_TABLE_DEF;
60   }
61   
62   public int getRowCount() {
63     return (1 << BIT_COUNT) * BIT_COUNT;
64   }
65
66   public TObject getCellContents(final int column, final int row) {
67     int c1 = row / BIT_COUNT;
68     if (column == 0) {
69       return columnValue(column, BigNumber.fromInt(c1));
70     }
71     else {
72       int priv_bit = (1 << (row % BIT_COUNT));
73       String JavaDoc priv_string = null;
74       if ((c1 & priv_bit) != 0) {
75         priv_string = Privileges.formatPriv(priv_bit);
76       }
77       return columnValue(column, priv_string);
78     }
79   }
80
81   // ---------- Overwritten from GTDataSource ----------
82

83   public SelectableScheme getColumnScheme(int column) {
84     if (column == 0) {
85       return new PrivMapSearch(this, column);
86     }
87     else {
88       return new BlindSearch(this, column);
89     }
90   }
91   
92   // ---------- Static ----------
93

94   /**
95    * The data table def that describes this table of data source.
96    */

97   static final DataTableDef DEF_DATA_TABLE_DEF;
98
99   static {
100
101     DataTableDef def = new DataTableDef();
102     def.setTableName(
103              new TableName(Database.SYSTEM_SCHEMA, "sUSRPrivMap"));
104
105     // Add column definitions
106
def.addColumn(numericColumn("priv_bit"));
107     def.addColumn( stringColumn("description"));
108
109     // Set to immutable
110
def.setImmutable();
111
112     DEF_DATA_TABLE_DEF = def;
113
114   }
115
116   // ---------- Inner classes ----------
117

118   /**
119    * A SelectableScheme that makes searching on the 'priv_bit' column a lot
120    * less painless!
121    */

122   private static final class PrivMapSearch extends CollatedBaseSearch {
123     
124     PrivMapSearch(TableDataSource table, int column) {
125       super(table, column);
126     }
127
128     public SelectableScheme copy(TableDataSource table, boolean immutable) {
129       // Return a fresh object. This implementation has no state so we can
130
// ignore the 'immutable' flag.
131
return new BlindSearch(table, getColumn());
132     }
133     
134     protected int searchFirst(TObject val) {
135       if (val.isNull()) {
136         return -1;
137       }
138
139       int num = ((BigNumber) val.getObject()).intValue();
140
141       if (num < 0) {
142         return -1;
143       }
144       else if (num > (1 << BIT_COUNT)) {
145         return -(((1 << BIT_COUNT) * BIT_COUNT) + 1);
146       }
147
148       return (num * BIT_COUNT);
149     }
150
151     protected int searchLast(TObject val) {
152       int p = searchFirst(val);
153       if (p >= 0) {
154         return p + (BIT_COUNT - 1);
155       }
156       else {
157         return p;
158       }
159     }
160     
161   }
162   
163 }
164
165
Popular Tags